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/08 21:27:56 UTC

[PATCH 0/2] Rudimentary API capability reporting

This patch adds /api/capabilities, which currently lists the operations
available in the collection. The next step would be for the client to parse
and use this info.

Tobias Crawley (2):
  Move actual capability test out to its own method.
  Added simple capability reporting to the API.

 server/lib/deltacloud/backend_capability.rb |    6 ++++-
 server/server.rb                            |   29 +++++++++++++++++++++++++-
 server/views/api/capabilities.html.haml     |   14 +++++++++++++
 server/views/api/capabilities.xml.haml      |    6 +++++
 server/views/api/show.html.haml             |    4 +-
 server/views/api/show.xml.haml              |    2 +-
 6 files changed, 55 insertions(+), 6 deletions(-)
 create mode 100644 server/views/api/capabilities.html.haml
 create mode 100644 server/views/api/capabilities.xml.haml

-- 
1.7.2.3


Re: [PATCH 2/2] Added simple capability reporting to the API.

Posted by Michal Fojtik <mf...@redhat.com>.
On 08/11/10 15:27 -0500, Tobias Crawley wrote:
>---
> server/server.rb                        |   29 +++++++++++++++++++++++++++--
> server/views/api/capabilities.html.haml |   14 ++++++++++++++
> server/views/api/capabilities.xml.haml  |    6 ++++++
> server/views/api/show.html.haml         |    4 ++--
> server/views/api/show.xml.haml          |    2 +-
> 5 files changed, 50 insertions(+), 5 deletions(-)
> create mode 100644 server/views/api/capabilities.html.haml
> create mode 100644 server/views/api/capabilities.xml.haml
>
>diff --git a/server/server.rb b/server/server.rb
>index b8720a9..9a4dcda 100644
>--- a/server/server.rb
>+++ b/server/server.rb
>@@ -28,6 +28,8 @@ end
> # whatever you want (eg. if you running API behind NAT)
> HOSTNAME=ENV['API_HOST'] ? ENV['API_HOST'] : nil
>
>+API_VERSION = 0.1
>+
> error Deltacloud::Validation::Failure do
>   report_error(400, "validation_failure")
> end
>@@ -49,7 +51,6 @@ Sinatra::Application.register Sinatra::RespondTo
> get '/' do redirect url_for('/api'); end
>
> get '/api\/?' do
>-    @version = 0.1
>     if params[:force_auth]
>       return [401, 'Authentication failed'] unless driver.valid_credentials?(credentials)
>     end
>@@ -57,7 +58,7 @@ get '/api\/?' do
>         format.xml { haml :"api/show" }
>         format.json do
>           { :api => {
>-            :version => @version,
>+            :version => API_VERSION,
>             :driver => DRIVER,
>             :links => entry_points.collect { |l| { :rel => l[0], :href => l[1]} }
>             }
>@@ -67,6 +68,30 @@ get '/api\/?' do
>     end
> end
>
>+get '/api/capabilities' do
>+  @capabilities = { }
>+  collections.values.each do |collection|
>+    collection_capabilities = []
>+    collection.operations.each do |name, operation|
>+      collection_capabilities << name if operation.has_capability?(driver)
>+    end
>+    @capabilities[collection.name] = collection_capabilities
>+  end
>+
>+  respond_to do |format|
>+    format.xml { haml :"api/capabilities" }
>+    format.json do
>+      { :api => {
>+          :version => API_VERSION,
>+          :driver => DRIVER,
>+          :capabilities => @capabilities
>+        }
>+      }.to_json
>+    end
>+    format.html { haml :"api/capabilities" }
>+  end
>+end
>+
> # Rabbit DSL
>
> collection :realms do
>diff --git a/server/views/api/capabilities.html.haml b/server/views/api/capabilities.html.haml
>new file mode 100644
>index 0000000..77f2291
>--- /dev/null
>+++ b/server/views/api/capabilities.html.haml
>@@ -0,0 +1,14 @@
>+%h1
>+  API v#{API_VERSION} Capabilities for #{DRIVER}
>+
>+%ul
>+  - @capabilities.keys.sort_by { |k| k.to_s }.each do |key|
>+    %li
>+      = link_to key.to_s.gsub('_', ' ').titlecase, url_for("/api/#{key}")
>+      %dl
>+        - @capabilities[key].each do |capability|
>+          %dt
>+            = capability
>+  %li
>+    %strong
>+      %a{:href => url_for("/api/docs")} Documentation (v#{API_VERSION})
>diff --git a/server/views/api/capabilities.xml.haml b/server/views/api/capabilities.xml.haml
>new file mode 100644
>index 0000000..01dea85
>--- /dev/null
>+++ b/server/views/api/capabilities.xml.haml
>@@ -0,0 +1,6 @@
>+%api_capabilities{ :version=>API_VERSION, :driver=>DRIVER }

Could you keep <api> root tag here ?
Like having:

<api driver="mock" version="nn">
  <capabilities>
   <link...>
     <capability...>
   </link>

Or just cut 'api_capabilities' to 'capabilities. But this is not a big
issue.

>+  - @capabilities.keys.each do |key|
>+    %link{ :rel => key, :href => url_for("/api/#{key}") }
>+      - @capabilities[key].each do |capability|
>+        %capability{ :name => capability }
>+
>diff --git a/server/views/api/show.html.haml b/server/views/api/show.html.haml
>index 0077972..fb0af93 100644
>--- a/server/views/api/show.html.haml
>+++ b/server/views/api/show.html.haml
>@@ -1,5 +1,5 @@
> %h1
>-  API v#{@version}
>+  API v#{API_VERSION}
>
> %ul
>   - collections.keys.sort_by { |k| k.to_s }.each do |key|
>@@ -12,4 +12,4 @@
>             = op
>   %li
>     %strong
>-      %a{:href => url_for("/api/docs")} Documentation (v#{@version})
>+      %a{:href => url_for("/api/docs")} Documentation (v#{API_VERSION})
>diff --git a/server/views/api/show.xml.haml b/server/views/api/show.xml.haml
>index 70c26c9..d01d80b 100644
>--- a/server/views/api/show.xml.haml
>+++ b/server/views/api/show.xml.haml
>@@ -1,4 +1,4 @@
>-%api{ :version=>@version, :driver=>DRIVER }
>+%api{ :version=>API_VERSION, :driver=>DRIVER }
>   - for entry_point in entry_points
>     %link{ :rel=>entry_point[0], :href=>entry_point[1] }
>       - for feature in driver.features(entry_point[0])
>--
>1.7.2.3
>

Patch looks good to me except some whitespaces. 
ACK from my side. However I would like to know if anyone else could review this
patch (David?) before push.

  -- Michal

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

[PATCH 2/2] Added simple capability reporting to the API.

Posted by Tobias Crawley <tc...@redhat.com>.
---
 server/server.rb                        |   29 +++++++++++++++++++++++++++--
 server/views/api/capabilities.html.haml |   14 ++++++++++++++
 server/views/api/capabilities.xml.haml  |    6 ++++++
 server/views/api/show.html.haml         |    4 ++--
 server/views/api/show.xml.haml          |    2 +-
 5 files changed, 50 insertions(+), 5 deletions(-)
 create mode 100644 server/views/api/capabilities.html.haml
 create mode 100644 server/views/api/capabilities.xml.haml

diff --git a/server/server.rb b/server/server.rb
index b8720a9..9a4dcda 100644
--- a/server/server.rb
+++ b/server/server.rb
@@ -28,6 +28,8 @@ end
 # whatever you want (eg. if you running API behind NAT)
 HOSTNAME=ENV['API_HOST'] ? ENV['API_HOST'] : nil
 
+API_VERSION = 0.1
+
 error Deltacloud::Validation::Failure do
   report_error(400, "validation_failure")
 end
@@ -49,7 +51,6 @@ Sinatra::Application.register Sinatra::RespondTo
 get '/' do redirect url_for('/api'); end
 
 get '/api\/?' do
-    @version = 0.1
     if params[:force_auth]
       return [401, 'Authentication failed'] unless driver.valid_credentials?(credentials)
     end
@@ -57,7 +58,7 @@ get '/api\/?' do
         format.xml { haml :"api/show" }
         format.json do
           { :api => {
-            :version => @version,
+            :version => API_VERSION,
             :driver => DRIVER,
             :links => entry_points.collect { |l| { :rel => l[0], :href => l[1]} }
             }
@@ -67,6 +68,30 @@ get '/api\/?' do
     end
 end
 
+get '/api/capabilities' do
+  @capabilities = { }
+  collections.values.each do |collection|
+    collection_capabilities = []
+    collection.operations.each do |name, operation|
+      collection_capabilities << name if operation.has_capability?(driver)
+    end
+    @capabilities[collection.name] = collection_capabilities
+  end
+  
+  respond_to do |format|
+    format.xml { haml :"api/capabilities" }
+    format.json do
+      { :api => {
+          :version => API_VERSION,
+          :driver => DRIVER,
+          :capabilities => @capabilities 
+        }
+      }.to_json
+    end
+    format.html { haml :"api/capabilities" }
+  end
+end
+
 # Rabbit DSL
 
 collection :realms do
diff --git a/server/views/api/capabilities.html.haml b/server/views/api/capabilities.html.haml
new file mode 100644
index 0000000..77f2291
--- /dev/null
+++ b/server/views/api/capabilities.html.haml
@@ -0,0 +1,14 @@
+%h1
+  API v#{API_VERSION} Capabilities for #{DRIVER}
+
+%ul
+  - @capabilities.keys.sort_by { |k| k.to_s }.each do |key|
+    %li
+      = link_to key.to_s.gsub('_', ' ').titlecase, url_for("/api/#{key}")
+      %dl
+        - @capabilities[key].each do |capability|
+          %dt
+            = capability
+  %li
+    %strong
+      %a{:href => url_for("/api/docs")} Documentation (v#{API_VERSION})
diff --git a/server/views/api/capabilities.xml.haml b/server/views/api/capabilities.xml.haml
new file mode 100644
index 0000000..01dea85
--- /dev/null
+++ b/server/views/api/capabilities.xml.haml
@@ -0,0 +1,6 @@
+%api_capabilities{ :version=>API_VERSION, :driver=>DRIVER }
+  - @capabilities.keys.each do |key|
+    %link{ :rel => key, :href => url_for("/api/#{key}") }
+      - @capabilities[key].each do |capability|
+        %capability{ :name => capability }
+
diff --git a/server/views/api/show.html.haml b/server/views/api/show.html.haml
index 0077972..fb0af93 100644
--- a/server/views/api/show.html.haml
+++ b/server/views/api/show.html.haml
@@ -1,5 +1,5 @@
 %h1
-  API v#{@version}
+  API v#{API_VERSION}
 
 %ul
   - collections.keys.sort_by { |k| k.to_s }.each do |key|
@@ -12,4 +12,4 @@
             = op
   %li
     %strong
-      %a{:href => url_for("/api/docs")} Documentation (v#{@version})
+      %a{:href => url_for("/api/docs")} Documentation (v#{API_VERSION})
diff --git a/server/views/api/show.xml.haml b/server/views/api/show.xml.haml
index 70c26c9..d01d80b 100644
--- a/server/views/api/show.xml.haml
+++ b/server/views/api/show.xml.haml
@@ -1,4 +1,4 @@
-%api{ :version=>@version, :driver=>DRIVER }
+%api{ :version=>API_VERSION, :driver=>DRIVER }
   - for entry_point in entry_points
     %link{ :rel=>entry_point[0], :href=>entry_point[1] }
       - for feature in driver.features(entry_point[0])
-- 
1.7.2.3


[PATCH 1/2] Move actual capability test out to its own method.

Posted by Tobias Crawley <tc...@redhat.com>.
---
 server/lib/deltacloud/backend_capability.rb |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)

diff --git a/server/lib/deltacloud/backend_capability.rb b/server/lib/deltacloud/backend_capability.rb
index bec8714..4e6b6a4 100644
--- a/server/lib/deltacloud/backend_capability.rb
+++ b/server/lib/deltacloud/backend_capability.rb
@@ -13,8 +13,12 @@ module Deltacloud::BackendCapability
     @capability = capability
   end
 
+  def has_capability?(backend)
+    !capability or backend.respond_to?(capability)
+  end
+  
   def check_capability(backend)
-    if capability and !backend.respond_to?(capability)
+    if !has_capability?(backend)
       raise Failure.new(capability, "#{capability} capability not supported by backend #{backend.class.name}")
     end
   end
-- 
1.7.2.3