You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltacloud.apache.org by lu...@apache.org on 2010/07/09 01:21:46 UTC

svn commit: r962083 - in /incubator/deltacloud/trunk/client-ruby: lib/deltacloud.rb lib/models/realm.rb specs/realms_spec.rb

Author: lutter
Date: Thu Jul  8 23:21:45 2010
New Revision: 962083

URL: http://svn.apache.org/viewvc?rev=962083&view=rev
Log:
Adding support for realms to client.

Added:
    incubator/deltacloud/trunk/client-ruby/lib/models/realm.rb
    incubator/deltacloud/trunk/client-ruby/specs/realms_spec.rb
Modified:
    incubator/deltacloud/trunk/client-ruby/lib/deltacloud.rb

Modified: incubator/deltacloud/trunk/client-ruby/lib/deltacloud.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/client-ruby/lib/deltacloud.rb?rev=962083&r1=962082&r2=962083&view=diff
==============================================================================
--- incubator/deltacloud/trunk/client-ruby/lib/deltacloud.rb (original)
+++ incubator/deltacloud/trunk/client-ruby/lib/deltacloud.rb Thu Jul  8 23:21:45 2010
@@ -4,6 +4,7 @@ require 'logger'
 require 'rexml/document'
 
 require 'models/flavor'
+require 'models/realm'
 require 'models/image'
 require 'models/instance'
 require 'models/storage_volume'
@@ -63,6 +64,39 @@ class DeltaCloud
     nil
   end
 
+  def realms(opts={})
+    realms = []
+    request( entry_points[:realms], :get, opts ) do |response|
+      if ( response.is_a?( Net::HTTPSuccess ) )
+        doc = REXML::Document.new( response.body )
+        doc.get_elements( 'realms/realm' ).each do |realm|
+          uri = realm.attributes['href']
+          realms << Realm.new( self, uri, realm )
+        end
+      end
+    end
+    realms
+  end
+
+  def realm(id)
+    request( entry_points[:realms], :get, {:id=>id } ) do |response|
+      if ( response.is_a?( Net::HTTPSuccess ) )
+        doc = REXML::Document.new( response.body )
+        doc.get_elements( 'realms/realm' ).each do |realm|
+          uri = realm.attributes['href']
+          return Realm.new( self, uri, realm )
+        end
+      end
+    end
+    nil
+  end
+
+  def fetch_realm(uri)
+    xml = fetch_resource( :realm, uri )
+    return Realm.new( self, uri, xml ) if xml
+    nil
+  end
+
   def images(opts={})
     images = []
     request_path = entry_points[:images]

Added: incubator/deltacloud/trunk/client-ruby/lib/models/realm.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/client-ruby/lib/models/realm.rb?rev=962083&view=auto
==============================================================================
--- incubator/deltacloud/trunk/client-ruby/lib/models/realm.rb (added)
+++ incubator/deltacloud/trunk/client-ruby/lib/models/realm.rb Thu Jul  8 23:21:45 2010
@@ -0,0 +1,28 @@
+require 'models/base_model'
+
+class Realm < BaseModel
+
+  xml_tag_name :realm
+
+  attribute :name
+  attribute :state
+  attribute :limit
+
+  def initialize(client, uri, xml=nil)
+    super( client, uri, xml )
+  end
+
+  def load_payload(xml=nil)
+    super(xml)
+    unless xml.nil?
+      @name = xml.text( 'name' )
+      @state = xml.text( 'state' )
+      @limit = xml.text( 'limit' )
+      if ( @limit.nil? || @limit == '' )
+        @limit = :unlimited
+      else
+        @limit = @limit.to_f
+      end
+    end
+  end
+end

Added: incubator/deltacloud/trunk/client-ruby/specs/realms_spec.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/client-ruby/specs/realms_spec.rb?rev=962083&view=auto
==============================================================================
--- incubator/deltacloud/trunk/client-ruby/specs/realms_spec.rb (added)
+++ incubator/deltacloud/trunk/client-ruby/specs/realms_spec.rb Thu Jul  8 23:21:45 2010
@@ -0,0 +1,48 @@
+require 'specs/spec_helper'
+
+describe "realms" do
+
+  it_should_behave_like "all resources"
+
+  it "should allow retrieval of all realms" do
+    DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
+      realms = client.realms
+      realms.should_not be_empty
+      realms.each do |realm|
+        realm.uri.should_not be_nil
+        realm.uri.should be_a(String)
+        realm.id.should_not be_nil
+        realm.id.should be_a(String)
+        realm.name.should_not be_nil
+        realm.name.should be_a(String)
+      end
+    end
+  end 
+
+
+  it "should allow fetching a realm by id" do
+    DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
+      realm = client.realm( 'us' )
+      realm.should_not be_nil
+      realm.id.should eql( 'us' )
+      realm.name.should eql( 'United States' )
+      realm.state.should eql( 'AVAILABLE' )
+      realm.limit.should eql( :unlimited )
+      realm = client.realm( 'eu' )
+      realm.should_not be_nil
+      realm.id.should eql( 'eu' )
+      realm.name.should eql( 'Europe' )
+      realm.state.should eql( 'AVAILABLE' )
+      realm.limit.should eql( :unlimited )
+    end
+  end
+
+  it "should allow fetching a realm by URI" do
+    DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
+      realm = client.fetch_realm( API_URL + '/realms/us' )
+      realm.should_not be_nil
+      realm.id.should eql( 'us' )
+    end
+  end
+
+end