incubator-deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@redhat.com
Subject [PATCH core 1/2] Advertising supported drivers and their entrypoints Advertising of credentials needed for backend authentication Drivers Hash moved to YAML configuration file
Date Fri, 18 Feb 2011 13:05:18 GMT
From: Michal Fojtik <mfojtik@redhat.com>

---
 server/config/rhevm_config.yml.EXAMPLE          |    1 -
 server/lib/deltacloud/drivers/ec2/ec2_driver.rb |   25 +-------
 server/lib/drivers.rb                           |   78 +++++++++++------------
 server/server.rb                                |    2 +
 server/tests/drivers/mock/api_test.rb           |   18 +++++-
 server/views/api/drivers.html.haml              |    2 +-
 server/views/api/drivers.xml.haml               |   11 +++-
 7 files changed, 67 insertions(+), 70 deletions(-)
 delete mode 100644 server/config/rhevm_config.yml.EXAMPLE

diff --git a/server/config/rhevm_config.yml.EXAMPLE b/server/config/rhevm_config.yml.EXAMPLE
deleted file mode 100644
index 4828b94..0000000
--- a/server/config/rhevm_config.yml.EXAMPLE
+++ /dev/null
@@ -1 +0,0 @@
-domain: demo
\ No newline at end of file
diff --git a/server/lib/deltacloud/drivers/ec2/ec2_driver.rb b/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
index 5083622..d40dfcb 100644
--- a/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
+++ b/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
@@ -515,34 +515,11 @@ module Deltacloud
           klass.new(credentials.user, credentials.password, {:server => endpoint_for_service(type),
:connection_mode => :per_thread})
         end
 
-        DEFAULT_SERVICE_ENDPOINTS = {
-          'ec2' => {
-            'ap-southeast-1' => 'ec2.ap-southeast-1.amazonaws.com',
-            'eu-west-1' => 'ec2.eu-west-1.amazonaws.com',
-            'us-east-1' => 'ec2.us-east-1.amazonaws.com',
-            'us-west-1' => 'ec2.us-west-1.amazonaws.com'
-          },
-          
-          'elb' => {
-            'ap-southeast-1' => 'elasticloadbalancing.ap-southeast-1.amazonaws.com',
-            'eu-west-1' => 'elasticloadbalancing.eu-west-1.amazonaws.com',
-            'us-east-1' => 'elasticloadbalancing.us-east-1.amazonaws.com',
-            'us-west-1' => 'elasticloadbalancing.us-west-1.amazonaws.com'
-          },
-
-          's3' => {
-            'us-east-1' => 's3.amazonaws.com',
-            'us-west-1' => 's3-us-west-1.amazonaws.com',
-            'ap-southeast-1' => 's3-ap-southeast-1.amazonaws.com',
-            'eu-west-1' => 's3-eu-west-1.amazonaws.com'
-          }
-        }
-
         def endpoint_for_service(service)
           endpoint = (Thread.current[:provider] || ENV['API_PROVIDER'] || DEFAULT_REGION)
           # return the endpoint if it does not map to a default endpoint, allowing
           # the endpoint to be a full hostname instead of a region.
-          DEFAULT_SERVICE_ENDPOINTS[service.to_s][endpoint] || endpoint
+          Deltacloud::Drivers::driver_config[:ec2][:entry_points][service.to_s][endpoint]
|| endpoint
         end
 
         def tag_instance(credentials, instance, name)
diff --git a/server/lib/drivers.rb b/server/lib/drivers.rb
index ab31627..d38bf95 100644
--- a/server/lib/drivers.rb
+++ b/server/lib/drivers.rb
@@ -18,56 +18,50 @@
 # FIXME: This should be moved into lib/ and be called Deltacloud::Drivers
 # or some such
 module Deltacloud
-  DRIVERS = {
-    :ec2 => { :name => "EC2" },
-	:sbc => { :name => "SBC" },
-    :rackspace => { :name => "Rackspace" },
-    :gogrid => { :name => "Gogrid" },
-    :rhevm => { :name => "RHEVM" },
-    :rimuhosting => { :name => "RimuHosting"},
-    :opennebula => { :name => "Opennebula", :class => "OpennebulaDriver" },
-    :terremark => { :name => "Terremark"},
-    :azure => { :name => "Azure" },
-    :mock => { :name => "Mock" }
-  }
 
-  DEFAULT_COLLECTIONS = [
-    :hardware_profiles,
-    :images,
-    :instances,
-    :instance_states,
-    :realms,
-    :storage_volumes,
-    :storage_snapshots
-  ]
+  module Drivers
 
