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
@@ -31,6 +31,10 @@ module Deltacloud::EC2
31
31
  :terminate_instances => { :method => :destroy_instance, :params => { 'InstanceId.1' => :id }},
32
32
  }
33
33
 
34
+ def self.mappings
35
+ MAPPINGS
36
+ end
37
+
34
38
  attr_reader :action
35
39
 
36
40
  def initialize(action)
@@ -38,12 +42,13 @@ module Deltacloud::EC2
38
42
  end
39
43
 
40
44
  def deltacloud_method
41
- MAPPINGS[action.action][:method]
45
+ self.class.mappings[action.action][:method]
42
46
  end
43
47
 
44
48
  def deltacloud_method_params
45
- MAPPINGS[action.action][:params].inject({}) do |result, p|
46
- result[p.last] = action.parameters.delete(p.first)
49
+ parameters = action.parameters.dup
50
+ self.class.mappings[action.action][:params].inject({}) do |result, p|
51
+ result[p.last] = parameters.delete(p.first)
47
52
  result.delete_if { |k,v| v.nil? }
48
53
  end
49
54
  end
@@ -137,7 +142,7 @@ module Deltacloud::EC2
137
142
  end
138
143
 
139
144
  def valid_actions
140
- ActionHandler::MAPPINGS.keys
145
+ ActionHandler::mappings.keys
141
146
  end
142
147
 
143
148
  def valid_action?
data/lib/ec2/server.rb CHANGED
@@ -29,7 +29,7 @@ module Deltacloud::EC2
29
29
  extend Deltacloud::Helpers::Drivers
30
30
 
31
31
  use Rack::ETag
32
- use Rack::CommonLogger
32
+ use Deltacloud[:ec2].logger
33
33
 
34
34
  helpers Sinatra::AuthHelper
35
35
  helpers Deltacloud::Helpers::Drivers
@@ -0,0 +1,3 @@
1
+ %keyName=object.name
2
+ %keyFingerprint=object.fingerprint
3
+ %keyMaterial=object.pem_rsa_key
@@ -0,0 +1 @@
1
+ %return true
@@ -0,0 +1,6 @@
1
+ %availabilityZoneInfo
2
+ - object.each do |item|
3
+ %item
4
+ %zoneName=item.id
5
+ %zoneState=item.state
6
+ %regionName=item.name
@@ -0,0 +1,10 @@
1
+ %imagesSet
2
+ - object.each do |item|
3
+ %item
4
+ %imageId=item.id
5
+ %imageState=item.state.downcase
6
+ %imageOwnerId=item.owner_id
7
+ %architecture=item.architecture
8
+ %imageType machine
9
+ %name=item.name
10
+ %description=item.description
@@ -0,0 +1,24 @@
1
+ %instanceSet
2
+ - object.each do |item|
3
+ %item
4
+ %instanceId=item.id
5
+ %imageId=item.image_id
6
+ %instanceType=item.instance_profile.name
7
+ %launchTime=item.launch_time || Time.now.to_s
8
+ - if item.public_addresses and not item.public_addresses.empty?
9
+ %ipAddress=item.public_addresses.first.address
10
+ %dnsName=item.public_addresses.first.address
11
+ - if item.private_addresses and not item.private_addresses.empty?
12
+ %privateIpAddress=item.private_addresses.first.address
13
+ %privateDnsName=item.private_addresses.first.address
14
+ %architecture=item.instance_profile.architecture || 'i386'
15
+ - if item.keyname
16
+ %keyName=item.keyname
17
+ - if item.state
18
+ %instanceState
19
+ %code=instance_state_code(item.state.downcase)
20
+ %name=item.state.downcase
21
+ %placement
22
+ %availabilityZone=item.realm_id
23
+ %groupName
24
+ %tenancy default
@@ -0,0 +1,9 @@
1
+ %reservationSet
2
+ %item
3
+ %reservationId r-11111111
4
+ %ownerId deltacloud
5
+ %groupSet
6
+ %item
7
+ %groupId sg-11111111
8
+ %groupName default
9
+ =render(:describe_instance_set, object)
@@ -0,0 +1,5 @@
1
+ %keySet
2
+ - object.each do |item|
3
+ %item
4
+ %keyName=item.name
5
+ %keyFingerprint=item.fingerprint
@@ -0,0 +1,4 @@
1
+ %Response
2
+ %Errors
3
+ %Code=code
4
+ %Message=message
@@ -0,0 +1,9 @@
1
+ %instancesSet
2
+ %item
3
+ %instanceId=object.id
4
+ %currentState
5
+ %code=instance_state_code(object.state.downcase)
6
+ %name=object.state.downcase
7
+ %previousState
8
+ %code -1
9
+ %name unknown
@@ -0,0 +1 @@
1
+ =render :instance_action, object
@@ -0,0 +1,7 @@
1
+ %reservationId r-11111111
2
+ %ownerId deltacloud
3
+ %groupSet
4
+ %item
5
+ %groupId sg-11111111
6
+ %groupName default
7
+ =render :describe_instance_set, [object]
@@ -0,0 +1 @@
1
+ =render :instance_action, object
@@ -0,0 +1 @@
1
+ =render :instance_action, object
@@ -0,0 +1,9 @@
1
+ %instancesSet
2
+ %item
3
+ %instanceId=context.params['InstanceId.1']
4
+ %currentState
5
+ %code=32
6
+ %name shutting-down
7
+ %previousState
8
+ %code -1
9
+ %name unknown
data/lib/sinatra.rb CHANGED
@@ -18,5 +18,4 @@ require_relative 'sinatra/rack_date'
18
18
  require_relative 'sinatra/rack_etag'
