deltacloud-core 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (187) hide show
  1. data/Rakefile +2 -1
  2. data/bin/deltacloudd +11 -5
  3. data/config.ru +3 -3
  4. data/deltacloud-core.gemspec +24 -18
  5. data/lib/cimi/collections/address_templates.rb +3 -3
  6. data/lib/cimi/collections/addresses.rb +6 -5
  7. data/lib/cimi/collections/entity_metadata.rb +0 -2
  8. data/lib/cimi/collections/machine_admins.rb +5 -5
  9. data/lib/cimi/collections/machine_configurations.rb +2 -2
  10. data/lib/cimi/collections/machine_images.rb +3 -3
  11. data/lib/cimi/collections/machines.rb +32 -10
  12. data/lib/cimi/collections/network_configurations.rb +4 -3
  13. data/lib/cimi/collections/network_templates.rb +4 -3
  14. data/lib/cimi/collections/networks.rb +8 -8
  15. data/lib/cimi/collections/routing_group_templates.rb +4 -3
  16. data/lib/cimi/collections/routing_groups.rb +4 -3
  17. data/lib/cimi/collections/volume_configurations.rb +3 -3
  18. data/lib/cimi/collections/volume_images.rb +3 -3
  19. data/lib/cimi/collections/volumes.rb +4 -3
  20. data/lib/cimi/collections/vsp_configurations.rb +4 -3
  21. data/lib/cimi/collections/vsp_templates.rb +4 -3
  22. data/lib/cimi/collections/vsps.rb +8 -7
  23. data/lib/cimi/helpers.rb +1 -1
  24. data/lib/cimi/helpers/cimi_helper.rb +11 -1
  25. data/lib/cimi/models.rb +4 -0
  26. data/lib/cimi/models/base.rb +11 -1
  27. data/lib/cimi/models/cloud_entry_point.rb +7 -14
  28. data/lib/cimi/models/disk.rb +40 -0
  29. data/lib/cimi/models/disk_collection.rb +38 -0
  30. data/lib/cimi/models/machine.rb +9 -43
  31. data/lib/cimi/models/machine_collection.rb +7 -4
  32. data/lib/cimi/models/machine_configuration.rb +8 -15
  33. data/lib/cimi/models/machine_configuration_collection.rb +6 -4
  34. data/lib/cimi/models/machine_image_collection.rb +6 -4
  35. data/lib/cimi/models/machine_template_collection.rb +6 -4
  36. data/lib/cimi/models/machine_volume.rb +42 -0
  37. data/lib/cimi/models/machine_volume_collection.rb +34 -0
  38. data/lib/cimi/models/network.rb +5 -9
  39. data/lib/cimi/models/network_collection.rb +6 -5
  40. data/lib/cimi/models/schema.rb +20 -0
  41. data/lib/cimi/models/volume_collection.rb +6 -4
  42. data/lib/cimi/models/volume_configuration_collection.rb +6 -4
  43. data/lib/cimi/models/volume_image_collection.rb +6 -4
  44. data/lib/cimi/models/volume_template_collection.rb +6 -4
  45. data/lib/deltacloud/api.rb +3 -3
  46. data/lib/deltacloud/collections/addresses.rb +1 -1
  47. data/lib/deltacloud/collections/firewalls.rb +4 -2
  48. data/lib/deltacloud/collections/keys.rb +1 -1
  49. data/lib/deltacloud/collections/load_balancers.rb +1 -1
  50. data/lib/deltacloud/collections/storage_volumes.rb +4 -4
  51. data/lib/deltacloud/drivers/base_driver.rb +12 -20
  52. data/lib/deltacloud/drivers/ec2/aws_vpc_monkey_patch.rb +294 -0
  53. data/lib/deltacloud/drivers/ec2/ec2_driver.rb +38 -6
  54. data/lib/deltacloud/drivers/fgcp/fgcp_client.rb +4 -0
  55. data/lib/deltacloud/drivers/fgcp/fgcp_driver.rb +40 -4
  56. data/lib/deltacloud/drivers/gogrid/gogrid_client.rb +8 -7
  57. data/lib/deltacloud/drivers/gogrid/gogrid_driver.rb +422 -416
  58. data/lib/deltacloud/drivers/google/google_driver.rb +2 -1
  59. data/lib/deltacloud/drivers/openstack/openstack_driver.rb +30 -16
  60. data/lib/deltacloud/drivers/rhevm/rhevm_driver.rb +1 -0
  61. data/lib/deltacloud/drivers/vsphere/vsphere_driver.rb +3 -9
  62. data/lib/deltacloud/helpers/deltacloud_helper.rb +0 -2
  63. data/lib/deltacloud/models/firewall_rule.rb +2 -0
  64. data/lib/deltacloud/models/hardware_profile.rb +61 -59
  65. data/{tests/deprecated/core_ext/string.rb → lib/deltacloud/version.rb} +3 -15
  66. data/lib/deltacloud_rack.rb +1 -0
  67. data/lib/ec2/server.rb +5 -3
  68. data/lib/sinatra/rack_accept.rb +53 -53
  69. data/lib/sinatra/rack_etag.rb +1 -1
  70. data/tests/cimi/collections/cloud_entry_point_test.rb +11 -2
  71. data/tests/deltacloud/common.rb +1 -1
  72. data/tests/deltacloud/rack_test.rb +3 -3
  73. data/tests/drivers/base/base_driver_test.rb +6 -2
  74. data/tests/drivers/base/common.rb +1 -0
  75. data/tests/drivers/base/library_test.rb +6 -2
  76. data/tests/drivers/ec2/common.rb +34 -15
  77. data/tests/drivers/ec2/images_test.rb +2 -3
  78. data/tests/drivers/ec2/instance_test.rb +12 -2
  79. data/tests/drivers/ec2/keys_test.rb +2 -2
  80. data/tests/drivers/ec2/realms_test.rb +7 -3
  81. data/tests/drivers/ec2/storage_snapshots_test.rb +2 -3
  82. data/tests/drivers/gogrid/common.rb +24 -0
  83. data/tests/drivers/gogrid/hardware_profiles_test.rb +51 -0
  84. data/tests/drivers/gogrid/images_test.rb +57 -0
  85. data/tests/drivers/gogrid/instances_test.rb +92 -0
  86. data/tests/drivers/gogrid/realms_test.rb +45 -0
  87. data/tests/drivers/google/buckets_test.rb +105 -0
  88. data/tests/drivers/google/common.rb +20 -0
  89. data/tests/drivers/mock/common.rb +1 -0
  90. data/tests/drivers/mock/images_test.rb +3 -2
  91. data/tests/drivers/mock/instances_test.rb +3 -2
  92. data/tests/drivers/mock/keys_test.rb +3 -2
  93. data/tests/drivers/mock/realms_test.rb +3 -2
  94. data/tests/drivers/mock/storage_snapshots_test.rb +3 -2
  95. data/tests/drivers/mock/storage_volumes_test.rb +3 -2
  96. data/tests/drivers/models/address_test.rb +3 -3
  97. data/tests/drivers/models/base_test.rb +4 -2
  98. data/tests/drivers/models/blob_test.rb +3 -3
  99. data/tests/drivers/models/common.rb +3 -0
  100. data/tests/drivers/models/hardware_profile_test.rb +5 -4
  101. data/tests/drivers/models/instance_address_test.rb +3 -3
  102. data/tests/drivers/models/instance_profile_test.rb +3 -3
  103. data/tests/drivers/models/instance_test.rb +3 -3
  104. data/tests/drivers/models/keys_test.rb +3 -3
  105. data/tests/drivers/models/metrics_test.rb +3 -3
  106. data/tests/drivers/openstack/common.rb +26 -0
  107. data/tests/drivers/openstack/hardware_profiles_test.rb +46 -0
  108. data/tests/drivers/openstack/images_test.rb +50 -0
  109. data/tests/drivers/openstack/instances_test.rb +66 -0
  110. data/tests/drivers/openstack/keys_test.rb +61 -0
  111. data/tests/drivers/openstack/realms_test.rb +46 -0
  112. data/tests/drivers/rhevm/common.rb +2 -0
  113. data/tests/drivers/rhevm/images_test.rb +3 -4
  114. data/tests/drivers/rhevm/instance_test.rb +2 -2
  115. data/tests/drivers/rhevm/provider_test.rb +2 -2
  116. data/tests/drivers/rhevm/realms_test.rb +2 -3
  117. data/tests/ec2/common.rb +5 -4
  118. data/tests/ec2/server_test.rb +3 -2
  119. data/tests/helpers/rack/common.rb +3 -0
  120. data/tests/helpers/rack/rack_accept_test.rb +65 -0
  121. data/tests/helpers/rack/rack_date_test.rb +27 -0
  122. data/tests/helpers/rack/rack_driver_select_test.rb +53 -0
  123. data/tests/helpers/rack/rack_etag_test.rb +29 -0
  124. data/tests/helpers/rack/rack_matrix_params_test.rb +72 -0
  125. data/tests/test_helper.rb +44 -36
  126. data/views/hardware_profiles/index.html.haml +1 -1
  127. data/views/hardware_profiles/show.html.haml +2 -2
  128. data/views/hardware_profiles/show.xml.haml +2 -1
  129. data/views/images/show.html.haml +6 -5
  130. metadata +74 -117
  131. data/tests/deprecated/api/common.rb +0 -1
  132. data/tests/deprecated/api/driver_test.rb +0 -79
  133. data/tests/deprecated/api/library_test.rb +0 -6
  134. data/tests/deprecated/cimi/features/step_definitions/common_steps.rb +0 -59
  135. data/tests/deprecated/cimi/features/step_definitions/machine_images_steps.rb +0 -0
  136. data/tests/deprecated/cimi/features/step_definitions/machines_steps.rb +0 -100
  137. data/tests/deprecated/cimi/features/step_definitions/volumes_steps.rb +0 -115
  138. data/tests/deprecated/cimi/features/support/env.rb +0 -75
  139. data/tests/deprecated/common.rb +0 -279
  140. data/tests/deprecated/drivers/ec2/api_test.rb +0 -19
  141. data/tests/deprecated/drivers/ec2/common.rb +0 -23
  142. data/tests/deprecated/drivers/ec2/drivers_test.rb +0 -120
  143. data/tests/deprecated/drivers/ec2/hardware_profiles_test.rb +0 -224
  144. data/tests/deprecated/drivers/ec2/images_test.rb +0 -230
  145. data/tests/deprecated/drivers/ec2/instances_test.rb +0 -356
  146. data/tests/deprecated/drivers/ec2/keys_test.rb +0 -181
  147. data/tests/deprecated/drivers/ec2/realms_test.rb +0 -146
  148. data/tests/deprecated/drivers/fgcp/api_test.rb +0 -47
  149. data/tests/deprecated/drivers/fgcp/hardware_profiles_test.rb +0 -54
  150. data/tests/deprecated/drivers/fgcp/realms_test.rb +0 -42
  151. data/tests/deprecated/drivers/fgcp/setup.rb +0 -13
  152. data/tests/deprecated/drivers/google/api_test.rb +0 -19
  153. data/tests/deprecated/drivers/google/buckets_test.rb +0 -100
  154. data/tests/deprecated/drivers/google/common.rb +0 -54
  155. data/tests/deprecated/drivers/mock/api_test.rb +0 -10
  156. data/tests/deprecated/drivers/mock/buckets_test.rb +0 -195
  157. data/tests/deprecated/drivers/mock/common.rb +0 -7
  158. data/tests/deprecated/drivers/mock/drivers_test.rb +0 -123
  159. data/tests/deprecated/drivers/mock/hardware_profiles_test.rb +0 -224
  160. data/tests/deprecated/drivers/mock/images_test.rb +0 -197
  161. data/tests/deprecated/drivers/mock/instances_test.rb +0 -343
  162. data/tests/deprecated/drivers/mock/keys_test.rb +0 -161
  163. data/tests/deprecated/drivers/mock/realms_test.rb +0 -132
  164. data/tests/deprecated/drivers/mock/storage_snapshots_test.rb +0 -114
  165. data/tests/deprecated/drivers/mock/storage_volumes_test.rb +0 -122
  166. data/tests/deprecated/drivers/openstack/api_test.rb +0 -46
  167. data/tests/deprecated/drivers/openstack/common.rb +0 -21
  168. data/tests/deprecated/drivers/openstack/hardware_profiles_test.rb +0 -64
  169. data/tests/deprecated/drivers/openstack/images_test.rb +0 -46
  170. data/tests/deprecated/drivers/openstack/instances_test.rb +0 -208
  171. data/tests/deprecated/drivers/openstack/realms_test.rb +0 -40
  172. data/tests/deprecated/drivers/rackspace/api_test.rb +0 -46
  173. data/tests/deprecated/drivers/rackspace/buckets_test.rb +0 -150
  174. data/tests/deprecated/drivers/rackspace/common.rb +0 -16
  175. data/tests/deprecated/drivers/rackspace/hardware_profiles_test.rb +0 -58
  176. data/tests/deprecated/drivers/rackspace/images_test.rb +0 -45
  177. data/tests/deprecated/drivers/rackspace/instances_test.rb +0 -166
  178. data/tests/deprecated/drivers/rackspace/realms_test.rb +0 -41
  179. data/tests/deprecated/drivers/rhevm/api_test.rb +0 -45
  180. data/tests/deprecated/drivers/rhevm/common.rb +0 -21
  181. data/tests/deprecated/drivers/rhevm/hardware_profiles_test.rb +0 -58
  182. data/tests/deprecated/drivers/rhevm/images_test.rb +0 -48
  183. data/tests/deprecated/drivers/rhevm/instances_test.rb +0 -182
  184. data/tests/deprecated/drivers/rhevm/realms_test.rb +0 -40
  185. data/tests/deprecated/minitest_common.rb +0 -58
  186. data/tests/deprecated/minitest_common_api_test.rb +0 -115
  187. data/tests/deprecated/rabbit_test.rb +0 -52
