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:43 UTC

Allow client to fetch drivers and regions

Hi,

This patch add two new methods to DeltaCloud class (client):

* DeltaCloud::connect(api_url, { :driver => '', :username => ''...}, &block)
  This will connect to API without credentials (and use HTTP header)

* DeltaCloud::drivers(api_url)
  This will return all supported drivers provided by API and also regions
  supported by each driver (EC2 for now as an example)


  -- Michal


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

Posted by mf...@redhat.com.
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


Re: Allow client to fetch drivers and regions

Posted by Michal Fojtik <mf...@redhat.com>.
On 10/12/10 07:10 -0500, Toby Crawley wrote:
>Michal:
>
>Should we use the more generic 'providers' at this level, instead of 
>'regions'? Not all drivers will support regions, but they may provide 
>some other separation concept.
>
>Also, you refer to the EU region as 'eu', instead of the full region 
>name ('eu-west-1'). In the AWS documentation, I've seen it referred 
>to both ways. I would rather use the full region name here, for 
>consistency.

Yes, that's a good point. Thank you for noticing me, dunno why I still have
'regions' in my mind.
I changed the patch and posted rev 2.

   -- Michal

>
>Toby
>
>On 12/10/2010 06:22 AM, mfojtik@redhat.com wrote:
>>Hi,
>>
>>This patch add two new methods to DeltaCloud class (client):
>>
>>* DeltaCloud::connect(api_url, { :driver =>  '', :username =>  ''...},&block)
>>   This will connect to API without credentials (and use HTTP header)
>>
>>* DeltaCloud::drivers(api_url)
>>   This will return all supported drivers provided by API and also regions
>>   supported by each driver (EC2 for now as an example)
>>
>>
>>   -- Michal
>>
>

-- 
--------------------------------------------------------
Michal Fojtik, mfojtik@redhat.com
Deltacloud API: http://deltacloud.org
--------------------------------------------------------

Re: Allow client to fetch drivers and regions

Posted by "marios@redhat.com" <ma...@redhat.com>.
Hi Toby


On 10/12/10 14:10, Toby Crawley wrote:
> Michal:
>
> Should we use the more generic 'providers' at this level, instead of
> 'regions'? Not all drivers will support regions, but they may provide
> some other separation concept.
>
> Also, you refer to the EU region as 'eu', instead of the full region
> name ('eu-west-1'). In the AWS documentation, I've seen it referred to
> both ways. I would rather use the full region name here, for consistency.


I think the confusion is because 'eu' *is* a valid location constraint 
for the s3 api 
http://docs.amazonwebservices.com/AmazonS3/latest/dev/index.html?RequestEndpoints.html 
  , whereas for ec2 you must specify eu-west-1 
http://docs.amazonwebservices.com/AWSEC2/2010-08-31/DeveloperGuide/index.html?Using_Endpoints.html#
- so thats nice and confusing :) I think in the end we're gonna have to 
do some string manipulation/juggling since we use the same driver for 
ec2 and s3


marios


>
> Toby
>
> On 12/10/2010 06:22 AM, mfojtik@redhat.com wrote:
>> Hi,
>>
>> This patch add two new methods to DeltaCloud class (client):
>>
>> * DeltaCloud::connect(api_url, { :driver => '', :username =>
>> ''...},&block)
>> This will connect to API without credentials (and use HTTP header)
>>
>> * DeltaCloud::drivers(api_url)
>> This will return all supported drivers provided by API and also regions
>> supported by each driver (EC2 for now as an example)
>>
>>
>> -- Michal
>>
>


Re: Allow client to fetch drivers and regions

Posted by Toby Crawley <tc...@redhat.com>.
Michal:

Should we use the more generic 'providers' at this level, instead of 'regions'? Not all drivers will support regions, but they may provide 
some other separation concept.

Also, you refer to the EU region as 'eu', instead of the full region name ('eu-west-1'). In the AWS documentation, I've seen it referred to 
both ways. I would rather use the full region name here, for consistency.

Toby

On 12/10/2010 06:22 AM, mfojtik@redhat.com wrote:
> Hi,
>
> This patch add two new methods to DeltaCloud class (client):
>
> * DeltaCloud::connect(api_url, { :driver =>  '', :username =>  ''...},&block)
>    This will connect to API without credentials (and use HTTP header)
>
> * DeltaCloud::drivers(api_url)
>    This will return all supported drivers provided by API and also regions
>    supported by each driver (EC2 for now as an example)
>
>
>    -- Michal
>