lorj_cloud 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +9 -0
  3. data/.gitreview +4 -0
  4. data/.rspec +2 -0
  5. data/.rubocop.yml +46 -0
  6. data/Gemfile +4 -0
  7. data/README.md +39 -0
  8. data/Rakefile +43 -0
  9. data/bin/console +14 -0
  10. data/bin/setup +7 -0
  11. data/lib/data.yaml +0 -0
  12. data/lib/defaults.yaml +0 -0
  13. data/lib/lorj_cloud/version.rb +4 -0
  14. data/lib/lorj_cloud.rb +6 -0
  15. data/lib/process/cloud/process/common.rb +60 -0
  16. data/lib/process/cloud/process/connection.rb +92 -0
  17. data/lib/process/cloud/process/external_network.rb +90 -0
  18. data/lib/process/cloud/process/flavor.rb +97 -0
  19. data/lib/process/cloud/process/images.rb +99 -0
  20. data/lib/process/cloud/process/internet_network.rb +33 -0
  21. data/lib/process/cloud/process/internet_server.rb +29 -0
  22. data/lib/process/cloud/process/keypairs.rb +332 -0
  23. data/lib/process/cloud/process/network.rb +107 -0
  24. data/lib/process/cloud/process/public_ip.rb +106 -0
  25. data/lib/process/cloud/process/router.rb +267 -0
  26. data/lib/process/cloud/process/rules.rb +120 -0
  27. data/lib/process/cloud/process/security_groups.rb +120 -0
  28. data/lib/process/cloud/process/server.rb +127 -0
  29. data/lib/process/cloud/process/server_log.rb +34 -0
  30. data/lib/process/cloud/process/subnetwork.rb +96 -0
  31. data/lib/process/cloud_process.rb +30 -0
  32. data/lib/providers/hpcloud/compute.rb +105 -0
  33. data/lib/providers/hpcloud/hpcloud.rb +463 -0
  34. data/lib/providers/hpcloud/network.rb +115 -0
  35. data/lib/providers/hpcloud/security_groups.rb +68 -0
  36. data/lib/providers/mock/mock.rb +144 -0
  37. data/lib/providers/openstack/openstack.rb +410 -0
  38. data/lib/providers/openstack/openstack_create.rb +206 -0
  39. data/lib/providers/openstack/openstack_delete.rb +28 -0
  40. data/lib/providers/openstack/openstack_get.rb +39 -0
  41. data/lib/providers/openstack/openstack_process.rb +26 -0
  42. data/lib/providers/openstack/openstack_query.rb +96 -0
  43. data/lib/providers/openstack/openstack_update.rb +35 -0
  44. data/lib/providers/templates/compute.rb +40 -0
  45. data/lib/providers/templates/mycloud.rb +72 -0
  46. data/lib/providers/templates/network.rb +32 -0
  47. data/lorj_cloud.gemspec +32 -0
  48. metadata +175 -0
