deltacloud-core 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (222) hide show
  1. data/Rakefile +99 -141
  2. data/bin/deltacloudd +4 -1
  3. data/config.ru +13 -6
  4. data/config/drivers/aruba.yaml +8 -0
  5. data/config/drivers/fgcp.yaml +3 -2
  6. data/deltacloud-core.gemspec +13 -9
  7. data/lib/cimi/collections/cloud_entry_point.rb +1 -1
  8. data/lib/cimi/models.rb +1 -7
  9. data/lib/cimi/models/base.rb +1 -4
  10. data/lib/cimi/models/cloud_entry_point.rb +14 -0
  11. data/lib/cimi/models/machine.rb +4 -4
  12. data/lib/cimi/models/machine_admin.rb +1 -0
  13. data/lib/cimi/models/machine_configuration.rb +4 -4
  14. data/lib/cimi/models/schema.rb +17 -14
  15. data/lib/cimi/models/volume_image.rb +1 -1
  16. data/lib/cimi/server.rb +1 -1
  17. data/lib/deltacloud/api.rb +55 -3
  18. data/lib/deltacloud/collections/addresses.rb +1 -1
  19. data/lib/deltacloud/collections/buckets.rb +12 -10
  20. data/lib/deltacloud/collections/firewalls.rb +2 -1
  21. data/lib/deltacloud/collections/hardware_profiles.rb +1 -1
  22. data/lib/deltacloud/collections/images.rb +2 -2
  23. data/lib/deltacloud/collections/instance_states.rb +6 -3
  24. data/lib/deltacloud/collections/instances.rb +2 -1
  25. data/lib/deltacloud/collections/keys.rb +6 -2
  26. data/lib/deltacloud/collections/load_balancers.rb +8 -7
  27. data/lib/deltacloud/collections/metrics.rb +2 -1
  28. data/lib/deltacloud/collections/realms.rb +1 -1
  29. data/lib/deltacloud/collections/storage_snapshots.rb +2 -1
  30. data/lib/deltacloud/collections/storage_volumes.rb +2 -2
  31. data/lib/deltacloud/core_ext.rb +1 -7
  32. data/lib/deltacloud/core_ext/hash.rb +7 -5
  33. data/lib/deltacloud/core_ext/string.rb +1 -0
  34. data/lib/deltacloud/drivers/aruba/aruba_driver.rb +469 -0
  35. data/lib/deltacloud/drivers/base_driver.rb +12 -17
  36. data/lib/deltacloud/drivers/ec2/ec2_driver.rb +50 -22
  37. data/lib/deltacloud/drivers/eucalyptus/eucalyptus_driver.rb +1 -1
  38. data/lib/deltacloud/drivers/exceptions.rb +12 -2
  39. data/lib/deltacloud/drivers/features.rb +8 -0
  40. data/lib/deltacloud/drivers/fgcp/fgcp_client.rb +21 -6
  41. data/lib/deltacloud/drivers/fgcp/fgcp_driver.rb +169 -57
  42. data/lib/deltacloud/drivers/gogrid/gogrid_driver.rb +1 -1
  43. data/lib/deltacloud/drivers/mock/mock_client.rb +2 -0
  44. data/lib/deltacloud/drivers/mock/mock_driver.rb +101 -13
  45. data/lib/deltacloud/drivers/opennebula/occi_client.rb +12 -6
  46. data/lib/deltacloud/drivers/opennebula/opennebula_driver.rb +10 -18
  47. data/lib/deltacloud/drivers/openstack/openstack_driver.rb +46 -1
  48. data/lib/deltacloud/drivers/rhevm/rhevm_driver.rb +17 -17
  49. data/lib/deltacloud/drivers/vsphere/vsphere_driver.rb +4 -4
  50. data/lib/deltacloud/helpers.rb +4 -4
  51. data/lib/deltacloud/helpers/blob_stream_helper.rb +4 -6
  52. data/lib/deltacloud/helpers/deltacloud_helper.rb +17 -1
  53. data/lib/deltacloud/helpers/driver_helper.rb +7 -3
  54. data/lib/deltacloud/helpers/url_helper.rb +4 -4
  55. data/lib/deltacloud/models/address.rb +1 -1
  56. data/lib/deltacloud/models/base_model.rb +0 -14
  57. data/lib/deltacloud/models/bucket.rb +0 -10
  58. data/lib/deltacloud/models/hardware_profile.rb +5 -26
  59. data/lib/deltacloud/models/image.rb +0 -12
  60. data/lib/deltacloud/models/instance.rb +6 -22
  61. data/lib/deltacloud/models/key.rb +2 -2
  62. data/lib/deltacloud/models/metric.rb +1 -1
  63. data/lib/deltacloud/models/storage_snapshot.rb +4 -0
  64. data/lib/deltacloud/server.rb +8 -2
  65. data/lib/deltacloud_rack.rb +40 -10
  66. data/lib/ec2/query_parser.rb +9 -4
  67. data/lib/ec2/server.rb +1 -1
  68. data/lib/ec2/views/create_key_pair.haml +3 -0
  69. data/lib/ec2/views/delete_key_pair.haml +1 -0
  70. data/lib/ec2/views/describe_availability_zones.haml +6 -0
  71. data/lib/ec2/views/describe_images.haml +10 -0
  72. data/lib/ec2/views/describe_instance_set.haml +24 -0
  73. data/lib/ec2/views/describe_instances.haml +9 -0
  74. data/lib/ec2/views/describe_key_pairs.haml +5 -0
  75. data/lib/ec2/views/error.haml +4 -0
  76. data/lib/ec2/views/instance_action.haml +9 -0
  77. data/lib/ec2/views/reboot_instances.haml +1 -0
  78. data/lib/ec2/views/run_instances.haml +7 -0
  79. data/lib/ec2/views/start_instances.haml +1 -0
  80. data/lib/ec2/views/stop_instances.haml +1 -0
  81. data/lib/ec2/views/terminate_instances.haml +9 -0
  82. data/lib/sinatra.rb +0 -1
  83. data/lib/sinatra/rack_accept.rb +1 -1
  84. data/lib/sinatra/rack_driver_select.rb +2 -2
  85. data/lib/sinatra/rack_etag.rb +5 -1
  86. data/lib/sinatra/rack_logger.rb +127 -0
  87. data/lib/sinatra/rack_matrix_params.rb +1 -1
  88. data/tests/cimi/collections/cloud_entry_point_test.rb +46 -0
  89. data/tests/cimi/collections/common.rb +17 -0
  90. data/tests/cimi/collections/machine_images_test.rb +44 -0
  91. data/tests/cimi/collections/machines_test.rb +41 -0
  92. data/tests/cimi/spec/cimi/model/machine_admin_spec.rb +35 -0
  93. data/tests/cimi/spec/cimi/model/machine_configuration_spec.rb +31 -0
  94. data/tests/cimi/spec/cimi/model/machine_image_spec.rb +33 -0
  95. data/tests/cimi/spec/cimi/model/machine_spec.rb +32 -0
  96. data/tests/cimi/spec/cimi/model/machine_template_spec.rb +32 -0
  97. data/tests/cimi/spec/cimi/model/schema_spec.rb +245 -0
  98. data/tests/cimi/spec/cimi/model/volume_configuration_spec.rb +34 -0
  99. data/tests/cimi/spec/cimi/model/volume_image_spec.rb +33 -0
  100. data/tests/cimi/spec/cimi/model/volume_spec.rb +32 -0
  101. data/tests/cimi/spec/cimi/model/volume_template_spec.rb +32 -0
  102. data/tests/cimi/spec/spec_helper.rb +136 -0
  103. data/tests/deltacloud/base_collection_test.rb +30 -0
  104. data/tests/deltacloud/collections/buckets_collection_test.rb +65 -0
  105. data/tests/deltacloud/collections/drivers_collection_test.rb +37 -0
  106. data/tests/deltacloud/collections/hardware_profiles_collection_test.rb +53 -0
  107. data/tests/deltacloud/collections/images_collection_test.rb +59 -0
  108. data/tests/deltacloud/collections/instance_states_collection_test.rb +34 -0
  109. data/tests/deltacloud/collections/instances_collection_test.rb +63 -0
  110. data/tests/deltacloud/collections/keys_collection_test.rb +63 -0
  111. data/tests/deltacloud/collections_test.rb +35 -0
  112. data/tests/deltacloud/common.rb +14 -0
  113. data/tests/deltacloud/deltacloud_helper_test.rb +55 -0
  114. data/tests/deltacloud/drivers_test.rb +73 -0
  115. data/tests/deltacloud/rack_test.rb +51 -0
  116. data/tests/deltacloud/server_test.rb +149 -0
  117. data/tests/{api → deprecated/api}/common.rb +0 -0
  118. data/tests/{api → deprecated/api}/driver_test.rb +0 -0
  119. data/tests/deprecated/api/library_test.rb +6 -0
  120. data/tests/{cimi → deprecated/cimi}/features/step_definitions/common_steps.rb +0 -0
  121. data/tests/{cimi → deprecated/cimi}/features/step_definitions/machine_images_steps.rb +0 -0
  122. data/tests/{cimi → deprecated/cimi}/features/step_definitions/machines_steps.rb +0 -0
  123. data/tests/{cimi → deprecated/cimi}/features/step_definitions/volumes_steps.rb +0 -0
  124. data/tests/{cimi → deprecated/cimi}/features/support/env.rb +0 -0
  125. data/tests/{common.rb → deprecated/common.rb} +0 -0
  126. data/tests/{core_ext → deprecated/core_ext}/string.rb +0 -0
  127. data/tests/{drivers → deprecated/drivers}/ec2/api_test.rb +0 -0
  128. data/tests/deprecated/drivers/ec2/common.rb +23 -0
  129. data/tests/{drivers → deprecated/drivers}/ec2/drivers_test.rb +0 -0
  130. data/tests/{drivers → deprecated/drivers}/ec2/hardware_profiles_test.rb +0 -0
  131. data/tests/deprecated/drivers/ec2/images_test.rb +230 -0
  132. data/tests/{drivers → deprecated/drivers}/ec2/instances_test.rb +0 -0
  133. data/tests/deprecated/drivers/ec2/keys_test.rb +181 -0
  134. data/tests/deprecated/drivers/ec2/realms_test.rb +146 -0
  135. data/tests/{drivers → deprecated/drivers}/fgcp/api_test.rb +0 -0
  136. data/tests/{drivers → deprecated/drivers}/fgcp/hardware_profiles_test.rb +0 -0
  137. data/tests/{drivers → deprecated/drivers}/fgcp/realms_test.rb +0 -0
  138. data/tests/{drivers → deprecated/drivers}/fgcp/setup.rb +0 -0
  139. data/tests/{drivers → deprecated/drivers}/google/api_test.rb +0 -0
  140. data/tests/{drivers → deprecated/drivers}/google/buckets_test.rb +0 -0
  141. data/tests/{drivers → deprecated/drivers}/google/common.rb +2 -2
  142. data/tests/{drivers → deprecated/drivers}/mock/api_test.rb +0 -0
  143. data/tests/{drivers → deprecated/drivers}/mock/buckets_test.rb +0 -0
  144. data/tests/{drivers → deprecated/drivers}/mock/common.rb +0 -0
  145. data/tests/{drivers → deprecated/drivers}/mock/drivers_test.rb +0 -0
  146. data/tests/{drivers → deprecated/drivers}/mock/hardware_profiles_test.rb +0 -0
  147. data/tests/deprecated/drivers/mock/images_test.rb +197 -0
  148. data/tests/deprecated/drivers/mock/instances_test.rb +343 -0
  149. data/tests/deprecated/drivers/mock/keys_test.rb +161 -0
  150. data/tests/deprecated/drivers/mock/realms_test.rb +132 -0
  151. data/tests/deprecated/drivers/mock/storage_snapshots_test.rb +114 -0
  152. data/tests/deprecated/drivers/mock/storage_volumes_test.rb +122 -0
  153. data/tests/{drivers → deprecated/drivers}/openstack/api_test.rb +0 -0
  154. data/tests/{drivers → deprecated/drivers}/openstack/common.rb +0 -0
  155. data/tests/{drivers → deprecated/drivers}/openstack/hardware_profiles_test.rb +0 -0
  156. data/tests/{drivers → deprecated/drivers}/openstack/images_test.rb +0 -0
  157. data/tests/{drivers → deprecated/drivers}/openstack/instances_test.rb +0 -0
  158. data/tests/{drivers → deprecated/drivers}/openstack/realms_test.rb +0 -0
  159. data/tests/{drivers → deprecated/drivers}/rackspace/api_test.rb +0 -0
  160. data/tests/{drivers → deprecated/drivers}/rackspace/buckets_test.rb +0 -0
  161. data/tests/{drivers → deprecated/drivers}/rackspace/common.rb +0 -0
  162. data/tests/{drivers → deprecated/drivers}/rackspace/hardware_profiles_test.rb +0 -0
  163. data/tests/{drivers → deprecated/drivers}/rackspace/images_test.rb +0 -0
  164. data/tests/{drivers → deprecated/drivers}/rackspace/instances_test.rb +0 -0
  165. data/tests/{drivers → deprecated/drivers}/rackspace/realms_test.rb +0 -0
  166. data/tests/{drivers → deprecated/drivers}/rhevm/api_test.rb +0 -0
  167. data/tests/deprecated/drivers/rhevm/common.rb +21 -0
  168. data/tests/{drivers → deprecated/drivers}/rhevm/hardware_profiles_test.rb +0 -0
  169. data/tests/deprecated/drivers/rhevm/images_test.rb +48 -0
  170. data/tests/{drivers → deprecated/drivers}/rhevm/instances_test.rb +0 -0
  171. data/tests/deprecated/drivers/rhevm/realms_test.rb +40 -0
  172. data/tests/{minitest_common.rb → deprecated/minitest_common.rb} +0 -0
  173. data/tests/{minitest_common_api_test.rb → deprecated/minitest_common_api_test.rb} +0 -0
  174. data/tests/{rabbit_test.rb → deprecated/rabbit_test.rb} +0 -0
  175. data/tests/drivers/base/base_driver_test.rb +124 -0
  176. data/tests/{api → drivers/base}/library_test.rb +2 -8
  177. data/tests/drivers/ec2/common.rb +23 -15
  178. data/tests/drivers/ec2/images_test.rb +30 -211
  179. data/tests/drivers/ec2/instance_test.rb +104 -0
  180. data/tests/drivers/ec2/keys_test.rb +32 -161
  181. data/tests/drivers/ec2/realms_test.rb +26 -128
  182. data/tests/drivers/ec2/storage_snapshots_test.rb +54 -0
  183. data/tests/drivers/mock/images_test.rb +37 -179
  184. data/tests/drivers/mock/instances_test.rb +103 -340
  185. data/tests/drivers/mock/keys_test.rb +30 -145
  186. data/tests/drivers/mock/realms_test.rb +23 -118
  187. data/tests/drivers/mock/storage_snapshots_test.rb +23 -100
  188. data/tests/drivers/mock/storage_volumes_test.rb +41 -104
  189. data/tests/drivers/models/address_test.rb +18 -0
  190. data/tests/drivers/models/base_test.rb +33 -0
  191. data/tests/drivers/models/blob_test.rb +18 -0
  192. data/tests/drivers/models/hardware_profile_test.rb +52 -0
  193. data/tests/drivers/models/instance_address_test.rb +24 -0
  194. data/tests/drivers/models/instance_profile_test.rb +21 -0
  195. data/tests/drivers/models/instance_test.rb +29 -0
  196. data/tests/drivers/models/keys_test.rb +25 -0
  197. data/tests/drivers/models/metrics_test.rb +20 -0
  198. data/tests/drivers/rhevm/common.rb +15 -15
  199. data/tests/drivers/rhevm/images_test.rb +61 -36
  200. data/tests/drivers/rhevm/instance_test.rb +104 -0
  201. data/tests/drivers/rhevm/provider_test.rb +48 -0
  202. data/tests/drivers/rhevm/realms_test.rb +40 -37
  203. data/tests/ec2/common.rb +24 -0
  204. data/tests/ec2/query_parser_test.rb +98 -0
  205. data/tests/ec2/server_test.rb +45 -0
  206. data/tests/helpers/core_ext/array_test.rb +24 -0
  207. data/tests/helpers/core_ext/hash_test.rb +42 -0
  208. data/tests/helpers/core_ext/integer_test.rb +19 -0
  209. data/tests/helpers/core_ext/string_test.rb +74 -0
  210. data/tests/test_helper.rb +66 -0
  211. data/views/api/show.html.haml +1 -2
  212. data/views/api/show.xml.haml +1 -2
  213. data/views/blobs/show.xml.haml +1 -2
  214. data/views/keys/new.html.haml +9 -2
  215. data/views/load_balancers/new.html.haml +35 -34
  216. data/views/metrics/index.html.haml +1 -1
  217. data/views/metrics/show.html.haml +2 -2
  218. data/views/metrics/show.xml.haml +6 -2
  219. data/views/storage_volumes/attach.html.haml +1 -1
  220. data/views/storage_volumes/show.html.haml +1 -1
  221. metadata +853 -544
  222. data/lib/sinatra/accept_media_types.rb +0 -154
