Class: VCAP::Services::CatalogManagerV1

Inherits:
CatalogManagerBase show all
Defined in:
lib/base/catalog_manager_v1.rb

Constant Summary collapse

REQ_OPTS =
%w(cloud_controller_uri token gateway_name logger).map {|o| o.to_sym}

Instance Method Summary collapse

Methods inherited from CatalogManagerBase

#create_http_request

Constructor Details

#initialize(opts) ⇒ CatalogManagerV1

Returns a new instance of CatalogManagerV1.

Raises:

  • (ArgumentError)


12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/base/catalog_manager_v1.rb', line 12

def initialize(opts)
  super(opts)

  missing_opts = REQ_OPTS.select {|o| !opts.has_key? o}
  raise ArgumentError, "Missing options: #{missing_opts.join(', ')}" unless missing_opts.empty?

  @gateway_name      = opts[:gateway_name]

  @cld_ctrl_uri      = opts[:cloud_controller_uri]
  @service_list_uri  = "#{@cld_ctrl_uri}/proxied_services/v1/offerings"
  @offering_uri      = "#{@cld_ctrl_uri}/services/v1/offerings"
  @logger            = opts[:logger]

  token_hdrs = VCAP::Services::Api::GATEWAY_TOKEN_HEADER
  @cc_req_hdrs  = {
    'Content-Type' => 'application/json',
    token_hdrs     => opts[:token],
  }

  @gateway_stats = {}
  @gateway_stats_lock = Mutex.new
end

Instance Method Details



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/base/catalog_manager_v1.rb', line 124

def advertise_service_to_cc(svc, active = true)
  offering = generate_cc_advertise_offering_request(svc, active)

  @logger.debug("CC Catalog Manager: Advertise service offering #{offering.inspect} to cloud_controller: #{@offering_uri}")
  return false unless offering

  create_http_request(:uri => @offering_uri, :method => "post", :head => @cc_req_hdrs, :body => offering) do |http, error|
    if !error
      if http.response_header.status == 200
        @logger.info("CC Catalog Manager: Successfully advertised offering: #{offering.inspect}")
        return true
      else
        @logger.error("CC Catalog Manager: Failed to advertise offerings:#{offering.inspect}, status=#{http.response_header.status}")
      end
    else
      @logger.error("CC Catalog Manager: Failed to advertise offerings:#{offering.inspect}: #{http.error}")
    end
  end
  return false
end

#create_key(label, version, provider) ⇒ Object



47
48
49
# File 'lib/base/catalog_manager_v1.rb', line 47

def create_key(label, version, provider)
  "#{label}-#{version}"
end

#delete_offering(id, version, provider) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/base/catalog_manager_v1.rb', line 145

def delete_offering(id, version, provider)
  # See: https://github.com/cloudfoundry/cloud_controller/blob/master/cloud_controller/config/routes.rb
  offering_id = "#{id}-#{version}/#{provider}"
  uri = "#{@offering_uri}/#{offering_id}"
  @logger.info("CC Catalog Manager: Delete service offering: #{offering_id}")

  create_http_request(:uri => uri, :method => "delete", :head => @cc_req_hdrs) do |http, error|
    if !error
      if http.response_header.status == 200
        @logger.info("CC Catalog Manager: Successfully deleted offering: #{offering_id}")
        return true
      else
        @logger.warn("CC Catalog Manager: Failed to delete offering: #{offering_id}, status: #{http.response_header.status}")
      end
    else
      @logger.warn("CC Catalog Manager: Failed to delete offering: #{offering_id} due to: #{http.error}")
    end
  end
  return false
end

#fetch_handles_from_cc(service_label, after_fetch_callback) ⇒ Object

Handles processing #####



168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/base/catalog_manager_v1.rb', line 168

def fetch_handles_from_cc(service_label, after_fetch_callback)
  return if @fetching_handles

  handles_uri = get_handles_uri(service_label)

  @logger.info("CC Catalog Manager: Fetching handles from cloud controller: #{handles_uri}")
  @fetching_handles = true

  create_http_request(:uri => handles_uri, :method => "get", :head => @cc_req_hdrs) do |http, error|
    @fetching_handles = false

    if !error
      if http.response_header.status == 200
        @logger.info("CC Catalog Manager: Successfully fetched handles")

        begin
          resp = VCAP::Services::Api::ListHandlesResponse.decode(http.response)
          after_fetch_callback.call(resp) if after_fetch_callback
        rescue => e
          @logger.error("CC Catalog Manager: Error decoding reply from gateway: #{e}")
        end
      else
        @logger.error("CC Catalog Manager: Failed fetching handles, status=#{http.response_header.status}")
      end
    else
      @logger.error("CC Catalog Manager: Failed fetching handles: #{http.error}")
    end
  end