-  DRIVER=ENV['API_DRIVER'] ? ENV['API_DRIVER'].to_sym : :mock
+    DEFAULT_COLLECTIONS = [
+      :hardware_profiles,
+      :images,
+      :instances,
+      :instance_states,
+      :realms,
+      :storage_volumes,
+      :storage_snapshots
+    ]
 
-  def driver_symbol
-    (Thread.current[:driver] || DRIVER).to_sym
-  end
+    DRIVER=ENV['API_DRIVER'] ? ENV['API_DRIVER'].to_sym : :mock
 
-  def driver_name
-    DRIVERS[:"#{driver_symbol}"][:name]
-  end
+    def driver_config
+      YAML::load(File.read(File.join(File.dirname(__FILE__), '..', 'config', 'drivers.yaml')))
+    end
 
-  def driver_class
-    basename = DRIVERS[:"#{driver_symbol}"][:class] || "#{driver_name}Driver"
-    Deltacloud::Drivers.const_get(driver_name).const_get(basename)
-  end
+    def driver_symbol
+      (Thread.current[:driver] || DRIVER).to_sym
+    end
 
-  def driver_source_name
-    File.join("deltacloud", "drivers", "#{driver_symbol}", "#{driver_symbol}_driver.rb")
-  end
+    def driver_name
+      driver_config[:"#{driver_symbol}"][:name]
+    end
 
-  def driver_mock_source_name
-    return File.join('deltacloud', 'drivers', "#{driver_symbol}",
-                     "#{driver_symbol}_driver.rb") if driver_name.eql? 'Mock'
-  end
+    def driver_class
+      basename = driver_config[:"#{driver_symbol}"][:class] || "#{driver_name}Driver"
+      Deltacloud::Drivers.const_get(driver_name).const_get(basename)
+    end
+
+    def driver_source_name
+      File.join("deltacloud", "drivers", "#{driver_symbol}", "#{driver_symbol}_driver.rb")
+    end
 
-  def driver
-    require driver_source_name
+    def driver_mock_source_name
+      return File.join('deltacloud', 'drivers', "#{driver_symbol}",
+		       "#{driver_symbol}_driver.rb") if driver_name.eql? 'Mock'
+    end
 
-    @driver ||= driver_class.new
+    def driver
+      require driver_source_name
+      @driver ||= driver_class.new
+    end
   end
 end
diff --git a/server/server.rb b/server/server.rb
index 3f092e4..4a8a869 100644
--- a/server/server.rb
+++ b/server/server.rb
@@ -33,6 +33,8 @@ require 'sinatra/rack_etag'
 
 set :version, '0.2.0'
 
+include Deltacloud::Drivers
+set :drivers, Proc.new { driver_config }
 
 use Rack::DriverSelect
 use Rack::ETag
diff --git a/server/tests/drivers/mock/api_test.rb b/server/tests/drivers/mock/api_test.rb
index 062b417..0657f72 100644
--- a/server/tests/drivers/mock/api_test.rb
+++ b/server/tests/drivers/mock/api_test.rb
@@ -67,11 +67,27 @@ module DeltacloudUnitTest
       end
     end
 
-    def test_it_repond_to_head
+    def test_it_respond_to_head
       head '/api/instances'
       last_response.headers['Allow'].should_not == nil
       last_response.headers['Allow'].split(',').include?('HEAD').should == true
     end
 
+    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
+    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
== ""
+    end
+
   end
 end
diff --git a/server/views/api/drivers.html.haml b/server/views/api/drivers.html.haml
index 62b90ca..9eef8cf 100644
--- a/server/views/api/drivers.html.haml
+++ b/server/views/api/drivers.html.haml
@@ -7,7 +7,7 @@
       %th ID
       %th Name
   %tbody
-    - DRIVERS.each do |id, details|
+    - settings.drivers.each do |id, details|
       %tr
         %td{ :width => '20%' }
           %tt= id
diff --git a/server/views/api/drivers.xml.haml b/server/views/api/drivers.xml.haml
index 7beb9c8..adae310 100644
--- a/server/views/api/drivers.xml.haml
+++ b/server/views/api/drivers.xml.haml
@@ -1,6 +1,15 @@
 %api{ :version => settings.version }
   %drivers
-    - DRIVERS.each do |id, details|
+    - settings.drivers.each do |id, details|
       %driver{ :id => id }
         %name<
           =details[:name]
+        - if details[:username] or details[:password]
+          %credentials
+            %username<=cdata(details[:username])
+            %password<=cdata(details[:password])
+        - if details[:entrypoints]
+          - details[:entrypoints].each do |list_id, entrypoints|
+            %entrypoints{:id => list_id}
+              - entrypoints.each do |entrypoint, url|
+                %entrypoint{ :id => entrypoint }<=cdata(url)
-- 
1.7.4


Mime
View raw message