lorj_cloud 0.1.3 → 0.1.4

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9a6728ac07fa2967a77fccc30673346724180474
4
- data.tar.gz: 0eda6415e42c3438d42ddd65a7e7225cd2c6254a
3
+ metadata.gz: 117a39e2598d4263f58768a8511b41748bb1fdae
4
+ data.tar.gz: 6d6b42bb3e7d2d4b68e5e7e0e23b2ae17e813a16
5
5
  SHA512:
6
- metadata.gz: dd1430bfbc8595a5b8d1300b73e29ddec98ee2ecde43a41863d167efeeaa96a7bcc8afc6b752ff4911b8f147052738dbad10db0a823972ccb064fc68ec8d304e
7
- data.tar.gz: 9a7910cfb451762e16e04977b6e0e63c11d7500768eac414ef22f5aa03096a1a3d00fa9574145f0492f2025842fc42e0865a6b3394389324946bac2cf4d7c545
6
+ metadata.gz: 2b8e84fca2d746bfcf44247ddf0254c05aa18b781bb097b0b3f854e38384f85655391766707d771b05b5802ee719dafe18b8d6dad480ee552ced2f3063e0aeb0
7
+ data.tar.gz: 552715dbf1cd40919058b2572ea01f639fb91b5a220d6ce773caf891072745aa37e17cd4610319178e78b232a2c6bacfd9a4b8ddf40b6f61dbe9693d00e72d02
@@ -1,5 +1,5 @@
1
1
  # LorjCloud module
2
2
  module LorjCloud
3
- VERSION = '0.1.3'
4
- DATE = '2015-06-22'
3
+ VERSION = '0.1.4'
4
+ DATE = '2015-06-24'
5
5
  end
@@ -50,7 +50,8 @@ class CloudProcess
50
50
  def forj_query_server(sCloudObj, sQuery, _hParams)
51
51
  ssl_error_obj = SSLErrorMgt.new
52
52
  begin
53
- query_single(sCloudObj, sQuery, config[:search_for])
53
+ controller_query(sCloudObj, sQuery)
54
+ # query_single(sCloudObj, sQuery, config[:search_for])
54
55
  rescue => e
55
56
  retry unless ssl_error_obj.error_detected(e.message, e.backtrace, e)
56
57
  end
@@ -24,21 +24,6 @@ module HPCompute
24
24
  oComputeConnect.addresses.all(sQuery)
25
25
  end
26
26
 
27
- def self.query_server(oComputeConnect, sQuery)
28
- oComputeConnect.servers.all(sQuery)
29
- end
30
-
31
- def self.query_image(oComputeConnect, sQuery)
32
- # HP Fog query is exact matching. No way to filter with a Regexp
33
- # Testing it and filtering it.
34
- # TODO: Be able to support Regexp in queries then extract all and filter.
35
- oComputeConnect.images.all(sQuery)
36
- end
37
-
38
- def self.query_flavor(oComputeConnect, sQuery)
39
- oComputeConnect.flavors.all(sQuery)
40
- end
41
-
42
27
  def self.create_server(oComputeConnect, options, oUser_data, oMeta_data)
43
28
  if oUser_data
44
29
  options[:user_data_encoded] = Base64.strict_encode64(oUser_data)
@@ -48,25 +33,6 @@ module HPCompute
48
33
  HPCompute.get_server(oComputeConnect, server.id) if server
49
34
  end
50
35
 
51
- def self.query_server_assigned_addresses(oComputeConnect, _oServer, sQuery)
52
- # CloudProcess used a simplified way to manage IPs.
53
- # Following is the translation to get the public IPs for the server
54
-
55
- result = []
56
- addresses = oComputeConnect.addresses.all
57
- addresses.each do |oElem|
58
- is_found = true
59
- sQuery.each do |key, value|
60
- if !oElem.attributes.key?(key) || oElem.attributes[key] != value
61
- is_found = false
62
- break
63
- end
64
- end
65
- result << oElem if is_found
66
- end
67
- result
68
- end
69
-
70
36
  def self.get_server_assigned_address(oComputeConnect, id)
71
37
  addresses = oComputeConnect.addresses.all
72
38
  addresses.each { |oElem| return oElem if oElem.attributes['id'] == id }
@@ -21,164 +21,14 @@ require 'fog' # We use fog to access HPCloud
21
21
 
22
22
  hpcloud_path = File.expand_path(File.dirname(__FILE__))
23
23
 
24
+ load File.join(hpcloud_path, 'hpcloud_declare.rb')
25
+ load File.join(hpcloud_path, 'hpcloud_generic.rb')
26
+ load File.join(hpcloud_path, 'hpcloud_query.rb')
27
+
24
28
  load File.join(hpcloud_path, 'compute.rb')
25
29
  load File.join(hpcloud_path, 'network.rb')
26
30
  load File.join(hpcloud_path, 'security_groups.rb')
27
31
 