end

#generate_cc_advertise_offering_request(svc, active = true) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/base/catalog_manager_v1.rb', line 69

def generate_cc_advertise_offering_request(svc, active = true)
  plans = svc["plans"] if svc["plans"].is_a?(Array)
  if svc["plans"].is_a?(Hash)
    plans = []
    svc["plans"].keys.each { |k| plans << k.to_s }
  end

  VCAP::Services::Api::ServiceOfferingRequest.new({
    :label => svc["label"],
    :description => svc["description"],

    :provider => svc["provider"] || 'core',

    :url => svc["url"],

    :plans => plans,
    :cf_plan_id => svc["cf_plan_id"],
    :default_plan => svc["default_plan"],

    :tags => svc["tags"] || [],

    :active => active,

    :acls => svc["acls"],

    :supported_versions => svc["supported_versions"],
    :version_aliases => svc["version_aliases"],

    :timeout => svc["timeout"],
  }).encode
end

#get_handles_uri(service_label) ⇒ Object



43
44
45
# File 'lib/base/catalog_manager_v1.rb', line 43

def get_handles_uri(service_label)
  "#{@cld_ctrl_uri}/services/v1/offerings/#{service_label}/handles"
end

#load_registered_services_from_ccObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/base/catalog_manager_v1.rb', line 101

def load_registered_services_from_cc
  @logger.info("CC Catalog Manager: Get registred services from cloud_controller: #{@service_list_uri}")

  services = {}
  create_http_request(:uri => @service_list_uri, :method => "get", :head => @cc_req_hdrs, :need_raise => true) do |http, error|
    if !error
      if http.response_header.status == 200
        resp = JSON.parse(http.response)
        resp["proxied_services"].each {|svc|
          @logger.info("CC Catalog Manager: Fetch #{@gateway_name} service from CC: label=#{svc["label"]} - #{svc.inspect}")
          services[svc["label"]] = svc
        }
      else
        raise "CC Catalog Manager: Failed to fetch #{@gateway_name} service from CC - status=#{http.response_header.status}"
      end
    else
      raise "CC Catalog Manager: Failed to fetch #{@gateway_name} service from CC: #{http.error}"
    end
  end

  return services
end

#snapshot_and_reset_statsObject



35
36
37
38
39
40
41
# File 'lib/base/catalog_manager_v1.rb', line 35

def snapshot_and_reset_stats
  stats_snapshot = {}
  @gateway_stats_lock.synchronize do
    stats_snapshot = @gateway_stats.dup
  end
  stats_snapshot
end

#update_catalog(activate, load_catalog_callback, after_update_callback = nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/base/catalog_manager_v1.rb', line 51

def update_catalog(activate, load_catalog_callback, after_update_callback = nil)
  f = Fiber.new do
    configured_services = load_catalog_callback.call()
    active_count = 0
    configured_services.values.each { |svc|
      advertise_service_to_cc(svc, activate)
      active_count += 1  if activate
    }

    @gateway_stats_lock.synchronize do
      @gateway_stats[:active_offerings] = active_count
    end

    after_update_callback.call if after_update_callback
  end
  f.resume
end

#update_handle_in_cc(service_label, handle, on_success_callback, on_failure_callback) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/base/catalog_manager_v1.rb', line 198

def update_handle_in_cc(service_label, handle, on_success_callback, on_failure_callback)
  @logger.debug("CC Catalog Manager: Update service handle: #{handle.inspect}")
  if not handle
    on_failure_callback.call if on_failure_callback
    return
  end

  uri = "#{get_handles_uri(service_label)}/#{handle["service_id"]}"

  create_http_request(:uri => uri, method => "post", :head => @cc_req_hdrs, :body => Yajl::Encoder.encode(handle)) do |http, error|
    if !error
      if http.response_header.status == 200
        @logger.info("CC Catalog Manager: Successful update handle #{handle["service_id"]}")
        on_success_callback.call if on_success_callback
      else
        @logger.error("CC Catalog Manager: Failed to update handle #{handle["service_id"]}: http status #{http.response_header.status}")
        on_failure_callback.call if on_failure_callback
      end
    else
      @logger.error("CC Catalog Manager: Failed to update handle #{handle["service_id"]}: #{http.error}")
      on_failure_callback.call if on_failure_callback
    end
  end
end