@@ -0,0 +1,410 @@
1
+ # encoding: UTF-8
2
+
3
+ # (c) Copyright 2014 Hewlett-Packard Development Company, L.P.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ # This class describes how to process some actions, and will do everything prior
18
+ # this task to make it to work.
19
+ require 'fog'
20
+ require 'uri'
21
+
22
+ hpcloud_path = File.expand_path(File.dirname(__FILE__))
23
+
24
+ require File.join(hpcloud_path, 'openstack_query.rb')
25
+ require File.join(hpcloud_path, 'openstack_get.rb')
26
+ require File.join(hpcloud_path, 'openstack_delete.rb')
27
+ require File.join(hpcloud_path, 'openstack_create.rb')
28
+ require File.join(hpcloud_path, 'openstack_update.rb')
29
+
30
+ # Defines Meta Openstack object
31
+ class Openstack
32
+ process_default :use_controller => true
33
+
34
+ define_obj :services
35
+ # Define Data used by service
36
+
37
+ obj_needs :data, :account_id, :mapping => :openstack_username
38
+ obj_needs :data, :account_key, :mapping => :openstack_api_key,
39
+ :decrypt => true
40
+ obj_needs :data, :auth_uri, :mapping => :openstack_auth_uri
41
+ obj_needs :data, :tenant, :mapping => :openstack_tenant
42
+ obj_needs :data, ':excon_opts/:connect_timeout', :default_value => 30
43
+ obj_needs :data, ':excon_opts/:read_timeout', :default_value => 240
44
+ obj_needs :data, ':excon_opts/:write_timeout', :default_value => 240
45
+
46
+ define_obj :compute_connection
47
+ # Defines Data used by compute.
48
+
49
+ obj_needs :data, :account_id, :mapping => :openstack_username
50
+ obj_needs :data, :account_key, :mapping => :openstack_api_key,
51
+ :decrypt => true
52
+ obj_needs :data, :auth_uri, :mapping => :openstack_auth_url
53
+ obj_needs :data, :tenant, :mapping => :openstack_tenant
54
+ obj_needs :data, :compute, :mapping => :openstack_region
55
+
56
+ define_obj :network_connection
57
+ obj_needs :data, :account_id, :mapping => :openstack_username
58
+ obj_needs :data, :account_key, :mapping => :openstack_api_key,
59
+ :decrypt => true
60
+ obj_needs :data, :auth_uri, :mapping => :openstack_auth_url
61
+ obj_needs :data, :tenant, :mapping => :openstack_tenant
62
+ obj_needs :data, :network, :mapping => :openstack_region
63
+
64
+ # Openstack tenants object
65
+ define_obj(:tenants, :create_e => :openstack_get_tenant)
66
+ obj_needs :CloudObject, :compute_connection
67
+ obj_needs :data, :tenant
68
+
69
+ # Openstack Network
70
+ define_obj :network
71
+ def_hdata :network_name, :mapping => :name
72
+ def_attr_mapping :external, :router_external
73
+
74
+ define_obj :keypairs
75
+
76
+ undefine_attribute :id # Do not return any predefined ID
77
+
78
+ define_obj :server_log
79
+
80
+ # Excon::Response object type
81
+ def_attr_mapping :output, 'output'
82
+
83
+ define_obj :security_groups
84
+ # Added tenant data to add in queries.
85
+ obj_needs :CloudObject, :tenants
86
+
87
+ define_obj :rule
88
+ obj_needs :data, :dir, :mapping => :direction
89
+ attr_value_mapping :IN, 'ingress'
90
+ attr_value_mapping :OUT, 'egress'
91
+
92
+ obj_needs :data, :proto, :mapping => :protocol
93
+ obj_needs :data, :port_min, :mapping => :port_range_min
94
+ obj_needs :data, :port_max, :mapping => :port_range_max
95
+ obj_needs :data, :addr_map, :mapping => :remote_ip_prefix
96
+ obj_needs :data, :sg_id, :mapping => :security_group_id
97
+
98
+ def_attr_mapping :dir, :direction
99
+ def_attr_mapping :proto, :protocol
100
+ def_attr_mapping :port_min, :port_range_min
101
+ def_attr_mapping :port_max, :port_range_max
102
+ def_attr_mapping :addr_map, :remote_ip_prefix
103
+ def_attr_mapping :sg_id, :security_group_id
104
+
105
+ define_data(:account_id,
106
+ :account => true,
107
+ :desc => 'Openstack Username',
108
+ :validate => /^.+/
109
+ )
110
+
111
+ define_data(:account_key,
112
+ :account => true,
113
+ :desc => 'Openstack Password',
114
+ :validate => /^.+/
115
+ )
116
+ define_data(:auth_uri,
117
+ :account => true,
118
+ :explanation => "The authentication service is identified as '"\
119
+ "identity' under your horizon UI - Project/Compute then "\
120
+ 'Access & security.',
121
+ :desc => 'Openstack Authentication service URL. '\
122
+ 'Ex: https://mycloud:5000/v2.0/tokens',
123
+ :validate => %r{^http(s)?:\/\/.*\/tokens$}
124
+ )
125
+ define_data(:tenant,
126
+ :account => true,
127
+ :explanation => 'The Project name is shown from your horizon UI'\
128
+ ', on top left, close to the logo',
129
+ :desc => 'Openstack Tenant Name',
130
+ :validate => /^.+/
131
+ )
132
+
133
+ define_data(:compute,
134
+ :account => true,
135
+ :default_value => '<%= config[:network] %>',
136
+ :explanation => 'Depending on your installation, you may need to'\
137
+ ' provide a Region name. This information is shown under your '\
138
+ 'horizon UI - close right to the project name (top left).'\
139
+ "\nYou can also get it from Project-Compute-Access & Security-"\
140
+ 'API, then download the Openstack RC file. The Region name is '\
141
+ 'set as OS_REGION_NAME.'\
142
+ "\nIf there is no region shown, you can ignore it.",
143
+ :desc => 'Openstack Compute Region (Ex: RegionOne)',
144
+ :depends_on => [:account_id, :account_key, :auth_uri, :tenant],
145
+ :list_values => {
146
+ :query_type => :controller_call,
147
+ :object => :services,
148
+ :query_call => :get_services,
149
+ :query_params => { :list_services => [:Compute, :compute] },
150
+ :validate => :list_strict
151
+ }
152
+ )
153
+
154
+ define_data(:network,
155
+ :account => true,
156
+ :default_value => '<%= config[:compute] %>',
157
+ :desc => 'Openstack Network Region (Ex: RegionOne)',
158
+ :explanation => 'Depending on your installation, you may need to'\
159
+ ' provide a Region name. This information is shown under your '\
160
+ 'horizon UI - close right to the project name (top left).'\
161
+ "\nYou can also get it from Project-Compute-Access & Security-"\
162
+ 'API, then download the Openstack RC file. The Region name is '\
163
+ 'set as OS_REGION_NAME.'\
164
+ "\nIf there is no region shown, you can ignore it.",
165
+ :depends_on => [:account_id, :account_key, :auth_uri, :tenant],
166
+ :list_values => {
167
+ :query_type => :controller_call,
168
+ :object => :services,
169
+ :query_call => :get_services,
170
+ :query_params => { :list_services => [:Networking, :network] },
171
+ :validate => :list_strict
172
+ }
173
+ )
174
+
175
+ define_obj :server
176
+ def_attr_mapping :status, :state
177
+ attr_value_mapping :create, 'BUILD'
178
+ attr_value_mapping :boot, :boot
179
+ attr_value_mapping :active, 'ACTIVE'
180
+ attr_value_mapping :error, 'ERROR'
181
+
182
+ def_attr_mapping :private_ip_address, :accessIPv4
183
+ def_attr_mapping :public_ip_address, :accessIPv4
184
+ def_attr_mapping :image_id, [:image, 'id']
185
+
186
+ define_obj :router
187
+ obj_needs_optional
188
+ obj_needs :data, :router_name, :mapping => :name
189
+
190
+ # The FORJ gateway_network_id is extracted
191
+ # from Fog::HP::Network::Router[:external_gateway_info][:network_id]
192
+
193
+ obj_needs :data,
194
+ :external_gateway_id,
195
+ :mapping => [:external_gateway_info, 'network_id']
196
+
197
+ def_attr_mapping :gateway_network_id, [:external_gateway_info, 'network_id']
198
+
199
+ # Port attributes used specifically by openstack fog API.
200
+ define_obj :port
201
+ def_attribute :device_owner
202
+ def_attribute :network_id
203
+
204
+ define_obj :public_ip
205
+ def_attr_mapping :server_id, :instance_id
206
+ def_attr_mapping :public_ip, :ip
207
+
208
+ define_obj :image
209
+ def_attr_mapping :image_name, :name
210
+ end
211
+
212
+ # Following class describe how FORJ should handle Openstack Cloud objects.
213
+ class OpenstackController
214
+ def self.def_cruds(*crud_types)
215
+ crud_types.each do |crud_type|
216
+ case crud_type
217
+ when :create, :delete
218
+ base_method(crud_type)
219
+ when :query, :get
220
+ query_method(crud_type)
221
+ when :update
222
+ update_method(crud_type)
223
+ end
224
+ end
225
+ end
226
+
227
+ def self.update_method(crud_type)
228
+ define_method(crud_type) do |sObjectType, obj, hParams|
229
+ method_name = "#{crud_type}_#{sObjectType}"
230
+ if self.class.method_defined? method_name
231
+ send(method_name, obj, hParams)
232
+ else
233
+ controller_error "'%s' is not a valid object for '%s'",
234
+ sObjectType, crud_type
235
+ end
236
+ end
237
+ end
238
+
239
+ def self.query_method(crud_type)
240
+ define_method(crud_type) do |sObjectType, sCondition, hParams|
241
+ method_name = "#{crud_type}_#{sObjectType}"
242
+ if self.class.method_defined? method_name
243
+ send(method_name, hParams, sCondition)
244
+ else
245
+ controller_error "'%s' is not a valid object for '%s'",
246
+ sObjectType, crud_type
247
+ end
248
+ end
249
+ end
250
+
251
+ def self.base_method(crud_type)
252
+ define_method(crud_type) do |sObjectType, hParams|
253
+ method_name = "#{crud_type}_#{sObjectType}"
254
+ if self.class.method_defined? method_name
255
+ send(method_name, hParams)
256
+ else
257
+ controller_error "'%s' is not a valid object for '%s'",
258
+ sObjectType, crud_type
259
+ end
260
+ end
261
+ end
262
+
263
+ # Define the Openstack controller handlers
264
+ def_cruds :create, :delete, :get, :query, :update
265
+
266
+ def connect(sObjectType, hParams)
267
+ case sObjectType
268
+ when :services
269
+ # Fog use URI type for auth uri: URI.parse(:auth_uri)
270
+ # Convert openstack_auth_uri to type URI
271
+ hParams[:hdata][:openstack_auth_uri] =
272
+ URI.parse(hParams[:hdata][:openstack_auth_uri])
273
+ retrieve_result =
274
+ Fog::OpenStack.retrieve_tokens_v2(hParams[:hdata],
275
+ hParams[:excon_opts])
276
+ creds = format_retrieve_result(retrieve_result)
277
+ return creds
278
+ when :compute_connection
279
+ Fog::Compute.new(
280
+ hParams[:hdata].merge(:provider => :openstack)
281
+ )
282
+ when :network_connection
283
+ Fog::Network::OpenStack.new(hParams[:hdata])
284
+ else
285
+ controller_error "'%s' is not a valid object for 'connect'", sObjectType
286
+ end
287
+ end
288
+
289
+ def set_attr(oControlerObject, key, value)
290
+ if oControlerObject.is_a?(Excon::Response)
291
+ controller_error "No set feature for '%s'", oControlerObject.class
292
+ end
293
+
294
+ attributes = oControlerObject.attributes
295
+
296
+ controller_error "attribute '%s' is unknown in '%s'. Valid one are : '%s'",
297
+ key[0],
298
+ oControlerObject.class,
299
+ oControlerObject.class.attributes unless
300
+ oControlerObject.class.attributes.include?(key[0])
301
+
302
+ attributes.rh_set(value, key)
303
+ rescue => e
304
+ controller_error "Unable to map '%s' on '%s'. %s",
305
+ key, oControlerObject, e.message
306
+ end
307
+
308
+ def get_attr(oControlerObject, key)
309
+ if oControlerObject.is_a?(Excon::Response)
310
+ oControlerObject.data.rh_get(:body, key)
311
+ else
312
+ attributes = oControlerObject.attributes
313
+ controller_error "attribute '%s' is unknown in '%s'."\
314
+ " Valid one are : '%s'",
315
+ key[0],
316
+ oControlerObject.class,
317
+ oControlerObject.class.attributes unless
318
+ oControlerObject.class.attributes.include?(key[0])
319
+ return attributes.rh_get(key) if attributes.rh_exist?(key)
320
+ _get_instance_attr(oControlerObject, key)
321
+ end
322
+ rescue => e
323
+ controller_error "==>Unable to map '%s'. %s", key, e.message
324
+ end
325
+
326
+ def _get_instance_attr(oControlerObject, key)
327
+ return nil if oControlerObject.send(key[0]).nil?
328
+ return oControlerObject.send(key[0]) if key.length == 1
329
+ oControlerObject.send(key[0]).rh_get(key[1..-1])
330
+ end
331
+ end
332
+
333
+ # Following class describe how FORJ should handle Openstack Cloud objects.
334
+ class OpenstackController
335
+ # This function requires to return an Array of values or nil.
336
+ def get_services(sObjectType, oParams)
337
+ case sObjectType
338
+ when :services
339
+ # oParams[sObjectType] will provide the controller object.
340
+ # This one can be interpreted only by controller code,
341
+ # except if controller declares how to map with this object.
342
+ # Processes can deal only process mapped data.
343
+ # Currently there is no services process function. No need to map.
344
+ services = oParams[:services]
345
+ if !oParams[:list_services].is_a?(Array)
346
+ service_to_find = [oParams[:list_services]]
347
+ else
348
+ service_to_find = oParams[:list_services]
349
+ end
350
+ # Search for service. Ex: Can be :Networking or network. I currently do
351
+ # not know why...
352
+ search_services = services.rh_get(:service_catalog)
353
+ service = nil
354
+ service_to_find.each do |sServiceElem|
355
+ if search_services.key?(sServiceElem)
356
+ service = sServiceElem
357
+ break
358
+ end
359
+ end
360
+
361
+ controller_error 'Unable to find services %s',
362
+ service_to_find if service.nil?
363
+ result = services.rh_get(:service_catalog, service).keys
364
+ result.delete('name')
365
+ result.each_index do |iIndex|
366
+ result[iIndex] = result[iIndex].to_s if result[iIndex].is_a?(Symbol)
367
+ end
368
+ return result
369
+ else
370
+ controller_error "'%s' is not a valid object for 'get_services'",
371
+ sObjectType
372
+ end
373
+ end
374
+
375
+ def format_retrieve_result(retrieve_result)
376
+ {
377
+ :auth_token => retrieve_result['access']['token']['id'],
378
+ :expires => retrieve_result['access']['token']['expires'],
379
+ :service_catalog =>
380
+ get_service_catalog(retrieve_result['access']['serviceCatalog']),
381
+ :endpoint_url => nil,
382
+ :cdn_endpoint_url => nil
383
+ }
384
+ end
385
+
386
+ def get_service_catalog(body)
387
+ fail 'Unable to parse service catalog.' unless body
388
+ service_catalog = {}
389
+ body.each do |s|
390
+ type = s['type']
391
+ next if type.nil?
392
+ type = type.to_sym
393
+ next if s['endpoints'].nil?
394
+ service_catalog[type] = {}
395
+ service_catalog[type]['name'] = s['name']
396
+ service_catalog = parse_service_catalog_endpoint(s, type, service_catalog)
397
+ end
398
+ service_catalog
399
+ end
400
+
401
+ def parse_service_catalog_endpoint(s, type, service_catalog)
402
+ s['endpoints'].each do |ep|
403
+ next if ep['region'].nil?
404
+ next if ep['publicURL'].nil?
405
+ next if ep['publicURL'].empty?
406
+ service_catalog[type][ep['region'].to_sym] = ep['publicURL']
407
+ end
408
+ service_catalog
409
+ end
410
+ end
@@ -0,0 +1,206 @@
1
+ # encoding: UTF-8
2
+
3
+ # (c) Copyright 2014 Hewlett-Packard Development Company, L.P.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ # Defined Openstack object create.
18
+ class OpenstackController
19
+ def create_security_groups(hParams)
20
+ required?(hParams, :network_connection)
21
+ required?(hParams, :tenants)
22
+ required?(hParams, :security_group)
23
+
24
+ service = hParams[:network_connection]
25
+
26
+ service.security_groups.create(:name => hParams[:security_group],
27
+ :tenant_id => hParams[:tenants].id)
28
+ end
29
+
30
+ def create_network(hParams)
31
+ required?(hParams, :network_connection)
32
+ required?(hParams, :network_name)
33
+
34
+ hParams[:network_connection].networks.create(hParams[:hdata])
35
+ end
36
+
37
+ def create_subnetwork(hParams)
38
+ required?(hParams, :network_connection)
39
+ required?(hParams, :network)
40
+
41
+ netconn = hParams[:network_connection]
42
+ netconn.subnets.create(
43
+ :network_id => hParams[:network].id,
44
+ :name => hParams[:subnetwork_name],
45
+ :cidr => get_next_subnet(netconn),
46
+ :ip_version => '4'
47
+ )
48
+ end
49
+
50
+ def create_rule(hParams)
51
+ required?(hParams, :network_connection)
52
+ required?(hParams, :security_groups)
53
+ hParams[:network_connection].security_group_rules.create(hParams[:hdata])
54
+ end
55
+
56
+ def create_router(hParams)
57
+ required?(hParams, :network_connection)
58
+ required?(hParams, :router_name)
59
+
60
+ # Forcelly used admin_status_up to true. Coming from HPCloud.
61
+ # But not sure if we need it or not.
62
+ # hParams[:hdata] = hParams[:hdata].merge(:admin_state_up => true)
63
+
64
+ hParams[:network_connection].routers.create(hParams[:hdata])
65
+ end
66
+
67
+ def create_router_interface(hParams)
68
+ required?(hParams, :network_connection)
69
+ required?(hParams, :router)
70
+ required?(hParams, :subnetwork)
71
+
72
+ service = hParams[:network_connection]
73
+ router = hParams[:router]
74
+ result = service.add_router_interface(router.id, hParams[:subnetwork].id)
75
+ fail if result.status != 200
76
+ result
77
+ end
78
+
79
+ def create_keypairs(hParams)
80
+ required?(hParams, :compute_connection)
81
+ required?(hParams, :keypair_name)
82
+ required?(hParams, :public_key)
83
+
84
+ # API:
85
+ # https://github.com/fog/fog/blob/master/lib/fog/openstack/docs/compute.md
86
+ service = hParams[:compute_connection]
87
+ service.key_pairs.create(:name => hParams[:keypair_name],
88
+ :public_key => hParams[:public_key])
89
+ end
90
+
91
+ def create_server(hParams)
92
+ [:compute_connection, :image,
93
+ :network, :flavor, :keypairs,
94
+ :security_groups, :server_name].each do |required_param|
95
+ required?(hParams, required_param)
96
+ end
97
+
98
+ options = {
99
+ :name => hParams[:server_name],
100
+ :flavor_ref => hParams[:flavor].id,
101
+ :image_ref => hParams[:image].id,
102
+ :key_name => hParams[:keypairs].name,
103
+ :security_groups => [hParams[:security_groups].name],
104
+ :nics => [{ :net_id => hParams[:network].id }]
105
+ }
106
+
107
+ if hParams[:user_data]
108
+ options[:user_data_encoded] =
109
+ Base64.strict_encode64(hParams[:user_data])
110
+ end
111
+ options[:metadata] = hParams[:meta_data] if hParams[:meta_data]
112
+
113
+ compute_connect = hParams[:compute_connection]
114
+
115
+ server = compute_connect.servers.create(options)
116
+ compute_connect.servers.get(server.id) if server
117
+ end
118
+
119
+ def create_public_ip(hParams)
120
+ required?(hParams, :compute_connection)
121
+ required?(hParams, :server)
122
+
123
+ compute_connect = hParams[:compute_connection]
124
+ server = hParams[:server]
125
+
126
+ while server.state != 'ACTIVE'
127
+ sleep(5)
128
+ server = compute_connect.servers.get(server.id)
129
+ return if server.state == 'ERROR'
130
+ end
131
+
132
+ addresses = compute_connect.addresses.all
133
+ address = nil
134
+ # Search for an available IP
135
+ addresses.each do |elem|
136
+ if elem.fixed_ip.nil?
137
+ address = elem
138
+ break
139
+ end
140
+ end
141
+
142
+ address = allocate_new_ip(compute_connect) if address.nil?
143
+ if address.nil?
144
+ controller_error("No Public IP to assign to server '%s'", server.name)
145
+ end
146
+
147
+ address.server = server # associate the server
148
+ address.reload
149
+ # This function needs to returns a list of object.
150
+ # This list must support the each function.
151
+ address
152
+ end
153
+
154
+ def allocate_new_ip(compute_connect)
155
+ # Create a new public IP to add in the pool.
156
+ pools = compute_connect.addresses.get_address_pools
157
+ controller_error('No IP Pool found') if pools.length == 0
158
+ # TODO: Be able to support choice of pool at setup time.
159
+ if pools.length > 1
160
+ Lorj.warning('Several pools found. Selecting the first one.')
161
+ end
162
+ compute_connect.addresses.create 'pool' => pools[0]['name']
163
+ end
164
+
165
+ def get_next_subnet(oNetworkConnect)
166
+ subnet_values = []
167
+ subnets = oNetworkConnect.subnets.all
168
+
169
+ subnets.each do|s|
170
+ subnet_values.push(s.cidr)
171
+ end
172
+
173
+ gap = false
174
+ count = 0
175
+ range_used = []
176
+ new_subnet = 0
177
+ new_cidr = ''
178
+
179
+ subnet_values = subnet_values.sort!
180
+
181
+ subnet_values.each do|value|
182
+ range_used.push(value[5])
183
+ end
184
+
185
+ range_used.each do |n|
186
+ if count.to_i == n.to_i
187
+ else
188
+ new_subnet = count
189
+ gap = true
190
+ break
191
+ end
192
+ count += 1
193
+ end
194
+
195
+ if gap
196
+ new_cidr = format('10.0.%s.0/24', count)
197
+ else
198
+ max_value = range_used.max
199
+ new_subnet = max_value.to_i + 1
200
+ new_cidr = format('10.0.%s.0/24', new_subnet)
201
+ end
202
+ new_cidr
203
+ rescue => e
204
+ Logging.error("%s\n%s", e.message, e.backtrace.join("\n"))
205
+ end
206
+ end
@@ -0,0 +1,28 @@
1
+ # encoding: UTF-8
2
+
3
+ # (c) Copyright 2014 Hewlett-Packard Development Company, L.P.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ # Defined Openstack object delete.
18
+ class OpenstackController
19
+ def delete_server(hParams)
20
+ required?(hParams, :compute_connection)
21
+ required?(hParams, :server)
22
+
23
+ compute_connect = hParams[:compute_connection]
24
+ server = hParams[:server]
25
+
26
+ compute_connect.servers.get(server.id).destroy
27
+ end
28
+ end
@@ -0,0 +1,39 @@
1
+ # encoding: UTF-8
2
+
3
+ # (c) Copyright 2014 Hewlett-Packard Development Company, L.P.
4
+ #
5
+ # Licensed under the Apache License, Version 2.0 (the "License");
6
+ # you may not use this file except in compliance with the License.
7
+ # You may obtain a copy of the License at
8
+ #
9
+ # http://www.apache.org/licenses/LICENSE-2.0
10
+ #
11
+ # Unless required by applicable law or agreed to in writing, software
12
+ # distributed under the License is distributed on an "AS IS" BASIS,
13
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ # See the License for the specific language governing permissions and
15
+ # limitations under the License.
16
+
17
+ # Defined Openstack object get.
18
+ class OpenstackController
19
+ def self.def_get(connection, name, property_name = nil)
20
+ property_name = property_name.nil? ? "#{name}s" : property_name.to_s
21
+ define_method("get_#{name}") do |hParams, sUniqId|
22
+ required?(hParams, connection)
23
+ hParams[connection].send(property_name).get(sUniqId)
24
+ end
25
+ end
26
+
27
+ def_get :compute_connection, :server
28
+
29
+ def_get :compute_connection, :image
30
+
31
+ def_get :network_connection, :network
32
+
33
+ def_get :compute_connection, :keypairs, :key_pairs
34
+
35
+ def get_server_log(hParams, sUniqId)
36
+ required?(hParams, :server)
37
+ hParams[:server].console(sUniqId)
38
+ end
39
+ end