forj 0.0.48 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,406 @@
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
+
18
+ # This class describes how to process some actions, and will do everything prior
19
+ # this task to make it to work.
20
+
21
+ require 'fog' # We use fog to access HPCloud
22
+
23
+ $HPCLOUD_PATH = File.expand_path(File.dirname(__FILE__))
24
+
25
+ require File.join($HPCLOUD_PATH, "compute.rb")
26
+ require File.join($HPCLOUD_PATH, "network.rb")
27
+ require File.join($HPCLOUD_PATH, "security_groups.rb")
28
+
29
+ # Defines Meta HPCloud object
30
+ class Hpcloud < BaseDefinition
31
+
32
+ define_obj :services
33
+ obj_needs :data, :account_id, :mapping => :hp_access_key
34
+ obj_needs :data, :account_key, :mapping => :hp_secret_key
35
+ obj_needs :data, :auth_uri, :mapping => :hp_auth_uri
36
+ obj_needs :data, :tenant, :mapping => :hp_tenant_id
37
+ obj_needs :data, ":excon_opts/:connect_timeout", :default_value => 30
38
+ obj_needs :data, ":excon_opts/:read_timeout", :default_value => 240
39
+ obj_needs :data, ":excon_opts/:write_timeout", :default_value => 240
40
+
41
+ # Defines Object structure and function stored on the Hpcloud class object.
42
+ # Compute Object
43
+ define_obj :compute_connection
44
+ # Defines Data used by compute.
45
+
46
+ obj_needs :data, :account_id, :mapping => :hp_access_key
47
+ obj_needs :data, :account_key, :mapping => :hp_secret_key
48
+ obj_needs :data, :auth_uri, :mapping => :hp_auth_uri
49
+ obj_needs :data, :tenant, :mapping => :hp_tenant_id
50
+ obj_needs :data, :compute, :mapping => :hp_avl_zone
51
+
52
+ define_obj :network_connection
53
+ obj_needs :data, :account_id, :mapping => :hp_access_key
54
+ obj_needs :data, :account_key, :mapping => :hp_secret_key
55
+ obj_needs :data, :auth_uri, :mapping => :hp_auth_uri
56
+ obj_needs :data, :tenant, :mapping => :hp_tenant_id
57
+ obj_needs :data, :network, :mapping => :hp_avl_zone
58
+
59
+ # Forj predefine following query mapping, used by ForjProcess
60
+ # id => id, name => name
61
+ # If we need to add another mapping, add
62
+ # query_mapping :id => :MyID
63
+ # If the query is not push through and Hash object, the Provider
64
+ # will needs to create his own mapping function.
65
+ define_obj :network
66
+ query_mapping :external, :router_external
67
+
68
+ define_obj :rule
69
+ obj_needs :data, :dir, :mapping => :direction
70
+ attr_value_mapping :IN, 'ingress'
71
+ attr_value_mapping :OUT, 'egress'
72
+
73
+ obj_needs :data, :proto, :mapping => :protocol
74
+ obj_needs :data, :port_min, :mapping => :port_range_min
75
+ obj_needs :data, :port_max, :mapping => :port_range_max
76
+ obj_needs :data, :addr_map, :mapping => :remote_ip_prefix
77
+ obj_needs :data, :sg_id, :mapping => :security_group_id
78
+
79
+ get_attr_mapping :dir, :direction
80
+ get_attr_mapping :proto, :protocol
81
+ get_attr_mapping :port_min, :port_range_min
82
+ get_attr_mapping :port_max, :port_range_max
83
+ get_attr_mapping :addr_map, :remote_ip_prefix
84
+ get_attr_mapping :sg_id, :security_group_id
85
+
86
+ define_obj :keypairs
87
+
88
+ undefine_attribute :id # Do not return any predefined ID
89
+
90
+ define_obj :router
91
+ # The FORJ gateway_network_id is extracted from Fog::HP::Network::Router[:external_gateway_info][:network_id]
92
+ get_attr_mapping :gateway_network_id, [:external_gateway_info, 'network_id']
93
+
94
+ # ************************************ SERVER Object
95
+ define_obj :server
96
+ get_attr_mapping :status, :state
97
+ attr_value_mapping :create, "BUILD"
98
+ attr_value_mapping :boot, :boot
99
+ attr_value_mapping :active, "ACTIVE"
100
+
101
+ # ************************************ SERVER log Object
102
+ define_obj :server_log
103
+
104
+ # Excon::Response object type
105
+ get_attr_mapping :output, "output"
106
+
107
+ # ************************************* Public IP Object
108
+ define_obj :public_ip
109
+ get_attr_mapping :server_id, :instance_id
110
+ get_attr_mapping :public_ip, :ip
111
+
112
+ # defines setup Cloud data (:account => true for setup)
113
+ define_data(:account_id, {
114
+ :account => true,
115
+ :desc => 'HPCloud Access Key (From horizon, user drop down, manage keys)',
116
+ :validate => /^[A-Z0-9]*$/
117
+ })
118
+ define_data(:account_key, {
119
+ :account => true,
120
+ :desc => 'HPCloud secret Key (From horizon, user drop down, manage keys)',
121
+ :encrypted => false,
122
+ :validate => /^.+/
123
+ })
124
+ define_data(:auth_uri, {
125
+ :account => true,
126
+ :desc => 'HPCloud Authentication service URL (default is HP Public cloud)',
127
+ :validate => /^http(s)?:\/\/.*$/,
128
+ :default_value => "https://region-a.geo-1.identity.hpcloudsvc.com:35357/v2.0/"
129
+ })
130
+ define_data(:tenant, {
131
+ :account => true,
132
+ :desc => 'HPCloud Tenant ID (from horizon, identity, projecs, Project ID)',
133
+ :validate => /^[0-9]+$/
134
+ })
135
+
136
+ define_data(:compute, {
137
+ :account => true,
138
+ :desc => 'HPCloud Compute service zone (Ex: region-a.geo-1)',
139
+ :depends_on => [:account_id, :account_key, :auth_uri,:tenant ],
140
+ :list_values => {
141
+ :query_type => :controller_call,
142
+ :object => :services,
143
+ :query_call => :get_services,
144
+ :query_params => { :list_services => [:Compute, :compute] },
145
+ :validate => :list_strict
146
+ }
147
+ })
148
+
149
+ define_data(:network, {
150
+ :account => true,
151
+ :desc => 'HPCloud Network service zone (Ex: region-a.geo-1)',
152
+ :depends_on => [:account_id, :account_key, :auth_uri,:tenant ],
153
+ :list_values => {
154
+ :query_type => :controller_call,
155
+ :object => :services ,
156
+ :query_call => :get_services,
157
+ :query_params => { :list_services => [:Networking, :network] },
158
+ :validate => :list_strict
159
+ }
160
+ })
161
+
162
+ data_value_mapping 'xsmall', "standard.xsmall"
163
+ data_value_mapping 'small', "standard.small"
164
+ data_value_mapping 'medium', "standard.medium"
165
+ data_value_mapping 'large', "standard.large"
166
+ data_value_mapping 'xlarge', "standard.xlarge"
167
+
168
+
169
+ end
170
+
171
+ # Following class describe how FORJ should handle HP Cloud objects.
172
+ # Except Cloud connection, all HPCloud objects management are described/called in HP* modules.
173
+ class HpcloudController < BaseController
174
+
175
+ def connect(sObjectType, hParams)
176
+ case sObjectType
177
+ when :services
178
+ Fog::HP.authenticate_v2(hParams[:hdata], hParams[:excon_opts])
179
+ when :compute_connection
180
+ Fog::Compute.new(hParams[:hdata].merge({:provider => :hp,:version => 'v2'}))
181
+ when :network_connection
182
+ Fog::HP::Network.new(hParams[:hdata])
183
+ else
184
+ forjError "'%s' is not a valid object for 'connect'" % sObjectType
185
+ end
186
+ end
187
+
188
+ def create(sObjectType, hParams)
189
+ case sObjectType
190
+ when :public_ip
191
+ required?(hParams, :compute_connection)
192
+ required?(hParams, :server)
193
+ HPCompute.server_assign_address(hParams[:compute_connection], hParams[:server])
194
+ when :server
195
+ required?(hParams, :compute_connection)
196
+ required?(hParams, :image)
197
+ required?(hParams, :network)
198
+ required?(hParams, :flavor)
199
+ required?(hParams, :keypairs)
200
+ required?(hParams, :security_groups)
201
+ required?(hParams, :server_name)
202
+ HPCompute.create_server(
203
+ hParams[:compute_connection],
204
+ hParams[:server_name], hParams[:security_groups],
205
+ hParams[:image], hParams[:network],
206
+ hParams[:flavor], hParams[:keypairs],
207
+ hParams[:user_data], hParams[:meta_data]
208
+ )
209
+ when :image
210
+ required?(hParams, :compute_connection)
211
+ required?(hParams, :image_name)
212
+ HPCompute.get_image(hParams[:compute_connection], hParams[:image_name])
213
+ when :network
214
+ required?(hParams, :network_connection)
215
+ required?(hParams, :network_name)
216
+ HPNetwork.create_network(hParams[:network_connection], hParams[:network_name])
217
+ when :subnetwork
218
+ required?(hParams, :network_connection)
219
+ required?(hParams, :network)
220
+ required?(hParams, :subnetwork_name)
221
+ HPNetwork.create_subnetwork(hParams[:network_connection], hParams[:network], hParams[:subnetwork_name])
222
+ when :security_group
223
+ required?(hParams, :network_connection)
224
+ required?(hParams, :security_group)
225
+ HPSecurityGroups.create_sg(hParams[:network_connection], hParams[:security_group], hParams[:sg_desc])
226
+ when :keypairs
227
+ required?(hParams, :compute_connection)
228
+ required?(hParams, :keypair_name)
229
+ required?(hParams, :public_key_file)
230
+ HPKeyPairs.create_keypair(hParams[:compute_connection], hParams[:keypair_name], hParams[:public_key_file])
231
+ when :router
232
+ required?(hParams, :network_connection)
233
+ if hParams.key?(:external_gateway_id)
234
+ hParams[:hdata][:external_gateway_info] = { 'network_id' => hParams[:external_gateway_id] }
235
+ end
236
+ hParams[:hdata] = hParams[:hdata].merge(:admin_state_up => true) # Forcelly used admin_status_up to true.
237
+
238
+ HPNetwork.create_router(hParams[:network_connection], hParams[:hdata])
239
+ when :rule
240
+ required?(hParams, :network_connection)
241
+ required?(hParams, :security_groups)
242
+ HPSecurityGroups.create_rule(hParams[:network_connection], hParams[:hdata])
243
+ else
244
+ forjError "'%s' is not a valid object for 'create'" % sObjectType
245
+ end
246
+ end
247
+
248
+ # This function return a collection which have to provide:
249
+ # functions: [], length, each
250
+ # Used by network process.
251
+ def query(sObjectType, sQuery, hParams)
252
+ case sObjectType
253
+ when :public_ip
254
+ required?(hParams, :compute_connection)
255
+ required?(hParams, :server)
256
+ HPCompute.query_server_assigned_addresses(hParams[:compute_connection], hParams[:server], sQuery)
257
+ when :server
258
+ required?(hParams, :compute_connection)
259
+ HPCompute.query_server(hParams[:compute_connection], sQuery)
260
+ when :image
261
+ required?(hParams, :compute_connection)
262
+ HPCompute.query_image(hParams[:compute_connection], sQuery)
263
+ when :network
264
+ required?(hParams, :network_connection)
265
+ HPNetwork.query_network(hParams[:network_connection], sQuery)
266
+ when :subnetwork
267
+ required?(hParams, :network_connection)
268
+ HPNetwork.query_subnetwork(hParams[:network_connection], sQuery)
269
+ when :router
270
+ required?(hParams, :network_connection)
271
+ HPNetwork.query_router(hParams[:network_connection], sQuery)
272
+ when :port
273
+ required?(hParams, :network_connection)
274
+ HPNetwork.query_port(hParams[:network_connection], sQuery)
275
+ when :security_groups
276
+ required?(hParams, :network_connection)
277
+ HPSecurityGroups.query_sg(hParams[:network_connection], sQuery)
278
+ when :rule
279
+ required?(hParams, :network_connection)
280
+ HPSecurityGroups.query_rule(hParams[:network_connection], sQuery)
281
+ when :keypairs
282
+ required?(hParams, :compute_connection)
283
+ HPKeyPairs.query_keypair(hParams[:compute_connection], sQuery)
284
+ when :flavor
285
+ required?(hParams, :compute_connection)
286
+ HPCompute.query_flavor(hParams[:compute_connection], sQuery)
287
+ else
288
+ forjError "'%s' is not a valid object for 'query'" % sObjectType
289
+ end
290
+ end
291
+
292
+ def delete(sObjectType, hParams)
293
+ case sObjectType
294
+ when :network
295
+ HPNetwork.delete_network(hParams[:network_connection], hParams[:network])
296
+ when :rule
297
+ HPSecurityGroups.delete_rule(hParams[:network_connection], hParams[:id])
298
+ hParams[:network_connection].security_group_rules.get(hParams[:id]).destroy
299
+ else
300
+ nil
301
+ end
302
+ end
303
+
304
+ def get(sObjectType, sUniqId, hParams)
305
+ case sObjectType
306
+ when :server_log
307
+ required?(hParams, :server)
308
+
309
+ hParams[:server].console_output(sUniqId)
310
+ when :server
311
+ required?(hParams, :compute_connection)
312
+ HPCompute.get_server(hParams[:compute_connection], sUniqId)
313
+ when :image
314
+ required?(hParams, :compute_connection)
315
+ HPCompute.get_image(hParams[:compute_connection], sUniqId)
316
+ when :network
317
+ required?(hParams, :network_connection)
318
+ HPNetwork.get_network(hParams[:network_connection], sUniqId)
319
+ else
320
+ forjError "'%s' is not a valid object for 'get'" % sObjectType
321
+ end
322
+ end
323
+
324
+ def query_each(oFogObject)
325
+ case oFogObject.class.to_s
326
+ when "Fog::HP::Network::Networks"
327
+ oFogObject.each { | value |
328
+ yield(value)
329
+ }
330
+ else
331
+ forjError "'%s' is not a valid list for 'each'" % oFogObject.class
332
+ end
333
+ end
334
+
335
+ def get_attr(oControlerObject, key)
336
+ begin
337
+ if oControlerObject.is_a?(Excon::Response)
338
+ rhGet(oControlerObject.data, :body, key)
339
+ else
340
+ attributes = oControlerObject.attributes
341
+ raise "attribute '%s' is unknown in '%s'. Valid one are : '%s'" % [key[0], oControlerObject.class, oControlerObject.class.attributes ] unless oControlerObject.class.attributes.include?(key[0])
342
+ rhGet(attributes, key)
343
+ end
344
+ rescue => e
345
+ forjError "Unable to map '%s'. %s" % [key, e.message]
346
+ end
347
+ end
348
+
349
+ def set_attr(oControlerObject, key, value)
350
+ begin
351
+ raise "No set feature for '%s'" % oControlerObject.class if oControlerObject.is_a?(Excon::Response)
352
+ attributes = oControlerObject.attributes
353
+ raise "attribute '%s' is unknown in '%s'. Valid one are : '%s'" % [key[0], oControlerObject.class, oControlerObject.class.attributes ] unless oControlerObject.class.attributes.include?(key[0])
354
+ rhSet(attributes, value, key)
355
+ rescue => e
356
+ forjError "Unable to map '%s' on '%s'" % [key, sObjectType]
357
+ end
358
+ end
359
+
360
+
361
+ def update(sObjectType, hParams)
362
+ case sObjectType
363
+ when :router
364
+ HPNetwork.update_router(hParams[:router])
365
+ else
366
+ forjError "'%s' is not a valid list for 'update'" % oFogObject.class
367
+ end
368
+ end
369
+
370
+ # This function requires to return an Array of values or nil.
371
+ def get_services(sObjectType, oParams)
372
+ case sObjectType
373
+ when :services
374
+ # oParams[sObjectType] will provide the controller object.
375
+ # This one can be interpreted only by controller code,
376
+ # except if controller declares how to map with this object.
377
+ # Processes can deal only process mapped data.
378
+ # Currently there is no services process function. No need to map.
379
+ hServices = oParams[:services]
380
+ if not oParams[:list_services].is_a?(Array)
381
+ hServiceToFind = [oParams[:list_services]]
382
+ else
383
+ hServiceToFind = oParams[:list_services]
384
+ end
385
+ # Search for service. Ex: Can be :Networking or network. I currently do not know why...
386
+ hSearchServices= rhGet(hServices, :service_catalog)
387
+ sService = nil
388
+ hServiceToFind.each { | sServiceElem |
389
+ if hSearchServices.key?(sServiceElem)
390
+ sService = sServiceElem
391
+ break
392
+ end
393
+ }
394
+
395
+ forjError "Unable to find services %s" % hServiceToFind if sService.nil?
396
+ result = rhGet(hServices, :service_catalog, sService).keys
397
+ result.delete("name")
398
+ result.each_index { | iIndex |
399
+ result[iIndex] = result[iIndex].to_s if result[iIndex].is_a?(Symbol)
400
+ }
401
+ return result
402
+ else
403
+ forjError "'%s' is not a valid object for 'get_services'" % sObjectType
404
+ end
405
+ end
406
+ end
@@ -0,0 +1,108 @@
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
+ module HPCompute
18
+ def HPCompute.get_server(oComputeConnect, sId)
19
+ oComputeConnect.servers.get(sId)
20
+ end
21
+
22
+ def HPCompute.query_addresses(oComputeConnect, sQuery)
23
+ oComputeConnect.addresses.all(sQuery)
24
+ end
25
+
26
+ def HPCompute.query_server(oComputeConnect, sQuery)
27
+ oComputeConnect.servers.all(sQuery)
28
+ end
29
+
30
+ def HPCompute.query_image(oComputeConnect, sQuery)
31
+ # HP Fog query is exact matching. No way to filter with a Regexp
32
+ # Testing it and filtering it.
33
+ # TODO: Be able to support Regexp in queries then extract all and filter.
34
+ oComputeConnect.images.all(sQuery)
35
+ end
36
+
37
+ def HPCompute.query_flavor(oComputeConnect, sQuery)
38
+ oComputeConnect.flavors.all(sQuery)
39
+ end
40
+
41
+ def HPCompute.create_server(oComputeConnect,
42
+ sServerName, oSecurity_groups,
43
+ oImage, oNetwork,
44
+ oFlavor, oKeypairs,
45
+ oUser_data, oMeta_data)
46
+
47
+ options = {
48
+ :name => sServerName,
49
+ :flavor_id => oFlavor.id,
50
+ :image_id => oImage.id,
51
+ :key_name => oKeypairs.name,
52
+ :security_groups => [oSecurity_groups.name],
53
+ :networks => [oNetwork.id]
54
+ }
55
+ options[:user_data_encoded] = Base64.strict_encode64(oUser_data) if oUser_data
56
+ options[:metadata] = oMeta_data if oMeta_data
57
+ server = oComputeConnect.servers.create(options)
58
+ HPCompute.get_server(oComputeConnect, server.id ) if server
59
+ end
60
+
61
+ def HPCompute.query_server_assigned_addresses(oComputeConnect, oServer, sQuery)
62
+ # CloudProcess used a simplified way to manage IPs.
63
+ # Following is the translation to get the public IPs for the server
64
+
65
+ result = []
66
+ oAddresses = oComputeConnect.addresses.all()
67
+ oAddresses.each { | oElem |
68
+ bFound = true
69
+ sQuery.each { | key, value |
70
+ if not oElem.attributes.key?(key) or oElem.attributes[key] != value
71
+ bFound = false
72
+ break
73
+ end
74
+ }
75
+ result << oElem if bFound
76
+ }
77
+ result
78
+ end
79
+
80
+ def HPCompute.server_assign_address(oComputeConnect, oServer)
81
+
82
+ while oServer.state != 'ACTIVE'
83
+ sleep(5)
84
+ oServer = oComputeConnect.servers.get(oServer.id)
85
+ end
86
+
87
+ oAddresses = oComputeConnect.addresses.all()
88
+ oAddress = nil
89
+ # Search for an available IP
90
+ oAddresses.each { | oElem |
91
+ if oElem.fixed_ip.nil?
92
+ oAddress = oElem
93
+ break
94
+ end
95
+ }
96
+
97
+ if oAddress.nil?
98
+ # Create a new public IP to add in the pool.
99
+ oAddress = oComputeConnect.addresses.create
100
+ end
101
+ raise "No Public IP to assign to server '%s'" % oServer.name if oAddress.nil?
102
+ oAddress.server = oServer # associate the server
103
+ oAddress.reload
104
+ # This function needs to returns a list of object.
105
+ # This list must support the each function.
106
+ oAddress
107
+ end
108
+ end
@@ -0,0 +1,107 @@
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
+
18
+ module HPNetwork
19
+ # Network driver
20
+ def HPNetwork.query_network(oNetworkConnect, sQuery)
21
+ oNetworkConnect.networks.all(sQuery)
22
+ end
23
+
24
+ def HPNetwork.create_network(oNetworkConnect, name)
25
+ oNetworkConnect.networks.create(:name => name)
26
+ end
27
+
28
+ def HPNetwork.delete_network(oNetworkConnect, oNetwork)
29
+ oNetworkConnect.networks.get(oNetwork.id).destroy
30
+ end
31
+
32
+ # SubNetwork driver
33
+ def HPNetwork.query_subnetwork(oNetworkConnect, sQuery)
34
+ oNetworkConnect.subnets.all(sQuery)
35
+ end
36
+
37
+ def HPNetwork.create_subnetwork(oNetworkConnect, oNetwork, name)
38
+ oNetworkConnect.subnets.create(
39
+ :network_id => oNetwork.id,
40
+ :name => name,
41
+ :cidr => get_next_subnet(oNetworkConnect),
42
+ :ip_version => '4'
43
+ )
44
+ end
45
+
46
+ def HPNetwork.delete_subnetwork(oNetworkConnect, oSubNetwork)
47
+ oNetworkConnect.subnets.get(oSubNetwork.id).destroy
48
+ end
49
+
50
+ def HPNetwork.get_next_subnet(oNetworkConnect)
51
+ begin
52
+ subnet_values = Array.new
53
+ subnets = oNetworkConnect.subnets.all
54
+
55
+ subnets.each do|s|
56
+ subnet_values.push(s.cidr)
57
+ end
58
+
59
+ gap = false
60
+ count = 0
61
+ range_used = Array.new
62
+ new_subnet = 0
63
+ new_cidr = ''
64
+
65
+ subnet_values = subnet_values.sort!
66
+
67
+ subnet_values.each do|value|
68
+ range_used.push(value[5])
69
+ end
70
+
71
+ range_used.each do |n|
72
+ if count.to_i == n.to_i
73
+ else
74
+ new_subnet = count
75
+ gap = true
76
+ break
77
+ end
78
+ count += 1
79
+ end
80
+
81
+ if gap
82
+ new_cidr = '10.0.%s.0/24' % [count]
83
+ else
84
+ max_value = range_used.max
85
+ new_subnet = max_value.to_i + 1
86
+ new_cidr = '10.0.%s.0/24' % [new_subnet]
87
+ end
88
+ new_cidr
89
+ rescue => e
90
+ Logging.error("%s\n%s" % [e.message, e.backtrace.join("\n")])
91
+ end
92
+ end
93
+
94
+ # router driver
95
+ def HPNetwork.query_router(oNetworkConnect, sQuery)
96
+ oNetworkConnect.routers.all(sQuery)
97
+ end
98
+
99
+ def HPNetwork.update_router(oRouter)
100
+ oRouters.save
101
+ end
102
+
103
+ # Port driver
104
+ def HPNetwork.query_port(oNetworkConnect, sQuery)
105
+ oNetworkConnect.ports.all(sQuery)
106
+ end
107
+ end
@@ -0,0 +1,67 @@
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
+ module HPSecurityGroups
18
+ def HPSecurityGroups.query_sg(oNetworkConnect, sQuery)
19
+ oNetworkConnect.security_groups.all(sQuery)
20
+ end
21
+
22
+ def HPSecurityGroups.create_sg(oNetwork, name, description)
23
+ params = {:name => name}
24
+ params[:description] = description if description
25
+ oFC.oNetwork.security_groups.create( params )
26
+ end
27
+
28
+ def HPSecurityGroups.create_rule(oNetwork, hData)
29
+ oNetwork.security_group_rules.create( hData )
30
+ end
31
+
32
+ def HPSecurityGroups.query_rule(oNetwork, sQuery)
33
+ oNetwork.security_group_rules.all(sQuery)
34
+ end
35
+
36
+ def HPSecurityGroups.delete_rule(oNetwork, rule_id)
37
+ oNetwork.security_group_rules.get(rule_id).destroy
38
+ end
39
+ end
40
+
41
+ module HPKeyPairs
42
+ def HPKeyPairs.query_keypair(oComputeConnect, sQuery)
43
+ cKeyPairs = oComputeConnect.key_pairs.all()
44
+ aResults = []
45
+ cKeyPairs.each { |sElem|
46
+ bSelect = true
47
+ attributes = sElem.instance_variable_get(:@attributes)
48
+ sQuery.each { | key, value |
49
+ if attributes[key] != value
50
+ bSelect = false
51
+ break
52
+ end
53
+ }
54
+ aResults.push sElem if bSelect
55
+ }
56
+ aResults
57
+ end
58
+
59
+ def HPKeyPairs.get_keypair(oComputeConnect, sId)
60
+ #byebug
61
+ oComputeConnect.key_pairs.get(sId)
62
+ end
63
+
64
+ def HPKeyPairs.create_keypair(oComputeConnect, name, pubkey)
65
+ oComputeConnect.key_pairs.create( :name => name, :public_key => pubkey)
66
+ end
67
+ end