@@ -13,10 +13,13 @@
13
13
  # License for the specific language governing permissions and limitations
14
14
  # under the License.
15
15
 
16
+ require 'open3'
17
+
16
18
  module Deltacloud::Collections
19
+
17
20
  class InstanceStates < Base
18
21
 
19
- check_capability :for => lambda { |m| !driver.send(m).nil? }
22
+ set :capability, lambda { |m| !driver.send(m).nil? }
20
23
 
21
24
  collection :instance_states do
22
25
  operation :index, :with_capability => :instance_state_machine do
@@ -42,11 +45,11 @@ module Deltacloud::Collections
42
45
  gv = erb(:"instance_states/show")
43
46
  png = ''
44
47
  cmd = 'dot -Kdot -Gpad="0.2,0.2" -Gsize="5.0,8.0" -Gdpi="180" -Tpng'
45
- Open3.popen3( cmd ) do |stdin, stdout, stderr|
48
+ ::Open3.popen3( cmd ) do |stdin, stdout, stderr|
46
49
  stdin.write( gv )
47
50
  stdin.close()
48
51
  png = stdout.read
49
- end
52
+ end rescue Errno::EPIPE
50
53
  content_type 'image/png'
51
54
  png
52
55
  end
@@ -18,7 +18,7 @@ module Deltacloud::Collections
18
18
 
