You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltacloud.apache.org by mf...@redhat.com on 2010/12/10 12:22:44 UTC

[PATCH core] Allow client to list supported drivers and regions

From: Michal Fojtik <mf...@redhat.com>

---
 client/lib/deltacloud.rb          |   52 +++++++++++++++++++++++++++++++++++++
 server/lib/drivers.rb             |    4 ++-
 server/views/api/drivers.xml.haml |    4 +++
 3 files changed, 59 insertions(+), 1 deletions(-)

diff --git a/client/lib/deltacloud.rb b/client/lib/deltacloud.rb
index 6ebd1db..18ccbd6 100644
--- a/client/lib/deltacloud.rb
+++ b/client/lib/deltacloud.rb
@@ -33,12 +33,44 @@ module DeltaCloud
   # @param [String, user_name] API user name
   # @param [String, password] API password
   # @param [String, user_name] API URL (eg. http://localhost:3001/api)
+  # @param [Hash, opts] Options (like :username, :password, :driver, :provider)
   # @return [DeltaCloud::API]
   def self.new(user_name, password, api_url, opts={}, &block)
     opts ||= {}
     API.new(user_name, password, api_url, opts, &block)
   end
 
+  # Connect to API without credentials
+  # This method is usefull for connecting to a core instance
+  # where you will set driver per_request
+  #
+  # @param [String, api_url] API url
+  # @param [Hash, opts] Options (like :username, :password, :driver, :provider)
+  # @return [DeltaCloud::API]
+  #
+  def self.connect(api_url, opts={}, &block)
+    opts ||= {}
+    API.new(nil, nil, api_url, opts, &block)
+  end
+
+  def self.drivers(api_url)
+    client = DeltaCloud::connect(api_url)
+    drivers = []
+    client.request(:get, "/drivers") do |body|
+      drivers_xml = Nokogiri::XML(body)
+      (drivers_xml/'/api/drivers/driver').each do |driver|
+        d = DeltaCloud::API::Driver.new(driver[:id], (driver/'name').text)
+        if (driver/"regions")
+          (driver/"regions/region").each do |region| 
+            d.add_region(region.text.strip)
+          end
+        end
+        drivers << d
+      end
+    end
+    drivers
+  end
+
   # Check given credentials if their are valid against
   # backend cloud provider
   #
@@ -318,6 +350,26 @@ module DeltaCloud
       end
     end
 
+    class Driver
+      attr_reader :id, :name, :regions
+
+      def initialize(id, name)
+        @id, @name = id, name
+        @regions = []
+      end
+
+      def add_region(region)
+        @regions << Region::new(region)
+      end
+
+      class Region
+        attr_reader :name
+        def initialize(name)
+          @name = name
+        end
+      end
+    end
+
     def handle_backend_error(response)
       raise BackendError.new(:message => (Nokogiri::XML(response)/'error/message').text)
     end
diff --git a/server/lib/drivers.rb b/server/lib/drivers.rb
index 420621c..95938c1 100644
--- a/server/lib/drivers.rb
+++ b/server/lib/drivers.rb
@@ -17,7 +17,9 @@
 
 module Deltacloud
   DRIVERS = {
-    :ec2 => { :name => "EC2" },
+    :ec2 => { :name => "EC2", :regions => [
+      :"us-west-1", :"us-east-1", :"eu", :"ap-southeast-1"
+    ] },
     :rackspace => { :name => "Rackspace" },
     :gogrid => { :name => "Gogrid" },
     :rhevm => { :name => "RHEVM" },
diff --git a/server/views/api/drivers.xml.haml b/server/views/api/drivers.xml.haml
index 7beb9c8..f3c6d15 100644
--- a/server/views/api/drivers.xml.haml
+++ b/server/views/api/drivers.xml.haml
@@ -4,3 +4,7 @@
       %driver{ :id => id }
         %name<
           =details[:name]
+        - if details[:regions]
+          %regions
+            - details[:regions].each do |region|
+              %region #{region}
-- 
1.7.3.2