deltacloud-core 1.1.1 → 1.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (220) hide show
  1. data/Rakefile +40 -28
  2. data/bin/deltacloud-db-upgrade +24 -0
  3. data/bin/deltacloudd +20 -9
  4. data/config.ru +12 -53
  5. data/db/migrations/1_add_realm_to_machine_template.rb +23 -0
  6. data/deltacloud-core.gemspec +11 -5
  7. data/lib/cimi/collections.rb +4 -31
  8. data/lib/cimi/collections/address_templates.rb +2 -5
  9. data/lib/cimi/collections/addresses.rb +2 -5
  10. data/lib/cimi/collections/base.rb +18 -6
  11. data/lib/cimi/collections/credentials.rb +2 -5
  12. data/lib/cimi/collections/machine_images.rb +2 -1
  13. data/lib/cimi/collections/machine_templates.rb +2 -5
  14. data/lib/cimi/collections/machines.rb +6 -9
  15. data/lib/cimi/collections/network_ports.rb +3 -3
  16. data/lib/cimi/collections/networks.rb +5 -8
  17. data/lib/cimi/collections/system_templates.rb +72 -0
  18. data/lib/cimi/collections/systems.rb +194 -0
  19. data/lib/cimi/collections/volume_configurations.rb +1 -1
  20. data/lib/cimi/collections/volume_images.rb +2 -1
  21. data/lib/cimi/collections/volume_templates.rb +3 -2
  22. data/lib/cimi/collections/volumes.rb +2 -2
  23. data/lib/cimi/dependencies.rb +0 -2
  24. data/lib/cimi/helpers/cimi_helper.rb +12 -57
  25. data/lib/cimi/helpers/database_helper.rb +0 -4
  26. data/lib/cimi/models.rb +25 -14
  27. data/lib/cimi/models/address.rb +4 -31
  28. data/lib/cimi/models/address_create.rb +51 -0
  29. data/lib/cimi/models/address_template.rb +8 -52
  30. data/lib/cimi/models/address_template_create.rb +44 -0
  31. data/lib/cimi/models/base.rb +44 -6
  32. data/lib/cimi/models/cloud_entry_point.rb +1 -1
  33. data/lib/cimi/models/credential.rb +1 -1
  34. data/lib/cimi/models/credential_create.rb +46 -0
  35. data/lib/cimi/models/credential_template.rb +24 -0
  36. data/lib/cimi/models/machine.rb +1 -71
  37. data/lib/cimi/models/machine_configuration.rb +3 -3
  38. data/lib/cimi/models/machine_create.rb +49 -0
  39. data/lib/cimi/models/machine_image.rb +2 -25
  40. data/lib/cimi/models/machine_image_create.rb +41 -0
  41. data/lib/cimi/models/machine_template.rb +14 -34
  42. data/lib/cimi/models/machine_template_create.rb +33 -0
  43. data/lib/cimi/models/network.rb +0 -38
  44. data/lib/cimi/models/network_create.rb +43 -0
  45. data/lib/cimi/models/network_port.rb +17 -17
  46. data/lib/cimi/models/network_template.rb +2 -3
  47. data/lib/cimi/models/resource.rb +22 -3
  48. data/lib/cimi/models/schema.rb +94 -8
  49. data/lib/cimi/models/system.rb +67 -0
  50. data/lib/cimi/models/system_template.rb +63 -0
  51. data/lib/cimi/models/volume.rb +2 -42
  52. data/lib/cimi/models/volume_configuration.rb +4 -4
  53. data/lib/cimi/models/volume_create.rb +58 -0
  54. data/lib/cimi/models/volume_image.rb +8 -17
  55. data/lib/cimi/models/volume_image_create.rb +47 -0
  56. data/lib/cimi/models/volume_template.rb +6 -19
  57. data/lib/cimi/models/volume_template_create.rb +33 -0
  58. data/lib/db.rb +14 -22
  59. data/lib/db/volume_template.rb +1 -1
  60. data/lib/deltacloud/api.rb +6 -5
  61. data/lib/deltacloud/collections.rb +4 -27
  62. data/lib/deltacloud/collections/base.rb +4 -0
  63. data/lib/deltacloud/collections/images.rb +1 -1
  64. data/lib/deltacloud/collections/instances.rb +2 -2
  65. data/lib/deltacloud/core_ext/array.rb +1 -0
  66. data/lib/deltacloud/core_ext/integer.rb +13 -9
  67. data/lib/deltacloud/core_ext/string.rb +45 -28
  68. data/lib/deltacloud/drivers/arubacloud/arubacloud_driver.rb +0 -9
  69. data/lib/deltacloud/drivers/base_driver.rb +45 -16
  70. data/lib/deltacloud/drivers/digitalocean/digitalocean_driver.rb +78 -8
  71. data/lib/deltacloud/drivers/ec2/ec2_driver.rb +13 -9
  72. data/lib/deltacloud/drivers/fgcp/fgcp_client.rb +44 -0
  73. data/lib/deltacloud/drivers/fgcp/fgcp_driver.rb +164 -90
  74. data/lib/deltacloud/drivers/fgcp/fgcp_driver_cimi_methods.rb +74 -0
  75. data/lib/deltacloud/drivers/mock/data/instances/inst0.yml +3 -3
  76. data/lib/deltacloud/drivers/mock/data/instances/inst1.yml +3 -3
  77. data/lib/deltacloud/drivers/mock/data/instances/inst2.yml +3 -3
  78. data/lib/deltacloud/drivers/mock/mock_client.rb +17 -1
  79. data/lib/deltacloud/drivers/mock/mock_driver.rb +161 -204
  80. data/lib/deltacloud/drivers/mock/mock_driver_cimi_methods.rb +80 -0
  81. data/lib/deltacloud/drivers/opennebula/opennebula_driver.rb +6 -1
  82. data/lib/deltacloud/drivers/openstack/openstack_driver.rb +61 -68
  83. data/lib/deltacloud/drivers/rackspace/rackspace_driver.rb +0 -9
  84. data/lib/deltacloud/drivers/rhevm/rhevm_driver.rb +11 -6
  85. data/lib/deltacloud/drivers/terremark/terremark_driver.rb +0 -8
  86. data/lib/deltacloud/drivers/vsphere/vsphere_client.rb +11 -4
  87. data/lib/deltacloud/drivers/vsphere/vsphere_driver.rb +0 -12
  88. data/lib/deltacloud/helpers/collection_helper.rb +106 -0
  89. data/lib/deltacloud/helpers/deltacloud_helper.rb +12 -8
  90. data/lib/deltacloud/models/address.rb +19 -17
  91. data/lib/deltacloud/models/base_model.rb +29 -26
  92. data/lib/deltacloud/models/blob.rb +22 -20
  93. data/lib/deltacloud/models/bucket.rb +21 -16
  94. data/lib/deltacloud/models/firewall.rb +18 -16
  95. data/lib/deltacloud/models/firewall_rule.rb +22 -20
  96. data/lib/deltacloud/models/image.rb +29 -28
  97. data/lib/deltacloud/models/instance.rb +92 -94
  98. data/lib/deltacloud/models/instance_address.rb +42 -40
  99. data/lib/deltacloud/models/instance_profile.rb +28 -26
  100. data/lib/deltacloud/models/key.rb +47 -45
  101. data/lib/deltacloud/models/load_balancer.rb +32 -31
  102. data/lib/deltacloud/models/metric.rb +76 -29
  103. data/lib/deltacloud/models/provider.rb +15 -13
  104. data/lib/deltacloud/models/realm.rb +15 -21
  105. data/lib/deltacloud/models/storage_snapshot.rb +23 -19
  106. data/lib/deltacloud/models/storage_volume.rb +35 -34
  107. data/lib/deltacloud/version.rb +1 -1
  108. data/lib/deltacloud_rack.rb +22 -0
  109. data/lib/initialize.rb +28 -0
  110. data/lib/initializers/database_initialize.rb +76 -0
  111. data/lib/initializers/frontend_initialize.rb +42 -0
  112. data/lib/initializers/mock_initialize.rb +33 -0
  113. data/lib/sinatra/rack_logger.rb +35 -24
  114. data/tests/cimi/collections/cloud_entry_point_test.rb +1 -7
  115. data/tests/cimi/collections/machine_images_test.rb +2 -2
  116. data/tests/cimi/collections/machine_templates_test.rb +75 -0
  117. data/tests/cimi/collections/machines_test.rb +2 -2
  118. data/tests/cimi/collections/system_templates_test.rb +41 -0
  119. data/tests/cimi/collections/systems_test.rb +50 -0
  120. data/tests/cimi/db/database_helper_test.rb +17 -25
  121. data/tests/cimi/db/db_helper.rb +1 -12
  122. data/tests/cimi/db/entity_test.rb +7 -8
  123. data/tests/cimi/model/machine_create_spec.rb +44 -0
  124. data/tests/cimi/model/machine_template_spec.rb +29 -0
  125. data/tests/cimi/model/resource_spec.rb +40 -0
  126. data/tests/cimi/model/schema_spec.rb +37 -0
  127. data/tests/cimi/spec_helper.rb +3 -0
  128. data/tests/deltacloud/collections/buckets_collection_test.rb +1 -1
  129. data/tests/deltacloud/collections/drivers_collection_test.rb +2 -2
  130. data/tests/deltacloud/collections/hardware_profiles_collection_test.rb +2 -2
  131. data/tests/deltacloud/collections/images_collection_test.rb +2 -2
  132. data/tests/deltacloud/collections/instance_states_collection_test.rb +1 -1
  133. data/tests/deltacloud/collections/instances_collection_test.rb +6 -3
  134. data/tests/deltacloud/collections/keys_collection_test.rb +2 -2
  135. data/tests/deltacloud/collections/realms_collection_test.rb +2 -2
  136. data/tests/deltacloud/collections/storage_snapshots_collection_test.rb +2 -2
  137. data/tests/deltacloud/collections/storage_volumes_collection_test.rb +2 -2
  138. data/tests/deltacloud/collections_test.rb +5 -5
  139. data/tests/deltacloud/common.rb +2 -13
  140. data/tests/deltacloud/launcher_test.rb +3 -3
  141. data/tests/deltacloud/rack_test.rb +2 -2
  142. data/tests/deltacloud/server_test.rb +1 -1
  143. data/tests/drivers/base/base_driver_test.rb +5 -5
  144. data/tests/drivers/base/common.rb +2 -12
  145. data/tests/drivers/ec2/buckets_test.rb +1 -1
  146. data/tests/drivers/ec2/images_test.rb +2 -2
  147. data/tests/drivers/ec2/instance_test.rb +6 -6
  148. data/tests/drivers/ec2/keys_test.rb +3 -3
  149. data/tests/drivers/ec2/realms_test.rb +2 -2
  150. data/tests/drivers/ec2/storage_snapshots_test.rb +1 -1
  151. data/tests/drivers/fgcp/common.rb +32 -0
  152. data/tests/drivers/fgcp/firewall_test.rb +70 -0
  153. data/tests/drivers/fgcp/hardware_profiles_test.rb +61 -0
  154. data/tests/drivers/fgcp/images_test.rb +46 -0
  155. data/tests/drivers/fgcp/provider_test.rb +27 -0
  156. data/tests/drivers/fgcp/realms_test.rb +53 -0
  157. data/tests/drivers/fgcp/storage_volumes_test.rb +58 -0
  158. data/tests/drivers/gogrid/images_test.rb +2 -2
  159. data/tests/drivers/gogrid/instances_test.rb +2 -2
  160. data/tests/drivers/gogrid/realms_test.rb +2 -2
  161. data/tests/drivers/mock/images_test.rb +4 -4
  162. data/tests/drivers/mock/instances_test.rb +9 -9
  163. data/tests/drivers/mock/keys_test.rb +3 -3
  164. data/tests/drivers/mock/realms_test.rb +2 -2
  165. data/tests/drivers/mock/storage_snapshots_test.rb +2 -2
  166. data/tests/drivers/mock/storage_volumes_test.rb +4 -4
  167. data/tests/drivers/models/instance_test.rb +2 -2
  168. data/tests/drivers/openstack/images_test.rb +2 -2
  169. data/tests/drivers/openstack/instances_test.rb +2 -3
  170. data/tests/drivers/openstack/keys_test.rb +1 -1
  171. data/tests/drivers/openstack/realms_test.rb +2 -11
  172. data/tests/drivers/rhevm/common.rb +33 -12
  173. data/tests/drivers/rhevm/images_test.rb +20 -12
  174. data/tests/drivers/rhevm/instance_test.rb +62 -46
  175. data/tests/drivers/rhevm/provider_test.rb +12 -6
  176. data/tests/drivers/rhevm/realms_test.rb +15 -9
  177. data/tests/ec2/query_parser_test.rb +1 -1
  178. data/tests/test_helper.rb +68 -12
  179. data/views/addresses/show.html.haml +1 -1
  180. data/views/buckets/show.html.haml +1 -1
  181. data/views/errors/{500.html.haml → common.html.haml} +0 -0
  182. data/views/errors/common.xml.haml +17 -0
  183. data/views/firewalls/index.xml.haml +2 -2
  184. data/views/firewalls/new.html.haml +2 -2
  185. data/views/images/show.html.haml +1 -1
  186. data/views/instances/run_command.html.haml +1 -1
  187. data/views/instances/show.html.haml +3 -3
  188. data/views/keys/index.html.haml +1 -0
  189. data/views/keys/show.html.haml +8 -4
  190. data/views/keys/show.xml.haml +3 -2
  191. data/views/load_balancers/new.html.haml +1 -1
  192. data/views/load_balancers/show.html.haml +2 -2
  193. data/views/metrics/show.html.haml +1 -1
  194. data/views/realms/index.html.haml +0 -2
  195. data/views/realms/show.html.haml +0 -4
  196. data/views/realms/show.xml.haml +0 -3
  197. data/views/storage_snapshots/index.html.haml +1 -1
  198. data/views/storage_snapshots/new.html.haml +1 -1
  199. data/views/storage_volumes/new.html.haml +2 -2
  200. data/views/storage_volumes/show.html.haml +3 -3
  201. metadata +90 -41
  202. data/views/errors/400.html.haml +0 -41
  203. data/views/errors/400.xml.haml +0 -3
  204. data/views/errors/401.html.haml +0 -26
  205. data/views/errors/401.xml.haml +0 -3
  206. data/views/errors/403.html.haml +0 -42
  207. data/views/errors/403.xml.haml +0 -9
  208. data/views/errors/404.html.haml +0 -28
  209. data/views/errors/404.xml.haml +0 -3
  210. data/views/errors/405.html.haml +0 -29
  211. data/views/errors/405.xml.haml +0 -5
  212. data/views/errors/409.html.haml +0 -47
  213. data/views/errors/409.xml.haml +0 -11
  214. data/views/errors/500.xml.haml +0 -13
  215. data/views/errors/501.html.haml +0 -44
  216. data/views/errors/501.xml.haml +0 -1
  217. data/views/errors/502.html.haml +0 -44
  218. data/views/errors/502.xml.haml +0 -1
  219. data/views/errors/504.html.haml +0 -43
  220. data/views/errors/504.xml.haml +0 -1