19
19
  require_relative 'sinatra/rack_matrix_params'
20
20
  require_relative 'sinatra/rack_driver_select'
21
- require_relative 'sinatra/accept_media_types'
22
21
  require_relative 'sinatra/rack_accept'
@@ -73,7 +73,7 @@ module Rack
73
73
  self[type] = handler
74
74
  end
75
75
  yield wants
76
- if ENV['API_FRONTEND'] == "cimi"
76
+ if Deltacloud.default_frontend.name == :cimi
77
77
  @media_type = (accepting_formats.has_key?(:xml) ? [:xml, accepting_formats[:xml]] : nil)
78
78
  end
79
79
  @media_type ||= accepting_formats.to_a.sort { |a,b| a[1]<=>b[1] }.reverse.select do |format, priority|
@@ -31,10 +31,10 @@ module Rack
31
31
  original_settings = { }
32
32
  req = Rack::Request.new(env)
33
33
  if req.params['api'] and req.params['api']['driver']
34
- env['HTTP_X_DELTACLOUD_DRIVER'] = req.params['api']['driver']
34
+ env['HTTP_X_DELTACLOUD_DRIVER'] = req.params['api']['driver']
35
35
  end
36
36
  if req.params['api'] and req.params['api']['provider']
37
- env['HTTP_X_DELTACLOUD_PROVIDER'] = req.params['api']['provider']
37
+ env['HTTP_X_DELTACLOUD_PROVIDER'] = req.params['api']['provider']
38
38
  end
39
39
  HEADER_TO_ENV_MAP.each do |header, name|
40
40
  original_settings[name] = Thread.current[name]
@@ -68,7 +68,11 @@ module Rack
68
68
 
69
69
  def digest_body(body)
70
70
  parts = []
71
- body.each { |part| parts << part }
71
+ if RUBY_VERSION =~ /^1\.8/
72
+ body.each { |part, b| parts << part }
73
+ else
74
+ body.each { |part| parts << part }
75
+ end
72
76
  string_body = parts.join
73
77
  digest = Digest::MD5.hexdigest(string_body) unless string_body.empty?
74
78
  [digest, parts]