19
19
  include Deltacloud::Features
20
20
 
21
- check_capability :for => lambda { |m| driver.respond_to? m }
21
+ set :capability, lambda { |m| driver.respond_to? m }
22
22
  check_features :for => lambda { |c, f| driver.class.has_feature?(c, f) }
23
23
 
24
24
  new_route_for(:instances) do
@@ -47,6 +47,7 @@ module Deltacloud::Collections
47
47
  param :image_id, :string, :required
48
48
  param :realm_id, :string, :optional
49
49
  param :hwp_id, :string, :optional
50
+ param :keyname, :string, :optional
50
51
  control do
51
52
  @instance = driver.create_instance(credentials, params[:image_id], params)
52
53
  if @instance.kind_of? Array
@@ -15,7 +15,10 @@
15
15
 
16
16
  module Deltacloud::Collections
17
17
  class Keys < Base
18
- check_capability :for => lambda { |m| driver.respond_to? m }
18
+
19
+ include Deltacloud::Features
20
+
21
+ set :capability, lambda { |m| driver.respond_to? m }
19
22
  check_features :for => lambda { |c, f| driver.class.has_feature?(c, f) }
20
23
 
21
24
  get route_for('/keys/new') do
@@ -31,8 +34,9 @@ module Deltacloud::Collections
31
34
 