@@ -15,6 +15,8 @@
15
15
  #
16
16
 
17
17
  require 'aws'
18
+ # Delete this once VPC support is merged upstream
19
+ require_relative 'aws_vpc_monkey_patch'
18
20
 
19
21
  require_relative '../../runner'
20
22
 
@@ -164,22 +166,35 @@ module Deltacloud
164
166
  end
165
167
 
166
168
  def realms(credentials, opts={})
169
+ # We have two different kinds of realms:
170
+ # (1) Availability Zones
171
+ # (2) Subnets in VPC's (scoped to an AZ)
172
+ # For the latter, the ID is AZ:SUBNET, and we can tell that we
173
+ # are looking at such a realm by checking if the id contains a colon
167
174
  ec2 = new_client(credentials)
168
175
  realms = []
169
176
  safely do
170
177
  if opts[:id] and !opts[:id].empty?
178
+ az, sn = opts[:id].split(":")
171
179
  begin
172
- ec2.describe_availability_zones([opts[:id]]).collect do |realm|
173
- realms << convert_realm(realm) unless realm.empty?
180
+ if sn
181
+ subnet = ec2.describe_subnets(sn).first
182
+ realms << convert_realm(subnet) if subnet
183
+ else
184
+ ec2.describe_availability_zones([az]).collect do |realm|
185
+ realms << convert_realm(realm) unless realm.empty?
186
+ end
174
187
  end