@@ -0,0 +1,127 @@
1
+ # Licensed to the Apache Software Foundation (ASF) under one or more
2
+ # contributor license agreements. See the NOTICE file distributed with
3
+ # this work for additional information regarding copyright ownership. The
4
+ # ASF licenses this file to you under the Apache License, Version 2.0 (the
5
+ # "License"); you may not use this file except in compliance with the
6
+ # License. You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12
+ # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13
+ # License for the specific language governing permissions and limitations
14
+ # under the License.
15
+
16
+ require_relative 'body_proxy'
17
+
18
+ # This module is based on Rack::CommonLogger[1]
19
+ # Copyright (C) 2007, 2008, 2009, 2010 Christian Neukirchen <purl.org/net/chneukirchen>
20
+ #
21
+ # [1] https://github.com/rack/rack/blob/master/lib/rack/commonlogger.rb
22
+
23
+ module Rack
24
+ # Rack::CommonLogger forwards every request to an +app+ given, and
25
+ # logs a line in the Apache common log format to the +logger+, or
26
+ # rack.errors by default.
27
+ class DeltacloudLogger
28
+
29
+ def self.log_path(path=nil)
30
+ @log_file ||= path
31
+ end
32
+
33
+ def self.verbose?
34
+ @verbose
35
+ end
36
+
37
+ def self.verbose(v=nil)
38
+ @verbose ||= v
39
+ end
40
+
41
+ def self.setup(path, be_verbose=false)
42
+ verbose(be_verbose)
43
+ return self if path.nil?
44
+ dir = ::File.dirname(path)
45
+ if ::File.exists?(dir) and ::File.writable?(dir)
46
+ log_path(path)
47
+ else
48
+ warn "Warning: The log directory (#{dir}) is not writeable."
49
+ end
50
+ self
51
+ end
52
+
53
+ # Common Log Format: http://httpd.apache.org/docs/1.3/logs.html#common
54
+ # lilith.local - - [07/Aug/2006 23:58:02] "GET / HTTP/1.1" 500 -
55
+ # %{%s - %s [%s] "%s %s%s %s" %d %s\n} %
56
+ FORMAT = %{%s - %s [%s] "%s %s%s %s" %d %s %0.4f\n}
57
+
58
+ VERBOSE_FORMAT = %{%s - %s [%s] "%s %s%s%s %s" %s %s %d %s %0.4f\n}
59
+
60
+ def initialize(app, logger=nil)
61
+ @app = app
62
+ unless self.class.log_path.nil?
63
+ @logger = ::Logger.new(self.class.log_path)
64
+ else
65
+ @logger = logger
66
+ end
67
+ end
68
+
69
+ def call(env)
70
+ began_at = Time.now
71
+ status, header, body = @app.call(env)
72
+ header = Utils::HeaderHash.new(header)
73
+ body = BodyProxy.new(body) do
74
+ self.class.verbose? ? verbose_log(env, status, header, began_at) : log(env, status, header, began_at)
75
+ end
76
+ [status, header, body]
77
+ end
78
+
79
+ private
80
+
81
+ def verbose_log(env, status, header, began_at)
82
+ now = Time.now
83
+ length = extract_content_length(header)
84
+ params = env['rack.request.form_hash'].nil? ? '' : ' '+env['rack.request.form_hash'].to_json
85
+
86
+ logger = @logger || env['rack.errors']
87
+ logger << VERBOSE_FORMAT % [
88
+ env['HTTP_X_FORWARDED_FOR'] || env["REMOTE_ADDR"] || "-",
89
+ env["REMOTE_USER"] || "-",
90
+ now.strftime("%d/%b/%Y %H:%M:%S"),
91
+ env["REQUEST_METHOD"],
92
+ env["PATH_INFO"],
93
+ env["QUERY_STRING"].empty? ? '' : "?"+env["QUERY_STRING"],
94
+ params,
95
+ env["HTTP_VERSION"],
96
+ env['HTTP_X_DELTACLOUD_DRIVER'] || ENV['API_DRIVER'] || 'mock',
97
+ env['HTTP_X_DELTACLOUD_PROVIDER'] || ENV['API_PROVIDER'] || '-',
98
+ status.to_s[0..3],
99
+ length,
100
+ now - began_at ]
101
+ end
102
+
103
+ def log(env, status, header, began_at)
104
+ now = Time.now
105
+ length = extract_content_length(header)
106
+
107
+ logger = @logger || env['rack.errors']
108
+ logger << FORMAT % [
109
+ env['HTTP_X_FORWARDED_FOR'] || env["REMOTE_ADDR"] || "-",
110
+ env["REMOTE_USER"] || "-",
111
+ now.strftime("%d/%b/%Y %H:%M:%S"),
112
+ env["REQUEST_METHOD"],
113
+ env["PATH_INFO"],
114
+ env["QUERY_STRING"].empty? ? "" : "?"+env["QUERY_STRING"],
115
+ env["HTTP_VERSION"],
116
+ status.to_s[0..3],
117
+ length,
118
+ now - began_at ]
119
+ end
120
+
121
+ def extract_content_length(headers)
122
+ value = headers['Content-Length'] or return '-'
123
+ value.to_s == '0' ? '-' : value
124
+ end
125
+ end
126
+ end
127
+
@@ -92,7 +92,7 @@ module Rack
92
92
  else
93
93
  # For other methods it's more complicated
94
94
  if env['REQUEST_METHOD']!='POST' and not matrix_params.keys.empty?
95
- env['QUERY_STRING'].gsub!(/;([^\/]*)/, '')
95
+ env['QUERY_STRING'] = env['QUERY_STRING'].gsub(/;([^\/]*)/, '')
96
96
  new_params = matrix_params.collect do |component, params|