32
35
  operation :create, :with_capability => :create_key do
33
36
  param :name, :string, :required
37
+ param :public_key, :string, :optional
34
38
  control do
35
- @key = driver.create_key(credentials, { :key_name => params[:name] })
39
+ @key = driver.create_key(credentials, { :key_name => params[:name], :public_key => params[:public_key]})
36
40
  status 201
37
41
  response['Location'] = key_url(@key.id)
38
42
  respond_to do |format|
@@ -15,7 +15,8 @@
15
15
 
16
16
  module Deltacloud::Collections
17
17
  class LoadBalancers < Base
18
- check_capability :for => lambda { |m| driver.has_capability? m }
18
+
19
+ set :capability, lambda { |m| driver.respond_to? m }
19
20
 
20
21
  new_route_for :load_balancers do
21
22
  @realms = driver.realms(credentials)
@@ -40,7 +41,7 @@ module Deltacloud::Collections
40
41
  end
41
42
  end
42
43
 
43
- operation :create do
44
+ operation :create, :with_capability => :create_load_balancer do
44
45
  param :name, :string, :required
45
46
  param :realm_id, :string, :required
46
47
  param :listener_protocol, :string, :required, ['HTTP', 'TCP']
@@ -58,11 +59,11 @@ module Deltacloud::Collections
58
59
  end
59
60
  end
60
61
 
61
- action :register do
62
+ action :register, :with_capability => :lb_register_instance do
62
63
  param :instance_id, :string, :required
63
64
  control do
64
65
  driver.lb_register_instance(credentials, params)
65
- @load_balancer = driver.load_balancer(credentials, params[:id])
66
+ @load_balancer = driver.load_balancer(credentials, :id => params[:id])
66
67
  respond_to do |format|
67
68
  format.xml { haml :'load_balancers/show' }
68
69
  format.json { xml_to_json('load_balancers/show') }
@@ -71,11 +72,11 @@ module Deltacloud::Collections
71
72
  end