175
188
  rescue => e
176
189
  raise e unless e.message =~ /Invalid availability zone/
177
190
  realms = []
178
191
  end
179
192
  else
180
- ec2.describe_availability_zones.collect do |realm|
181
- realms << convert_realm(realm) unless realm.empty?
193
+ realms = ec2.describe_availability_zones.collect do |realm|
194
+ convert_realm(realm) unless realm.empty?
182
195
  end
196
+ realms = realms +
197
+ ec2.describe_subnets.map { |sn| convert_realm(sn) }
183
198
  end
184
199
  end
185
200
  realms
@@ -248,8 +263,15 @@ module Deltacloud
248
263
  if opts[:metrics] and !opts[:metrics].empty?
249
264
  instance_options[:monitoring_enabled] = true
250
265
  end
266
+ if opts[:realm_id]
267
+ az, sn = opts[:realm_id].split(":")
268
+ if sn
269
+ instance_options[:subnet_id] = sn
270
+ else
271
+ instance_options[:availability_zone] = az
272
+ end
273
+ end
251
274
  instance_options[:key_name] = opts[:keyname] if opts[:keyname]
252
- instance_options[:availability_zone] = opts[:realm_id] if opts[:realm_id]
253
275
  instance_options[:instance_type] = opts[:hwp_id] if opts[:hwp_id] && opts[:hwp_id].length > 0
254
276
  firewalls = opts.inject([]){|res, (k,v)| res << v if k =~ /firewalls\d+$/; res}
255
277
  instance_options[:group_ids] = firewalls unless firewalls.empty?
@@ -871,6 +893,12 @@ module Deltacloud
871
893
  end
872
894
 
873
895
  def convert_realm(realm)
896
+ # We also allow subnets as realms
897
+ if realm[:subnet_id]
898
+ realm[:zone_name] =
899
+ "#{realm[:availability_zone]}:#{realm[:subnet_id]}"
900
+ realm[:zone_state] = realm[:state]
901
+ end
874
902
  Realm.new(
875
903
  :id => realm[:zone_name],
876
904
  :name => realm[:zone_name],
@@ -898,6 +926,10 @@ module Deltacloud
898
926
  if instance[:aws_instance_type] == "t1.micro"
899
927
  inst_profile_opts[:hwp_architecture]=instance[:architecture]
900
928
  end
929
+ realm_id = instance[:aws_availability_zone]
930
+ unless instance[:subnet_id].empty?
931
+ realm_id = "#{realm_id}:#{instance[:subnet_id]}"
932
+ end
901
933
  Instance.new(
902
934
  :id => instance[:aws_instance_id],
903
935
  :name => instance[:aws_image_id],
@@ -908,7 +940,7 @@ module Deltacloud
908
940
  :keyname => instance[:ssh_key_name],
909
941
  :launch_time => instance[:aws_launch_time],
910
942
  :instance_profile => InstanceProfile.new(instance[:aws_instance_type], inst_profile_opts),
911
- :realm_id => instance[:aws_availability_zone],
943
+ :realm_id => realm_id,
912
944
  :public_addresses => [InstanceAddress.new(instance[:dns_name], :type => :hostname)],
913
945
  :private_addresses => [InstanceAddress.new(instance[:private_dns_name], :type => :hostname)],
914
946
  :firewalls => instance[:aws_groups],
@@ -90,6 +90,10 @@ class FgcpClient
90
90
  request('GetVSYSConfiguration', {'vsysId' => vsys_id})
91
91
  end
92
92
 
93
+ def list_vsys_descriptor
94
+ request('ListVSYSDescriptor')
95
+ end
96
+
93
97
  def list_vservers(vsys_id)
94
98
  request('ListVServer', {'vsysId' => vsys_id})
95
99
  end
@@ -845,6 +845,7 @@ eofwpxml
845
845
  fw = client.get_efm_configuration(opts[:id], 'FW_POLICY', configuration_xml)
846
846
  fw_name = fw['efm'][0]['efmName'][0] # currently always 'Firewall'
847
847
  fw_owner_id = fw['efm'][0]['creator'][0]
848
+ rule50000_log = true
848
849
 
849
850
  fw['efm'][0]['firewall'][0]['directions'][0]['direction'].each do |direction|
850
851
 
@@ -874,15 +875,33 @@ eofwpxml
874
875
 
875
876
  rules << FirewallRule.new({
876
877
  :id => policy['id'][0],
878
+ :rule_action => policy['action'][0].downcase,
879
+ :log_rule => policy['log'][0] == 'On',
877
880
  :allow_protocol => policy['protocol'][0],
878
881
  :port_from => policy['srcPort'] ? policy['srcPort'][0] : nil, # not set for e.g. ICMP
879
882
  :port_to => policy['dstPort'] ? policy['dstPort'][0] : nil, # not set for e.g. ICMP
880
883
  :direction => ingress,
881
884
  :sources => sources
882
- }) unless policy['action'][0] == 'Deny' or policy['id'][0] == '50000' # exclude special case
885
+ }) unless policy['id'][0] == '50000' # special case added later
886
+
887
+ rule50000_log = (policy['log'][0] == 'On') if policy['id'][0] == '50000'
883
888
  end
884
889
  end
885
890
 
891
+ # add "all deny" rule 50000
892
+ source_any = {
893
+ :type => 'address',
894
+ :family => 'ipv4',
895
+ :address => '0.0.0.0',
896
+ :prefix => '0'
897
+ }
898
+ rules << FirewallRule.new({
899
+ :id => '50000',
900
+ :rule_action => 'deny',
901
+ :log_rule => rule50000_log,
902
+ :sources => [source_any]
903
+ })
904
+
886
905
  vsys = client.get_vsys_attributes(client.extract_vsys_id(opts[:id]))['vsys'][0]