@@ -1,5 +1,5 @@
1
1
  ---
2
- :instance_profile: !ruby/object:InstanceProfile
2
+ :instance_profile: !ruby/object:Deltacloud::InstanceProfile
3
3
  memory: "12288"
4
4
  id: m1-large
5
5
  :realm_id: us
@@ -8,11 +8,11 @@
8
8
  - :stop
9
9
  :owner_id: mockuser
10
10
  :public_addresses:
11
- - !ruby/object:InstanceAddress
11
+ - !ruby/object:Deltacloud::InstanceAddress
12
12
  address: img1.inst0.public.com
13
13
  address_type: :hostname
14
14
  :private_addresses:
15
- - !ruby/object:InstanceAddress
15
+ - !ruby/object:Deltacloud::InstanceAddress
16
16
  address: img1.inst0.private.com
17
17
  address_type: :hostname
18
18
  :create_image: true
@@ -1,14 +1,14 @@
1
1
  ---
2
- :instance_profile: !ruby/object:InstanceProfile
2
+ :instance_profile: !ruby/object:Deltacloud::InstanceProfile
3
3
  id: m1-small
4
4
  :realm_id: us
5
5
  :owner_id: mockuser
6
6
  :public_addresses:
7
- - !ruby/object:InstanceAddress
7
+ - !ruby/object:Deltacloud::InstanceAddress
8
8
  address: img1.inst1.public.com
