deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@redhat.com
Subject [PATCH core 4/6] Client: Added .providers method on driver objects. Client can now retrieve list of configured API_PROVIDERS for active driver.
Date Tue, 24 Jan 2012 11:31:12 GMT
From: Michal Fojtik <mfojtik@redhat.com>

Signed-off-by: Michal fojtik <mfojtik@redhat.com>
---
 client/lib/base_object.rb       |   18 ++++++++++++++++++
 client/lib/deltacloud.rb        |   10 ++++++++--
 server/lib/deltacloud/server.rb |    4 +++-
 3 files changed, 29 insertions(+), 3 deletions(-)

diff --git a/client/lib/base_object.rb b/client/lib/base_object.rb
index 1af93f5..fb9738f 100644
--- a/client/lib/base_object.rb
+++ b/client/lib/base_object.rb
@@ -90,6 +90,18 @@ module DeltaCloud
           :values => value
         }
       end
+
+      def add_provider!(provider_id, entrypoints)
+        @providers ||= []
+        @providers << {
+          provider_id.intern => entrypoints.map { |e| { :kind => e[:kind], :url =>
e.text } }
+        }
+        @objects << {
+          :type => :collection,
+          :method_name => 'providers',
+          :values => @providers
+        }
+      end
       
 
       # This method define collection of text elements inside REST model
@@ -135,6 +147,12 @@ module DeltaCloud
         # First of all search throught array for method name
         m = search_for_method(method_name)
         if m.nil?
+          if method_name == :"valid_provider?"
+            return providers.any? { |p| p.keys.include? args.first.to_sym }
+          end
+          if method_name == :"valid_provider_url?"
+            return providers.map { |p| !p.find { |k, v| v.find { |u| u[:url] == args.first
} }.nil? }
+          end
           super
         else
           # Call appropriate handler for method
diff --git a/client/lib/deltacloud.rb b/client/lib/deltacloud.rb
index 6fec8bb..81b7d3b 100644
--- a/client/lib/deltacloud.rb
+++ b/client/lib/deltacloud.rb
@@ -239,6 +239,11 @@ module DeltaCloud
           obj.add_authentication!(attribute[:type], (attribute/'*')) && next
         end
 
+        #deal with providers
+        if(attribute.name == 'provider')
+          obj.add_provider!(attribute.attributes['id'].value, (attribute/'entrypoint')) &&
next
+        end
+
         # Deal with collections like public-addresses, private-addresses
         if (attribute/'./*').length > 0
           obj.add_collection!(attribute.name, (attribute/'*').collect { |value| value.text
}) && next
@@ -362,7 +367,7 @@ module DeltaCloud
       if conf[:method].eql?(:post)
         resource = RestClient::Resource.new(conf[:path], :open_timeout => conf[:open_timeout],
:timeout => conf[:timeout])
         resource.send(:post, conf[:form_data], default_headers.merge(extended_headers)) do
|response, request, block|
-          handle_backend_error(response) if [500, 502, 501].include? response.code
+          handle_backend_error(response) if [500, 502, 501, 401].include? response.code
           if response.respond_to?('body')
             yield response.body if block_given?
           else
@@ -372,7 +377,7 @@ module DeltaCloud
       else
         resource = RestClient::Resource.new(conf[:path], :open_timeout => conf[:open_timeout],
:timeout => conf[:timeout])
         resource.send(conf[:method], default_headers.merge(extended_headers)) do |response,
request, block|
-          handle_backend_error(response) if [500, 502, 501].include? response.code
+          handle_backend_error(response) if [500, 502, 501, 401].include? response.code
           if conf[:method].eql?(:get) and [301, 302, 307].include? response.code
             response.follow_redirection(request) do |response, request, block|
               if response.respond_to?('body')
@@ -397,6 +402,7 @@ module DeltaCloud
     class BackendError < StandardError
 
       def initialize(opts={})
+        opts[:message] = "Not authorized / Invalid credentials" if opts[:code] == 401
         super("#{opts[:code]} : #{opts[:message]}")
         set_backtrace(opts[:backtrace].split("\n").map { |l| l.strip }[0..10]) if opts[:backtrace]
       end
diff --git a/server/lib/deltacloud/server.rb b/server/lib/deltacloud/server.rb
index a6fac31..9801d60 100644
--- a/server/lib/deltacloud/server.rb
+++ b/server/lib/deltacloud/server.rb
@@ -163,7 +163,9 @@ EOS
     param :id,      :string
     control do
       @name = params[:id].to_sym
-      @providers = driver.providers(credentials) if driver.respond_to? :providers
+      if driver_symbol == @name
+        @providers = driver.providers(credentials)  if driver.respond_to? :providers
+      end
       @driver = settings.drivers[@name]
       return [404, "Driver #{@name} not found"] unless @driver
       respond_to do |format|
-- 
1.7.4.4


Mime
View raw message