72
73
  end
73
74
 
74
- action :unregister do
75
+ action :unregister, :with_capability => :lb_unregister_instance do
75
76
  param :instance_id, :string, :required
76
77
  control do
77
78
  driver.lb_unregister_instance(credentials, params)
78
- @load_balancer = driver.load_balancer(credentials, params[:id])
79
+ @load_balancer = driver.load_balancer(credentials, :id => params[:id])
79
80
  respond_to do |format|
80
81
  format.xml { haml :'load_balancers/show' }
81
82
  format.json { xml_to_json('load_balancers/show')}
@@ -84,7 +85,7 @@ module Deltacloud::Collections
84
85
  end
85
86
  end
86
87
 
87
- operation :destroy do
88
+ operation :destroy, :with_capability => :destroy_load_balancer do
88
89
  control do
89
90
  driver.destroy_load_balancer(credentials, params[:id])
90
91
  status 204
@@ -15,7 +15,8 @@
15
15
 
16
16
  module Deltacloud::Collections
17
17
  class Metrics < Base
18
- check_capability :for => lambda { |m| driver.respond_to? m }
18
+
19
+ set :capability, lambda { |m| driver.respond_to? m }
19
20
 
20
21
  collection :metrics do
21
22
  description 'Metrics provide monitoring for the cloud resources'
@@ -15,7 +15,7 @@
15
15
 
16
16
  module Deltacloud::Collections
17
17
  class Realms < Base
18
- check_capability :for => lambda { |m| driver.respond_to? m }
18
+ set :capability, lambda { |m| driver.respond_to? m }
19
19
 
20
20
  collection :realms do
21
21
  description "Within a cloud provider a realm represents a boundary containing resources"
@@ -15,7 +15,8 @@
15
15
 
16
16
  module Deltacloud::Collections
17
17
  class StorageSnapshots < Base
18
- check_capability :for => lambda { |m| driver.respond_to? m }
18
+
19
+ set :capability, lambda { |m| driver.respond_to? m }
19
20
  check_features :for => lambda { |c, f| driver.class.has_feature?(c, f) }
20
21
 
21
22
  new_route_for(:storage_snapshots)
@@ -16,7 +16,7 @@
16
16
  module Deltacloud::Collections
17
17
  class StorageVolumes < Base
18
18
 
19
- check_capability :for => lambda { |m| driver.respond_to? m }
19
+ set :capability, lambda { |m| driver.respond_to? m }
20
20
  check_features :for => lambda { |c, f| driver.class.has_feature?(c, f) }
21
21
 
22
22
  new_route_for(:storage_volumes)
@@ -37,7 +37,7 @@ module Deltacloud::Collections
37
37
  control { show(:storage_volume) }
38
38
  end
39
39
 
40
- operation :create do
40
+ operation :create, :with_capability => :create_storage_volume do
41
41
  param :snapshot_id, :string, :optional
42
42
  param :capacity, :string, :optional
43
43
  param :realm_id, :string, :optional
@@ -14,13 +14,7 @@
14
14
  # under the License.
15
15
  #
16
16
 
17
- unless Kernel.respond_to?(:require_relative)
18
- module Kernel
19
- def require_relative(path)
20
- require File.join(File.dirname(caller[0]), path.to_str)
21
- end
22
- end
23
- end
17
+ require 'require_relative' if RUBY_VERSION < '1.9'
24
18
 
25
19
  require_relative './core_ext/array'
26
20
  require_relative './core_ext/hash'
@@ -16,15 +16,17 @@
16
16
  class Hash
17
17
  def gsub_keys(rgx_pattern, replacement)
18
18
  remove = []
19
- self.each_key do |key|
19
+ new_hash = {}
20
+ each_key do |key|
20
21
  if key.to_s.match(rgx_pattern)
21
22
  new_key = key.to_s.gsub(rgx_pattern, replacement).downcase
22
- self[new_key] = self[key]
23
- remove << key
23
+ new_hash[new_key] = self[key]
24
+ else
25
+ new_hash[key] = self[key]
24
26
  end
25
27
  end
26
- #remove the original keys
27
- self.delete_if{|k,v| remove.include?(k)}
28
+ clear
29
+ merge!(new_hash)
28
30
  end
29
31
 
30
32
  # Method copied from https://github.com/rails/rails/blob/77efc20a54708ba37ba679ffe90021bf8a8d3a8a/activesupport/lib/active_support/core_ext/hash/keys.rb#L23
@@ -40,6 +40,7 @@ class String
40
40
  end
41
41
 
42
42
  def singularize
43
+ return self.gsub(/ies$/, 'y') if self =~ /ies$/
43
44
  return self.gsub(/es$/, '') if self =~ /sses$/
44
45
  self.gsub(/s$/, '')
45
46
  end