9
9
  address_type: :hostname
10
10
  :private_addresses:
11
- - !ruby/object:InstanceAddress
11
+ - !ruby/object:Deltacloud::InstanceAddress
12
12
  address: img1.inst1.private.com
13
13
  address_type: :hostname
14
14
  :create_image: true
@@ -1,14 +1,14 @@
1
1
  ---
2
- :instance_profile: !ruby/object:InstanceProfile
2
+ :instance_profile: !ruby/object:Deltacloud::InstanceProfile
3
3
  id: m1-large
4
4
  :realm_id: us
5
5
  :owner_id: anotheruser
6
6
  :public_addresses:
7
- - !ruby/object:InstanceAddress
7
+ - !ruby/object:Deltacloud::InstanceAddress
8
8
  address: img1.inst2.public.com
9
9
  address_type: :hostname
10
10
  :private_addresses:
11
- - !ruby/object:InstanceAddress
11
+ - !ruby/object:Deltacloud::InstanceAddress
12
12
  address: img1.inst2.private.com
13
13
  address_type: :hostname
14
14
  :create_image: true
@@ -19,9 +19,13 @@
19
19
 
20
20
  module Deltacloud::Drivers::Mock
21
21
 
22
+ require 'yaml'
22
23
  require 'fileutils'
23
24
 