97
97
  params.collect { |k,v| "#{component}[#{k}]=#{CGI::escape(v.to_s)}" }
98
98
  end.flatten
@@ -0,0 +1,46 @@
1
+ require 'rubygems'
2
+ require 'require_relative' if RUBY_VERSION < '1.9'
3
+
4
+ require 'minitest/autorun'
5
+ require_relative './common.rb'
6
+
7
+ describe CIMI::Collections::CloudEntryPoint do
8
+
9
+ before do
10
+ def app; CIMI::API; end
11
+ @collection = CIMI::Collections.collection(:cloudEntryPoint)
12
+ end
13
+
14
+ it 'has index operation' do
15
+ @collection.operation(:index).must_equal Sinatra::Rabbit::CloudentrypointCollection::IndexOperation
16
+ end
17
+
18
+ it 'set the CIMI-Version header' do
19
+ get root_url
20
+ headers['X-CIMI-Specification-Version'].wont_be_nil
21
+ headers['X-CIMI-Specification-Version'].must_equal '1.0.0'
22
+ end
23
+
24
+ it 'advertise CIMI collections in XML format' do
25
+ get root_url + '/cloudEntryPoint'
26
+ xml.root.name.must_equal 'CloudEntryPoint'
27
+ (xml.root/'description').first.text.wont_be_empty
28
+ (xml.root/'id').first.text.wont_be_empty
29
+ end
30
+
31
+ it 'advertise CIMI collections in JSON format' do
32
+ get root_url + '/cloudEntryPoint?format=json'
33
+ json.wont_be_empty
34
+ json['description'].wont_be_empty
35
+ json['id'].wont_be_empty
36
+ end
37
+
38
+ it 'allow to force authentication using force_auth parameter in URI' do
39
+ get root_url + '/cloudEntryPoint?force_auth=1'
40
+ status.must_equal 401
41
+ authorize 'mockuser', 'mockpassword'
42
+ get root_url + '/cloudEntryPoint?force_auth=1'
43
+ status.must_equal 200
44
+ end
45
+
46
+ end
@@ -0,0 +1,17 @@
1
+ require_relative File.join('..', '..', '..', 'lib', 'deltacloud_rack.rb')
2
+ require_relative '../../test_helper.rb'
3
+
4
+ # Setup CIMI::API Sinatra instance
5
+ #
6
+ Deltacloud::configure(:cimi) do |server|
7
+ server.root_url '/cimi'
8
+ server.version '1.0.0'
9
+ server.klass 'CIMI::API'
10
+ server.default_driver :mock
11
+ server.logger Rack::DeltacloudLogger.setup(ENV['API_LOG'], ENV['API_VERBOSE'])
12
+ end
13
+
14
+ Deltacloud[:cimi].require!
15
+ Deltacloud[:cimi].default_frontend!
16
+
17
+ def formats; [ 'application/xml', 'application/json' ]; end
@@ -0,0 +1,44 @@
1
+ require 'rubygems'
2
+ require 'require_relative' if RUBY_VERSION < '1.9'
3
+ require 'minitest/autorun'
4
+ require_relative './common.rb'
5
+
6
+ describe CIMI::Collections::MachineImages do
7
+
8
+ before do
9
+ def app; CIMI::API; end
10
+ authorize 'mockuser', 'mockpassword'
11
+ @collection = CIMI::Collections.collection(:machine_images)
12
+ end
13
+
14
+ it 'has index operation' do
15
+ @collection.operation(:index).must_equal Sinatra::Rabbit::MachineImagesCollection::IndexOperation
16
+ end
17
+
18
+ it 'has show operation' do
19
+ @collection.operation(:show).must_equal Sinatra::Rabbit::MachineImagesCollection::ShowOperation
20
+ end
21
+
22
+ it 'returns list of images in various formats with index operation' do
23
+ formats.each do |format|
24
+ header 'Accept', format
25
+ get root_url + '/machine_images'
26
+ status.must_equal 200
27
+ end
28
+ end
29
+
30
+ it 'should allow to retrieve the single image' do
31
+ get root_url '/machine_images/img1'
32
+ status.must_equal 200
33
+ xml.root.name.must_equal 'MachineImage'
34
+ end
35
+
36
+ it 'should allow to filter using CIMISelect' do
37
+ get root_url '/machine_images?CIMISelect=description'
38
+ status.must_equal 200
39
+ xml.root.name.must_equal 'MachineImageCollection'
40
+ (xml/'description').wont_be_empty
41
+ (xml/'id').must_be_empty
42
+ end
43
+
44
+ end