@@ -0,0 +1,469 @@
1
+ #
2
+ # Licensed to the Apache Software Foundation (ASF) under one or more
3
+ # contributor license agreements. See the NOTICE file distributed with
4
+ # this work for additional information regarding copyright ownership. The
5
+ # ASF licenses this file to you under the Apache License, Version 2.0 (the
6
+ # "License"); you may not use this file except in compliance with the
7
+ # License. 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, WITHOUT
13
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14
+ # License for the specific language governing permissions and limitations
15
+ # under the License.
16
+ #
17
+
18
+ require "savon"
19
+
20
+ Savon.configure do |config|
21
+ config.log = false
22
+ end
23
+ HTTPI.log = false
24
+
25
+
26
+ module Deltacloud
27
+ module Drivers
28
+ module Aruba
29
+
30
+ class ArubaDriver < Deltacloud::BaseDriver
31
+
32
+ DEFAULT_DC = 'dc1'
33
+ feature :instances, :authentication_password
34
+ feature :instances, :user_name
35
+
36
+ define_instance_states do
37
+ start.to( :pending ) .automatically
38
+ pending.to( :running ) .automatically
39
+ running.to( :running ) .on( :reboot)
40
+ running.to( :stopping ) .on( :stop )
41
+ stopping.to( :stopped ) .automatically
42
+ stopped.to( :pending ) .on( :start )
43
+ stopped.to( :finish ) .on( :destroy )
44
+ end
45
+
46
+ define_hardware_profile 'VMWare' do
47
+ cpu ( 1..8 )
48
+ memory ( 1024 .. 32*1024 )
49
+ storage ( 10 .. 500)
50
+ architecture ['x86_64', 'i386']
51
+ end
52
+
53
+ define_hardware_profile 'HyperV' do
54
+ cpu ( 1..4 )
55
+ memory ( 1024 .. 16*1024 )
56
+ storage ( 10 .. 500 )
57
+ architecture ['x86_64', 'i386']
58
+ end
59
+
60
+ define_hardware_profile 'HyperVLowCost' do
61
+ cpu ( 1..4 )
62
+ memory ( 1024 .. 16*1024 )
63
+ storage ( 10 .. 500)
64
+ architecture ['x86_64', 'i386']
65
+ end
66
+
67
+ def valid_credentials?(credentials)
68
+ begin
69
+ new_client(credentials)
70
+ rescue
71
+ return false
72
+ end
73
+ true
74
+ end
75
+
76
+ def realms(credentials, opts=nil)
77
+ client = new_client(credentials)
78
+ safely do
79
+ datacenters = get_all(client,
80
+ :datacenter_configurations,
81
+ :datacenter_config).inject([]) do |res, dc|
82
+ res << Realm.new(
83
+ :id => dc[:datacenter_id],
84
+ :name => dc[:name],
85
+ :limit => :unlimited,
86
+ :state => dc[:status]
87
+ )
88
+ res
89
+ end
90
+ datacenters
91
+ end
92
+ end
93
+
94
+ def images(credentials, opts = {})
95
+ client = new_client(credentials)
96
+ safely do
97
+ hvs = get_all client, :hypervisors
98
+ images = []
99
+ hvs.each do |hv|
100
+ hv[:templates][:template_details].each do |tpl|
101
+ if opts[:id] and opts[:id] != tpl[:id]
102
+ next
103
+ end
104
+ arch = tpl[:description] !~ /64bit/ ? "i386" : "x86_64"
105
+ images << Image.new(
106
+ :id => tpl[:id],
107
+ :name => tpl[:name],
108
+ :description => tpl[:description],
109
+ :architecture => arch,
110
+ :owner_id => 0,
111
+ :state => "AVAILABLE",
112
+ :hardware_profiles => [InstanceProfile.new(hv[:hypervisor_type])]
113
+ )
114
+ end
115
+ end
116
+ images = filter_on( images, :architecture, opts )
117
+ images = filter_on( images, :owner_id, opts )
118
+ images = filter_on( images, :id, opts )
119
+ images.sort_by{|e| [e.owner_id, e.architecture, e.name, e.description]}
120
+ end
121
+ end
122
+
123
+ def instances(credentials, opts={})
124
+ client = new_client(credentials)
125
+
126
+ safely do
127
+ if opts[:id]
128
+ instances = [convert_instance(client, opts[:id])]
129
+ else
130
+ instances = get_all(client, :servers).collect do |srv|
131
+ convert_instance(client, srv[:server_id])
132
+ end
133
+ end
134
+ filter_on(instances, :state, opts)
135
+ end
136
+ end
137
+
138
+ def create_instance(credentials, image_id, opts={})
139
+ client = new_client(credentials, opts[:realm_id])
140
+ safely do
141
+ ram = opts[:hwp_memory].to_i / 1024
142
+ if not opts[:password] or opts[:password] == ''
143
+ raise "Missing Parameter 'password'"
144
+ end
145
+ params = {
146
+ :server => {
147
+ "arub:AdministratorPassword" => opts[:password].to_s,
148
+ "arub:CPUQuantity" => opts[:hwp_cpu].to_s,
149
+ "arub:Name" => opts[:name],
150
+ "arub:OSTemplateId" => opts[:image_id].to_s,
151
+ "arub:RAMQuantity" => ram.to_s,
152
+ "arub:VirtualDisks" => {
153
+ "arub:VirtualDiskDetails" => {
154
+ "arub:Size" => opts[:hwp_storage]
155
+ }
156
+ }
157
+ }
158
+ }
159
+ # since Aruba API does not return the new server id when calling
160
+ # set_enqueue_server_creation, get the server_id from the job list
161
+ prev_jobs = get_jobs(client, "AddVirtualMachine")
162
+ request(client, :enqueue_server_creation, params, "set")
163
+ jobs = get_jobs(client, "AddVirtualMachine") - prev_jobs
164
+ convert_instance(client, jobs.sort_by {|j| j[:job_id].to_i}.reverse.first[:server_id])
165
+ end
166
+ end
167
+
168
+ def start_instance(credentials, id)
169
+ client = new_client(credentials)
170
+ safely do
171
+ request(client, :enqueue_server_start, {:serverId => id}, "set")
172
+ convert_instance client, id
173
+ end
174
+ end
175
+
176
+ def stop_instance(credentials, id)
177
+ client = new_client(credentials)
178
+ safely do
179
+ request(client, :enqueue_server_stop, {:serverId => id}, "set")
180
+ convert_instance client, id
181
+ end
182
+ end
183
+
184
+ def reboot_instance(credentials, id)
185
+ client = new_client(credentials)
186
+ safely do
187
+ request(client, :enqueue_server_restart, {:serverId => id}, "set")
188
+ convert_instance client, id
189
+ end
190
+ end
191
+
192
+ def destroy_instance(credentials, id)
193
+ client = new_client(credentials)
194
+ safely do
195
+ request(client, :enqueue_server_deletion, {:serverId => id}, "set")
196
+ convert_instance client, id
197
+ end
198
+ end
199
+
200
+ def supported_collections
201
+ DEFAULT_COLLECTIONS + [ :addresses ]
202
+ end
203
+
204
+ def configured_providers
205
+ Deltacloud::Drivers::driver_config[:aruba][:entrypoints]["compute"].keys
206
+ end
207
+
208
+ def storage_volumes(credentials, opts={})
209
+ client = new_client(credentials)
210
+ safely do
211
+ servers = get_all client, :servers
212
+ volumes = servers.inject([]) do |result, srv|
213
+ instance = convert_instance(client, srv[:server_id])
214
+ details = request client, :server_details, {:serverId => instance.id}
215
+ vdisks = details[:virtual_disks][:virtual_disk]
216
+ vdisks = vdisks.kind_of?(Array) ? vdisks : [vdisks]
217
+ vdisks.each do |vd|
218
+ if instance.state == "STOPPED"
219
+ actions = vd[:resource_type] == "HardDrive0" ? [:snapshot] : [:snapshot, :detach]
220
+ else
221
+ actions = []
222
+ end
223
+ result << StorageVolume.new(
224
+ :id => vd[:resource_id],
225
+ :name => "#{instance.name}-#{vd[:resource_type]}",
226
+ :capacity => vd[:size],
227
+ :instance_id => instance.id,
228
+ :realm_id => instance.realm_id,
229
+ :state => "IN-USE",
230
+ :created => vd[:creation_date],
231
+ :kind => 'data',
232
+ :actions => actions,
233
+ :device => nil
234
+ ) if opts[:id].nil? or opts[:id] == vd[:resource_id]
235
+ end
236
+ result
237
+ end
238
+ volumes
239
+ end
240
+ end
241
+
242
+ def addresses(credentials, opts={})
243
+ client = new_client(credentials)
244
+ safely do
245
+ get_all(client, :purchased_ip_addresses, :ip_address).collect do |ip|
246
+ Address.new(:id => ip[:value], :instance_id => ip[:server_id])
247
+ end
248
+ end
249
+ end
250
+
251
+ def address(credentials, opts={})
252
+ addrs = addresses(credentials, opts)
253
+ filter_on(addrs, :id, opts).first
254
+ end
255
+
256
+ def create_address(credentials, opts={})
257
+ client = new_client(credentials)
258
+ safely do
259
+ ip = request(client, :purchase_ip_address, {}, "set")
260
+ address = Address.new(:id => ip[:value], :instance_id => nil)
261
+ end
262
+ end
263
+
264
+ def destroy_address(credentials, opts={})
265
+ client = new_client(credentials)
266
+ safely do
267
+ get_all(client, :purchased_ip_addresses, :ip_address).collect do |ip|
268
+ if ip[:value] == opts[:id]
269
+ request client, :remove_ip_address, {:ipAddressResourceId => ip[:resource_id]}, "set"
270
+ return
271
+ end
272
+ end
273
+ raise "Could not find ip #{opts[:id]}"
274
+ end
275
+ end
276
+
277
+ def associate_address(credentials, opts={})
278
+ client = new_client(credentials)
279
+ safely do
280
+
281
+ get_all(client, :purchased_ip_addresses, :ip_address).collect do |ip|
282
+ if ip[:value] == opts[:id]
283
+ details = request client, :server_details, {:serverId => opts[:instance_id]}
284
+ params = {
285
+ :ipAddressResourceIds => {"arr:int" => ip[:resource_id]},
286
+ :networkAdapterId => details[:network_adapters][:network_adapter].first[:id]
287
+ }
288
+ request client, :enqueue_associate_ip_address, params, "set"
289
+ return
290
+ end
291
+ end
292
+ raise "Could not find ip #{opts[:id]}"
293
+ end
294
+ end
295
+
296
+ def disassociate_address(credentials, opts={})
297
+ client = new_client(credentials)
298
+ safely do
299
+ get_all(client, :purchased_ip_addresses, :ip_address).collect do |ip|
300
+ if ip[:value] == opts[:id]
301
+ details = request client, :server_details, {:serverId => ip[:server_id]}
302
+ params = {
303
+ :ipAddressResourceIds => {"arr:int" => ip[:resource_id]},
304
+ :networkAdapterId => details[:network_adapters][:network_adapter].first[:id]
305
+ }
306
+ request client, :enqueue_deassociate_ip_address, params, "set"
307
+ return
308
+ end
309
+ end
310
+ raise "Could not find ip #{opts[:id]}"
311
+ end
312
+ end
313
+
314
+ private
315
+
316
+ def new_client(credentials, realm_id=nil)
317
+ safely do
318
+ wsdl = realm_id ? Deltacloud::Drivers::driver_config[:aruba][:entrypoints]["compute"]["dc#{realm_id}"] : endpoint
319
+ client = Savon.client(wsdl)
320
+ client.wsse.credentials credentials.user, credentials.password
321
+ client.request :get_user_authentication_token
322
+ client
323
+ end
324
+ end
325
+
326
+ def get_all(client, entity, field = nil)
327
+ method = "get_#{entity}".to_sym
328
+ field = field ? field : entity.to_s.chomp("s").to_sym
329
+ response = client.request method
330
+ result = response.body["#{method}_response".to_sym]["#{method}_result".to_sym][:value][field]
331
+ result = result.nil? ? [] : result.kind_of?(Array) ? result : [result]
332
+ result
333
+ end
334
+
335
+ def request(client, entity, params, verb="get")
336
+ meth = verb ? "#{verb}_#{entity}".to_sym : entity.to_sym
337
+ opts = {}
338
+ params.map{ |k, v| opts["tns:#{k}"] = v }
339
+ response = client.request meth do
340
+ soap.namespaces["xmlns:arub"] = "http://schemas.datacontract.org/2004/07/Aruba.Cloud.Provisioning.Entities"
341
+ soap.namespaces["xmlns:arr"] = "http://schemas.microsoft.com/2003/10/Serialization/Arrays"
342
+ soap.body = opts
343
+ end
344
+ data = response.body["#{meth}_response".to_sym]["#{meth}_result".to_sym]
345
+ if not data[:success]
346
+ raise data[:result_message]
347
+ end
348
+
349
+ data [:value]
350
+ end
351
+
352
+ def get_jobs(client, type)
353
+ get_all(client, :jobs).inject([]) do |res, j|
354
+ if j[:operation_name] == type
355
+ res << j
356
+ end
357
+ res
358
+ end
359
+ end
360
+
361
+ def convert_state(state)
362
+ case state
363
+ when "Stopped"
364
+ "STOPPED"
365
+ when "Running"
366
+ "RUNNING"
367
+ when "Creating"
368
+ "PENDING"
369
+ end
370
+ end
371
+
372
+ def convert_instance(client, id)
373
+ safely do
374
+ details = request client, :server_details, {:serverId => id}
375
+ ip_addresses = details[:network_adapters][:network_adapter].inject([]) do |result, adp|
376
+ if adp[:ip_addresses]
377
+ if adp[:ip_addresses][:ip_address].kind_of? Array
378
+ result += adp[:ip_addresses][:ip_address].map{ |x|
379
+ InstanceAddress.new(x[:value], :type => :ipv4)
380
+ }
381
+ else
382
+ result << InstanceAddress.new(
383
+ adp[:ip_addresses][:ip_address][:value],
384
+ :type => :ipv4
385
+ )
386
+ end
387
+ end
388
+ result
389
+ end
390
+
391
+ storage = details[:virtual_disks][:virtual_disk].kind_of?(Array) ?
392
+ details[:virtual_disks][:virtual_disk].first[:size] :
393
+ details[:virtual_disks][:virtual_disk][:size]
394
+ profile = InstanceProfile.new(details[:hypervisor_type],
395
+ :hwp_memory => details[:ram_quantity][:quantity].to_i * 1024,
396
+ :hwp_cpu => details[:cpu_quantity][:quantity],
397
+ :hwp_storage => storage)
398
+
399
+ state = convert_state(details[:server_status])
400
+ key_pairs = details[:parameters][:key_value_pair]
401
+ key_pairs = key_pairs.kind_of?(Array) ? key_pairs : [key_pairs]
402
+ private_addresses = key_pairs.inject([]) do |result, kp|
403
+ result << InstanceAddress.new(kp[:value], :type=> :ipv4) if kp[:key] == "HostIp"
404
+ result
405
+ end
406
+
407
+ vdisks = details[:virtual_disks][:virtual_disk]
408
+ vdisks = vdisks.kind_of?(Array) ? vdisks : [vdisks]
409
+
410
+ actions = instance_actions_for(state)
411
+
412
+ Instance.new(
413
+ :id => id,
414
+ :name => details[:name],
415
+ :state => state,
416
+ :image_id => details[:os_template][:id],
417
+ :owner_id => details[:user_id],
418
+ :actions => actions,
419
+ :launch_time => details[:creation_date],
420
+ :realm_id => details[:datacenter_id],
421
+ :create_image => true,
422
+ :public_addresses => ip_addresses,
423
+ :private_addresses => private_addresses,
424
+ :instance_profile => profile,
425
+ :storage_volumes => vdisks.collect {|vd| {vd[:resource_id] => nil} }
426
+ )
427
+ end
428
+ end
429
+
430
+ def endpoint
431
+ endpoint = (api_provider || DEFAULT_DC)
432
+ Deltacloud::Drivers::driver_config[:aruba][:entrypoints]["compute"][endpoint] || endpoint
433
+ end
434
+
435
+ exceptions do
436
+ on /InvalidSecurity/ do
437
+ status 401
438
+ end
439
+
440
+ on /Could not find/ do
441
+ status 404
442
+ end
443
+
444
+ on /SocketError|ProviderError/ do
445
+ status 502
446
+ end
447
+
448
+ on /ActionNotSupported|DeserializationFailed|Unknown|Missing Parameter/ do
449
+ status 400
450
+ end
451
+
452
+ on /Operation already enqueued/ do
453
+ status 412
454
+ end
455
+
456
+ on /Too many Public IP/ do
457
+ status 502
458
+ end
459
+
460
+ on /IP address.*is associated/ do
461
+ status 400
462
+ end
463
+
464
+ end
465
+ end
466
+
467
+ end #module Aruba
468
+ end #module Drivers
469
+ end #module Deltacloud