24
25
  class Client
26
+
27
+ include Deltacloud
28
+
25
29
  def initialize(storage_root)
26
30
  @storage_root = storage_root
27
31
  @collections = []
@@ -66,6 +70,7 @@ module Deltacloud::Drivers::Mock
66
70
  def store(collection, obj)
67
71
  raise "Why no obj[:id] ?" unless obj[:id]
68
72
  File::open(file(collection, obj[:id]), "w") { |f| YAML.dump(obj, f) }
73
+ obj
69
74
  end
70
75
 
71
76
  # Return the object with id +id+ of class +klass+ from the collection
@@ -91,6 +96,17 @@ module Deltacloud::Drivers::Mock
91
96
  FileUtils.rm(fname) if File::exists?(fname)
92
97
  end
93
98
 
99
+ def store_cimi(collection, obj)
100
+ raise "Why no obj.name?" unless obj.name
101
+ File::open(cimi_file(collection, obj.name), "w") { |f| f.write(obj.to_json) }
102
+ end
103
+
104
+ def destroy_cimi(collection, id)
105
+ fname = cimi_file(collection, id)
106
+ raise "No such object: #{id} in #{collection} collection" unless File::exists?(fname)
107
+ FileUtils.rm(fname)
108
+ end
109
+
94
110
  def load_all_cimi(model_name)
95
111
  model_files = Dir[File::join(cimi_dir(model_name), "*.json")]
96
112
  model_files.map{|f| File.read(f)}
@@ -111,7 +127,7 @@ module Deltacloud::Drivers::Mock
111
127
  private
112
128
 
113
129
  def collection_name(klass)
114
- klass.name.underscore.pluralize
130
+ klass.name.split('::').last.underscore.pluralize
115
131
  end
116
132
  end
117
133
 
@@ -14,32 +14,17 @@
14
14
  # License for the specific language governing permissions and limitations
15
15
  # under the License.
16
16
 
17
- require 'yaml'
18
- require 'base64'
19
- require 'etc'
20
17
  require 'ipaddr'
21
18
 
22
19
  require_relative 'mock_client'
23
20
  require_relative 'mock_driver_cimi_methods'
21
+
24
22
  require_relative '../../runner'
25
23
 
26
24
  module Deltacloud::Drivers::Mock
27
25
 
28
26
  class MockDriver < Deltacloud::BaseDriver
29
27
 
30
- ( REALMS = [
31
- Realm.new({
32
- :id=>'us',
33
- :name=>'United States',
34
- :limit=>:unlimited,
35
- :state=>'AVAILABLE'}),
36
- Realm.new({
37
- :id=>'eu',
38
- :name=>'Europe',
39
- :limit=>:unlimited,
40
- :state=>'AVAILABLE'}),
41
- ] ) unless defined?( REALMS )
42
-
43
28
  define_hardware_profile('m1-small') do
44
29
  cpu 1
45
30
  memory 1.7 * 1024
@@ -48,15 +33,15 @@ module Deltacloud::Drivers::Mock
48
33
  end
49
34
 
50
35
  define_hardware_profile('m1-large') do
51
- cpu (1..6)
52
- memory ( 7680.. 15*1024), :default => 10 * 1024
36
+ cpu 1..6
37
+ memory 7680..(15*1024), :default => 10 * 1024
53
38
  storage [ 850, 1024 ]
54
39
  architecture 'x86_64'
55
40
  end
56
41
 
57
42
  define_hardware_profile('m1-xlarge') do
58
43
  cpu 4
59
- memory (12*1024 .. 32*1024)
44
+ memory 12*1024..32*1024
60
45
  storage [ 1024, 2048, 4096 ]
61
46
  architecture 'x86_64'
62
47
  end
@@ -76,13 +61,16 @@ module Deltacloud::Drivers::Mock
76
61
  stopped.to( :finish ) .on( :destroy )
77
62
  end
78
63
 
79
- feature :instances, :user_name
80
- feature :instances, :user_data
81
- feature :instances, :authentication_key
82
- feature :instances, :metrics
83
- feature :instances, :realm_filter
84
- feature :images, :user_name
85
- feature :images, :user_description
64
+ feature :instances,
65
+ :user_name,
66
+ :user_Data,
67
+ :authentication_key,
68
+ :metrics,
69
+ :realm_filter
70
+
71
+ feature :images,
72
+ :user_name,
73
+ :user_description
86
74
 
