deltacloud-core 0.5.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (294) hide show
  1. data/NOTICE +1 -1
  2. data/Rakefile +63 -21
  3. data/bin/deltacloudd +10 -6
  4. data/config.ru +62 -6
  5. data/config/drivers/ec2.yaml +8 -0
  6. data/config/drivers/fgcp.yaml +13 -0
  7. data/deltacloud-core.gemspec +10 -2
  8. data/lib/cimi/collections.rb +58 -0
  9. data/lib/cimi/collections/address_templates.rb +49 -0
  10. data/lib/cimi/collections/addresses.rb +74 -0
  11. data/lib/cimi/collections/cloud_entry_point.rb +37 -0
  12. data/lib/{deltacloud/helpers/conversion_helper.rb → cimi/collections/entity_metadata.rb} +24 -16
  13. data/lib/cimi/collections/machine_admins.rb +74 -0
  14. data/lib/cimi/collections/machine_configurations.rb +49 -0
  15. data/lib/cimi/collections/machine_images.rb +50 -0
  16. data/lib/cimi/collections/machines.rb +157 -0
  17. data/lib/cimi/collections/network_configurations.rb +47 -0
  18. data/lib/cimi/collections/network_templates.rb +48 -0
  19. data/lib/cimi/collections/networks.rb +125 -0
  20. data/lib/cimi/collections/routing_group_templates.rb +47 -0
  21. data/lib/cimi/collections/routing_groups.rb +48 -0
  22. data/lib/cimi/collections/volume_configurations.rb +48 -0
  23. data/lib/cimi/collections/volume_images.rb +50 -0
  24. data/lib/cimi/collections/volumes.rb +80 -0
  25. data/lib/cimi/collections/vsp_configurations.rb +48 -0
  26. data/lib/cimi/collections/vsp_templates.rb +50 -0
  27. data/lib/cimi/collections/vsps.rb +108 -0
  28. data/lib/cimi/dependencies.rb +1 -0
  29. data/lib/cimi/helpers.rb +116 -0
  30. data/lib/cimi/helpers/cimi_helper.rb +6 -2
  31. data/lib/cimi/models.rb +72 -0
  32. data/lib/cimi/{model → models}/action.rb +1 -1
  33. data/lib/cimi/models/address.rb +72 -0
  34. data/lib/cimi/models/address_collection.rb +34 -0
  35. data/lib/cimi/models/address_template.rb +54 -0
  36. data/lib/cimi/models/address_template_collection.rb +34 -0
  37. data/lib/cimi/{model → models}/base.rb +2 -2
  38. data/lib/cimi/{model → models}/cloud_entry_point.rb +3 -5
  39. data/lib/cimi/{model → models}/entity_metadata.rb +7 -6
  40. data/lib/cimi/{model → models}/entity_metadata_collection.rb +2 -2
  41. data/lib/cimi/{model → models}/errors.rb +8 -0
  42. data/lib/cimi/{model → models}/machine.rb +10 -10
  43. data/lib/cimi/{model → models}/machine_admin.rb +1 -1
  44. data/lib/cimi/{model → models}/machine_admin_collection.rb +2 -2
  45. data/lib/cimi/{model → models}/machine_collection.rb +2 -2
  46. data/lib/cimi/{model → models}/machine_configuration.rb +9 -6
  47. data/lib/cimi/{model → models}/machine_configuration_collection.rb +2 -2
  48. data/lib/cimi/{model → models}/machine_image.rb +1 -1
  49. data/lib/cimi/{model → models}/machine_image_collection.rb +2 -2
  50. data/lib/cimi/{model → models}/machine_template.rb +0 -0
  51. data/lib/cimi/{model → models}/machine_template_collection.rb +2 -2
  52. data/lib/cimi/models/network.rb +109 -0
  53. data/lib/cimi/{model → models}/network_collection.rb +2 -2
  54. data/lib/cimi/{model → models}/network_configuration.rb +3 -4
  55. data/lib/cimi/{model → models}/network_configuration_collection.rb +2 -2
  56. data/lib/cimi/models/network_template.rb +36 -0
  57. data/lib/cimi/models/network_template_collection.rb +35 -0
  58. data/lib/cimi/models/routing_group.rb +34 -0
  59. data/lib/cimi/models/routing_group_collection.rb +34 -0
  60. data/lib/cimi/models/routing_group_template.rb +34 -0
  61. data/lib/cimi/models/routing_group_template_collection.rb +35 -0
  62. data/lib/cimi/{model → models}/schema.rb +0 -0
  63. data/lib/cimi/{model → models}/volume.rb +4 -4
  64. data/lib/cimi/{model → models}/volume_collection.rb +2 -2
  65. data/lib/cimi/{model → models}/volume_configuration.rb +1 -1
  66. data/lib/cimi/{model → models}/volume_configuration_collection.rb +2 -2
  67. data/lib/cimi/{model → models}/volume_image.rb +1 -1
  68. data/lib/cimi/{model → models}/volume_image_collection.rb +2 -2
  69. data/lib/cimi/{model → models}/volume_template.rb +0 -0
  70. data/lib/cimi/{model → models}/volume_template_collection.rb +2 -2
  71. data/lib/cimi/models/vsp.rb +102 -0
  72. data/lib/cimi/models/vsp_collection.rb +34 -0
  73. data/lib/cimi/models/vsp_configuration.rb +40 -0
  74. data/lib/cimi/models/vsp_configuration_collection.rb +34 -0
  75. data/lib/cimi/models/vsp_template.rb +34 -0
  76. data/lib/cimi/models/vsp_template_collection.rb +34 -0
  77. data/lib/cimi/server.rb +27 -549
  78. data/lib/deltacloud/api.rb +79 -0
  79. data/lib/deltacloud/collections.rb +54 -0
  80. data/lib/deltacloud/collections/addresses.rb +91 -0
  81. data/lib/deltacloud/collections/buckets.rb +273 -0
  82. data/lib/deltacloud/collections/drivers.rb +51 -0
  83. data/lib/deltacloud/collections/firewalls.rb +116 -0
  84. data/lib/deltacloud/collections/hardware_profiles.rb +29 -0
  85. data/lib/deltacloud/collections/images.rb +73 -0
  86. data/lib/deltacloud/collections/instance_states.rb +59 -0
  87. data/lib/deltacloud/collections/instances.rb +113 -0
  88. data/lib/deltacloud/collections/keys.rb +61 -0
  89. data/lib/deltacloud/collections/load_balancers.rb +102 -0
  90. data/lib/deltacloud/collections/metrics.rb +28 -0
  91. data/lib/deltacloud/collections/realms.rb +28 -0
  92. data/lib/deltacloud/collections/storage_snapshots.rb +51 -0
  93. data/lib/deltacloud/collections/storage_volumes.rb +99 -0
  94. data/lib/deltacloud/core_ext.rb +14 -6
  95. data/lib/deltacloud/core_ext/string.rb +17 -5
  96. data/lib/deltacloud/drivers.rb +6 -42
  97. data/lib/deltacloud/drivers/azure/azure_driver.rb +0 -4
  98. data/lib/deltacloud/{base_driver → drivers}/base_driver.rb +56 -18
  99. data/lib/deltacloud/drivers/condor/condor_driver.rb +1 -8
  100. data/lib/deltacloud/drivers/ec2/ec2_driver.rb +142 -33
  101. data/lib/deltacloud/drivers/eucalyptus/eucalyptus_driver.rb +2 -6
  102. data/lib/deltacloud/{base_driver → drivers}/exceptions.rb +25 -2
  103. data/lib/deltacloud/drivers/features.rb +154 -0
  104. data/lib/deltacloud/drivers/fgcp/fgcp_client.rb +387 -0
  105. data/lib/deltacloud/drivers/fgcp/fgcp_driver.rb +1435 -0
  106. data/lib/deltacloud/drivers/gogrid/gogrid_driver.rb +8 -11
  107. data/lib/deltacloud/drivers/google/google_driver.rb +2 -5
  108. data/lib/deltacloud/drivers/mock/data/keys/test-key.yml +28 -0
  109. data/lib/deltacloud/drivers/mock/mock_client.rb +2 -2
  110. data/lib/deltacloud/drivers/mock/mock_driver.rb +58 -40
  111. data/lib/deltacloud/drivers/mock/mock_driver_cimi_methods.rb +145 -0
  112. data/lib/deltacloud/drivers/opennebula/cloud_client.rb +107 -73
  113. data/lib/deltacloud/drivers/opennebula/occi_client.rb +285 -145
  114. data/lib/deltacloud/drivers/opennebula/opennebula_driver.rb +189 -126
  115. data/lib/deltacloud/drivers/openstack/openstack_driver.rb +427 -8
  116. data/lib/deltacloud/drivers/rackspace/rackspace_driver.rb +7 -9
  117. data/lib/deltacloud/drivers/rhevm/rhevm_driver.rb +48 -66
  118. data/lib/deltacloud/drivers/rimuhosting/rimuhosting_client.rb +44 -51
  119. data/lib/deltacloud/drivers/rimuhosting/rimuhosting_driver.rb +7 -8
  120. data/lib/deltacloud/drivers/sbc/sbc_client.rb +1 -1
  121. data/lib/deltacloud/drivers/sbc/sbc_driver.rb +3 -3
  122. data/lib/deltacloud/drivers/terremark/terremark_driver.rb +8 -8
  123. data/lib/deltacloud/drivers/vsphere/vsphere_driver.rb +39 -13
  124. data/lib/deltacloud/drivers/vsphere/vsphere_filemanager.rb +1 -0
  125. data/lib/deltacloud/helpers.rb +79 -7
  126. data/lib/{sinatra/lazy_auth.rb → deltacloud/helpers/auth_helper.rb} +5 -9
  127. data/lib/deltacloud/helpers/{blob_stream.rb → blob_stream_helper.rb} +7 -7
  128. data/lib/deltacloud/helpers/deltacloud_helper.rb +286 -0
  129. data/lib/deltacloud/helpers/driver_helper.rb +58 -0
  130. data/lib/deltacloud/helpers/rabbit_helper.rb +34 -0
  131. data/lib/{sinatra/url_for.rb → deltacloud/helpers/url_helper.rb} +31 -9
  132. data/lib/deltacloud/models.rb +19 -17
  133. data/lib/deltacloud/models/bucket.rb +4 -0
  134. data/lib/deltacloud/{hardware_profile.rb → models/hardware_profile.rb} +4 -2
  135. data/lib/deltacloud/models/image.rb +1 -0
  136. data/lib/deltacloud/models/instance.rb +4 -2
  137. data/lib/deltacloud/models/instance_address.rb +4 -0
  138. data/lib/deltacloud/models/key.rb +4 -0
  139. data/lib/deltacloud/models/metric.rb +40 -0
  140. data/lib/deltacloud/{state_machine.rb → models/state_machine.rb} +18 -1
  141. data/lib/deltacloud/server.rb +41 -1222
  142. data/lib/deltacloud_rack.rb +79 -0
  143. data/lib/{cimi/model/network_template.rb → ec2/helpers.rb} +7 -10
  144. data/lib/{deltacloud/backend_capability.rb → ec2/helpers/errors.rb} +24 -29
  145. data/lib/ec2/helpers/result.rb +31 -0
  146. data/lib/ec2/query_parser.rb +152 -0
  147. data/lib/ec2/server.rb +70 -0
  148. data/lib/{deltacloud.rb → sinatra.rb} +7 -12
  149. data/lib/sinatra/rack_accept.rb +13 -7
  150. data/lib/sinatra/rack_driver_select.rb +1 -1
  151. data/lib/sinatra/rack_matrix_params.rb +1 -3
  152. data/public/images/metric.png +0 -0
  153. data/public/javascripts/jquery.mobile-1.0.1.min.js +177 -0
  154. data/public/stylesheets/jquery.mobile-1.0.1.min.css +2 -0
  155. data/public/stylesheets/new.css +10 -0
  156. data/support/fedora/deltacloud-core.spec +1 -1
  157. data/tests/api/common.rb +1 -0
  158. data/tests/api/driver_test.rb +79 -0
  159. data/tests/api/library_test.rb +48 -0
  160. data/tests/cimi/features/step_definitions/common_steps.rb +2 -2
  161. data/tests/cimi/features/step_definitions/machines_steps.rb +5 -4
  162. data/tests/cimi/features/step_definitions/volumes_steps.rb +8 -8
  163. data/tests/cimi/features/support/env.rb +33 -11
  164. data/tests/common.rb +7 -8
  165. data/tests/drivers/ec2/api_test.rb +19 -0
  166. data/tests/drivers/ec2/common.rb +23 -0
  167. data/tests/drivers/ec2/drivers_test.rb +120 -0
  168. data/tests/drivers/ec2/hardware_profiles_test.rb +224 -0
  169. data/tests/drivers/ec2/images_test.rb +230 -0
  170. data/tests/drivers/ec2/instances_test.rb +356 -0
  171. data/tests/drivers/ec2/keys_test.rb +181 -0
  172. data/tests/drivers/ec2/realms_test.rb +146 -0
  173. data/tests/drivers/fgcp/api_test.rb +47 -0
  174. data/tests/drivers/fgcp/hardware_profiles_test.rb +54 -0
  175. data/tests/drivers/fgcp/realms_test.rb +42 -0
  176. data/tests/drivers/{rackspace → fgcp}/setup.rb +5 -6
  177. data/tests/drivers/google/api_test.rb +10 -26
  178. data/tests/drivers/google/buckets_test.rb +79 -95
  179. data/tests/drivers/google/common.rb +54 -0
  180. data/tests/drivers/mock/api_test.rb +4 -127
  181. data/tests/drivers/mock/buckets_test.rb +195 -0
  182. data/tests/drivers/mock/common.rb +7 -0
  183. data/tests/drivers/mock/drivers_test.rb +123 -0
  184. data/tests/drivers/mock/hardware_profiles_test.rb +190 -100
  185. data/tests/drivers/mock/images_test.rb +162 -103
  186. data/tests/drivers/mock/instances_test.rb +310 -220
  187. data/tests/drivers/mock/keys_test.rb +161 -0
  188. data/tests/drivers/mock/realms_test.rb +109 -70
  189. data/tests/drivers/mock/storage_snapshots_test.rb +114 -0
  190. data/tests/drivers/mock/storage_volumes_test.rb +122 -0
  191. data/tests/drivers/openstack/api_test.rb +8 -3
  192. data/tests/drivers/openstack/common.rb +21 -0
  193. data/tests/drivers/openstack/hardware_profiles_test.rb +20 -9
  194. data/tests/drivers/openstack/images_test.rb +11 -5
  195. data/tests/drivers/openstack/instances_test.rb +61 -16
  196. data/tests/drivers/openstack/realms_test.rb +11 -7
  197. data/tests/drivers/rackspace/api_test.rb +7 -2
  198. data/tests/drivers/rackspace/buckets_test.rb +7 -2
  199. data/tests/drivers/rackspace/common.rb +16 -0
  200. data/tests/drivers/rackspace/hardware_profiles_test.rb +7 -2
  201. data/tests/drivers/rackspace/images_test.rb +7 -2
  202. data/tests/drivers/rackspace/instances_test.rb +10 -5
  203. data/tests/drivers/rackspace/realms_test.rb +7 -2
  204. data/tests/drivers/rhevm/api_test.rb +12 -6
  205. data/tests/drivers/rhevm/{setup.rb → common.rb} +8 -1
  206. data/tests/drivers/rhevm/hardware_profiles_test.rb +7 -2
  207. data/tests/drivers/rhevm/images_test.rb +8 -2
  208. data/tests/drivers/rhevm/instances_test.rb +7 -2
  209. data/tests/drivers/rhevm/realms_test.rb +7 -2
  210. data/tests/minitest_common.rb +58 -0
  211. data/tests/minitest_common_api_test.rb +115 -0
  212. data/views/addresses/associate.html.haml +1 -1
  213. data/views/addresses/index.html.haml +1 -1
  214. data/views/addresses/show.html.haml +2 -3
  215. data/views/api/show.html.haml +22 -9
  216. data/views/api/show.xml.haml +8 -7
  217. data/views/blobs/show.xml.haml +7 -4
  218. data/views/buckets/new.html.haml +2 -2
  219. data/views/cimi/errors/401.xml.haml +1 -1
  220. data/views/error.html.haml +3 -3
  221. data/views/errors/400.html.haml +1 -1
  222. data/views/errors/400.xml.haml +1 -2
  223. data/views/errors/401.html.haml +8 -7
  224. data/views/errors/401.xml.haml +1 -1
  225. data/views/errors/404.xml.haml +1 -0
  226. data/views/errors/500.xml.haml +4 -2
  227. data/views/{cimi/errors/403.html.haml → errors/501.html.haml} +4 -2
  228. data/views/errors/501.xml.haml +1 -0
  229. data/views/errors/502.xml.haml +1 -7
  230. data/views/{cimi/errors/500.html.haml → errors/504.html.haml} +0 -0
  231. data/views/errors/504.xml.haml +1 -0
  232. data/views/firewalls/new.html.haml +2 -2
  233. data/views/firewalls/new_rule.html.haml +1 -1
  234. data/views/firewalls/show.html.haml +1 -1
  235. data/views/hardware_profiles/index.html.haml +3 -1
  236. data/views/hardware_profiles/index.xml.haml +2 -2
  237. data/views/hardware_profiles/show.html.haml +3 -3
  238. data/views/hardware_profiles/show.xml.haml +3 -3
  239. data/views/images/show.html.haml +5 -0
  240. data/views/images/show.xml.haml +6 -1
  241. data/views/instance_states/show.html.haml +1 -1
  242. data/views/instances/index.html.haml +1 -1
  243. data/views/instances/new.html.haml +54 -39
  244. data/views/instances/run_command.html.haml +24 -15
  245. data/views/instances/show.html.haml +7 -3
  246. data/views/instances/show.xml.haml +2 -2
  247. data/views/keys/show.xml.haml +1 -0
  248. data/views/layout.html.haml +5 -9
  249. data/views/load_balancers/show.html.haml +12 -6
  250. data/views/metrics/index.html.haml +13 -0
  251. data/views/metrics/index.xml.haml +5 -0
  252. data/views/metrics/show.html.haml +23 -0
  253. data/views/metrics/show.xml.haml +9 -0
  254. data/views/realms/show.xml.haml +2 -1
  255. data/views/root/index.html.haml +1 -1
  256. data/views/storage_snapshots/show.html.haml +1 -1
  257. data/views/storage_snapshots/show.xml.haml +1 -0
  258. data/views/storage_volumes/attach.html.haml +1 -2
  259. data/views/storage_volumes/index.html.haml +1 -1
  260. data/views/storage_volumes/new.html.haml +22 -16
  261. data/views/storage_volumes/show.html.haml +10 -4
  262. data/views/storage_volumes/show.xml.haml +3 -4
  263. metadata +547 -519
  264. data/DISCLAIMER +0 -8
  265. data/lib/cimi/model.rb +0 -52
  266. data/lib/cimi/model/network.rb +0 -69
  267. data/lib/deltacloud/base_driver.rb +0 -18
  268. data/lib/deltacloud/base_driver/features.rb +0 -262
  269. data/lib/deltacloud/base_driver/mock_driver.rb +0 -78
  270. data/lib/deltacloud/drivers/ec2/ec2_mock_driver.rb +0 -186
  271. data/lib/deltacloud/drivers/rhevm/rhevm_client.rb +0 -521
  272. data/lib/deltacloud/helpers/application_helper.rb +0 -267
  273. data/lib/deltacloud/helpers/hardware_profiles_helper.rb +0 -50
  274. data/lib/deltacloud/helpers/json_helper.rb +0 -31
  275. data/lib/deltacloud/method_serializer.rb +0 -83
  276. data/lib/deltacloud/validation.rb +0 -107
  277. data/lib/sinatra/rabbit.rb +0 -441
  278. data/lib/sinatra/rack_runtime.rb +0 -47
  279. data/lib/sinatra/rack_syslog.rb +0 -86
  280. data/lib/sinatra/sinatra_verbose.rb +0 -73
  281. data/lib/sinatra/static_assets.rb +0 -99
  282. data/public/javascripts/jquery.mobile-1.0rc1.min.js +0 -170
  283. data/public/stylesheets/jquery.mobile-1.0rc1.min.css +0 -12
  284. data/tests/drivers/google/setup.rb +0 -38
  285. data/tests/drivers/mock/instance_states_test.rb +0 -71
  286. data/tests/drivers/mock/setup.rb +0 -3
  287. data/tests/drivers/mock/url_for_test.rb +0 -67
  288. data/tests/drivers/openstack/setup.rb +0 -20
  289. data/views/cimi/errors/400.html.haml +0 -41
  290. data/views/cimi/errors/401.html.haml +0 -41
  291. data/views/cimi/errors/404.html.haml +0 -29
  292. data/views/cimi/errors/405.html.haml +0 -29
  293. data/views/cimi/errors/502.html.haml +0 -43
  294. data/views/cimi/errors/backend_capability_failure.html.haml +0 -29