887
906
  firewalls << Firewall.new({
888
907
  :id => opts[:id],
@@ -914,8 +933,14 @@ eofwpxml
914
933
  def create_firewall(credentials, opts={})
915
934
  safely do
916
935
  client = new_client(credentials)
917
- # using 'description' as vsysDescriptor
918
- vsys_id = client.create_vsys(opts['description'], opts['name'])['vsysId'][0]
936
+ begin
937
+ # using 'description' as vsysDescriptor
938
+ vsys_id = client.create_vsys(opts['description'], opts['name'])['vsysId'][0]
939
+ rescue Exception => ex
940
+ raise ex unless ex.message =~ /Template does not exist.*/
941
+ descriptors = client.list_vsys_descriptor['vsysdescriptors'][0]['vsysdescriptor'].collect { |desc| desc['vsysdescriptorId'][0] }
942
+ raise "Descriptor [#{opts['name']}] does not exist. Specify one of [#{descriptors.join(', ')}] as firewall description"
943
+ end
919
944
  fw_id = vsys_id + '-S-0001'
920
945
  Firewall.new({
921
946
  :id => fw_id,
@@ -934,7 +959,13 @@ eofwpxml
934
959
  end
935
960
  end
936
961
 
937
- #TODO
962
+ # FW rule creation not supported:
963
+ # fgcp backend requires a mandatory rule id to create (insert) a new rule
964
+ # into the existing accept/deny rules. Also, the first two digits of the
965
+ # five digit rule identify what from and to network segment (e.g. Internet
966
+ # to DMZ, or Secure2 to Secure1) the rule applies to.
967
+ # The current Deltacloud firewall collection API does not cover such
968
+ # functionality so it was deemed not suitable to implement.
938
969
  # def create_firewall_rule(credentials, opts={})
939
970
  # p opts
940
971
  # end
@@ -1288,6 +1319,11 @@ eofwopxml
1288
1319
  status 404
1289
1320
  end
1290
1321
 
1322
+ # wrong FW description (vsys descriptor)
1323
+ on /does not exist. Specify one of / do
1324
+ status 404
1325
+ end
1326
+
1291
1327
  # trying an operation that is not supported (yet) by the target region
1292
1328
  on /NOTFOUND: API to the Version/ do
1293
1329
  status 501 # Not Implemented
@@ -17,6 +17,7 @@
17
17
  require 'digest/md5'
18
18
  require 'cgi'
19
19
  require 'open-uri'
20
+ require 'json'
20
21
 
21
22
  module Kernel
22
23
  def suppress_warnings
@@ -44,7 +45,7 @@ class GoGridClient
44
45
  apikey='YOUR API KEY',
45
46
  secret='YOUR SHARED SECRET',
46
47
  format='json',
47
- version='1.6')
48
+ version='1.9')
48
49
  @server = server
49
50
  @secret = secret
50
51
  @default_params = {'format'=>format, 'v'=>version,'api_key' => apikey}
@@ -52,13 +53,13 @@ class GoGridClient
52
53
 
53
54
  def getRequestURL(method,params)
54
55
  requestURL = @server+'/'+method+'?'
55
- call_params = @default_params.merge(params)
56
- call_params['sig']=getSignature(@default_params['api_key'],@secret)
57
- requestURL = requestURL+encode_params(call_params)
56
+ call_params = @default_params.merge(params)
57
+ call_params['sig']=getSignature(@default_params['api_key'],@secret)
58
+ requestURL = requestURL+encode_params(call_params)
58
59
  end
59
60
 
60
61
  def getSignature(key,secret)
61
- Digest::MD5.hexdigest(key+secret+"%.0f"%Time.new.to_f)
62
+ Digest::MD5.hexdigest(key+secret+"%.0f"%Time.now.to_f)
62
63
  end
63
64
 
64
65
  def sendAPIRequest(method,params={})
@@ -71,8 +72,8 @@ class GoGridClient
71
72
  if version
72
73
  @default_params['v'] = version
73
74
  else
74
- @default_params['v'] = '1.5'
75
- end
75
+ @default_params['v'] = '1.9'
76
+ end
76
77
  request = sendAPIRequest(method, params)
77
78
  JSON::parse(request)
78
79
  end
@@ -30,471 +30,477 @@ module Deltacloud
30
30
  module Drivers
31
31
  module Gogrid
32
32
 
33
- class GogridDriver < Deltacloud::BaseDriver
33
+ class GogridDriver < Deltacloud::BaseDriver
34
34
 
35
- feature :instances, :user_name do
36
- { :max_length => 20 }
37
- end
38
-
39
- feature :instances, :authentication_password
40
- feature :instances, :sandboxing
41
-
42
- define_hardware_profile 'default'
35
+ feature :instances, :user_name do
36
+ { :max_length => 20 }
37
+ end
43
38
 
44
- USER_NAME_MAX = constraints(:collection => :instances, :feature => :user_name)[:max_length]
39
+ feature :instances, :authentication_password
40
+ feature :instances, :sandboxing
41
+
42
+ define_hardware_profile 'default'
43
+
44
+ USER_NAME_MAX = constraints(:collection => :instances, :feature => :user_name)[:max_length]
45
+
46
+ def hardware_profiles(credentials, opts={})
47
+ client = new_client(credentials)
48
+ safely do
49
+ server_types = client.request('common/lookup/list', { 'lookup' => 'server.type' })
50
+ server_rams = client.request('common/lookup/list', { 'lookup' => 'server.ram' })
51
+ @hardware_profiles = []
52
+ server_types['list'].each do |type|
53
+ memory_values = server_rams['list'].collect do |r|
54
+ r['name'] =~ /MB$/ ? r['name'].gsub(/MB$/, '').to_i : (r['name'].gsub(/(\w{2})$/, '')).to_i*1024
55
+ end
56
+ @hardware_profiles << ::Deltacloud::HardwareProfile.new(type['name'].tr(' ', '-').downcase) do
57
+ cpu 2
58
+ memory memory_values
59
+ storage 25
60
+ end
61
+ end
62
+ end
63
+ opts[:name] ||= opts[:id]
64
+ filter_on( @hardware_profiles, :name, opts )
65
+ end
45
66
 
46
- def hardware_profiles(credentials, opts={})
47
- client = new_client(credentials)
48
- safely do
49
- server_types = client.request('common/lookup/list', { 'lookup' => 'server.type' })
50
- server_rams = client.request('common/lookup/list', { 'lookup' => 'server.ram' })
51
- @hardware_profiles = []
52
- server_types['list'].each do |type|
53
- memory_values = server_rams['list'].collect do |r|
54
- r['name'] =~ /MB$/ ? r['name'].gsub(/MB$/, '').to_i : (r['name'].gsub(/(\w{2})$/, '')).to_i*1024
67
+ def hardware_profile(credentials, opts={})
68
+ hardware_profiles(credentials, :id => opts[:id]).first
55
69
  end
56
- @hardware_profiles << ::Deltacloud::HardwareProfile.new(type['name'].tr(' ', '-').downcase) do
57
- cpu 2
58
- memory memory_values
59
- storage 25
70
+
71
+ def images(credentials, opts=nil)
72
+ imgs = []
73
+ if opts and opts[:id]
74
+ safely do
75
+ begin
76
+ imgs = [convert_image(new_client(credentials).request('grid/image/get', { 'id' => opts[:id] })['list'].first)]
77
+ rescue => e
78
+ raise e unless e.message =~ /Bad Request/
79
+ imgs = []
80
+ end
81
+ end
82
+ else
83
+ safely do
84
+ imgs = new_client(credentials).request('grid/image/list', { 'state' => 'Available'})['list'].collect do |image|
85
+ convert_image(image, credentials.user)
86
+ end
87
+ end
88
+ end
89
+ imgs = filter_on( imgs, :architecture, opts )
90
+ imgs.sort_by{|e| [e.owner_id, e.description]}
60
91
  end
61
- end
62
- end
63
- @hardware_profiles
64
- end
65
92
 
66
- def images(credentials, opts=nil)
67
- imgs = []
68
- if opts and opts[:id]
69
- safely do
70
- imgs = [convert_image(new_client(credentials).request('grid/image/get', { 'id' => opts[:id] })['list'].first)]
71
- end
72
- else
73
- safely do
74
- imgs = new_client(credentials).request('grid/image/list', { 'state' => 'Available'})['list'].collect do |image|
75
- convert_image(image, credentials.user)
93
+ def realms(credentials, opts={})
94
+ realms = []
95
+ client = new_client(credentials)
96
+ safely do
97
+ client.request('common/lookup/list', { 'lookup' => 'ip.datacenter' })['list'].collect do |realm|
98
+ realms << convert_realm(realm)
99
+ end
100
+ end
101
+ realms = filter_on(realms, :id, opts)
76
102
  end
77
- end
78
- end
79
- imgs = filter_on( imgs, :architecture, opts )
80
- imgs.sort_by{|e| [e.owner_id, e.description]}
81
- end
82
103
 
83
- def realms(credentials, opts=nil)
84
- realms = []
85
- client = new_client(credentials)
86
- safely do
87
- client.request('common/lookup/list', { 'lookup' => 'ip.datacenter' })['list'].collect do |realm|
88
- realms << convert_realm(realm)
89
- end
90
- end
91
- realms = filter_on(realms, :id, opts)
92
- end
104
+ def create_instance(credentials, image_id, opts={})
105
+ server_ram = nil
106
+ if opts[:hwp_memory]
107
+ mem = opts[:hwp_memory].to_i
108
+ server_ram = (mem == 512) ? "512MB" : "#{mem / 1024}GB"
109
+ else
110
+ server_ram = "512MB"
111
+ end
112
+
113
+ name = opts[:name]
114
+ if not name
115
+ name = "Server #{Time.now.to_i.to_s.reverse[0..3]}#{rand(9)}"
116
+ end
117
+
118
+ if name.length > USER_NAME_MAX
119
+ raise "Parameter name must be #{USER_NAME_MAX} characters or less"
120
+ end
121
+
122
+ client = new_client(credentials)
123
+ params = {
124
+ 'name' => name,
125
+ 'image' => image_id,
126
+ 'server.ram' => server_ram,
127
+ 'ip' => get_free_ip_from_realm(credentials, opts[:realm_id] || '1')
128
+ }
129
+ params.merge!('isSandbox' => 'true') if opts[:sandbox]
130
+ safely do
131
+ instance = client.request('grid/server/add', params)['list'].first
132
+ if instance
133
+ login_data = get_login_data(client, instance[:id])
134
+ if login_data['username'] and login_data['password']
135
+ instance['username'] = login_data['username']
136
+ instance['password'] = login_data['password']
137
+ inst = convert_instance(instance, credentials.user)
138
+ else
139
+ inst = convert_instance(instance, credentials.user)
140
+ inst.authn_error = "Unable to fetch password"
141
+ end
142
+ return inst
143
+ else
144
+ return nil
145
+ end
146
+ end
147
+ end
93
148
 
94
- def create_instance(credentials, image_id, opts={})
95
- server_ram = nil
96
- if opts[:hwp_memory]
97
- mem = opts[:hwp_memory].to_i
98
- server_ram = (mem == 512) ? "512MB" : "#{mem / 1024}GB"
99
- else
100
- server_ram = "512MB"
101
- end
149
+ def run_on_instance(credentials, opts={})
150
+ target = instance(credentials, opts[:id])
151
+ param = {}
152
+ param[:credentials] = {
153
+ :username => target.username,
154
+ :password => target.password,
155
+ }
156
+ param[:credentials].merge!({ :password => opts[:password]}) if opts[:password].length>0
157
+ param[:port] = opts[:port] || '22'
158
+ param[:ip] = opts[:ip] || target.public_addresses.first.address
159
+ Deltacloud::Runner.execute(opts[:cmd], param)
160
+ end
102
161
 
103
- name = opts[:name]
104
- if not name
105
- name = "Server #{Time.now.to_i.to_s.reverse[0..3]}#{rand(9)}"
106
- end
162
+ def list_instances(credentials, id)
163
+ instances = []
164
+ safely do
165
+ new_client(credentials).request('grid/server/list')['list'].collect do |instance|
166
+ if id.nil? or instance['name'] == id
167
+ instances << convert_instance(instance, credentials.user)
168
+ end
169
+ end
170
+ end
171
+ instances
172
+ end
107
173
 
108
- if name.length > USER_NAME_MAX
109
- raise "Parameter name must be #{USER_NAME_MAX} characters or less"
110
- end
174
+ def instances(credentials, opts=nil)
175
+ instances = []
176
+ if opts and opts[:id]
177
+ begin
178
+ client = new_client(credentials)
179
+ instance = client.request('grid/server/get', { 'name' => opts[:id] })['list'].first
180
+ login_data = get_login_data(client, instance['id'])
181
+ if login_data['username'] and login_data['password']
182
+ instance['username'] = login_data['username']
183
+ instance['password'] = login_data['password']
184
+ inst = convert_instance(instance, credentials.user)
185
+ else
186
+ inst = convert_instance(instance, credentials.user)
187
+ inst.authn_error = "Unable to fetch password"
188
+ end
189
+ instances = [inst]
190
+ rescue Exception => e
191
+ if e.message == "400 Bad Request"
192
+ # in the case of a VM that we just made, the grid/server/get method
193
+ # throws a "400 Bad Request error". In this case we try again by
194
+ # getting a full listing a filtering on the id. This could
195
+ # potentially take a long time, but I don't see another way to get
196
+ # information about a newly created instance
197
+ instances = list_instances(credentials, opts[:id])
198
+ end
199
+ end
200
+ else
201
+ instances = list_instances(credentials, nil)
202
+ end
203
+ instances = filter_on( instances, :state, opts )
204
+ instances
205
+ end
111
206
 
112
- client = new_client(credentials)
113
- params = {
114
- 'name' => name,
115
- 'image' => image_id,
116
- 'server.ram' => server_ram,
117
- 'ip' => get_free_ip_from_realm(credentials, opts[:realm_id] || '1')
118
- }
119
- params.merge!('isSandbox' => 'true') if opts[:sandbox]
120
- safely do
121
- instance = client.request('grid/server/add', params)['list'].first
122
- if instance
123
- login_data = get_login_data(client, instance[:id])
124
- if login_data['username'] and login_data['password']
125
- instance['username'] = login_data['username']
126
- instance['password'] = login_data['password']
127
- inst = convert_instance(instance, credentials.user)
128
- else
129
- inst = convert_instance(instance, credentials.user)
130
- inst.authn_error = "Unable to fetch password"
131
- end
132
- return inst
133
- else
134
- return nil
135
- end
136
- end
137
- end
207
+ def reboot_instance(credentials, id)
208
+ safely do
209
+ new_client(credentials).request('grid/server/power', { 'name' => id, 'power' => 'reboot'})
210
+ end
211
+ end
138
212
 
139
- def run_on_instance(credentials, opts={})
140
- target = instance(credentials, opts[:id])
141
- param = {}
142
- param[:credentials] = {
143
- :username => target.username,
144
- :password => target.password,
145
- }
146
- param[:credentials].merge!({ :password => opts[:password]}) if opts[:password].length>0
147
- param[:port] = opts[:port] || '22'
148
- param[:ip] = opts[:ip] || target.public_addresses.first.address
149
- Deltacloud::Runner.execute(opts[:cmd], param)
150
- end
213
+ def destroy_instance(credentials, id)
214
+ safely do
215
+ new_client(credentials).request('grid/server/delete', { 'name' => id})
216
+ end
217
+ end
151
218
 
219
+ def stop_instance(credentials, id)
220
+ safely do
221
+ new_client(credentials).request('grid/server/power', { 'name' => id, 'power' => 'off'})
222
+ end
223
+ end
152
224
 
225
+ def start_instance(credentials, id)
226
+ safely do
227
+ new_client(credentials).request('grid/server/power', { 'name' => id, 'power' => 'on'})
228
+ end
229
+ end
153
230
 
154
- def list_instances(credentials, id)
155
- instances = []
156
- safely do
157
- new_client(credentials).request('grid/server/list')['list'].collect do |instance|
158
- if id.nil? or instance['name'] == id
159
- instances << convert_instance(instance, credentials.user)
231
+ def create_load_balancer(credentials, opts={})
232
+ gogrid = new_client(credentials)
233
+ balancer, l_instance = nil, nil
234
+ safely do
235
+ virtip = get_free_ip_from_realm(credentials, opts['realm_id'])
236
+ if opts['instance_id']
237
+ l_instance = instance(credentials, :id => opts['instance_id'])
238
+ real_ip = {
239
+ 'realiplist.0.port' => opts['listener_instance_port'].to_i,
240
+ 'realiplist.0.ip' => l_instance ? l_instance.public_addresses.first : ""
241
+ }
242
+ else
243
+ real_ip = false
244
+ end
245
+ request = {
246
+ 'name' => opts['name'],
247
+ 'virtualip.ip' => virtip,
248
+ 'virtualip.port' => opts['listener_balancer_port'].to_i,
249
+ }
250
+ request.merge!(real_ip) if real_ip
251
+ balancer = gogrid.request('grid/loadbalancer/add', request)['list'].first
252
+ end
253
+ balancer = convert_load_balancer(credentials, balancer)
254
+ balancer.instances = [l_instance] if l_instance
255
+ balancer
160
256
  end
161
- end
162
- end
163
- instances
164
- end
165
257
 
166
- def instances(credentials, opts=nil)
167
- instances = []
168
- if opts and opts[:id]
169
- begin
170
- client = new_client(credentials)
171
- instance = client.request('grid/server/get', { 'name' => opts[:id] })['list'].first
172
- login_data = get_login_data(client, instance['id'])
173
- if login_data['username'] and login_data['password']
174
- instance['username'] = login_data['username']
175
- instance['password'] = login_data['password']
176
- inst = convert_instance(instance, credentials.user)
177
- else
178
- inst = convert_instance(instance, credentials.user)
179
- inst.authn_error = "Unable to fetch password"
180
- end
181
- instances = [inst]
182
- rescue Exception => e
183
- if e.message == "400 Bad Request"
184
- # in the case of a VM that we just made, the grid/server/get method
185
- # throws a "400 Bad Request error". In this case we try again by
186
- # getting a full listing a filtering on the id. This could
187
- # potentially take a long time, but I don't see another way to get
188
- # information about a newly created instance
189
- instances = list_instances(credentials, opts[:id])
258
+ def destroy_load_balancer(credentials, id)
259
+ gogrid = new_client(credentials)
260
+ balancer = nil
261
+ safely do
262
+ balancer = gogrid.request('grid/loadbalancer/delete', { 'name' => id })
263
+ end
190
264
  end
191
- end
192
- else
193
- instances = list_instances(credentials, nil)
194
- end
195
- instances = filter_on( instances, :state, opts )
196
- instances
197
- end
198
265
 
199
- def reboot_instance(credentials, id)
200
- safely do
201
- new_client(credentials).request('grid/server/power', { 'name' => id, 'power' => 'reboot'})
202
- end
203
- end
266
+ def load_balancers(credentials, opts={})
267
+ gogrid = new_client(credentials)
268
+ balancers = []
269
+ safely do
270
+ gogrid.request('grid/loadbalancer/list', opts || {})['list'].each do |b|
271
+ balancers << b
272
+ end
273
+ end
274
+ balancers.collect { |b| convert_load_balancer(credentials, b) }
275
+ end
204
276
 
205
- def destroy_instance(credentials, id)
206
- safely do
207
- new_client(credentials).request('grid/server/delete', { 'name' => id})
208
- end
209
- end
277
+ def load_balancer(credentials, opts={})
278
+ gogrid = new_client(credentials)
279
+ balancer = nil
280
+ begin
281
+ balancer = gogrid.request('grid/loadbalancer/get', { 'name' => opts[:id] })['list'].first
282
+ balancer['instances'] = instances(credentials)
283
+ return convert_load_balancer(credentials, balancer)
284
+ rescue OpenURI::HTTPError
285
+ balancer = load_balancers(credentials, :id => opts[:id]).first
286
+ end
287
+ end
210
288
 
211
- def stop_instance(credentials, id)
212
- safely do
213
- new_client(credentials).request('grid/server/power', { 'name' => id, 'power' => 'off'})
214
- end
215
- end
216
289
 
217
- def start_instance(credentials, id)
218
- safely do
219
- new_client(credentials).request('grid/server/power', { 'name' => id, 'power' => 'on'})
220
- end
221
- end
290
+ def lb_register_instance(credentials, opts={})
291
+ client = new_client(credentials)
292
+ instance = instance(credentials, :id => opts[:instance_id])
293
+ balancer = client.request('grid/loadbalancer/get', { 'name' => opts[:id]})['list'].first
294
+ safely do
295
+ convert_load_balancer(credentials, client.request('grid/loadbalancer/edit', {
296
+ "id" => balancer['id'],
297
+ "realiplist.#{balancer['realiplist'].size}.ip" => instance.public_addresses.first,
298
+ "realiplist.#{balancer['realiplist'].size}.port" => balancer['virtualip']['port']
299
+ }))
300
+ end
301
+ end
222
302
 
223
- def create_load_balancer(credentials, opts={})
224
- gogrid = new_client(credentials)
225
- balancer, l_instance = nil, nil
226
- safely do
227
- virtip = get_free_ip_from_realm(credentials, opts['realm_id'])
228
- if opts['instance_id']
229
- l_instance = instance(credentials, :id => opts['instance_id'])
230
- real_ip = {
231
- 'realiplist.0.port' => opts['listener_instance_port'].to_i,
232
- 'realiplist.0.ip' => l_instance ? l_instance.public_addresses.first : ""
233
- }
234
- else
235
- real_ip = false
236
- end
237
- request = {
238
- 'name' => opts['name'],
239
- 'virtualip.ip' => virtip,
240
- 'virtualip.port' => opts['listener_balancer_port'].to_i,
241
- }
242
- request.merge!(real_ip) if real_ip
243
- balancer = gogrid.request('grid/loadbalancer/add', request)['list'].first
244
- end
245
- balancer = convert_load_balancer(credentials, balancer)
246
- balancer.instances = [l_instance] if l_instance
247
- balancer
248
- end
303
+ # Move this to capabilities
304
+ def lb_unregister_instance(credentials, opts={})
305
+ raise Deltacloud::BackendFeatureUnsupported.new('501',
306
+ 'Unregistering instances from load balancer is not supported in GoGrid')
307
+ end
249
308
 
250
- def destroy_load_balancer(credentials, id)
251
- gogrid = new_client(credentials)
252
- balancer = nil
253
- safely do
254
- balancer = gogrid.request('grid/loadbalancer/delete', { 'name' => id })
255
- end
256
- end
309
+ def key(credentials, opts=nil)
310
+ keys(credentials, opts).first
311
+ end
257
312
 
258
- def load_balancers(credentials, opts={})
259
- gogrid = new_client(credentials)
260
- balancers = []
261
- safely do
262
- balancer = gogrid.request('grid/loadbalancer/list', opts || {})['list'].each do |balancer|
263
- balancers << balancer
264
- end
265
- end
266
- balancers.collect { |b| convert_load_balancer(credentials, b) }
267
- end
313
+ def keys(credentials, opts=nil)
314
+ gogrid = new_client( credentials )
315
+ creds = []
316
+ safely do
317
+ gogrid.request('support/password/list')['list'].each do |password|
318
+ creds << convert_key(password)
319
+ end
320
+ end
321
+ return creds
322
+ end
268
323
 
269
- def load_balancer(credentials, opts={})
270
- gogrid = new_client(credentials)
271
- balancer = nil
272
- begin
273
- balancer = gogrid.request('grid/loadbalancer/get', { 'name' => opts[:id] })['list'].first
274
- balancer['instances'] = instances(credentials)
275
- return convert_load_balancer(credentials, balancer)
276
- rescue OpenURI::HTTPError
277
- balancer = load_balancers(credentials, :id => opts[:id]).first
278
- end
279
- end
324
+ def valid_credentials?(credentials)
325
+ # FIXME: We need to do this call to determine if
326
+ # GoGrid is working with given credentials. There is no
327
+ # other way to check, if given credentials are valid or not.
328
+ return false unless new_client(credentials).request('common/lookup/list', { 'lookup' => 'ip.datacenter' })
329
+ true
330
+ end
280
331
 
332
+ define_instance_states do
333
+ start.to( :pending ) .automatically
334
+ pending.to( :running ) .automatically
335
+ running.to( :stopped ) .on( :stop )
336
+ stopped.to( :running ) .on( :start )
337
+ running.to( :finish ) .on( :destroy )
338
+ stopped.to( :finish ) .on( :destroy )
339
+ end
281
340
 
282
- def lb_register_instance(credentials, opts={})
283
- client = new_client(credentials)
284
- instance = instance(credentials, :id => opts[:instance_id])
285
- balancer = client.request('grid/loadbalancer/get', { 'name' => opts[:id]})['list'].first
286
- safely do
287
- convert_load_balancer(credentials, client.request('grid/loadbalancer/edit', {
288
- "id" => balancer['id'],
289
- "realiplist.#{balancer['realiplist'].size}.ip" => instance.public_addresses.first,
290
- "realiplist.#{balancer['realiplist'].size}.port" => balancer['virtualip']['port']
291
- }))
292
- end
293
- end
341
+ exceptions do
294
342
 
295
- # Move this to capabilities
296
- def lb_unregister_instance(credentials, opts={})
297
- raise Deltacloud::BackendFeatureUnsupported.new('501',
298
- 'Unregistering instances from load balancer is not supported in GoGrid')
299
- end
343
+ on /Forbidden/ do
344
+ status 401
345
+ end
300
346
 
301
- def key(credentials, opts=nil)
302
- keys(credentials, opts).first
303
- end
347
+ end
304
348
 
305
- def keys(credentials, opts=nil)
306
- gogrid = new_client( credentials )
307
- creds = []
308
- safely do
309
- gogrid.request('support/password/list')['list'].each do |password|
310
- creds << convert_key(password)
311
- end
312
- end
313
- return creds
314
- end
349
+ private
315
350
 
316
- def valid_credentials?(credentials)
317
- client = new_client(credentials)
318
- # FIXME: We need to do this call to determine if
319
- # GoGrid is working with given credentials. There is no
320
- # other way to check, if given credentials are valid or not.
321
- return false unless new_client(credentials).request('common/lookup/list', { 'lookup' => 'ip.datacenter' })
322
- true
323
- end
351
+ def new_client(credentials)
352
+ GoGridClient.new('https://api.gogrid.com/api', credentials.user, credentials.password)
353
+ end
324
354
 
325
- define_instance_states do
326
- start.to( :pending ) .automatically
327
- pending.to( :running ) .automatically
328
- running.to( :stopped ) .on( :stop )
329
- stopped.to( :running ) .on( :start )
330
- running.to( :finish ) .on( :destroy )
331
- stopped.to( :finish ) .on( :destroy )
332
- end
355
+ def convert_load_balancer(credentials, loadbalancer)
356
+ if loadbalancer['datacenter']
357
+ b_realm = realm(credentials, :id => loadbalancer['datacenter']['id'].to_s)
358
+ else
359
+ # Report first Realm until loadbalancer become ready
360
+ b_realm = realm(credentials, :id => 1)
361
+ end
362
+ balancer = LoadBalancer.new({
363
+ :id => loadbalancer['name'],
364
+ :realms => [b_realm]
365
+ })
366
+ balancer.public_addresses = [loadbalancer['virtualip']['ip']['ip']] if loadbalancer['virtualip'] and loadbalancer['virtualip']['ip']
367
+ balancer.listeners = []
368
+ balancer.instances = []
369
+ instance_ips = []
370
+ if loadbalancer['realiplist'].size > 0
371
+ loadbalancer['realiplist'].each do |instance_ip|
372
+ balancer.add_listener({
373
+ :protocol => 'TCP',
374
+ :load_balancer_port => loadbalancer['virtualip']['port'],
375
+ :instance_port => instance_ip['port']
376
+ })
377
+ instance_ips << instance_ip['ip']['ip']
378
+ end
379
+ else
380
+ balancer.add_listener({:protocol=>'TCP', :load_balancer_port =>loadbalancer['virtualip']['port'],
381
+ :instance_port => "unassigned"})
382
+ end
383
+ balancer.instances = get_load_balancer_instances(instance_ips, loadbalancer['instances']) if loadbalancer['instances']
384
+ return balancer
385
+ end
333
386
 
334
- def catched_exceptions_list
335
- {
336
- :auth => [ /Forbidden/ ],
337
- :error => [ /Error/ ],
338
- :glob => [ /(.*)/ ]
339
- }
340
- end
387
+ def get_load_balancer_instances(instance_ips, instances)
388
+ instances.select { |i| instance_ips.include?(i.public_addresses.first) } if instances
389
+ end
341
390
 
342
- private
343
391
 
344
- def new_client(credentials)
345
- GoGridClient.new('https://api.gogrid.com/api', credentials.user, credentials.password)
346
- end
392
+ def get_login_data(client, instance_id)
393
+ login_data = {}
394
+ begin
395
+ client.request('support/password/list')['list'].each do |passwd|
396
+ next unless passwd['server']
397
+ if passwd['server']['id'] == instance_id
398
+ login_data['username'], login_data['password'] = passwd['username'], passwd['password']
399
+ break
400
+ end
401
+ end
402
+ rescue Exception => e
403
+ login_data[:error] = e.message
404
+ end
405
+ return login_data
406
+ end
347
407
 
348
- def convert_load_balancer(credentials, loadbalancer)
349
- if loadbalancer['datacenter']
350
- b_realm = realm(credentials, :id => loadbalancer['datacenter']['id'].to_s)
351
- else
352
- # Report first Realm until loadbalancer become ready
353
- b_realm = realm(credentials, :id => 1)
354
- end
355
- balancer = LoadBalancer.new({
356
- :id => loadbalancer['name'],
357
- :realms => [b_realm]
358
- })
359
- balancer.public_addresses = [loadbalancer['virtualip']['ip']['ip']] if loadbalancer['virtualip'] and loadbalancer['virtualip']['ip']
360
- balancer.listeners = []
361
- balancer.instances = []
362
- instance_ips = []
363
- if loadbalancer['realiplist'].size > 0
364
- loadbalancer['realiplist'].each do |instance_ip|
365
- balancer.add_listener({
366
- :protocol => 'TCP',
367
- :load_balancer_port => loadbalancer['virtualip']['port'],
368
- :instance_port => instance_ip['port']
369
- })
370
- instance_ips << instance_ip['ip']['ip']
371
- end
372
- else
373
- balancer.add_listener({:protocol=>'TCP', :load_balancer_port =>loadbalancer['virtualip']['port'],
374
- :instance_port => "unassigned"})
375
- end
376
- balancer.instances = get_load_balancer_instances(instance_ips, loadbalancer['instances']) if loadbalancer['instances']
377
- return balancer
378
- end
408
+ def convert_key(password)
409
+ Key.new({
410
+ :id => password['id'],
411
+ :username => password['username'],
412
+ :password => password['password'],
413
+ :credential_type => :password
414
+ })
415
+ end
379
416
 
380
- def get_load_balancer_instances(instance_ips, instances)
381
- instances.select { |i| instance_ips.include?(i.public_addresses.first) } if instances
382
- end
417
+ def convert_image(gg_image, owner_id=nil)
418
+ Image.new( {
419
+ :id=>gg_image['id'].to_s,
420
+ :name => "#{gg_image['friendlyName']}",
421
+ :description=> convert_description(gg_image).to_s,
422
+ :owner_id=>gg_image['owner']['name'].to_s,
423
+ :architecture=>convert_arch(gg_image['description']),
424
+ :state => gg_image['state']['name'].upcase
425
+ } )
426
+ end
383
427
 
428
+ def convert_description(image)
429
+ if image['price'].eql?(0)
430
+ image['description']
431
+ else
432
+ "#{image['description']} (#{image['price']}$)"
433
+ end
434
+ end
384
435
 
385
- def get_login_data(client, instance_id)
386
- login_data = {}
387
- begin
388
- client.request('support/password/list')['list'].each do |passwd|
389
- next unless passwd['server']
390
- if passwd['server']['id'] == instance_id
391
- login_data['username'], login_data['password'] = passwd['username'], passwd['password']
392
- break
436
+ def convert_realm(realm)
437
+ Realm.new(
438
+ :id => realm['id'].to_s,
439
+ :name => realm['name'],
440
+ :state => :unlimited,
441
+ :storage => :unlimited
442
+ )
393
443
  end
394
- end
395
- rescue Exception => e
396
- login_data[:error] = e.message
397
- end
398
- return login_data
399
- end
400
444
 
401
- def convert_key(password)
402
- Key.new({
403
- :id => password['id'],
404
- :username => password['username'],
405
- :password => password['password'],
406
- :credential_type => :password
407
- })
408
- end
445
+ def convert_arch(description)
446
+ return 'i386' unless description
447
+ description.include?('64-bit') ? 'x86_64' : 'i386'
448
+ end
409
449
 
410
- def convert_image(gg_image, owner_id=nil)
411
- Image.new( {
412
- :id=>gg_image['id'].to_s,
413
- :name => "#{gg_image['friendlyName']}",
414
- :description=> convert_description(gg_image).to_s,
415
- :owner_id=>gg_image['owner']['name'].to_s,
416
- :architecture=>convert_arch(gg_image['description']),
417
- :state => gg_image['state']['name'].upcase
418
- } )
419
- end
450
+ def convert_instance(instance, owner_id)
451
+ opts = {}
452
+ unless instance['ram']['id'] == "1"
453
+ mem = instance['ram']['name']
454
+ if mem == "512MB"
455
+ opts[:hwp_memory] = "512"
456
+ else
457
+ opts[:hwp_memory] = (mem.to_i * 1024).to_s
458
+ end
459
+ end
460
+
461
+ prof = InstanceProfile.new("server", opts)
462
+ state = convert_server_state(instance['state']['name'], instance['id'])
463
+
464
+ Instance.new(
465
+ # note that we use 'name' as the id here, because newly created instances
466
+ # don't get a real ID until later on. The name is good enough; from
467
+ # what I can tell, 'name' per user is unique, so it should be sufficient
468
+ # to uniquely identify this instance.
469
+ :id => instance['name'],
470
+ :owner_id => owner_id,
471
+ :image_id => "#{instance['image']['id']}",
472
+ :instance_profile => prof,
473
+ :name => instance['name'],
474
+ :realm_id => instance['ip']['datacenter']['id'],
475
+ :state => state,
476
+ :actions => instance_actions_for(state),
477
+ :public_addresses => [ InstanceAddress.new(instance['ip']['ip']) ],
478
+ :private_addresses => [],
479
+ :username => instance['username'],
480
+ :password => instance['password'],
481
+ :create_image => 'true'.eql?(instance['isSandbox'])
482
+ )
483
+ end
420
484
 
421
- def convert_description(image)
422
- if image['price'].eql?(0)
423
- image['description']
424
- else
425
- "#{image['description']} (#{image['price']}$)"
426
- end
427
- end
485
+ def convert_server_state(state, id)
486
+ return 'PENDING' unless id
487
+ state.eql?('Off') ? 'STOPPED' : 'RUNNING'
488
+ end
428
489
 
429
- def convert_realm(realm)
430
- Realm.new(
431
- :id => realm['id'].to_s,
432
- :name => realm['name'],
433
- :state => :unlimited,
434
- :storage => :unlimited
435
- )
436
- end
490
+ def get_free_ip_from_realm(credentials, realm_id, ip_type = 1)
491
+ ip = ""
492
+ safely do
493
+ ip = new_client(credentials).request('grid/ip/list', {
494
+ 'ip.type' => "#{ip_type}",
495
+ 'ip.state' => '1',
496
+ 'datacenter' => realm_id
497
+ })['list'].first['ip']
498
+ end
499
+ return ip
500
+ end
437
501
 
438
- def convert_arch(description)
439
- return 'i386' unless description
440
- description.include?('64-bit') ? 'x86_64' : 'i386'
441
- end
442
502
 
443
- def convert_instance(instance, owner_id)
444
- opts = {}
445
- unless instance['ram']['id'] == "1"
446
- mem = instance['ram']['name']
447
- if mem == "512MB"
448
- opts[:hwp_memory] = "512"
449
- else
450
- opts[:hwp_memory] = (mem.to_i * 1024).to_s
451
503
  end
452
- end
453
-
454
- prof = InstanceProfile.new("server", opts)
455
- hwp_name = instance['image']['name']
456
- state = convert_server_state(instance['state']['name'], instance['id'])
457
-
458
- Instance.new(
459
- # note that we use 'name' as the id here, because newly created instances
460
- # don't get a real ID until later on. The name is good enough; from
461
- # what I can tell, 'name' per user is unique, so it should be sufficient
462
- # to uniquely identify this instance.
463
- :id => instance['name'],
464
- :owner_id => owner_id,
465
- :image_id => "#{instance['image']['id']}",
466
- :instance_profile => prof,
467
- :name => instance['name'],
468
- :realm_id => instance['ip']['datacenter']['id'],
469
- :state => state,
470
- :actions => instance_actions_for(state),
471
- :public_addresses => [ InstanceAddress.new(instance['ip']['ip']) ],
472
- :private_addresses => [],
473
- :username => instance['username'],
474
- :password => instance['password'],
475
- :create_image => 'true'.eql?(instance['isSandbox'])
476
- )
477
- end
478
-
479
- def convert_server_state(state, id)
480
- return 'PENDING' unless id
481
- state.eql?('Off') ? 'STOPPED' : 'RUNNING'
482
- end
483
-
484
- def get_free_ip_from_realm(credentials, realm_id, ip_type = 1)
485
- ip = ""
486
- safely do
487
- ip = new_client(credentials).request('grid/ip/list', {
488
- 'ip.type' => "#{ip_type}",
489
- 'ip.state' => '1',
490
- 'datacenter' => realm_id
491
- })['list'].first['ip']
492
- end
493
- return ip
494
- end
495
-
496
-
497
- end
498
504
 
499
505
  end
500
506
  end