You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltacloud.apache.org by lu...@redhat.com on 2011/03/29 01:41:21 UTC

[PATCH 2/3] drivers: new collection

From: David Lutterkort <lu...@redhat.com>

---
 .../lib/deltacloud/helpers/application_helper.rb   |   14 ++++++
 server/server.rb                                   |   43 ++++++++++++++++---
 server/views/api/drivers.html.haml                 |   14 ------
 server/views/api/drivers.xml.haml                  |   11 -----
 server/views/api/show.html.haml                    |    2 +-
 server/views/drivers/index.html.haml               |   15 +++++++
 server/views/drivers/index.xml.haml                |    7 +++
 7 files changed, 73 insertions(+), 33 deletions(-)
 delete mode 100644 server/views/api/drivers.html.haml
 delete mode 100644 server/views/api/drivers.xml.haml
 create mode 100644 server/views/drivers/index.html.haml
 create mode 100644 server/views/drivers/index.xml.haml

diff --git a/server/lib/deltacloud/helpers/application_helper.rb b/server/lib/deltacloud/helpers/application_helper.rb
index 4ee0480..345aaff 100644
--- a/server/lib/deltacloud/helpers/application_helper.rb
+++ b/server/lib/deltacloud/helpers/application_helper.rb
@@ -204,4 +204,18 @@ module ApplicationHelper
     "#{text[0..(length/2)]}#{end_string}"
   end
 
+  # Reverse the entrypoints hash for a driver from drivers.yaml; note that
+  # +d+ is a hash, not an actual driver object
+  def driver_provider(d)
+    result = {}
+    if d[:entrypoints]
+      d[:entrypoints].each do |kind, details|
+        details.each do |prov, url|
+          result[prov] ||= {}
+          result[prov][kind] = url
+        end
+      end
+    end
+    result
+  end
 end
diff --git a/server/server.rb b/server/server.rb
index 3c4273c..d287031 100644
--- a/server/server.rb
+++ b/server/server.rb
@@ -83,17 +83,11 @@ Sinatra::Application.register Sinatra::RespondTo
 # Redirect to /api
 get '/' do redirect url_for('/api'), 301; end
 
-get '/api/drivers\/?' do
-  respond_to do |format|
-    format.xml { haml :"api/drivers" }
-    format.html { haml :"api/drivers" }
-  end
-end
-
 get '/api\/?' do
   if params[:force_auth]
     return [401, 'Authentication failed'] unless driver.valid_credentials?(credentials)
   end
+  @collections = [:drivers] + driver.supported_collections
   respond_to do |format|
     format.xml { haml :"api/show" }
     format.json do
@@ -110,6 +104,41 @@ end
 
 # Rabbit DSL
 
+collection :drivers do
+  global!
+
+  description <<EOS
+List all the drivers supported by this server.
+EOS
+
+  operation :index do
+    description "List all drivers"
+    control do
+      @drivers = settings.drivers
+      respond_to do |format|
+        format.xml { haml :"drivers/index" }
+        format.json { @drivers.to_json }
+        format.html { haml :"drivers/index" }
+      end
+    end
+  end
+
+  operation :show do
+    description "Show details for a driver"
+    param :id,      :string
+    control do
+      @name = params[:id].to_sym
+      @driver = settings.drivers[@name]
+      return [404, "Driver #{@name} not found"] unless @driver
+      respond_to do |format|
+        format.xml { haml :"drivers/show" }
+        format.json { @driver.to_json }
+        format.html { haml :"drivers/show" }
+      end
+    end
+  end
+end
+
 collection :realms do
   description <<END
   Within a cloud provider a realm represents a boundary containing resources.
diff --git a/server/views/api/drivers.html.haml b/server/views/api/drivers.html.haml
deleted file mode 100644
index 9eef8cf..0000000
--- a/server/views/api/drivers.html.haml
+++ /dev/null
@@ -1,14 +0,0 @@
-%h1
-  Available Drivers
-
-%table.display
-  %thead
-    %tr
-      %th ID
-      %th Name
-  %tbody
-    - settings.drivers.each do |id, details|
-      %tr
-        %td{ :width => '20%' }
-          %tt= id
-        %td= details[:name]
diff --git a/server/views/api/drivers.xml.haml b/server/views/api/drivers.xml.haml
deleted file mode 100644
index 93975f0..0000000
--- a/server/views/api/drivers.xml.haml
+++ /dev/null
@@ -1,11 +0,0 @@
-%api{ :version => settings.version }
-  %drivers
-    - settings.drivers.each do |id, details|
-      %driver{ :id => id }
-        %name<
-          =details[:name]
-        - if details[:entrypoints]
-          - details[:entrypoints].each do |list_id, entrypoints|
-            %entrypoints{:id => list_id}
-              - entrypoints.each do |entrypoint, url|
-                %entrypoint{ :id => entrypoint }<=cdata(url)
diff --git a/server/views/api/show.html.haml b/server/views/api/show.html.haml
index 199c9fd..287b989 100644
--- a/server/views/api/show.html.haml
+++ b/server/views/api/show.html.haml
@@ -2,7 +2,7 @@
   Deltacloud API #{settings.version}
 
 %ul
-  - driver.supported_collections.sort_by { |k| k.to_s }.each do |key|
+  - @collections.sort_by { |k| k.to_s }.each do |key|
     %li
       = link_to key.to_s.gsub('_', ' ').titlecase, url_for("/api/#{key}")
       %dl
diff --git a/server/views/drivers/index.html.haml b/server/views/drivers/index.html.haml
new file mode 100644
index 0000000..fc35115
--- /dev/null
+++ b/server/views/drivers/index.html.haml
@@ -0,0 +1,15 @@
+%h1
+  Available Drivers
+
+%table.display
+  %thead
+    %tr
+      %th ID
+      %th Name
+  %tbody
+    - @drivers.each do |id, details|
+      %tr
+        %td{ :width => '20%' }
+          %tt
+            %a{ :href => driver_url(id) }= id
+        %td= details[:name]
diff --git a/server/views/drivers/index.xml.haml b/server/views/drivers/index.xml.haml
new file mode 100644
index 0000000..63e809f
--- /dev/null
+++ b/server/views/drivers/index.xml.haml
@@ -0,0 +1,7 @@
+%drivers
+  - @drivers.each do |id, details|
+    %driver{ :href => driver_url(id), :id => id }
+      %name<
+        =details[:name]
+      - driver_provider(details).keys.each do |prov|
+        %provider{ :id => prov }
-- 
1.7.4