@@ -1,441 +0,0 @@
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
- require 'sinatra/base'
18
- require 'sinatra/url_for'
19
- require 'deltacloud/validation'
20
- require 'deltacloud/backend_capability'
21
-
22
- module Sinatra
23
-
24
- module Rabbit
25
-
26
- def self.routes
27
- @routes ||= []
28
- end
29
-
30
- class DuplicateParamException < Deltacloud::ExceptionHandler::DeltacloudException; end
31
- class DuplicateOperationException < Deltacloud::ExceptionHandler::DeltacloudException; end
32
- class DuplicateCollectionException < Deltacloud::ExceptionHandler::DeltacloudException; end
33
- class UnsupportedCollectionException < Deltacloud::ExceptionHandler::DeltacloudException
34
- def initialize
35
- # The server understood the request, but is refusing to fulfill it. Authorization will not help and the request
36
- # SHOULD NOT be repeated. If the request method was not HEAD and the server wishes to make public why the request
37
- # has not been fulfilled, it SHOULD describe the reason for the refusal in the entity. If the server does not wish
38
- # to make this information available to the client, the status code 404 (Not Found) can be used instead.
39
- super(403, 'UnsupportedCollection', "Requested collection is not supported for current provider", [])
40
- end
41
- end
42
-
43
- class Operation
44
- attr_reader :name, :method, :collection, :member
45
-
46
- include ::Deltacloud::BackendCapability
47
- include ::Deltacloud::Validation
48
- include ::ApplicationHelper
49
-
50
- STANDARD = {
51
- :new => { :method => :get, :member => false, :form => true },
52
- :index => { :method => :get, :member => false },
53
- :show => { :method => :get, :member => true },
54
- :create => { :method => :post, :member => false },
55
- :update => { :method => :put, :member => true },
56
- :destroy => { :method => :delete, :member => true }
57
- }
58
-
59
- def initialize(coll, name, opts, &block)
60
- @name = name.to_sym
61
- opts = STANDARD[@name].merge(opts) if standard?
62
- @path_generator = opts[:path_generator]
63
- @collection, @standard = coll, opts[:standard]
64
- raise "No method for operation #{name}" unless opts[:method]
65
- @method = opts[:method].to_sym
66
- @member = opts[:member]
67
- @description = ""
68
- instance_eval(&block) if block_given?
69
- generate_documentation
70
- generate_options
71
- end
72
-
73
- def http_method
74
- @method
75
- end
76
-
77
- def standard?
78
- STANDARD.keys.include?(name) || @standard
79
- end
80
-
81
- def form?
82
- STANDARD[name] and STANDARD[name][:form]
83
- end
84
-
85
- def description(text="")
86
- return @description if text.blank?
87
- @description = text
88
- end
89
-
90
- def generate_documentation
91
- coll, oper = @collection, self
92
- Rabbit::routes << [:get, "#{settings.root_url}/docs/#{@collection.name}/#{@name}"]
93
- ::Sinatra::Application.get("#{settings.root_url}/docs/#{@collection.name}/#{@name}") do
94
- @collection, @operation = coll, oper
95
- @features = driver.features_for_operation(coll.name, oper.name)
96
- respond_to do |format|
97
- format.html { haml :'docs/operation' }
98
- format.xml { haml :'docs/operation' }
99
- end
100
- end
101
- end
102
-
103
- def generate_options
104
- current_operation = self
105
- Rabbit::routes << [:options, "#{settings.root_url}/#{current_operation.collection.name}/#{current_operation.name}"]
106
- ::Sinatra::Application.options("#{settings.root_url}/#{current_operation.collection.name}/#{current_operation.name}") do
107
- required_params = current_operation.effective_params(driver).collect do |name, validation|
108
- name.to_s if validation.type.eql?(:required)
109
- end.compact.join(',')
110
- optional_params = current_operation.effective_params(driver).collect do |name, validation|
111
- name.to_s if validation.type.eql?(:optional)
112
- end.compact.join(',')
113
- headers 'X-Required-Parameters' => required_params
114
- headers 'X-Optional-Parameters' => optional_params
115
- [200, '']
116
- end
117
- end
118
-
119
- def control(&block)
120
- op = self
121
- @control = Proc.new do
122
- op.collection.check_supported(driver)
123
- op.check_capability(driver)
124
- op.validate(driver, op.effective_params(driver), params, credentials)
125
- instance_eval(&block)
126
- end
127
- end
128
-
129
- def member?
130
- if standard?
131
- @member || STANDARD[name][:member]
132
- else
133
- @member
134
- end
135
- end
136
-
137
- def path(args = {})
138
- return @path_generator.call(self) if @path_generator
139
- if member?
140
- if standard?
141
- "#{@collection.name}/:id"
142
- else
143
- "#{@collection.name}/:id/#{name}"
144
- end
145
- else
146
- if form?
147
- "#{@collection.name}/#{name}"
148
- else
149
- "#{@collection.name}"
150
- end
151
- end
152
- end
153
-
154
- def generate
155
- Rabbit::routes << [@method, "#{settings.root_url}/#{path}"]
156
- ::Sinatra::Application.send(@method, "#{settings.root_url}/#{path}", {}, &@control)
157
- # Set up some Rails-like URL helpers
158
- if name == :index
159
- gen_route "#{@collection.name}_url"
160
- elsif name == :show
161
- gen_route "#{@collection.name.to_s.singularize}_url"
162
- else
163
- gen_route "#{name}_#{@collection.name.to_s.singularize}_url"
164
- end
165
- end
166
-
167
- # Return a hash of all params, the params statically defined for this
168
- # operation plus the params defined by any features in the +driver+
169
- # that might modify this operation
170
- def effective_params(driver)
171
- driver.features(@collection.name).collect do |f|
172
- f.decl.operation(@name)
173
- end.flatten.select { |op| op }.inject(params.dup) do |result, fop|
174
- fop.params.each_key do |k|
175
- if result.has_key?(k)
176
- raise DuplicateParamException, "Parameter '#{k}' for operation #{fop.name} in collection #{@collection.name}"
177
- else
178
- result[k] = fop.params[k]
179
- end
180
- end
181
- result
182
- end
183
- end
184
-
185
- private
186
- def gen_route(name)
187
- route_url = path
188
- if @member
189
- ::Sinatra::Application.send(:define_method, name) do |id, *args|
190
- url = query_url(route_url, args[0])
191
- api_url_for url.gsub(/:id/, id.to_s), :full
192
- end
193
- else
194
- ::Sinatra::Application.send(:define_method, name) do |*args|
195
- url = query_url(route_url, args[0])
196
- api_url_for url, :full
197
- end
198
- end
199
- end
200
- end
201
-
202
- class Collection
203
- attr_reader :name, :operations, :subcollections
204
-
205
- def initialize(name, options={}, &block)
206
- @name = name
207
- @description = ""
208
- @operations, @subcollections = {}, {}
209
- @global = options[:global] || false
210
- instance_eval(&block) if block_given?
211
- generate_documentation
212
- generate_head
213
- generate_options
214
- end
215
-
216
- def subcollection?
217
- self.class == SubCollection
218
- end
219
-
220
- # Set/Return description for collection
221
- # If first parameter is not present, full description will be
222
- # returned.
223
- def description(text='')
224
- return @description if text.blank?
225
- @description = text
226
- end
227
-
228
- # Mark this collection as global, i.e. independent of any specific
229
- # driver
230
- def global!
231
- @global = true
232
- end
233
-
234
- # Return +true+ if this collection is global, i.e. independent of any
235
- # specific driver
236
- def global?
237
- @global
238
- end
239
-
240
- def generate_head
241
- current_collection = self
242
- Rabbit::routes << [:head, "#{settings.root_url}/#{name}"]
243
- ::Sinatra::Application.head("#{settings.root_url}/#{name}") do
244
- methods_allowed = current_collection.operations.collect { |o| o[1].method.to_s.upcase }.uniq.join(',')
245
- headers 'Allow' => "HEAD,OPTIONS,#{methods_allowed}"
246
- [200, '']
247
- end
248
- end
249
-
250
- def generate_options
251
- current_collection = self
252
- Rabbit::routes << [:options, "#{settings.root_url}/#{name}"]
253
- ::Sinatra::Application.options("#{settings.root_url}/#{name}") do
254
- operations_allowed = current_collection.operations.collect { |o| o[0] }.join(',')
255
- headers 'X-Operations-Allowed' => operations_allowed
256
- [200, '']
257
- end
258
- end
259
-
260
- def generate_documentation
261
- coll = self
262
- Rabbit::routes << [:get, "#{settings.root_url}/docs/#{@name}"]
263
- ::Sinatra::Application.get("#{settings.root_url}/docs/#{@name}") do
264
- coll.check_supported(driver)
265
- @collection = coll
266
- @operations = coll.operations
267
- @features = driver.features(coll.name)
268
- respond_to do |format|
269
- format.html { haml :'docs/collection' }
270
- format.xml { haml :'docs/collection' }
271
- end
272
- end
273
- end
274
-
275
- # Add a new operation for this collection. For the standard REST
276
- # operations :index, :show, :update, and :destroy, we already know
277
- # what method to use and whether this is an operation on the URL for
278
- # individual elements or for the whole collection.
279
- #
280
- # For non-standard operations, options must be passed:
281
- # :method : one of the HTTP methods
282
- # :member : whether this is an operation on the collection or an
283
- # individual element (FIXME: custom operations on the
284
- # collection will use a nonsensical URL) The URL for the
285
- # operation is the element URL with the name of the operation
286
- # appended
287
- #
288
- # This also defines a helper method like show_instance_url that returns
289
- # the URL to this operation (in request context)
290
- def operation(name, opts = {}, &block)
291
- if @operations.keys.include?(name)
292
- raise DuplicateOperationException::new(500, "DuplicateOperation", "Operation #{name} is already defined", [])
293
- end
294
- @operations[name] = Operation.new(self, name, opts, &block)
295
- end
296
-
297
- def collection(name, opts={}, &block)
298
- if subcollections.keys.include?(name)
299
- raise DuplicateOperationException::new(500, "DuplicateSubcollection", "Subcollection #{name} is already defined", [])
300
- end
301
- subcollections[name] = SubCollection.new(self, name, opts, &block)
302
- subcollections[name].generate
303
- end
304
-
305
- def generate
306
- operations.values.reject { |op| op.member }.each { |o| o.generate }
307
- operations.values.select { |op| op.member }.each { |o| o.generate }
308
- app = ::Sinatra::Application
309
- collname = name # Work around Ruby's weird scoping/capture
310
- app.send(:define_method, "#{name.to_s.singularize}_url") do |id|
311
- api_url_for "#{collname}/#{id}", :full
312
- end
313
- if index_op = operations[:index]
314
- app.send(:define_method, "#{name}_url") do
315
- api_url_for index_op.path.gsub(/\/\?$/,''), :full
316
- end
317
- end
318
- end
319
-
320
- def check_supported(driver)
321
- unless global? || driver.has_collection?(@name) || self.kind_of?(Sinatra::Rabbit::SubCollection)
322
- raise UnsupportedCollectionException
323
- end
324
- end
325
- end
326
-
327
- class SubCollection < Collection
328
-
329
- attr_accessor :parent
330
-
331
- def initialize(parent, name, opts={}, &block)
332
- self.parent = parent
333
- super(name, &block)
334
- end
335
-
336
- def operation(name, opts = {}, &block)
337
- if @operations.keys.include?(name)
338
- raise DuplicateOperationException::new(500, "DuplicateOperation", "Operation #{name} is already defined", [])
339
- end
340
- # Preserve self as local variable to workaround Ruby namespace
341
- # weirdness
342
- c = self
343
- path_generator = Proc.new do |obj|
344
- if obj.member?
345
- if obj.standard?
346
- "#{parent.name}/:#{parent.name.to_s.singularize}/:#{c.name.to_s.singularize}"
347
- else
348
- "#{parent.name}/:#{parent.name.to_s.singularize}/:#{c.name.to_s.singularize}/#{name}"
349
- end
350
- else
351
- if obj.form?
352
- "#{parent.name}/:id/:#{parent.name.to_s.singularize}/#{obj.name}"
353
- else
354
- "#{parent.name}/:#{parent.name.to_s.singularize}"
355
- end
356
- end
357
- end
358
- opts.merge!({
359
- :path_generator => path_generator
360
- })
361
- @operations[name] = Operation.new(self, name, opts, &block)
362
- end
363
-
364
- def generate
365
- operations.values.reject { |op| op.member }.each { |o| o.generate }
366
- operations.values.select { |op| op.member }.each { |o| o.generate }
367
- app = ::Sinatra::Application
368
- collname = name # Work around Ruby's weird scoping/capture
369
- app.send(:define_method, "#{parent.name.to_s}_#{name.to_s.singularize}_url") do |id, subid|
370
- api_url_for "#{collname}/#{id}/#{subid}", :full
371
- end
372
- if index_op = operations[:index]
373
- app.send(:define_method, "#{parent.name.to_s}_#{name}_url") do
374
- api_url_for index_op.path.gsub(/\/\?$/,''), :full
375
- end
376
- end
377
- end
378
-
379
- end
380
-
381
- def collections
382
- @collections ||= {}
383
- end
384
-
385
- # Create a new collection. NAME should be the pluralized name of the
386
- # collection.
387
- #
388
- # Adds a helper method #{name}_url which returns the URL to the :index
389
- # operation on this collection.
390
- def collection(name, &block)
391
- raise DuplicateCollectionException if collections[name]
392
- collections[name] = Collection.new(name, &block)
393
- collections[name].generate
394
- end
395
-
396
- def global_collection(name, &block)
397
- raise DuplicateCollectionException if collections[name]
398
- collections[name] = Collection.new(name, { :global => true }, &block)
399
- collections[name].generate
400
- end
401
-
402
- # Make sure this collection can be accessed, regardless of whether the
403
- # driver supports it or not
404
- def global_collection(name, &block)
405
- raise DuplicateCollectionException if collections[name]
406
- collections[name] = Collection.new(name, :global => true, &block)
407
- collections[name].generate
408
- end
409
- end
410
-
411
- module RabbitHelper
412
- def query_url(url, params)
413
- return url if params.nil? || params.empty?
414
- url + "?#{URI.escape(params.collect{|k,v| "#{k}=#{v}"}.join('&'))}"
415
- end
416
-
417
- def entry_points
418
- collections.values.select { |coll|
419
- coll.global? || driver.has_collection?(coll.name)
420
- }.inject([]) do |m, coll|
421
- url = api_url_for coll.operations[:index].path, :full
422
- m << [ coll.name, url ]
423
- end
424
- end
425
- end
426
-
427
- register Rabbit
428
- helpers RabbitHelper
429
- end
430
-
431
- # In Sinatra < 1.2 there was no helper to create OPTIONS route
432
- unless Sinatra::Base.respond_to? :options
433
- configure do
434
- class << Sinatra::Base
435
- def options(path, opts={}, &block)
436
- route 'OPTIONS', path, opts, &block
437
- end
438
- end
439
- Sinatra::Delegator.delegate :options
440
- end
441
- end
@@ -1,47 +0,0 @@
1
- # Copyright (c) 2008 The Committers
2
-
3
- # Permission is hereby granted, free of charge, to any person obtaining a copy
4
- # of this software and associated documentation files (the "Software"), to
5
- # deal in the Software without restriction, including without limitation the
6
- # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7
- # sell copies of the Software, and to permit persons to whom the Software is
8
- # furnished to do so, subject to the following conditions:
9
- #
10
- # The above copyright notice and this permission notice shall be included in
11
- # all copies or substantial portions of the Software.
12
- #
13
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16
- # THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
- # IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
- # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19
-
20
- module Rack
21
- # Sets an "X-Runtime" response header, indicating the response
22
- # time of the request, in seconds
23
- #
24
- # You can put it right before the application to see the processing
25
- # time, or before all the other middlewares to include time for them,
26
- # too.
27
- class Runtime
28
- def initialize(app, name = nil)
29
- @app = app
30
- @header_name = "X-Runtime"
31
- @header_name << "-#{name}" if name
32
- end
33
-
34
- def call(env)
35
- start_time = Time.now
36
- status, headers, body = @app.call(env)
37
- request_time = Time.now - start_time
38
-
39
- if !headers.has_key?(@header_name)
40
- headers[@header_name] = "%0.6f" % request_time
41
- end
42
-
43
- [status, headers, body]
44
- end
45
- end
46
- end
47
-