87
75
  #cimi features
88
76
  feature :machines, :default_initial_state do
@@ -94,14 +82,7 @@ module Deltacloud::Drivers::Mock
94
82
 
95
83
 
96
84
  def initialize
97
- if ENV["DELTACLOUD_MOCK_STORAGE"]
98
- storage_root = ENV["DELTACLOUD_MOCK_STORAGE"]
99
- elsif Etc.getlogin
100
- storage_root = File::join("/var/tmp", "deltacloud-mock-#{ENV["USER"]}")
101
- else
102
- raise "Please set either the DELTACLOUD_MOCK_STORAGE or USER environment variable"
103
- end
104
- @client = Client.new(storage_root)
85
+ @client = Client.new(MOCK_STORAGE_DIR)
105
86
  end
106
87
 
107
88
  def realms(credentials, opts={})
@@ -114,33 +95,55 @@ module Deltacloud::Drivers::Mock
114
95
  raise "NotImplementedTest" if opts and opts[:id] == "501"
115
96
  raise "ProviderErrorTest" if opts and opts[:id] == "502"
116
97
  raise "ProviderTimeoutTest" if opts and opts[:id] == "504"
117
- results = REALMS
118
- end
119
- results = filter_on( results, :id, opts )
98
+ results = [
99
+ Realm.new(
100
+ :id=>'us',
101
+ :name=>'United States',
102
+ :limit=>:unlimited,
103
+ :state=>'AVAILABLE'
104
+ ),
105
+ Realm.new(
106
+ :id=>'eu',
107
+ :name=>'Europe',
108
+ :limit=>:unlimited,
109
+ :state=>'AVAILABLE'
110
+ ),
111
+ ]
112
+ end
113
+ results = filter_on( results, opts, :id )
120
114
  results
121
115
  end
122
116
 
117
+ def filter_by_owner(images, owner_id)
118
+ return images unless owner_id
119
+ if owner_id == 'self'
120
+ images.select { |e| e.owner_id == credentials.user }
121
+ else
122
+ filter_on(images, { :owner_id => owner_id}, :owner_id )
123
+ end
124
+ end
125
+
123
126
  #
124
127
  # Images
125
128
  #
126
- def images(credentials, opts=nil )
127
- check_credentials( credentials )
128
- images = []
129
+ def images(credentials, opts={})
130
+ check_credentials(credentials)
129
131
  images = @client.build_all(Image)
130
- images = filter_on( images, :id, opts )
131
- images = filter_on( images, :architecture, opts )
132
- if ( opts && opts[:owner_id] == 'self' )
133
- images = images.select{|e| e.owner_id == credentials.user }
134
- else
135
- images = filter_on( images, :owner_id, opts )
136
- end
132
+
133
+ images = filter_on(images, opts, :id, :architecture)
134
+ images = filter_by_owner(images, opts[:owner_id])
135
+
136
+ # Add hardware profiles to each image
137
137
  images = images.map { |i| (i.hardware_profiles = hardware_profiles(nil)) && i }
138
+
138
139
  images.sort_by{|e| [e.owner_id, e.description]}
139
140
  end
140
141
 
141
142
  def create_image(credentials, opts={})
142
143
  check_credentials(credentials)
143
- instance = instance(credentials, :id => opts[:id])
144
+
145
+ instance = instance(credentials, opts)
146
+
144
147
  safely do
145
148
  raise 'CreateImageNotSupported' unless instance and instance.can_create_image?