28
- # Defines Meta HPCloud object
29
- class Hpcloud
30
- define_obj :services
31
- obj_needs :data, 'credentials#account_id', :mapping => :hp_access_key
32
- obj_needs :data, 'credentials#account_key', :mapping => :hp_secret_key
33
- obj_needs :data, 'credentials#auth_uri', :mapping => :hp_auth_uri
34
- obj_needs :data, 'credentials#tenant', :mapping => :hp_tenant_id
35
- obj_needs :data, ':excon_opts/:connect_timeout', :default_value => 30
36
- obj_needs :data, ':excon_opts/:read_timeout', :default_value => 240
37
- obj_needs :data, ':excon_opts/:write_timeout', :default_value => 240
38
-
39
- # Defines Object structure and function stored on the Hpcloud class object.
40
- # Compute Object
41
- define_obj :compute_connection
42
- # Defines Data used by compute.
43
-
44
- obj_needs :data, 'credentials#account_id', :mapping => :hp_access_key
45
- obj_needs :data, 'credentials#account_key', :mapping => :hp_secret_key
46
- obj_needs :data, 'credentials#auth_uri', :mapping => :hp_auth_uri
47
- obj_needs :data, 'credentials#tenant', :mapping => :hp_tenant_id
48
- obj_needs :data, 'services#compute', :mapping => :hp_avl_zone
49
-
50
- define_obj :network_connection
51
- obj_needs :data, 'credentials#account_id', :mapping => :hp_access_key
52
- obj_needs :data, 'credentials#account_key', :mapping => :hp_secret_key
53
- obj_needs :data, 'credentials#auth_uri', :mapping => :hp_auth_uri
54
- obj_needs :data, 'credentials#tenant', :mapping => :hp_tenant_id
55
- obj_needs :data, 'services#network', :mapping => :hp_avl_zone
56
-
57
- # Forj predefine following query mapping, used by ForjProcess
58
- # id => id, name => name
59
- # If we need to add another mapping, add
60
- # query_mapping :id => :MyID
61
- # If the query is not push through and Hash object, the Provider
62
- # will needs to create his own mapping function.
63
- define_obj :network
64
- def_attr_mapping :external, :router_external
65
-
66
- define_obj :rule
67
- obj_needs :data, :dir, :mapping => :direction
68
- attr_value_mapping :IN, 'ingress'
69
- attr_value_mapping :OUT, 'egress'
70
-
71
- obj_needs :data, :proto, :mapping => :protocol
72
- obj_needs :data, :port_min, :mapping => :port_range_min
73
- obj_needs :data, :port_max, :mapping => :port_range_max
74
- obj_needs :data, :addr_map, :mapping => :remote_ip_prefix
75
- obj_needs :data, :sg_id, :mapping => :security_group_id
76
-
77
- def_attr_mapping :dir, :direction
78
- def_attr_mapping :proto, :protocol
79
- def_attr_mapping :port_min, :port_range_min
80
- def_attr_mapping :port_max, :port_range_max
81
- def_attr_mapping :addr_map, :remote_ip_prefix
82
- def_attr_mapping :sg_id, :security_group_id
83
-
84
- define_obj :keypairs
85
-
86
- undefine_attribute :id # Do not return any predefined ID
87
-
88
- # ************************************ Router Object
89
- define_obj :router
90
-
91
- obj_needs_optional
92
- obj_needs :data, :router_name, :mapping => :name
93
- # The FORJ gateway_network_id is extracted from
94
- # Fog::HP::Network::Router[:external_gateway_info][:network_id]
95
- obj_needs :data, :external_gateway_id, :mapping => [:external_gateway_info,
96
- 'network_id']
97
-
98
- def_attr_mapping :gateway_network_id, [:external_gateway_info, 'network_id']
99
-
100
- # ************************************ SERVER Object
101
- define_obj :server
102
- def_attr_mapping :private_ip_addresses, [:addresses, '{key}', [:addr]]
103
- def_attr_mapping :meta_data, :metadata
104
- def_attr_mapping :status, :state
105
- attr_value_mapping :create, 'BUILD'
106
- attr_value_mapping :boot, :boot
107
- attr_value_mapping :active, 'ACTIVE'
108
- attr_value_mapping :error, 'ERROR'
109
- attr_value_mapping :shutdown, 'SHUTOFF'
110
-
111
- # ************************************ SERVER log Object
112
- define_obj :server_log
113
-
114
- # Excon::Response object type
115
- def_attr_mapping :output, 'output'
116
-
117
- # ************************************* Public IP Object
118
- define_obj :public_ip
119
- def_attr_mapping :server_id, :instance_id
120
- def_attr_mapping :public_ip, :ip
121
-
122
- # defines setup Cloud data (:account => true for setup)
123
- define_data('credentials#account_id',
124
- :account => true,
125
- :desc => 'HPCloud Access Key (From horizon, user drop down, '\
126
- 'manage keys)',
127
- :validate => /^[A-Z0-9]*$/
128
- )
129
- define_data('credentials#account_key',
130
- :account => true,
131
- :desc => 'HPCloud secret Key (From horizon, user drop down, '\
132
- 'manage keys)',
133
- :encrypted => false,
134
- :validate => /^.+/
135
- )
136
- define_data('credentials#auth_uri',
137
- :account => true,
138
- :desc => 'HPCloud Authentication service URL (default is HP '\
139
- 'Public cloud)',
140
- :validate => %r{^http(s)?://.*$},
141
- :default_value => 'https://region-a.geo-1.identity.hpcloudsvc'\
142
- '.com:35357/v2.0/'
143
- )
144
- define_data('credentials#tenant',
145
- :account => true,
146
- :desc => 'HPCloud Tenant ID (from horizon, identity, projecs,'\
147
- ' Project ID)',
148
- :validate => /^[0-9]+$/
149
- )
150
-
151
- define_data('services#compute',
152
- :account => true,
153
- :desc => 'HPCloud Compute service zone (Ex: region-a.geo-1)',
154
- :list_values => {
155
- :query_type => :controller_call,
156
- :object => :services,
157
- :query_call => :get_services,
158
- :query_params => { :list_services => [:Compute, :compute] },
159
- :validate => :list_strict
160
- }
161
- )
162
-
163
- define_data('services#network',
164
- :account => true,
165
- :desc => 'HPCloud Network service zone (Ex: region-a.geo-1)',
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
- data_value_mapping 'xsmall', 'standard.xsmall'
176
- data_value_mapping 'small', 'standard.small'
177
- data_value_mapping 'medium', 'standard.medium'
178
- data_value_mapping 'large', 'standard.large'
179
- data_value_mapping 'xlarge', 'standard.xlarge'
180
- end
181
-
182
32
  # Following class describe how FORJ should handle HP Cloud objects.
183
33
  # Except Cloud connection, all HPCloud objects management are described/called
184
34
  # in HP* modules.
@@ -282,50 +132,6 @@ class HpcloudController # rubocop: disable Metrics/ClassLength
282
132
  end
283
133
  end
284
134
 
285
- # This function return a collection which have to provide:
286
- # functions: [], length, each
287
- # Used by network process.
288
- def query(sObjectType, sQuery, hParams)
289
- case sObjectType
290
- when :public_ip
291
- required?(hParams, :compute_connection)
292
- required?(hParams, :server)
293
- HPCompute.query_server_assigned_addresses(hParams[:compute_connection],
294
- hParams[:server], sQuery)
295
- when :server
296
- required?(hParams, :compute_connection)
297
- HPCompute.query_server(hParams[:compute_connection], sQuery)
298
- when :image
299
- required?(hParams, :compute_connection)
300
- HPCompute.query_image(hParams[:compute_connection], sQuery)
301
- when :network
302
- required?(hParams, :network_connection)
303
- HPNetwork.query_network(hParams[:network_connection], sQuery)
304
- when :subnetwork
305
- required?(hParams, :network_connection)
306
- HPNetwork.query_subnetwork(hParams[:network_connection], sQuery)
307
- when :router
308
- required?(hParams, :network_connection)
309
- HPNetwork.query_router(hParams[:network_connection], sQuery)
310
- when :port
311
- required?(hParams, :network_connection)
312
- HPNetwork.query_port(hParams[:network_connection], sQuery)
313
- when :security_groups
314
- required?(hParams, :network_connection)
315
- HPSecurityGroups.query_sg(hParams[:network_connection], sQuery)
316
- when :rule
317
- required?(hParams, :network_connection)
318
- HPSecurityGroups.query_rule(hParams[:network_connection], sQuery)
319
- when :keypairs
320
- required?(hParams, :compute_connection)
321
- HPKeyPairs.query_keypair(hParams[:compute_connection], sQuery)
322
- when :flavor
323
- required?(hParams, :compute_connection)
324
- HPCompute.query_flavor(hParams[:compute_connection], sQuery)
325
- else
326
- controller_error "'%s' is not a valid object for 'query'", sObjectType
327
- end
328
- end
329
135
  # rubocop: enable CyclomaticComplexity, MethodLength
330
136
 
331
137
  def delete(sObjectType, hParams)
@@ -389,34 +195,35 @@ class HpcloudController # rubocop: disable Metrics/ClassLength
389
195
  if oControlerObject.is_a?(Excon::Response)
390
196
  oControlerObject.data.rh_get(:body, key)
391
197
  else
392
- attributes = oControlerObject.attributes
393
- def_attributes = oControlerObject.class.attributes
394
- controller_error "attribute '%s' is unknown in '%s'. "\
395
- "Valid one are : '%s'",
396
- key[0], oControlerObject.class,
397
- def_attributes unless def_attributes.include?(key[0])
398
- return attributes.rh_get(key) if attributes.rh_exist?(key)
399
198
  _get_instance_attr(oControlerObject, key)
400
199
  end
401
200
  rescue => e
402
201
  controller_error "Unable to map '%s'. %s", key, e.message
403
202
  end
404
203
 
405
- def _server_metadata_get(oControlerObject)
204
+ def _server_metadata_get(oControlerObject, key)
205
+ return [false, nil] unless key == :metadata
406
206
  ret = {}
407
207
  oControlerObject.metadata.each do |m|
408
208
  k = m.attributes[:key]
409
209
  v = m.attributes[:value]
410
210
  ret[k] = v
411
211
  end
412
- ret
212
+ [true, ret]
413
213
  end
414
214
 
415
215
  def _get_instance_attr(oControlerObject, key)
416
- return _server_metadata_get(oControlerObject) if key[0] == :metadata
417
- return nil if oControlerObject.send(key[0]).nil?
418
- return oControlerObject.send(key[0]) if key.length == 1
419
- oControlerObject.send(key[0]).rh_get(key[1..-1])
216
+ found, ret = _server_metadata_get(oControlerObject, key[0])
217
+ found, ret = _get_from(oControlerObject, key[0]) unless found
218
+
219
+ unless found
220
+ Lorj.debug(4, "Unable to get '%s' from '%s'. Attribute inexistent.",
221
+ key[0], oControlerObject.class)
222
+ return nil
223
+ end
224
+
225
+ return ret if key.length == 1 || !ret.is_a?(Hash)
226
+ ret.rh_get(key[1..-1])
420
227
  end
421
228
 
422
229
  def set_attr(oControlerObject, key, value)
@@ -0,0 +1,179 @@
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
+
20
+ # Defines Meta HPCloud object
21
+ class Hpcloud
22
+ define_obj :services
23
+ obj_needs :data, 'credentials#account_id', :mapping => :hp_access_key
24
+ obj_needs :data, 'credentials#account_key', :mapping => :hp_secret_key
25
+ obj_needs :data, 'credentials#auth_uri', :mapping => :hp_auth_uri
26
+ obj_needs :data, 'credentials#tenant', :mapping => :hp_tenant_id
27
+ obj_needs :data, ':excon_opts/:connect_timeout', :default_value => 30
28
+ obj_needs :data, ':excon_opts/:read_timeout', :default_value => 240
29
+ obj_needs :data, ':excon_opts/:write_timeout', :default_value => 240
30
+
31
+ # Defines Object structure and function stored on the Hpcloud class object.
32
+ # Compute Object
33
+ define_obj :compute_connection
34
+ # Defines Data used by compute.
35
+
36
+ obj_needs :data, 'credentials#account_id', :mapping => :hp_access_key
37
+ obj_needs :data, 'credentials#account_key', :mapping => :hp_secret_key
38
+ obj_needs :data, 'credentials#auth_uri', :mapping => :hp_auth_uri
39
+ obj_needs :data, 'credentials#tenant', :mapping => :hp_tenant_id
40
+ obj_needs :data, 'services#compute', :mapping => :hp_avl_zone
41
+
42
+ define_obj :network_connection
43
+ obj_needs :data, 'credentials#account_id', :mapping => :hp_access_key
44
+ obj_needs :data, 'credentials#account_key', :mapping => :hp_secret_key
45
+ obj_needs :data, 'credentials#auth_uri', :mapping => :hp_auth_uri
46
+ obj_needs :data, 'credentials#tenant', :mapping => :hp_tenant_id
47
+ obj_needs :data, 'services#network', :mapping => :hp_avl_zone
48
+
49
+ # Forj predefine following query mapping, used by ForjProcess
50
+ # id => id, name => name
51
+ # If we need to add another mapping, add
52
+ # query_mapping :id => :MyID
53
+ # If the query is not push through and Hash object, the Provider
54
+ # will needs to create his own mapping function.
55
+ define_obj :network
56
+ def_attr_mapping :external, :router_external
57
+
58
+ define_obj :rule
59
+ obj_needs :data, :dir, :mapping => :direction
60
+ attr_value_mapping :IN, 'ingress'
61
+ attr_value_mapping :OUT, 'egress'
62
+
63
+ obj_needs :data, :proto, :mapping => :protocol
64
+ obj_needs :data, :port_min, :mapping => :port_range_min
65
+ obj_needs :data, :port_max, :mapping => :port_range_max
66
+ obj_needs :data, :addr_map, :mapping => :remote_ip_prefix
67
+ obj_needs :data, :sg_id, :mapping => :security_group_id
68
+
69
+ def_attr_mapping :dir, :direction
70
+ def_attr_mapping :proto, :protocol
71
+ def_attr_mapping :port_min, :port_range_min
72
+ def_attr_mapping :port_max, :port_range_max
73
+ def_attr_mapping :addr_map, :remote_ip_prefix
74
+ def_attr_mapping :sg_id, :security_group_id
75
+
76
+ define_obj :keypairs
77
+
78
+ undefine_attribute :id # Do not return any predefined ID
79
+
80
+ # ************************************ Router Object
81
+ define_obj :router
82
+
83
+ obj_needs_optional
84
+ obj_needs :data, :router_name, :mapping => :name
85
+ # The FORJ gateway_network_id is extracted from
86
+ # Fog::HP::Network::Router[:external_gateway_info][:network_id]
87
+ obj_needs :data, :external_gateway_id, :mapping => [:external_gateway_info,
88
+ 'network_id']
89
+
90
+ def_attr_mapping :gateway_network_id, [:external_gateway_info, 'network_id']
91
+
92
+ # ************************************ SERVER Object
93
+ define_obj :server
94
+
95
+ def_attr_mapping :private_ip_addresses,
96
+ [:addresses, '{/.*/}',
97
+ '<%= data["OS-EXT-IPS:type"] == "fixed" %>|addr']
98
+ def_attr_mapping :public_ip_address,
99
+ [:addresses, '{/.*/}',
100
+ '<%= data["OS-EXT-IPS:type"] == "floating" %>|addr']
101
+ def_attr_mapping :meta_data, :metadata
102
+
103
+ def_attr_mapping :status, :state
104
+ attr_value_mapping :create, 'BUILD'
105
+ attr_value_mapping :boot, :boot
106
+ attr_value_mapping :active, 'ACTIVE'
107
+ attr_value_mapping :error, 'ERROR'
108
+ attr_value_mapping :shutdown, 'SHUTOFF'
109
+
110
+ # ************************************ SERVER log Object
111
+ define_obj :server_log
112
+
113
+ # Excon::Response object type
114
+ def_attr_mapping :output, 'output'
115
+
116
+ # ************************************* Public IP Object
117
+ define_obj :public_ip
118
+ def_attr_mapping :server_id, :instance_id
119
+ def_attr_mapping :public_ip, :ip
120
+
121
+ # defines setup Cloud data (:account => true for setup)
122
+ define_data('credentials#account_id',
123
+ :account => true,
124
+ :desc => 'HPCloud Access Key (From horizon, user drop down, '\
125
+ 'manage keys)',
126
+ :validate => /^[A-Z0-9]*$/
127
+ )
128
+ define_data('credentials#account_key',
129
+ :account => true,
130
+ :desc => 'HPCloud secret Key (From horizon, user drop down, '\
131
+ 'manage keys)',
132
+ :encrypted => false,
133
+ :validate => /^.+/
134
+ )
135
+ define_data('credentials#auth_uri',
136
+ :account => true,
137
+ :desc => 'HPCloud Authentication service URL (default is HP '\
138
+ 'Public cloud)',
139
+ :validate => %r{^http(s)?://.*$},
140
+ :default_value => 'https://region-a.geo-1.identity.hpcloudsvc'\
141
+ '.com:35357/v2.0/'
142
+ )
143
+ define_data('credentials#tenant',
144
+ :account => true,
145
+ :desc => 'HPCloud Tenant ID (from horizon, identity, projecs,'\
146
+ ' Project ID)',
147
+ :validate => /^[0-9]+$/
148
+ )
149
+
150
+ define_data('services#compute',
151
+ :account => true,
152
+ :desc => 'HPCloud Compute service zone (Ex: region-a.geo-1)',
153
+ :list_values => {
154
+ :query_type => :controller_call,
155
+ :object => :services,
156
+ :query_call => :get_services,
157
+ :query_params => { :list_services => [:Compute, :compute] },
158
+ :validate => :list_strict
159
+ }
160
+ )
161
+
162
+ define_data('services#network',
163
+ :account => true,
164
+ :desc => 'HPCloud Network service zone (Ex: region-a.geo-1)',
165
+ :list_values => {
166
+ :query_type => :controller_call,
167
+ :object => :services,
168
+ :query_call => :get_services,
169
+ :query_params => { :list_services => [:Networking, :network] },
170
+ :validate => :list_strict
171
+ }
172
+ )
173
+
174
+ data_value_mapping 'xsmall', 'standard.xsmall'
175
+ data_value_mapping 'small', 'standard.small'
176
+ data_value_mapping 'medium', 'standard.medium'
177
+ data_value_mapping 'large', 'standard.large'
178
+ data_value_mapping 'xlarge', 'standard.xlarge'
179
+ end
@@ -0,0 +1,85 @@
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
+ # Define the HPCloud controller interface with Lorj.
18
+ # Lorj uses following functions:
19
+ # - create(object_type, params)
20
+ # - delete(object_type, params)
21
+ # - get(object_type, id, param)
22
+ # - query(object_type, query, params)
23
+ # - update(object_type, object, param)
24
+ #
25
+ # The controller driver will need to dispatch the task to
26
+ # the real FOG task.
27
+ #
28
+ # Currently, Hpcloud controller is moving a more generic ruby code.
29
+ # and only query has been implemented that way, in order to use
30
+ # Most of lorj and FOG features.
31
+ class HpcloudController
32
+ def self.def_cruds(*crud_types)
33
+ crud_types.each do |crud_type|
34
+ case crud_type
35
+ when :create, :delete
36
+ base_method(crud_type)
37
+ when :query, :get
38
+ query_method(crud_type)
39
+ when :update
40
+ update_method(crud_type)
41
+ end
42
+ end
43
+ end
44
+
45
+ def self.update_method(crud_type)
46
+ define_method(crud_type) do |sObjectType, obj, hParams|
47
+ method_name = "#{crud_type}_#{sObjectType}"
48
+ if self.class.method_defined? method_name
49
+ send(method_name, obj, hParams)
50
+ else
51
+ controller_error "'%s' is not a valid object for '%s'",
52
+ sObjectType, crud_type
53
+ end
54
+ end
55
+ end
56
+
57
+ def self.query_method(crud_type)
58
+ define_method(crud_type) do |sObjectType, sCondition, hParams|
59
+ method_name = "#{crud_type}_#{sObjectType}"
60
+ if self.class.method_defined? method_name
61
+ send(method_name, hParams, sCondition)
62
+ else
63
+ controller_error "'%s' is not a valid object for '%s'",
64
+ sObjectType, crud_type
65
+ end
66
+ end
67
+ end
68
+
69
+ def self.base_method(crud_type)
70
+ define_method(crud_type) do |sObjectType, hParams|
71
+ method_name = "#{crud_type}_#{sObjectType}"
72
+ if self.class.method_defined? method_name
73
+ send(method_name, hParams)
74
+ else
75
+ controller_error "'%s' is not a valid object for '%s'",
76
+ sObjectType, crud_type
77
+ end
78
+ end
79
+ end
80
+
81
+ # Define the Openstack controller handlers
82
+ # def_cruds :create, :delete, :get, :query, :update
83
+ # Moving to use the Openstack code model.
84
+ def_cruds :query
85
+ end
@@ -0,0 +1,73 @@
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 HPCloud object query.
18
+ class HpcloudController
19
+ # Implementation of API NOT supporting query Hash
20
+ # The function will filter itself.
21
+ # It must support
22
+ # - Regexp
23
+ # - simple value equality
24
+ def self.def_query(requires, name, property_name = nil)
25
+ property_name = property_name.nil? ? name.to_s + 's' : property_name.to_s
26
+
27
+ define_method("query_#{name}") do |hParams, query|
28
+ requires = [requires] unless requires.is_a?(Array)
29
+ requires.each { |r| required?(hParams, r) }
30
+
31
+ connection = requires[0]
32
+
33
+ yield hParams, query if block_given?
34
+
35
+ func = hParams[connection].send(property_name).method(:all)
36
+ if func.parameters.length > 0
37
+ Lorj.debug(4, "'%s' uses HPCloud API filter feature.", __method__)
38
+ objects = func.call ctrl_query_select(query, String)
39
+ else
40
+ objects = func.call
41
+ end
42
+ # Uses :[] or :<key> to match object and query attr.
43
+ Lorj.debug(4, "'%s' gets %d records", __method__, objects.length)
44
+ ctrl_query_each objects, query # Return the select objects.
45
+ end
46
+ end
47
+
48
+ def_query :compute_connection, :tenant
49
+
50
+ def_query :compute_connection, :image
51
+
52
+ def_query :compute_connection, :flavor
53
+
54
+ def_query :compute_connection, :server
55
+
56
+ def_query :network_connection, :network
57
+
58
+ def_query :network_connection, :subnetwork, :subnets
59
+
60
+ def_query :network_connection, :router
61
+
62
+ def_query :network_connection, :port
63
+
64
+ def_query :network_connection, :security_groups, :security_groups
65
+
66
+ def_query :network_connection, :rule, :security_group_rules
67
+
68
+ def_query :compute_connection, :keypairs, :key_pairs
69
+
70
+ def_query :compute_connection, :public_ip, :addresses
71
+
72
+ def_query :compute_connection, :tenants, :tenants
73
+ end
@@ -17,10 +17,6 @@
17
17
  # HPcloud network class
18
18
  module HPNetwork
19
19
  # Network driver
20
- def self.query_network(oNetworkConnect, sQuery)
21
- oNetworkConnect.networks.all(sQuery)
22
- end
23
-
24
20
  def self.create_network(oNetworkConnect, name)
25
21
  oNetworkConnect.networks.create(:name => name)
26
22
  end
@@ -29,11 +25,6 @@ module HPNetwork
29
25
  oNetworkConnect.networks.get(oNetwork.id).destroy
30
26
  end
31
27
 
32
- # SubNetwork driver
33
- def self.query_subnetwork(oNetworkConnect, sQuery)
34
- oNetworkConnect.subnets.all(sQuery)
35
- end
36
-
37
28
  def self.create_subnetwork(oNetworkConnect, oNetwork, name)
38
29
  oNetworkConnect.subnets.create(
39
30
  :network_id => oNetwork.id,
@@ -90,9 +81,6 @@ module HPNetwork
90
81
  end
91
82
 
92
83
  # router driver
93
- def self.query_router(oNetworkConnect, sQuery)
94
- oNetworkConnect.routers.all(sQuery)
95
- end
96
84
 
97
85
  def self.update_router(oRouters)
98
86
  oRouters.save
@@ -107,9 +95,4 @@ module HPNetwork
107
95
  def self.add_interface(oRouter, oSubNetwork)
108
96
  oRouter.add_interface(oSubNetwork.id, nil)
109
97
  end
110
-
111
- # Port driver
112
- def self.query_port(oNetworkConnect, sQuery)
113
- oNetworkConnect.ports.all(sQuery)
114
- end
115
98
  end
@@ -16,10 +16,6 @@
16
16
 
17
17
  # HPCloud security groups
18
18
  module HPSecurityGroups
19
- def self.query_sg(oNetworkConnect, sQuery)
20
- oNetworkConnect.security_groups.all(sQuery)
21
- end
22
-
23
19
  def self.create_sg(oNetwork, name, description)
24
20
  params = { :name => name }
25
21
  params[:description] = description if description
@@ -30,10 +26,6 @@ module HPSecurityGroups
30
26
  oNetwork.security_group_rules.create(hData)
31
27
  end
32
28
 
33
- def self.query_rule(oNetwork, sQuery)
34
- oNetwork.security_group_rules.all(sQuery)
35
- end
36
-
37
29
  def self.delete_rule(oNetwork, rule_id)
38
30
  oNetwork.security_group_rules.get(rule_id).destroy
39
31
  end
@@ -41,23 +33,6 @@ end
41
33
 
42
34
  # HPCloud keypairs
43
35
  module HPKeyPairs
44
- def self.query_keypair(oComputeConnect, sQuery)
45
- keypairs = oComputeConnect.key_pairs.all
46
- results = []
47
- keypairs.each do |sElem|
48
- is_selected = true
49
- attributes = sElem.instance_variable_get(:@attributes)
50
- sQuery.each do |key, value|
51
- if attributes[key] != value
52
- is_selected = false
53
- break
54
- end
55
- end
56
- results.push sElem if is_selected
57
- end
58
- results
59
- end
60
-
61
36
  def self.get_keypair(oComputeConnect, sId)
62
37
  oComputeConnect.key_pairs.get(sId)
63
38
  end
@@ -243,6 +243,10 @@ class Openstack
243
243
  def_attribute :network_id
244
244
 
245
245
  define_obj :public_ip
246
+ # Used by floating_ip if needed
247
+ obj_needs :CloudObject, :network_connection, :for => [:create_e]
248
+ obj_needs :CloudObject, :external_network, :for => [:create_e]
249
+
246
250
  def_attr_mapping :server_id, :instance_id
247
251
  def_attr_mapping :public_ip, :ip
248
252
 
@@ -123,7 +123,8 @@ class OpenstackController
123
123
  compute_connect = hParams[:compute_connection]
124
124
  server = hParams[:server]
125
125
 
126
- while server.state != 'ACTIVE'
126
+ Lorj.debug(3, "Waiting for server '%s' to become ready...", server.name)
127
+ until server.ready?
127
128
  sleep(5)
128
129
  server = compute_connect.servers.get(server.id)
129
130
 
@@ -133,37 +134,61 @@ class OpenstackController
133
134
  end
134
135
  end
135
136
 
136
- addresses = compute_connect.addresses.all
137
+ # if 2 processes run in // to allocate a floating to different server
138
+ # openstack can re-allocate and the last who set the server win.
139
+ # So, we need to set it, and wait to see if the allocation is confirmed
140
+ # To the current server.
141
+ # This is required as there is no guarantee who did the last allocation
142
+ # of this IP.
143
+ # This should reduce strongly the risk but not completely.
144
+ # The best approach is that openstack MUST required to disassociate before
145
+ # associate...
146
+ # And if the second want to do the same, an error should be reported and
147
+ # a retry to be spawned.
137
148
  address = nil
138
- # Search for an available IP
139
- addresses.each do |elem|
140
- if elem.fixed_ip.nil?
141
- address = elem
142
- break
143
- end
149
+ loop do
150
+ address = addresses_all(hParams)
151
+ Lorj.debug(5, "Trying to associate '%s' to '%s'.",
152
+ address.ip, server.name)
153
+
154
+ address.server = server # associate the server
155
+ sleep(1 + rand(3))
156
+ address.reload
157
+ break if address.instance_id == server.id
144
158
  end
159
+ Lorj.debug(4, "'%s' is confirmed to be associated to '%s'.",
160
+ address.ip, server.name)
161
+ address
162
+ end
163
+ end
145
164
 
146
- address = allocate_new_ip(compute_connect) if address.nil?
147
- if address.nil?
148
- controller_error("No Public IP to assign to server '%s'", server.name)
165
+ # Internal functions
166
+ class OpenstackController # :nodoc:
167
+ def addresses_all(hParams)
168
+ addresses = hParams[:compute_connection].addresses.all
169
+ # Search for an available IP
170
+ loop do
171
+ addresses.each { |elem| return elem if elem.fixed_ip.nil? }
172
+ # If no IP are available, create a new one.
173
+ Lorj.debug(3, 'No more Free IP. Allocate a new one.')
174
+ create_floating_ip(hParams)
175
+ addresses.reload
149
176
  end
150
-
151
- address.server = server # associate the server
152
- address.reload
153
- # This function needs to returns a list of object.
154
- # This list must support the each function.
155
- address
156
177
  end
157
178
 
158
- def allocate_new_ip(compute_connect)
179
+ def create_floating_ip(params)
180
+ required?(params, :network_connection)
181
+ required?(params, :external_network)
182
+
183
+ network = params[:network_connection]
184
+ ext_net = params[:external_network]
185
+
159
186
  # Create a new public IP to add in the pool.
160
- pools = compute_connect.addresses.get_address_pools
161
- controller_error('No IP Pool found') if pools.length == 0
162
- # TODO: Be able to support choice of pool at setup time.
163
- if pools.length > 1
164
- Lorj.warning('Several pools found. Selecting the first one.')
187
+ begin
188
+ network.floating_ips.create(:floating_network_id => ext_net.id)
189
+ rescue => e
190
+ controller_error('Unable to get a new floating IP. %s', e)
165
191
  end
166
- compute_connect.addresses.create 'pool' => pools[0]['name']
167
192
  end
168
193
 
169
194
  def get_next_subnet(oNetworkConnect)
@@ -33,16 +33,34 @@ class OpenstackController
33
33
  yield hParams, query if block_given?
34
34
 
35
35
  func = hParams[connection].send(property_name).method(:all)
36
+ objects = _compat_query(func, __method__, query)
37
+ # Uses :[] or :<key> to match object and query attr.
38
+ Lorj.debug(4, "'%s' gets %d records", __method__, objects.length)
39
+ ctrl_query_each objects, query # Return the select objects.
40
+ end
41
+ end
42
+
43
+ def _compat_query(func, cur_method, query)
44
+ # Ruby 1.9.2 introduce Method.parameters.
45
+ if RUBY_VERSION < '1.9.2'
46
+ Lorj.debug(4, "RUBY '%s': '%s' try Openstack API filter feature.",
47
+ RUBY_VERSION, cur_method)
48
+ begin
49
+ objects = func.call ctrl_query_select(query, String)
50
+ rescue
51
+ Lorj.debug(4, "RUBY '%s': '%s' No filter parameter.",
52
+ RUBY_VERSION, cur_method)
53
+ objects = func.call
54
+ end
55
+ else
36
56
  if func.parameters.length > 0
37
- Lorj.debug(4, "'%s' uses Openstack API filter feature.", __method__)
57
+ Lorj.debug(4, "'%s' uses Openstack API filter feature.", cur_method)
38
58
  objects = func.call ctrl_query_select(query, String)
39
59
  else
40
60
  objects = func.call
41
61
  end
42
- # Uses :[] or :<key> to match object and query attr.
43
- Lorj.debug(4, "'%s' gets %d records", __method__, objects.length)
44
- ctrl_query_each objects, query # Return the select objects.
45
62
  end
63
+ objects
46
64
  end
47
65
 
48
66
  def_query :compute_connection, :tenant
data/lorj_cloud.gemspec CHANGED
@@ -33,4 +33,13 @@ Gem::Specification.new do |spec|
33
33
  spec.add_runtime_dependency "mime-types", '< 2.0'
34
34
  spec.add_runtime_dependency "nokogiri", '< 1.6'
35
35
  # Ruby 1.8 restrictions - END
36
+ if RUBY_VERSION.match(/1\.8/)
37
+ spec.add_development_dependency "ruby-debug"
38
+ elsif RUBY_VERSION.match(/1\.9/)
39
+ spec.add_development_dependency "debugger"
40
+ spec.add_development_dependency "rubocop", "~> 0.30.0"
41
+ else
42
+ spec.add_development_dependency "byebug"
43
+ spec.add_development_dependency "rubocop", "~> 0.30.0"
44
+ end
36
45
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lorj_cloud
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christophe Larsonneur
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-06-22 00:00:00.000000000 Z
11
+ date: 2015-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -122,6 +122,34 @@ dependencies:
122
122
  - - "<"
123
123
  - !ruby/object:Gem::Version
124
124
  version: '1.6'
125
+ - !ruby/object:Gem::Dependency
126
+ name: byebug
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rubocop
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: 0.30.0
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: 0.30.0
125
153
  description: 'simplify cloud management, thanks to predefined process to manage cloud
126
154
  obj and make it work. '
127
155
  email:
@@ -162,6 +190,9 @@ files:
162
190
  - lib/process/cloud/process/subnetwork.rb
163
191
  - lib/process/cloud/providers/hpcloud/compute.rb
164
192
  - lib/process/cloud/providers/hpcloud/hpcloud.rb
193
+ - lib/process/cloud/providers/hpcloud/hpcloud_declare.rb
194
+ - lib/process/cloud/providers/hpcloud/hpcloud_generic.rb
195
+ - lib/process/cloud/providers/hpcloud/hpcloud_query.rb
165
196
  - lib/process/cloud/providers/hpcloud/network.rb
166
197
  - lib/process/cloud/providers/hpcloud/security_groups.rb
167
198
  - lib/process/cloud/providers/mock/mock.rb