From: David Lutterkort <[email protected]>

---
 .../lib/deltacloud/helpers/application_helper.rb   |   14 ++++++
 server/server.rb                                   |   43 ++++++++++++++++---
 server/tests/drivers/mock/api_test.rb              |   16 +++++---
 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 +++
 server/views/drivers/show.html.haml                |   20 +++++++++
 server/views/drivers/show.xml.haml                 |    7 +++
 10 files changed, 110 insertions(+), 39 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
 create mode 100644 server/views/drivers/show.html.haml
 create mode 100644 server/views/drivers/show.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/tests/drivers/mock/api_test.rb 
b/server/tests/drivers/mock/api_test.rb
index 1b0fd55..be4c7ee 100644
--- a/server/tests/drivers/mock/api_test.rb
+++ b/server/tests/drivers/mock/api_test.rb
@@ -76,17 +76,21 @@ module DeltacloudUnitTest
     def test_it_expose_available_drivers
       do_xml_request '/api/drivers'
       last_response.status.should == 200
-      (last_xml_response/"api/drivers").length.should > 0
-      (last_xml_response/'api/drivers/driver').length.should > 0
+      (last_xml_response/"drivers").length.should > 0
+      (last_xml_response/'drivers/driver').length.should > 0
+      (last_xml_response/"drivers/driver[@id = 'mock']").length.should == 1
     end
 
     def test_it_expose_ec2_driver_entrypoints
       do_xml_request '/api/drivers'
       last_response.status.should == 200
-      (last_xml_response/"api/drivers").length.should > 0
-      
(last_xml_response/'api/drivers/driver[@id=ec2]/entrypoints').length.should > 0
-      
(last_xml_response/'api/drivers/driver[@id=ec2]/entrypoints/entrypoint').first[:id].should_not
 == nil
-      
(last_xml_response/'api/drivers/driver[@id=ec2]/entrypoints/entrypoint').first.text.should_not
 == ""
+      ec2 = (last_xml_response/'drivers/driver[@id=ec2]').first
+      (ec2/"provider").length.should > 0
+      (ec2/"provider[@id = 'eu-west-1']").length.should == 1
+      do_xml_request ec2[:href]
+      eu_west = (last_xml_response/"provider[@id = 'eu-west-1']").first
+      (eu_west/"entrypoint").length.should > 0
+      (eu_west/"entrypoint[@kind = 'ec2']").length.should == 1
     end
 
     def test_it_supports_matrix_params
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 }
diff --git a/server/views/drivers/show.html.haml 
b/server/views/drivers/show.html.haml
new file mode 100644
index 0000000..00f5d36
--- /dev/null
+++ b/server/views/drivers/show.html.haml
@@ -0,0 +1,20 @@
+%h1
+  Details for driver #{@name}
+
+%dl
+  %di
+    %dt ID
+    %dd= @name
+  %di
+    %dt Name
+    %dd= @driver[:name]
+  %di
+    %dt Provider
+    %dd
+      - providers = driver_provider(@driver).keys.collect { |k| k.to_s }.sort
+      - if providers.empty?
+        None
+      - else
+        %ol
+          - providers.each do |k|
+            %li= k
diff --git a/server/views/drivers/show.xml.haml 
b/server/views/drivers/show.xml.haml
new file mode 100644
index 0000000..ea5f508
--- /dev/null
+++ b/server/views/drivers/show.xml.haml
@@ -0,0 +1,7 @@
+%driver{ :href => driver_url(@name), :id => @name }
+  %name<
+    = @driver[:name]
+  - driver_provider(@driver).each do |prov, details|
+    %provider{ :id => prov }
+      - details.each do |kind, url|
+        %entrypoint{ :kind => kind }<=cdata(url)
-- 
1.7.4

Reply via email to