146
149
  image = {
@@ -175,62 +178,66 @@ module Deltacloud::Drivers::Mock
175
178
  def instances(credentials, opts={})
176
179
  check_credentials( credentials )
177
180
  instances = @client.build_all(Instance)
178
- instances = filter_on( instances, :owner_id, :owner_id => credentials.user )
179
- instances = filter_on( instances, :id, opts )
180
- instances = filter_on( instances, :state, opts )
181
- instances = filter_on( instances, :realm_id, opts)
182
- instances
181
+ opts.merge!( :owner_id => credentials.user ) unless opts.has_key?(:owner_id)
182
+ filter_on(instances, opts, :owner_id, :id, :state, :realm_id)
183
183
  end
184
184
 
185
- def create_instance(credentials, image_id, opts)
186
- check_credentials( credentials )
185
+ def generate_instance_id
187
186
  ids = @client.members(:instances)
188
-
189
- count = 0
190
- while true
191
- next_id = "inst" + count.to_s
192
- if not ids.include?(next_id)
193
- break
194
- end
187
+ count, next_id = 0, ''
188
+ loop do
189
+ break unless ids.include?(next_id = "inst#{count}")
195
190
  count = count + 1
196
191
  end
192
+ next_id
193
+ end
197
194
 
198
- realm_id = opts[:realm_id]
199
- if ( realm_id.nil? )
200
- realm = realms(credentials).first
201
- ( realm_id = realm.id ) if realm
202
- end
195
+ def create_instance(credentials, image_id, opts={})
196
+ check_credentials( credentials )
197
+
198
+ instance_id = generate_instance_id
199
+ realm_id = opts[:realm_id] || realms(credentials).first.id
203
200
 
204
- hwp = find_hardware_profile(credentials, opts[:hwp_id], image_id)
205
- hwp ||= find_hardware_profile(credentials, 'm1-small', image_id)
201
+ if opts[:hwp_id]
202
+ hwp = find_hardware_profile(credentials, opts[:hwp_id], image_id)
203
+ else
204
+ hwp = find_hardware_profile(credentials, 'm1-small', image_id)
205
+ end
206
206
 
207
207
  name = opts[:name] || "i-#{Time.now.to_i}"
208
+
208
209
  initial_state = opts[:initial_state] || "RUNNING"
210
+
209
211
  instance = {
210
- :id => next_id,
211
- :name=>name,
212
- :state=> (initial_state == "STARTED" ? "RUNNING" : initial_state),
212
+ :id => instance_id,
213
+ :name => name,
214
+ :state => (initial_state == "STARTED" ? "RUNNING" : initial_state),
213
215
  :keyname => opts[:keyname],
214
- :image_id=>image_id,
215
- :owner_id=>credentials.user,
216
- :public_addresses=>[ InstanceAddress.new("#{image_id}.#{next_id}.public.com", :type => :hostname) ],
217
- :private_addresses=>[ InstanceAddress.new("#{image_id}.#{next_id}.private.com", :type => :hostname) ],
216
+ :image_id => image_id,
217
+ :owner_id => credentials.user,
218
+ :public_addresses => [
219
+ InstanceAddress.new("#{image_id}.#{instance_id}.public.com", :type => :hostname)
220
+ ],
221
+ :private_addresses =>[
222
+ InstanceAddress.new("#{image_id}.#{instance_id}.private.com", :type => :hostname)
223
+ ],
218
224
  :instance_profile => InstanceProfile.new(hwp.name, opts),
219
- :realm_id=>realm_id,
220
- :create_image=>true,
221
- :actions=>instance_actions_for((initial_state == "STARTED" ? "RUNNING" : initial_state)),
225
+ :realm_id => realm_id,
226
+ :create_image => true,
227
+ :actions => instance_actions_for((initial_state == "STARTED" ? "RUNNING" : initial_state)),
222
228
  :user_data => opts[:user_data] ? Base64::decode64(opts[:user_data]) : nil
223
229
  }
230
+
224
231
  @client.store(:instances, instance)
225
232
  Instance.new( instance )
226
233
  end
227
234
 
228
235
  def update_instance_state(credentials, id, state)
229
236
  instance = @client.load_collection(:instances, id)
230
- instance[:state] = state
231
- instance[:actions] = instance_actions_for( instance[:state] )
232
- @client.store(:instances, instance)
233
- Instance.new( instance )
237
+ Instance.new(@client.store(:instances, instance.merge(
238
+ :state => state,
239
+ :actions => instance_actions_for(state)
240
+ )))
234
241
  end
235
242
 
236
243
  def start_instance(credentials, id)
@@ -245,45 +252,44 @@ module Deltacloud::Drivers::Mock
245
252
  update_instance_state(credentials, id, 'STOPPED')
246
253
  end
247
254
 
248
-
249
255
  def destroy_instance(credentials, id)
250
256
  check_credentials( credentials )
251
257
  @client.destroy(:instances, id)
252
258
  end
253
259
 
254
260
  # mock object to mimick Net::SSH object
255
- class Mock_ssh
261
+ class MockSSH
256
262
  attr_accessor :command
257
263
  end
258
264
 
259
265
  def run_on_instance(credentials, opts={})
260
- ssh = Mock_ssh.new
266
+ ssh = MockSSH.new
261
267
  ssh.command = opts[:cmd]
262
- Deltacloud::Runner::Response.new(ssh, "This is where the output would appear if this were not a mock provider")
268
+ Deltacloud::Runner::Response.new(
269
+ ssh,
270
+ "This is where the output from '#{ssh.command}' would appear if this were not a mock provider"
271
+ )
263
272
  end
264
273
 
265
274
  #
266
275
  # Storage Volumes
267
276
  #
268
- def storage_volumes(credentials, opts=nil)
269
- check_credentials( credentials )
270
- volumes = @client.build_all(StorageVolume)
271
- volumes = filter_on( volumes, :id, opts )
272
- volumes
277
+ def storage_volumes(credentials, opts={})
278
+ check_credentials(credentials)
279
+ filter_on(@client.build_all(StorageVolume), opts, :id)
273
280
  end
274
281
 
275
282
  def create_storage_volume(credentials, opts={})
276
283
  check_credentials(credentials)
277
284
  opts[:capacity] ||= "1"
278
- id = "Volume#{Time.now.to_i}"
279
- volume = {
280
- :id => id,
281
- :name => opts[:name] ? opts[:name] : id,
282
- :created => Time.now.to_s,
283
- :state => "AVAILABLE",
284
- :capacity => opts[:capacity],
285
- }
286
- @client.store(:storage_volumes, volume)
285
+ volume_id = "volume_#{Time.now.to_i}"
286
+ volume = @client.store(:storage_volumes, {
287
+ :id => volume_id,
288
+ :name => opts[:name] ? opts[:name] : "Volume#{volume_id}",
289
+ :created => Time.now.to_s,
290
+ :state => "AVAILABLE",
291
+ :capacity => opts[:capacity],
292
+ })
287
293
  StorageVolume.new(volume)
288
294
  end
289
295
 
@@ -298,7 +304,7 @@ module Deltacloud::Drivers::Mock
298
304
  attach_volume_instance(opts[:id], opts[:device], opts[:instance_id])
299
305
  end
300
306
 
301
- def detach_storage_volume(credentials, opts)
307
+ def detach_storage_volume(credentials, opts={})
302
308
  check_credentials(credentials)
303
309
  detach_volume_instance(opts[:id], opts[:instance_id])
304
310
  end
@@ -307,11 +313,9 @@ module Deltacloud::Drivers::Mock
307
313
  # Storage Snapshots
308
314
  #
309
315
 
310
- def storage_snapshots(credentials, opts=nil)
316
+ def storage_snapshots(credentials, opts={})
311
317
  check_credentials( credentials )
312
- snapshots = @client.build_all(StorageSnapshot)
313
- snapshots = filter_on(snapshots, :id, opts )
314
- snapshots
318
+ filter_on(@client.build_all(StorageSnapshot), opts, :id)
315
319
  end
316
320
 
317
321
  def create_storage_snapshot(credentials, opts={})
@@ -323,10 +327,9 @@ module Deltacloud::Drivers::Mock
323
327
  :state => "COMPLETED",
324
328
  :storage_volume_id => opts[:volume_id],
325
329
  }
