You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltacloud.apache.org by Tobias Crawley <tc...@redhat.com> on 2010/11/04 14:51:18 UTC

[PATCH 0/3] Add capability checking to rabbit and server.rb

This patchset adds backend capability checking to the rabbit dsl, 
allowing each operation to specify the required capability with:

with_capability :some_driver_method

If the driver does not respond to :some_driver_method, a 405 will
be returned to the client.

This has been ACK'ed by mfojtik via #deltacloud.

Tobias Crawley (3):
  Add support files for capability checking.
  Add capability checking to rabbit and to key management initially.
  Added capability checking for all operations.

 server/lib/deltacloud/backend_capability.rb        |   21 +++++++++++
 server/lib/sinatra/rabbit.rb                       |    3 ++
 server/server.rb                                   |   36 +++++++++++++++----
 .../errors/backend_capability_failure.html.haml    |   11 ++++++
 .../errors/backend_capability_failure.xml.haml     |    4 ++
 5 files changed, 67 insertions(+), 8 deletions(-)
 create mode 100644 server/lib/deltacloud/backend_capability.rb
 create mode 100644 server/views/errors/backend_capability_failure.html.haml
 create mode 100644 server/views/errors/backend_capability_failure.xml.haml

-- 
1.7.2.3


[PATCH 1/3] Add support files for capability checking.

Posted by Tobias Crawley <tc...@redhat.com>.
---
 server/lib/deltacloud/backend_capability.rb        |   21 ++++++++++++++++++++
 .../errors/backend_capability_failure.html.haml    |   11 ++++++++++
 .../errors/backend_capability_failure.xml.haml     |    4 +++
 3 files changed, 36 insertions(+), 0 deletions(-)
 create mode 100644 server/lib/deltacloud/backend_capability.rb
 create mode 100644 server/views/errors/backend_capability_failure.html.haml
 create mode 100644 server/views/errors/backend_capability_failure.xml.haml

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}
+
-- 
1.7.2.3


[PATCH 3/3] Added capability checking for all operations.

Posted by Tobias Crawley <tc...@redhat.com>.
---
 server/server.rb |   21 +++++++++++++++++++++
 1 files changed, 21 insertions(+), 0 deletions(-)

diff --git a/server/server.rb b/server/server.rb
index 7e226a8..b8720a9 100644
--- a/server/server.rb
+++ b/server/server.rb
@@ -85,6 +85,7 @@ END
     Operation will list all available realms. Realms can be filtered using
     the "architecture" parameter.
 END
+    with_capability :realms
     param :id,            :string
     param :architecture,  :string,  :optional,  [ 'i386', 'x86_64' ]
     control { filter_all(:realms) }
@@ -93,6 +94,7 @@ END
   #FIXME: It always shows whole list
   operation :show do
     description 'Show an realm identified by "id" parameter.'
+    with_capability :realm
     param :id,           :string, :required
     control { show(:realm) }
   end
@@ -111,6 +113,7 @@ END
     available to the current use. Images can be filtered using the
     "owner_id" and "architecture" parameters.
 END
+    with_capability :images
     param :id,            :string
     param :architecture,  :string,  :optional
     control { filter_all(:images) }
@@ -118,6 +121,7 @@ END
 
   operation :show do
     description 'Show an image identified by "id" parameter.'
+    with_capability :image
     param :id,           :string, :required
     control { show(:image) }
   end
@@ -184,6 +188,7 @@ END
 
   operation :index do
     description "List all instances."
+    with_capability :instances
     param :id,            :string,  :optional
     param :state,         :string,  :optional
     control { filter_all(:instances) }
@@ -191,12 +196,14 @@ END
 
   operation :show do
     description 'Show an instance identified by "id" parameter.'
+    with_capability :instance
     param :id,           :string, :required
     control { show(:instance) }
   end
 
   operation :create do
     description "Create a new instance."
+    with_capability :create_instance
     param :image_id,     :string, :required
     param :realm_id,     :string, :optional
     param :hwp_id,       :string, :optional
@@ -220,24 +227,28 @@ END
 
   operation :reboot, :method => :post, :member => true do
     description "Reboot a running instance."
+    with_capability :reboot_instance
     param :id,           :string, :required
     control { instance_action(:reboot) }
   end
 
   operation :start, :method => :post, :member => true do
     description "Start an instance."
+    with_capability :start_instance
     param :id,           :string, :required
     control { instance_action(:start) }
   end
 
   operation :stop, :method => :post, :member => true do
     description "Stop a running instance."
+    with_capability :stop_instance
     param :id,           :string, :required
     control { instance_action(:stop) }
   end
 
   operation :destroy do
     description "Destroy an instance."
+    with_capability :destroy_instance
     param :id,           :string, :required
     control { instance_action(:destroy) }
   end
@@ -253,6 +264,7 @@ END
 
   operation :index do
     description "List of available hardware profiles."
+    with_capability :hardware_profiles
     param :id,          :string
     param :architecture,  :string,  :optional,  [ 'i386', 'x86_64' ]
     control do
@@ -267,6 +279,7 @@ END
 
   operation :show do
     description "Show specific hardware profile."
+    with_capability :hardware_profile
     param :id,          :string,    :required
     control do
       @profile =  driver.hardware_profile(credentials, params[:id])
@@ -289,12 +302,14 @@ collection :storage_snapshots do
 
   operation :index do
     description "List of storage snapshots."
