Skip to content

Instantly share code, notes, and snippets.

@tobias
Created November 3, 2010 17:45
Show Gist options
  • Save tobias/661415 to your computer and use it in GitHub Desktop.
Save tobias/661415 to your computer and use it in GitHub Desktop.
diff --git a/server/lib/deltacloud/backend_capability.rb b/server/lib/deltacloud/backend_capability.rb
new file mode 100644
index 0000000..bec8714
--- /dev/null
+++ b/server/lib/deltacloud/backend_capability.rb
@@ -0,0 +1,21 @@
+module Deltacloud::BackendCapability
+
+ class Failure < StandardError
+ attr_reader :capability
+ def initialize(capability, msg='')
+ super(msg)
+ @capability = capability
+ end
+ end
+
+ attr_reader :capability
+ def with_capability(capability)
+ @capability = capability
+ end
+
+ def check_capability(backend)
+ if capability and !backend.respond_to?(capability)
+ raise Failure.new(capability, "#{capability} capability not supported by backend #{backend.class.name}")
+ end
+ end
+end
diff --git a/server/views/errors/backend_capability_failure.html.haml b/server/views/errors/backend_capability_failure.html.haml
new file mode 100644
index 0000000..dc830d8
--- /dev/null
+++ b/server/views/errors/backend_capability_failure.html.haml
@@ -0,0 +1,11 @@
+%h1 Backend Capability Failure
+
+%p= @error.message
+
+%dl
+ %di
+ %dt Request URL
+ %dd= request.env['REQUEST_URI']
+ %di
+ %dt Capability
+ %dd= @error.capability
diff --git a/server/views/errors/backend_capability_failure.xml.haml b/server/views/errors/backend_capability_failure.xml.haml
new file mode 100644
index 0000000..83892fb
--- /dev/null
+++ b/server/views/errors/backend_capability_failure.xml.haml
@@ -0,0 +1,4 @@
+%error{:url => "#{request.env['REQUEST_URI']}", :status => "#{response.status}"}
+ %capability #{@error.capability}
+ %message #{@error.message}
+
diff --git a/server/lib/sinatra/rabbit.rb b/server/lib/sinatra/rabbit.rb
index f411268..94c74e3 100644
--- a/server/lib/sinatra/rabbit.rb
+++ b/server/lib/sinatra/rabbit.rb
@@ -1,6 +1,7 @@
require 'sinatra/base'
require 'sinatra/url_for'
require 'deltacloud/validation'
+require 'deltacloud/backend_capability'
module Sinatra
@@ -13,6 +14,7 @@ module Sinatra
class Operation
attr_reader :name, :method
+ include ::Deltacloud::BackendCapability
include ::Deltacloud::Validation
STANDARD = {
@@ -58,6 +60,7 @@ module Sinatra
def control(&block)
op = self
@control = Proc.new do
+ op.check_capability(driver)
op.validate(params)
instance_eval(&block)
end
diff --git a/server/server.rb b/server/server.rb
index af8bc09..7e226a8 100644
--- a/server/server.rb
+++ b/server/server.rb
@@ -32,6 +32,9 @@ error Deltacloud::Validation::Failure do
report_error(400, "validation_failure")
end
+error Deltacloud::BackendCapability::Failure do
+ report_error(405, "backend_capability_failure")
+end
error Deltacloud::AuthException do
report_error(403, "auth_exception")
end
@@ -324,6 +327,7 @@ collection :keys do
operation :index do
description "List all available credentials which could be used for instance authentication."
+ with_capability :keys
control do
filter_all :keys
end
@@ -331,18 +335,16 @@ collection :keys do
operation :show do
description "Show details about given instance credential."
+ with_capability :key
param :id, :string, :required
control { show :key }
end
operation :create do
description "Create a new instance credential if backend supports this."
+ with_capability :create_key
param :name, :string, :required
control do
- unless driver.respond_to?(:create_key)
- raise Deltacloud::BackendFeatureUnsupported.new('501',
- 'Creating instance credentials is not supported in backend')
- end
@key = driver.create_key(credentials, { :key_name => params[:name] })
respond_to do |format|
format.html { haml :"keys/show" }
@@ -353,12 +355,9 @@ collection :keys do
operation :destroy do
description "Destroy given instance credential if backend supports this."
+ with_capability :destroy_key
param :id, :string, :required
control do
- unless driver.respond_to?(:destroy_key)
- raise Deltacloud::BackendFeatureUnsupported.new('501',
- 'Creating instance credentials is not supported in backend')
- end
driver.destroy_key(credentials, { :key_name => params[:id]})
redirect(keys_url)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment