vcap_services_base 0.2.10
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/base/abstract.rb +11 -0
- data/lib/base/api/message.rb +31 -0
- data/lib/base/asynchronous_service_gateway.rb +529 -0
- data/lib/base/backup.rb +206 -0
- data/lib/base/barrier.rb +54 -0
- data/lib/base/base.rb +159 -0
- data/lib/base/base_async_gateway.rb +164 -0
- data/lib/base/base_job.rb +5 -0
- data/lib/base/catalog_manager_base.rb +67 -0
- data/lib/base/catalog_manager_v1.rb +225 -0
- data/lib/base/catalog_manager_v2.rb +291 -0
- data/lib/base/cloud_controller_services.rb +75 -0
- data/lib/base/datamapper_l.rb +148 -0
- data/lib/base/gateway.rb +167 -0
- data/lib/base/gateway_service_catalog.rb +68 -0
- data/lib/base/http_handler.rb +101 -0
- data/lib/base/job/async_job.rb +71 -0
- data/lib/base/job/config.rb +27 -0
- data/lib/base/job/lock.rb +153 -0
- data/lib/base/job/package.rb +112 -0
- data/lib/base/job/serialization.rb +365 -0
- data/lib/base/job/snapshot.rb +354 -0
- data/lib/base/node.rb +471 -0
- data/lib/base/node_bin.rb +154 -0
- data/lib/base/plan.rb +63 -0
- data/lib/base/provisioner.rb +1120 -0
- data/lib/base/provisioner_v1.rb +125 -0
- data/lib/base/provisioner_v2.rb +193 -0
- data/lib/base/service.rb +93 -0
- data/lib/base/service_advertiser.rb +184 -0
- data/lib/base/service_error.rb +122 -0
- data/lib/base/service_message.rb +94 -0
- data/lib/base/service_plan_change_set.rb +11 -0
- data/lib/base/simple_aop.rb +63 -0
- data/lib/base/snapshot_v2/snapshot.rb +227 -0
- data/lib/base/snapshot_v2/snapshot_client.rb +158 -0
- data/lib/base/snapshot_v2/snapshot_job.rb +95 -0
- data/lib/base/utils.rb +63 -0
- data/lib/base/version.rb +7 -0
- data/lib/base/warden/instance_utils.rb +161 -0
- data/lib/base/warden/node_utils.rb +205 -0
- data/lib/base/warden/service.rb +426 -0
- data/lib/base/worker_bin.rb +76 -0
- data/lib/vcap_services_base.rb +16 -0
- metadata +364 -0
@@ -0,0 +1,125 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
# Copyright (c) 2009-2011 VMware, Inc.
|
3
|
+
|
4
|
+
$LOAD_PATH.unshift File.dirname(__FILE__)
|
5
|
+
require "base/provisioner"
|
6
|
+
|
7
|
+
module VCAP::Services::Base::ProvisionerV1
|
8
|
+
|
9
|
+
attr_accessor :prov_svcs
|
10
|
+
|
11
|
+
# Updates our internal handle state from external v1 handles, e.g., ccdb handles
|
12
|
+
# @param hash handles
|
13
|
+
def update_handles(handles)
|
14
|
+
@logger.info("[#{service_description}] Updating #{handles.size} handles v1")
|
15
|
+
handles.each do |handle|
|
16
|
+
unless verify_handle_format(handle)
|
17
|
+
@logger.warn("Skip not well-formed handle:#{handle}.")
|
18
|
+
next
|
19
|
+
end
|
20
|
+
|
21
|
+
h = handle.deep_dup
|
22
|
+
@prov_svcs[h['service_id']] = {
|
23
|
+
:configuration => h['configuration'],
|
24
|
+
:credentials => h['credentials'],
|
25
|
+
:service_id => h['service_id']
|
26
|
+
}
|
27
|
+
end
|
28
|
+
@logger.info("[#{service_description}] Handles updated")
|
29
|
+
end
|
30
|
+
|
31
|
+
def verify_handle_format(handle)
|
32
|
+
return nil unless handle
|
33
|
+
return nil unless handle.is_a? Hash
|
34
|
+
|
35
|
+
VCAP::Services::Internal::ServiceHandle.new(handle)
|
36
|
+
true
|
37
|
+
rescue => e
|
38
|
+
@logger.warn("Verify handle #{handle} failed:#{e}")
|
39
|
+
return nil
|
40
|
+
end
|
41
|
+
|
42
|
+
def indexing_handles(handles)
|
43
|
+
# instance handles hash's key is service_id, value is handle
|
44
|
+
# binding handles hash's key is credentials name & username, value is handle
|
45
|
+
ins_handles = {}
|
46
|
+
bin_handles = {}
|
47
|
+
|
48
|
+
handles.each do |h|
|
49
|
+
if h["service_id"] == h["credentials"]["name"]
|
50
|
+
ins_handles[h["service_id"]] = h
|
51
|
+
else
|
52
|
+
user = h["credentials"]["username"] || h["credentials"]["user"]
|
53
|
+
next unless user
|
54
|
+
key = h["credentials"]["name"] + user
|
55
|
+
bin_handles[key] = h
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
[ins_handles, bin_handles]
|
60
|
+
end
|
61
|
+
|
62
|
+
def get_instance_id_list(node_id, &blk)
|
63
|
+
@logger.debug("Get instance id list for migration")
|
64
|
+
|
65
|
+
id_list = []
|
66
|
+
@prov_svcs.each do |k, v|
|
67
|
+
id_list << k if (k == v[:credentials]["name"] && node_id == v[:credentials]["node_id"])
|
68
|
+
end
|
69
|
+
blk.call(success(id_list))
|
70
|
+
end
|
71
|
+
|
72
|
+
########
|
73
|
+
# Helpers
|
74
|
+
########
|
75
|
+
|
76
|
+
def get_all_instance_handles
|
77
|
+
instance_handles = @prov_svcs.select {|service_id, entity| service_id.to_s == entity[:credentials]["name"]}
|
78
|
+
instance_handles.each {|service_id, handle| yield handle if block_given?}
|
79
|
+
instance_handles
|
80
|
+
end
|
81
|
+
|
82
|
+
def get_all_binding_handles
|
83
|
+
binding_handles = @prov_svcs.select {|service_id, entity| service_id.to_s != entity[:credentials]["name"]}
|
84
|
+
binding_handles.each {|service_id, handle| yield handle if block_given?}
|
85
|
+
binding_handles
|
86
|
+
end
|
87
|
+
|
88
|
+
def get_instance_handle(instance_id)
|
89
|
+
@prov_svcs[instance_id].deep_dup
|
90
|
+
end
|
91
|
+
|
92
|
+
def get_binding_handle(binding_id)
|
93
|
+
@prov_svcs[binding_id].deep_dup
|
94
|
+
end
|
95
|
+
|
96
|
+
def add_instance_handle(response)
|
97
|
+
@prov_svcs[response[:service_id]] = response
|
98
|
+
end
|
99
|
+
|
100
|
+
def add_binding_handle(response)
|
101
|
+
@prov_svcs[response[:service_id]] = response
|
102
|
+
end
|
103
|
+
|
104
|
+
def delete_instance_handle(instance_handle)
|
105
|
+
@prov_svcs.delete(instance_handle[:service_id])
|
106
|
+
end
|
107
|
+
|
108
|
+
def delete_binding_handle(binding_handle)
|
109
|
+
@prov_svcs.delete(binding_handle[:service_id])
|
110
|
+
end
|
111
|
+
|
112
|
+
def find_instance_bindings(instance_id)
|
113
|
+
binding_handles = []
|
114
|
+
@prov_svcs.each do |_, handle|
|
115
|
+
if handle[:credentials]["name"] == instance_id
|
116
|
+
binding_handles << handle if handle[:service_id] != instance_id
|
117
|
+
end
|
118
|
+
end
|
119
|
+
binding_handles
|
120
|
+
end
|
121
|
+
|
122
|
+
def get_all_handles
|
123
|
+
@prov_svcs.deep_dup
|
124
|
+
end
|
125
|
+
end
|
@@ -0,0 +1,193 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
# Copyright (c) 2009-2011 VMware, Inc.
|
3
|
+
|
4
|
+
$LOAD_PATH.unshift File.dirname(__FILE__)
|
5
|
+
require "base/provisioner"
|
6
|
+
|
7
|
+
module VCAP::Services::Base::ProvisionerV2
|
8
|
+
|
9
|
+
attr_accessor :service_instances
|
10
|
+
attr_accessor :service_bindings
|
11
|
+
|
12
|
+
# Updates our internal handle state from external v2 handles, e.g., ccdb handles
|
13
|
+
# @param array handles
|
14
|
+
# handles[0] contains all instance handles
|
15
|
+
# handles[1] contains all binding handles
|
16
|
+
def update_handles(handles)
|
17
|
+
if handles.size == 2
|
18
|
+
@logger.info("[#{service_description}] Updating #{handles[0].size} instance handles and #{handles[1].size} binding handles in v2 api...")
|
19
|
+
update_instance_handles(handles[0])
|
20
|
+
update_binding_handles(handles[1])
|
21
|
+
@logger.info("[#{service_description}] Handles updated")
|
22
|
+
elsif handles.size == 1
|
23
|
+
# this is for the internal update when doing version update and some other updates
|
24
|
+
# and thus it would only be updating either binding or instance handle;
|
25
|
+
# binding handle would have a field of gateway_name but instance handle would not;
|
26
|
+
if handles[0].has_key?('gateway_name')
|
27
|
+
internal_handle = { handles[0]['gateway_name'] => handles[0] }
|
28
|
+
update_binding_handles(internal_handle)
|
29
|
+
else
|
30
|
+
internal_handle = { handles[0]['credentials']['name'] => handles[0] }
|
31
|
+
update_instance_handles(internal_handle)
|
32
|
+
end
|
33
|
+
else
|
34
|
+
raise "unknown handle type in update handles v2"
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def update_instance_handles(instance_handles)
|
39
|
+
instance_handles.each do |instance_id, instance_handle|
|
40
|
+
unless verify_instance_handle_format(instance_handle)
|
41
|
+
@logger.warn("Skip not well-formed instance handle:#{instance_handle}.")
|
42
|
+
next
|
43
|
+
end
|
44
|
+
|
45
|
+
handle = instance_handle.deep_dup
|
46
|
+
@service_instances[instance_id] = {
|
47
|
+
:credentials => handle['credentials'],
|
48
|
+
# NOTE on gateway we have have 'configuration' field in instance handle in replacement
|
49
|
+
# of the 'gateway_data' field as in ccdb handle, this is for a easy management/translation
|
50
|
+
# between gateway v1 and v2 provisioner code
|
51
|
+
:configuration => handle['gateway_data'],
|
52
|
+
:gateway_name => handle['credentials']['name'],
|
53
|
+
}
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def update_binding_handles(binding_handles)
|
58
|
+
binding_handles.each do |binding_id, binding_handle|
|
59
|
+
unless verify_binding_handle_format(binding_handle)
|
60
|
+
@logger.warn("Skip not well-formed binding handle:#{binding_handle}.")
|
61
|
+
next
|
62
|
+
end
|
63
|
+
|
64
|
+
handle = binding_handle.deep_dup
|
65
|
+
@service_bindings[binding_id] = {
|
66
|
+
:credentials => handle['credentials'],
|
67
|
+
# NOTE on gateway we have have 'configuration' field in binding handle in replacement
|
68
|
+
# of the 'gateway_data' field as in ccdb, this is for a easy management/translation
|
69
|
+
# between gateway v1 and v2 provisioner code
|
70
|
+
:configuration => handle['gateway_data'],
|
71
|
+
:gateway_name => handle['gateway_name'],
|
72
|
+
}
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def verify_instance_handle_format(handle)
|
77
|
+
return nil unless handle
|
78
|
+
return nil unless handle.is_a? Hash
|
79
|
+
|
80
|
+
VCAP::Services::Internal::ServiceInstanceHandleV2.new(handle)
|
81
|
+
true
|
82
|
+
rescue => e
|
83
|
+
@logger.warn("Verify v2 instance handle #{handle} failed:#{e}")
|
84
|
+
return nil
|
85
|
+
end
|
86
|
+
|
87
|
+
def verify_binding_handle_format(handle)
|
88
|
+
return nil unless handle
|
89
|
+
return nil unless handle.is_a? Hash
|
90
|
+
|
91
|
+
VCAP::Services::Internal::ServiceBindingHandleV2.new(handle)
|
92
|
+
true
|
93
|
+
rescue => e
|
94
|
+
@logger.warn("Verify v2 binding handle #{handle} failed:#{e}")
|
95
|
+
return nil
|
96
|
+
end
|
97
|
+
|
98
|
+
# indexing handles for (double) check orphan only
|
99
|
+
# @param: array handles
|
100
|
+
# handles[0] contains an array of instance handles
|
101
|
+
# handles[1] contains an array of binding handles
|
102
|
+
# returns: array of handles which contains two hashes for instance handles and binding handle.
|
103
|
+
def indexing_handles(handles)
|
104
|
+
instance_handles = {}
|
105
|
+
binding_handles = {}
|
106
|
+
handles[0].each { |instance_id, _| instance_handles[instance_id] = nil }
|
107
|
+
handles[1].each do |binding_id, binding_handle|
|
108
|
+
user = binding_handle["credentials"]["username"] || binding_handle["credentials"]["user"]
|
109
|
+
next unless user
|
110
|
+
key = binding_handle["credentials"]["name"] + user
|
111
|
+
binding_handles[key] = nil
|
112
|
+
end
|
113
|
+
[instance_handles, binding_handles]
|
114
|
+
end
|
115
|
+
|
116
|
+
def get_instance_id_list(node_id, &blk)
|
117
|
+
@logger.debug("Get instance id list for migration")
|
118
|
+
|
119
|
+
id_list = []
|
120
|
+
@service_instances.each do |service_id, entity|
|
121
|
+
id_list << service_id if node_id == entity[:credentials]["node_id"]
|
122
|
+
end
|
123
|
+
blk.call(success(id_list))
|
124
|
+
end
|
125
|
+
|
126
|
+
########
|
127
|
+
# Helpers
|
128
|
+
########
|
129
|
+
|
130
|
+
def get_all_instance_handles
|
131
|
+
instance_handles = @service_instances.deep_dup
|
132
|
+
instance_handles.each {|instance_id, handle| yield handle if block_given?}
|
133
|
+
instance_handles
|
134
|
+
end
|
135
|
+
|
136
|
+
def get_all_binding_handles
|
137
|
+
binding_handles = @service_bindings.deep_dup
|
138
|
+
binding_handles.each {|binding_id, handle| yield handle if block_given?}
|
139
|
+
binding_handles
|
140
|
+
end
|
141
|
+
|
142
|
+
def get_instance_handle(instance_id)
|
143
|
+
@service_instances[instance_id].deep_dup
|
144
|
+
end
|
145
|
+
|
146
|
+
def get_binding_handle(binding_id)
|
147
|
+
@service_bindings[binding_id].deep_dup
|
148
|
+
end
|
149
|
+
|
150
|
+
def add_instance_handle(entity)
|
151
|
+
# NOTE this handle contains a subset of the information of the handles in ccdb
|
152
|
+
# the handle will possess the full information after the next fetch handle operation
|
153
|
+
# on the gateway and update the corresponding handle; but these information is sufficient
|
154
|
+
# for current operations
|
155
|
+
@service_instances[entity[:service_id]] = {
|
156
|
+
:credentials => entity[:credentials],
|
157
|
+
:configuration => entity[:configuration],
|
158
|
+
:gateway_name => entity[:service_id],
|
159
|
+
}
|
160
|
+
end
|
161
|
+
|
162
|
+
def add_binding_handle(entity)
|
163
|
+
# NOTE this handle contains a subset of the information of the handles in ccdb
|
164
|
+
# the handle will possess the full information after the next fetch handle operation
|
165
|
+
# on the gateway and update the corresponding handle; but these information is sufficient
|
166
|
+
# for current operations
|
167
|
+
@service_bindings[entity[:service_id]] = {
|
168
|
+
:credentials => entity[:credentials],
|
169
|
+
:configuration => entity[:configuration],
|
170
|
+
:gateway_name => entity[:service_id],
|
171
|
+
}
|
172
|
+
end
|
173
|
+
|
174
|
+
def delete_instance_handle(instance_handle)
|
175
|
+
@service_instances.delete(instance_handle[:credentials]["name"])
|
176
|
+
end
|
177
|
+
|
178
|
+
def delete_binding_handle(binding_handle)
|
179
|
+
@service_bindings.delete(binding_handle[:gateway_name])
|
180
|
+
end
|
181
|
+
|
182
|
+
def find_instance_bindings(instance_id)
|
183
|
+
binding_handles = []
|
184
|
+
@service_bindings.each do |_, handle|
|
185
|
+
binding_handles << handle if handle[:credentials]["name"] == instance_id
|
186
|
+
end
|
187
|
+
binding_handles
|
188
|
+
end
|
189
|
+
|
190
|
+
def get_all_handles
|
191
|
+
@service_instances.merge(@service_bindings).deep_dup
|
192
|
+
end
|
193
|
+
end
|
data/lib/base/service.rb
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
require "base/plan"
|
2
|
+
require "base/service_plan_change_set"
|
3
|
+
|
4
|
+
module VCAP::Services
|
5
|
+
class Service
|
6
|
+
attr_reader :description, :provider, :version, :url, :info_url, :documentation_url, :plans, :tags,
|
7
|
+
:unique_id, :label, :active, :tags, :plan_options, :acls, :timeout,
|
8
|
+
:default_plan, :supported_versions, :version_aliases, :extra, :bindable
|
9
|
+
attr_accessor :guid
|
10
|
+
|
11
|
+
def initialize(attrs)
|
12
|
+
@unique_id = attrs.fetch('unique_id')
|
13
|
+
@label = attrs['label']
|
14
|
+
@active = attrs['active']
|
15
|
+
@active = true if @active.nil?
|
16
|
+
@tags = attrs['tags']
|
17
|
+
@plan_options = attrs['plan_options']
|
18
|
+
@acls = attrs['acls']
|
19
|
+
@timeout = attrs['timeout']
|
20
|
+
@default_plan = attrs['default_plan']
|
21
|
+
@supported_versions = attrs['supported_versions']
|
22
|
+
@version_aliases = attrs['version_aliases']
|
23
|
+
@extra = attrs.fetch('extra')
|
24
|
+
@info_url = attrs['info_url']
|
25
|
+
@documentation_url = attrs['documentation_url']
|
26
|
+
@bindable = attrs.fetch('bindable', true)
|
27
|
+
@guid = attrs['guid']
|
28
|
+
@description = attrs['description']
|
29
|
+
@provider = attrs.fetch('provider')
|
30
|
+
@version = attrs.fetch('version')
|
31
|
+
@url = attrs.fetch('url')
|
32
|
+
@plans = Plan.plan_hash_as_plan_array(attrs.fetch('plans'))
|
33
|
+
@tags = attrs['tags']
|
34
|
+
end
|
35
|
+
|
36
|
+
def create_change_set(service_in_ccdb)
|
37
|
+
|
38
|
+
if service_in_ccdb
|
39
|
+
service_guid = service_in_ccdb.guid
|
40
|
+
plans_to_update = []
|
41
|
+
plans_to_add = []
|
42
|
+
|
43
|
+
self.plans.each do |catalog_plan|
|
44
|
+
ccdb_plan = service_in_ccdb.plans.find { |ccp| catalog_plan.unique_id == ccp.unique_id }
|
45
|
+
ccdb_plan = service_in_ccdb.plans.find { |ccp| catalog_plan.name.to_s == ccp.name.to_s } unless ccdb_plan
|
46
|
+
|
47
|
+
if ccdb_plan
|
48
|
+
catalog_plan.guid = ccdb_plan.guid
|
49
|
+
plans_to_update << catalog_plan
|
50
|
+
else
|
51
|
+
plans_to_add << catalog_plan
|
52
|
+
end
|
53
|
+
end
|
54
|
+
else
|
55
|
+
service_guid = nil
|
56
|
+
plans_to_add = self.plans
|
57
|
+
plans_to_update = []
|
58
|
+
end
|
59
|
+
|
60
|
+
ServicePlanChangeSet.new(self, service_guid,
|
61
|
+
plans_to_add: plans_to_add,
|
62
|
+
plans_to_update: plans_to_update,
|
63
|
+
)
|
64
|
+
end
|
65
|
+
|
66
|
+
def to_hash
|
67
|
+
{
|
68
|
+
"description" => description,
|
69
|
+
"provider" => provider,
|
70
|
+
"version" => version,
|
71
|
+
"url" => url,
|
72
|
+
"documentation_url" => documentation_url,
|
73
|
+
"plans" => Plan.plans_array_to_hash(plans),
|
74
|
+
"unique_id" => unique_id,
|
75
|
+
"label" => label,
|
76
|
+
"active" => active,
|
77
|
+
"acls" => acls,
|
78
|
+
"timeout" => timeout,
|
79
|
+
"extra" => extra,
|
80
|
+
"bindable" => bindable,
|
81
|
+
"tags" => tags
|
82
|
+
}
|
83
|
+
end
|
84
|
+
|
85
|
+
def same_tuple?(other)
|
86
|
+
[self.label, self.provider, self.version].each { |x| return false if x.nil? }
|
87
|
+
|
88
|
+
(self.label == other.label &&
|
89
|
+
self.provider == other.provider &&
|
90
|
+
self.version == other.version)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -0,0 +1,184 @@
|
|
1
|
+
module VCAP::Services
|
2
|
+
class ServiceAdvertiser
|
3
|
+
attr_reader :logger, :active, :catalog_services, :registered_services
|
4
|
+
|
5
|
+
def initialize(options)
|
6
|
+
@catalog_services = options.fetch(:current_catalog)
|
7
|
+
@registered_services = options.fetch(:catalog_in_ccdb)
|
8
|
+
@http_handler = options.fetch(:http_handler)
|
9
|
+
@logger = options.fetch(:logger)
|
10
|
+
@active = options.fetch(:active, true)
|
11
|
+
@offering_uri = "/v2/services"
|
12
|
+
@service_plans_uri = "/v2/service_plans"
|
13
|
+
|
14
|
+
build_service_lists_and_prepare_guids
|
15
|
+
end
|
16
|
+
|
17
|
+
def advertise_services
|
18
|
+
logger.debug("CCNG Catalog Manager: Registered in ccng: #{registered_services.inspect}")
|
19
|
+
logger.debug("CCNG Catalog Manager: Current catalog: #{catalog_services.inspect}")
|
20
|
+
|
21
|
+
active_services.each do |active_service|
|
22
|
+
service_in_ccdb = registered_services.find do |registered_service|
|
23
|
+
active_service.guid == registered_service.guid
|
24
|
+
end
|
25
|
+
|
26
|
+
service_change_set = active_service.create_change_set(service_in_ccdb)
|
27
|
+
logger.debug("CCNG Catalog Manager: service_change_set = #{service_change_set.inspect}")
|
28
|
+
advertise_service_to_cc(active_service,
|
29
|
+
active_service.guid,
|
30
|
+
service_change_set.plans_to_add,
|
31
|
+
service_change_set.plans_to_update)
|
32
|
+
end
|
33
|
+
|
34
|
+
new_services.each do |service|
|
35
|
+
service_plan_change_set = service.create_change_set(nil)
|
36
|
+
logger.debug("CCNG Catalog Manager: plans_to_add = #{service_plan_change_set.plans_to_add.inspect}")
|
37
|
+
|
38
|
+
logger.debug("CCNG Catalog Manager: Add new offering: #{service.inspect}")
|
39
|
+
advertise_service_to_cc(service, nil, service_plan_change_set.plans_to_add, {}) # nil guid => new service, so add all plans
|
40
|
+
end
|
41
|
+
|
42
|
+
logger.info("CCNG Catalog Manager: Found #{active_services.size} active, #{disabled_count} disabled and #{new_services.size} new service offerings")
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
def active_count
|
47
|
+
active ? @catalog_services.size : 0
|
48
|
+
end
|
49
|
+
|
50
|
+
def disabled_count
|
51
|
+
active ? inactive_services.size : registered_services.size
|
52
|
+
end
|
53
|
+
|
54
|
+
def active_services
|
55
|
+
@active_services
|
56
|
+
end
|
57
|
+
|
58
|
+
def inactive_services
|
59
|
+
@inactive_services
|
60
|
+
end
|
61
|
+
|
62
|
+
def new_services
|
63
|
+
@new_services
|
64
|
+
end
|
65
|
+
|
66
|
+
private
|
67
|
+
|
68
|
+
def build_service_lists_and_prepare_guids
|
69
|
+
@inactive_services = registered_services.dup
|
70
|
+
@active_services = []
|
71
|
+
@new_services = []
|
72
|
+
|
73
|
+
@catalog_services.each do |service|
|
74
|
+
registered_service = registered_services.find { |rs| service.unique_id == rs.unique_id }
|
75
|
+
|
76
|
+
unless registered_service
|
77
|
+
registered_service = registered_services.find { |rs| service.same_tuple?(rs) }
|
78
|
+
|
79
|
+
if registered_service && service.unique_id
|
80
|
+
logger.warn("CCNG Catalog Manager: Service with unique id #{service.unique_id} in broker catalog matched service with unique id #{registered_service.unique_id} from cloud controller using label-version-provider tuple.")
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
if registered_service
|
85
|
+
service.guid = registered_service.guid
|
86
|
+
@active_services << service
|
87
|
+
@inactive_services.delete(registered_service)
|
88
|
+
else
|
89
|
+
@new_services << service
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def add_or_update_offering(offering, guid)
|
95
|
+
update = !guid.nil?
|
96
|
+
uri = update ? "#{@offering_uri}/#{guid}" : @offering_uri
|
97
|
+
service_guid = nil
|
98
|
+
|
99
|
+
logger.debug("CCNG Catalog Manager: #{update ? "Update" : "Advertise"} service offering #{offering.inspect} to cloud_controller: #{uri}")
|
100
|
+
|
101
|
+
offerings_hash = offering.to_hash
|
102
|
+
method = update ? "put" : "post"
|
103
|
+
if method == 'put'
|
104
|
+
offerings_hash.delete('unique_id')
|
105
|
+
end
|
106
|
+
offerings_hash.delete('plans')
|
107
|
+
@http_handler.cc_http_request(:uri => uri,
|
108
|
+
:method => method,
|
109
|
+
:body => Yajl::Encoder.encode(offerings_hash)) do |http|
|
110
|
+
if !http.error
|
111
|
+
if (200..299) === http.response_header.status
|
112
|
+
response = JSON.parse(http.response)
|
113
|
+
logger.info("CCNG Catalog Manager: Advertise offering response (code=#{http.response_header.status}): #{response.inspect}")
|
114
|
+
service_guid = response["metadata"]["guid"]
|
115
|
+
else
|
116
|
+
logger.error("CCNG Catalog Manager: Failed advertise offerings:#{offering.inspect}, status=#{http.response_header.status}")
|
117
|
+
end
|
118
|
+
else
|
119
|
+
logger.error("CCNG Catalog Manager: Failed advertise offerings:#{offering.inspect}: #{http.error}")
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
return service_guid
|
124
|
+
end
|
125
|
+
|
126
|
+
def advertise_service_to_cc(service, guid, plans_to_add, plans_to_update)
|
127
|
+
service_guid = add_or_update_offering(service, guid)
|
128
|
+
return false if service_guid.nil?
|
129
|
+
|
130
|
+
return true if !service.active # If deactivating, don't update plans
|
131
|
+
|
132
|
+
logger.debug("CCNG Catalog Manager: Processing plans for: #{service_guid} -Add: #{plans_to_add.size} plans, Update: #{plans_to_update.size} plans")
|
133
|
+
|
134
|
+
plans_to_add.each { |plan|
|
135
|
+
add_plan(plan, service_guid)
|
136
|
+
}
|
137
|
+
|
138
|
+
plans_to_update.each { |plan|
|
139
|
+
update_plan(plan, service_guid)
|
140
|
+
}
|
141
|
+
return true
|
142
|
+
end
|
143
|
+
|
144
|
+
def add_plan(plan, service_guid)
|
145
|
+
uri = @service_plans_uri
|
146
|
+
method = 'post'
|
147
|
+
plan_as_hash = plan.get_add_hash(service_guid)
|
148
|
+
|
149
|
+
logger.info("CCNG Catalog Manager: Add new plan #{plan.inspect} via #{uri}")
|
150
|
+
|
151
|
+
make_plan_request(uri, method, plan_as_hash, plan)
|
152
|
+
end
|
153
|
+
|
154
|
+
def update_plan(plan, service_guid)
|
155
|
+
uri = "#{@service_plans_uri}/#{plan.guid}"
|
156
|
+
method = 'put'
|
157
|
+
plan_as_hash = plan.get_update_hash(service_guid)
|
158
|
+
|
159
|
+
logger.info("CCNG Catalog Manager: Update plan (guid: #{plan.guid}) to #{plan.inspect} via #{uri}")
|
160
|
+
|
161
|
+
make_plan_request(uri, method, plan_as_hash, plan)
|
162
|
+
end
|
163
|
+
|
164
|
+
def make_plan_request(uri, method, plan_as_hash, plan)
|
165
|
+
add_plan = method == 'post'
|
166
|
+
|
167
|
+
@http_handler.cc_http_request(:uri => uri,
|
168
|
+
:method => method,
|
169
|
+
:body => Yajl::Encoder.encode(plan_as_hash)) do |http|
|
170
|
+
if !http.error
|
171
|
+
if (200..299) === http.response_header.status
|
172
|
+
logger.info("CCNG Catalog Manager: Successfully #{add_plan ? "added" : "updated"} service plan: #{plan.inspect}")
|
173
|
+
return true
|
174
|
+
else
|
175
|
+
logger.error("CCNG Catalog Manager: Failed to #{add_plan ? "add" : "update"} plan: #{plan.inspect}, status=#{http.response_header.status}")
|
176
|
+
end
|
177
|
+
else
|
178
|
+
logger.error("CCNG Catalog Manager: Failed to #{add_plan ? "add" : "update"} plan: #{plan.inspect}: #{http.error}")
|
179
|
+
end
|
180
|
+
end
|
181
|
+
return false
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|