+    with_capability :storage_snapshots
     param :id,            :string
     control { filter_all(:storage_snapshots) }
   end
 
   operation :show do
     description "Show storage snapshot."
+    with_capability :storage_snapshot
     param :id,          :string,    :required
     control { show(:storage_snapshot) }
   end
@@ -305,12 +320,14 @@ collection :storage_volumes do
 
   operation :index do
     description "List of storage volumes."
+    with_capability :storage_volumes
     param :id,            :string
     control { filter_all(:storage_volumes) }
   end
 
   operation :show do
     description "Show storage volume."
+    with_capability :storage_volume
     param :id,          :string,    :required
     control { show(:storage_volume) }
   end
@@ -397,6 +414,7 @@ collection :buckets do
 
   operation :index do
     description "List buckets associated with this account"
+    with_capability :buckets
     param :id,        :string
     param :name,      :string
     param :size,      :string
@@ -405,12 +423,14 @@ collection :buckets do
 
   operation :show do
     description "Show bucket"
+    with_capability :bucket
     param :id,        :string
     control { show(:bucket) }
   end
 
   operation :create do
     description "Create a new bucket (POST /api/buckets)"
+    with_capability :create_bucket
     param :name,      :string,    :required
     control do
       @bucket = driver.create_bucket(credentials, params[:name], params)
@@ -430,6 +450,7 @@ collection :buckets do
 
   operation :destroy do
     description "Delete a bucket by name - bucket must be empty"
+    with_capability :delete_bucket
     param :id,    :string,    :required
     control do
       driver.delete_bucket(credentials, params[:id], params)
-- 
1.7.2.3


[PATCH 2/3] Add capability checking to rabbit and to key management initially.

Posted by Tobias Crawley <tc...@redhat.com>.
---
 server/lib/sinatra/rabbit.rb |    3 +++
 server/server.rb             |   15 +++++++--------
 2 files changed, 10 insertions(+), 8 deletions(-)

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
-- 
1.7.2.3


Re: [PATCH 0/3] Add capability checking to rabbit and server.rb

Posted by Toby Crawley <tc...@redhat.com>.
On 11/04/2010 11:30 AM, Michal Fojtik wrote:
> On 04/11/10 09:51 -0400, Tobias Crawley wrote:
>> This patchset adds backend capability checking to the rabbit dsl,
>> allowing each operation to specify the required capability with:
>>
>> with_capability :some_driver_method
>>
>> If the driver does not respond to :some_driver_method, a 405 will
>> be returned to the client.
>>
>> This has been ACK'ed by mfojtik via #deltacloud.
>
> Yeah, actually I think this is an extra useful thing to have.
>
> Also I have in me mind that we should expose this in API somehow.
>
> Maybe create a new collection (/api/capabilities) and list all
> possible actions from a driver here. So client can pick up that and
> instead of trying executing some actions and getting 405 responses,
> client should 'prepare' and for example avoid creating methods that
> is not supported by backend driver.
> Does it make sense to you ?
>

Yes, that is a great idea. The only actions that don't have capabilities defined are the */new actions, and the /api/:bucket/:blob/* 
actions, as they don't use rabbit. I assume the */new actions aren't currently used by the client, and are only used for browser Deltacloud 
access, but the blob actions should probably be advertised.

Can you commit the changes that we have so far? I have storage volume patches to send next that depend on these changes. How are you 
currently tracking to-do tasks like adding /api/capabilities? Do you store them in a jira somewhere? I would be glad to help implement that, 
but won't be able to do it right away.

FYI: I have submitted by signed CLA to secretary@apache.org.

- Toby



Re: [PATCH 0/3] Add capability checking to rabbit and server.rb

Posted by Michal Fojtik <mf...@redhat.com>.
On 04/11/10 09:51 -0400, Tobias Crawley wrote:
>This patchset adds backend capability checking to the rabbit dsl,
>allowing each operation to specify the required capability with:
>
>with_capability :some_driver_method
>
>If the driver does not respond to :some_driver_method, a 405 will
>be returned to the client.
>
>This has been ACK'ed by mfojtik via #deltacloud.

Yeah, actually I think this is an extra useful thing to have.

Also I have in me mind that we should expose this in API somehow.

Maybe create a new collection (/api/capabilities) and list all
possible actions from a driver here. So client can pick up that and
instead of trying executing some actions and getting 405 responses,
client should 'prepare' and for example avoid creating methods that
is not supported by backend driver.
Does it make sense to you ?

  -- Michal

>
>Tobias Crawley (3):
>  Add support files for capability checking.
>  Add capability checking to rabbit and to key management initially.
>  Added capability checking for all operations.
>
> server/lib/deltacloud/backend_capability.rb        |   21 +++++++++++
> server/lib/sinatra/rabbit.rb                       |    3 ++
> server/server.rb                                   |   36 +++++++++++++++----
> .../errors/backend_capability_failure.html.haml    |   11 ++++++
> .../errors/backend_capability_failure.xml.haml     |    4 ++
> 5 files changed, 67 insertions(+), 8 deletions(-)
> create mode 100644 server/lib/deltacloud/backend_capability.rb
> create mode 100644 server/views/errors/backend_capability_failure.html.haml
> create mode 100644 server/views/errors/backend_capability_failure.xml.haml
>
>--
>1.7.2.3
>

-- 
--------------------------------------------------------
Michal Fojtik, mfojtik@redhat.com
Deltacloud API: http://deltacloud.org
--------------------------------------------------------