326
- snapshot.merge!({:name=>opts[:name]}) if opts[:name]
327
- snapshot.merge!({:description=>opts[:description]}) if opts[:description]
328
- @client.store(:storage_snapshots, snapshot)
329
- StorageSnapshot.new(snapshot)
330
+ snapshot.merge!({:name => opts[:name]}) if opts[:name]
331
+ snapshot.merge!({:description => opts[:description]}) if opts[:description]
332
+ StorageSnapshot.new(@client.store(:storage_snapshots, snapshot))
330
333
  end
331
334
 
332
335
  def destroy_storage_snapshot(credentials, opts={})
@@ -336,9 +339,7 @@ module Deltacloud::Drivers::Mock
336
339
 
337
340
  def keys(credentials, opts={})
338
341
  check_credentials(credentials)
339
- result = @client.build_all(Key)
340
- result = filter_on( result, :id, opts )
341
- result
342
+ filter_on(@client.build_all(Key), opts, :id)
342
343
  end
343
344
 
344
345
  def key(credentials, opts={})
@@ -355,9 +356,8 @@ module Deltacloud::Drivers::Mock
355
356
  }
356
357
  safely do
357
358
  raise "KeyExist" if @client.load_collection(:keys, key_hash[:id])
358
- @client.store(:keys, key_hash)
359
+ Key.new(@client.store(:keys, key_hash))
359
360
  end
360
- return Key.new(key_hash)
361
361
  end
362
362
 
363
363
  def destroy_key(credentials, opts={})
@@ -367,15 +367,15 @@ module Deltacloud::Drivers::Mock
367
367
 
368
368
  def addresses(credentials, opts={})
369
369
  check_credentials(credentials)
370
- addresses = @client.build_all(Address)
371
- addresses = filter_on( addresses, :id, opts )
370
+ filter_on(@client.build_all(Address), opts, :id)
372
371
  end
373
372
 
374
373
  def create_address(credentials, opts={})
375
374
  check_credentials(credentials)
376
- address = {:id => allocate_mock_address.to_s, :instance_id=>nil}
377
- @client.store(:addresses, address)
378
- Address.new(address)
375
+ Address.new(@client.store(:addresses, {
376
+ :id => allocate_mock_address.to_s,
377
+ :instance_id => nil
378
+ }))
379
379
  end
380
380
 
381
381
  def destroy_address(credentials, opts={})
@@ -402,7 +402,9 @@ module Deltacloud::Drivers::Mock
402
402
  raise "AddressNotInUse" unless address[:instance_id]
403
403
  instance = @client.load_collection(:instances, address[:instance_id])
404
404
  address[:instance_id] = nil
405
- instance[:public_addresses] = [InstanceAddress.new("#{instance[:image_id]}.#{instance[:id]}.public.com", :type => :hostname)]
405
+ instance[:public_addresses] = [
406
+ InstanceAddress.new("#{instance[:image_id]}.#{instance[:id]}.public.com", :type => :hostname)
407
+ ]
406
408
  @client.store(:addresses, address)
407
409
  @client.store(:instances, instance)
408
410
  end
@@ -419,7 +421,7 @@ module Deltacloud::Drivers::Mock
419
421
  map
420
422
  end
421
423
  buckets.each { |bucket| bucket.blob_list = blob_map[bucket.id] }
422
- filter_on( buckets, :id, opts )
424
+ filter_on( buckets, opts, :id)
423
425
  end
424
426
 
425
427
  #--
@@ -454,8 +456,8 @@ module Deltacloud::Drivers::Mock
454
456
  def blobs(credentials, opts = {})
455
457
  check_credentials(credentials)
456
458
  blobs = @client.build_all(Blob)
457
- filter_on( blobs, :bucket, :bucket => opts['bucket'] )
458
- filter_on( blobs, :id, opts )
459
+ opts.merge!( :bucket => opts.delete('bucket') )
460
+ filter_on(blobs, opts, :id, :bucket)
459
461
  end
460
462
 
461
463
  #--
@@ -497,8 +499,8 @@ module Deltacloud::Drivers::Mock
497
499
  :content => blob_data
498
500
  })
499
501
  end
500
- @client.store(:blobs, blob)
501
- Blob.new(blob)
502
+ update_bucket_size(bucket_id, :plus)
503
+ Blob.new(@client.store(:blobs, blob))
502
504
  end
503
505
 
504
506
  #--
@@ -508,6 +510,7 @@ module Deltacloud::Drivers::Mock
508
510
  check_credentials(credentials)
509
511
  safely do
510
512
  raise "NotExistentBlob" unless @client.load_collection(:blobs, blob_id)
513
+ update_bucket_size(bucket_id, :minus)
511
514
  @client.destroy(:blobs, blob_id)
512
515
  end
513
516
  end
@@ -517,11 +520,7 @@ module Deltacloud::Drivers::Mock
517
520
  #--
518
521
  def blob_metadata(credentials, opts={})
519
522
  check_credentials(credentials)
