incubator-deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@redhat.com
Subject [PATCH core] Allow client to list supported drivers and regions
Date Fri, 10 Dec 2010 12:21:33 GMT
From: Michal Fojtik <mfojtik@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..3ab7cf7 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/"providers")
+          (driver/"providers/provider").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..70c92bf 100644
--- a/server/lib/drivers.rb
+++ b/server/lib/drivers.rb
@@ -17,7 +17,9 @@
 
 module Deltacloud
   DRIVERS = {
-    :ec2 => { :name => "EC2" },
+    :ec2 => { :name => "EC2", :providers => [
+      :"us-west-1", :"us-east-1", :"eu-west-1", :"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..9be71e5 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]
+          %providers
+            - details[:regions].each do |region|
+              %provider #{region}
-- 
1.7.3.2


Mime
View raw message