520
- if blob = @client.load_collection(:blobs, opts[:id])
521
- blob[:user_metadata]
522
- else
523
- nil
524
- end
523
+ (blob = @client.load_collection(:blobs, opts[:id])) ? blob[:user_metadata] : nil
525
524
  end
526
525
 
527
526
  #--
@@ -530,10 +529,13 @@ module Deltacloud::Drivers::Mock
530
529
  def update_blob_metadata(credentials, opts={})
531
530
  check_credentials(credentials)
532
531
  safely do
533
- blob = @client.load_collection(:blobs, opts[:id])
534
- return false unless blob
535
- blob[:user_metadata] = BlobHelper::rename_metadata_headers(opts['meta_hash'], '')
536
- @client.store(:blobs, blob)
532
+ if blob = @client.load_collection(:blobs, opts[:id])
533
+ @client.store(:blobs, blob.merge(
534
+ :user_metadata => BlobHelper::rename_metadata_headers(opts['meta_hash'], '')
535
+ ))
536
+ else
537
+ false
538
+ end
537
539
  end
538
540
  end
539
541
 
@@ -541,59 +543,24 @@ module Deltacloud::Drivers::Mock
541
543
  # Metrics
542
544
  #--
543
545
  def metrics(credentials, opts={})
544
- check_credentials( credentials )
545
- instances = @client.build_all(Instance)
546
- instances = filter_on( instances, :id, opts )
547
-
548
- metrics_arr = instances.collect do |instance|
549
- Metric.new(
550
- :id => instance.id,
551
- :entity => instance.name
546
+ check_credentials(credentials)
547
+ instances(credentials).map do |inst|
548
+ metric = Metric.new(
549
+ :id => inst.id,
550
+ :entity => inst.name
552
551
  )
552
+ Metric::MOCK_METRICS_NAMES.each { |metric_name| metric.add_property(metric_name) }
553
+ metric.properties.sort! { |a,b| a.name <=> b.name }
554
+ metric
553
555
  end
554
-
555
- # add metric names to metrics
556
- metrics_arr.each do |metric|
557
- @@METRIC_NAMES.each do |name|
558
- metric.add_property(name)
559
- end
560
- metric.properties.sort! {|a,b| a.name <=> b.name}
561
- end
562
- metrics_arr
563
556
  end
564
557
 
565
558
  def metric(credentials, opts={})
566
- metric = metrics(credentials, opts).first
567
-
568
- metric.properties.each do |property|
569
-
570
- property.values = (0..5).collect do |i|
571
-
572
- unit = metric_unit_for(property.name)
573
- average = (property.name == 'cpuUtilization') ? (rand * 1000).to_i / 10.0 : rand(1000)
574
- max = (property.name == 'cpuUtilization') ? (1000 + 10 * average).to_i / 20.0 : average * (i + 1)
575
- min = (property.name == 'cpuUtilization') ? (2.5 * average).to_i / 10.0 : (average / 4).to_i
576
- {
577
- :minimum => min,
578
- :maximum => max,
579
- :average => average,
580
- :timestamp => Time.now - i * 60,
581
- :unit => unit
582
- }
583
- end
584
- end
559
+ metric = metrics(credentials).first
560
+ metric.properties.each { |p| p.generate_mock_values! }
585
561
  metric
586
562
  end
587
563
 
588
- def valid_credentials?(credentials)
589
- begin
590
- check_credentials(credentials)
591
- return true
592
- rescue
593
- end
594
- return false
595
- end
596
-
597
564
  private
598
565
 
599
566
  def check_credentials(credentials)
@@ -603,10 +570,12 @@ module Deltacloud::Drivers::Mock
603
570
  end
604
571
  end
605
572
  end
573
+ alias :new_client :check_credentials
606
574
 
607
- #Mock allocation of 'new' address
608
- #There is a synchronization problem (but it's the mock driver,
609
- #mutex seemed overkill)
575
+ # Mock allocation of 'new' address
576
+ # There is a synchronization problem (but it's the mock driver,
577
+ # mutex seemed overkill)
578
+ #
610
579
  def allocate_mock_address
611
580
  addresses = []
612
581
  @client.members(:addresses).each do |addr|
@@ -641,30 +610,18 @@ module Deltacloud::Drivers::Mock
641
610
  StorageVolume.new(volume)
642
611
  end
643
612
 
644
- def metric_unit_for(name)
645
- case name
646
- when /Utilization/ then 'Percent'
647
- when /Byte/ then 'Bytes'
648
- when /Sector/ then 'Count'
649
- when /Count/ then 'Count'
650
- when /Packet/ then 'Count'
651
- else 'None'
613
+ def update_bucket_size(id, change)
614
+ bucket = @client.load_collection(:buckets, id)
615
+ raise 'BucketNotExist' if bucket.nil?
616
+ bucket[:size] = case change
617
+ when :plus then bucket[:size].to_i + 1
618
+ when :minus then bucket[:size].to_i - 1
619
+ else
620
+ raise "unkown update operation for bucket!"
652
621
  end
622
+ @client.store(:buckets, bucket)
653
623
  end
654
624
 
655
- # names copied from FGCP driver
656
- @@METRIC_NAMES = [
657
- 'cpuUtilization',
658
- 'diskReadRequestCount',
659
- 'diskReadSector',
660
- 'diskWriteRequestCount',
661
- 'diskWriteSector',
662
- 'nicInputByte',
663
- 'nicInputPacket',
664
- 'nicOutputByte',
665
- 'nicOutputPacket'
666
- ]
667
-
668
625
  exceptions do
669
626
 
670
627
  on /AuthFailure/ do