incubator-deltacloud-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lut...@apache.org
Subject svn commit: r962005 - in /incubator/deltacloud/trunk/client-ruby: .gitignore lib/deltacloud.rb specs/discovery_spec.rb specs/flavors_spec.rb specs/images_spec.rb specs/spec_helper.rb
Date Thu, 08 Jul 2010 23:16:20 GMT
Author: lutter
Date: Thu Jul  8 23:16:20 2010
New Revision: 962005

URL: http://svn.apache.org/viewvc?rev=962005&view=rev
Log:
Add support for fetching image collection.

Added:
    incubator/deltacloud/trunk/client-ruby/specs/images_spec.rb
Removed:
    incubator/deltacloud/trunk/client-ruby/specs/discovery_spec.rb
Modified:
    incubator/deltacloud/trunk/client-ruby/.gitignore
    incubator/deltacloud/trunk/client-ruby/lib/deltacloud.rb
    incubator/deltacloud/trunk/client-ruby/specs/flavors_spec.rb
    incubator/deltacloud/trunk/client-ruby/specs/spec_helper.rb

Modified: incubator/deltacloud/trunk/client-ruby/.gitignore
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/client-ruby/.gitignore?rev=962005&r1=962004&r2=962005&view=diff
==============================================================================
--- incubator/deltacloud/trunk/client-ruby/.gitignore (original)
+++ incubator/deltacloud/trunk/client-ruby/.gitignore Thu Jul  8 23:16:20 2010
@@ -1 +1,2 @@
 *.swp
+credentials.yml

Modified: incubator/deltacloud/trunk/client-ruby/lib/deltacloud.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/client-ruby/lib/deltacloud.rb?rev=962005&r1=962004&r2=962005&view=diff
==============================================================================
--- incubator/deltacloud/trunk/client-ruby/lib/deltacloud.rb (original)
+++ incubator/deltacloud/trunk/client-ruby/lib/deltacloud.rb Thu Jul  8 23:16:20 2010
@@ -24,19 +24,36 @@ class DeltaCloud
     block.call( self ) if block
   end
 
-  def flavors(opts={}, &block)
+  def flavors(opts={})
     flavors = []
     request( entry_points[:flavors] ) do |response|
       if ( response.is_a?( Net::HTTPSuccess ) )
         doc = REXML::Document.new( response.body )
         doc.get_elements( 'flavors/flavor' ).each do |flavor|
-          flavors << convert_to_hash( flavor )
+          flavors << convert( :flavor, flavor )
         end
       end
     end
     flavors 
   end
 
+  def images(opts={})
+    images = []
+    request_path = entry_points[:images]
+    if ( opts[:owner] )
+      request_path += "?owner=#{opts[:owner]}"
+    end
+    request( request_path ) do |response|
+      if ( response.is_a?( Net::HTTPSuccess ) )
+        doc = REXML::Document.new( response.body )
+        doc.get_elements( 'images/image' ).each do |image|
+          images << convert( :image, image )
+        end
+      end
+    end
+    images
+  end
+
   def api_host
     @api_uri.host
   end
@@ -51,12 +68,24 @@ class DeltaCloud
 
   private
 
+  CONVERSIONS = {
+    :flavor=>{
+      :storage=>:to_f,
+      :memory=>:to_f,
+    }
+  }
+
   attr_reader :http
 
-  def convert_to_hash(elem)
+  def convert(type, elem)
     hash = {}
     elem.elements.each do |element|
-      hash[element.name] = element.text
+      key = element.name.to_sym
+      value = element.text
+      conversions = CONVERSIONS[type]
+      ( conversion = conversions[key] ) if conversions
+      ( value = value.send( conversion ) ) if conversion
+      hash[key] = value
     end
     hash
   end

Modified: incubator/deltacloud/trunk/client-ruby/specs/flavors_spec.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/client-ruby/specs/flavors_spec.rb?rev=962005&r1=962004&r2=962005&view=diff
==============================================================================
--- incubator/deltacloud/trunk/client-ruby/specs/flavors_spec.rb (original)
+++ incubator/deltacloud/trunk/client-ruby/specs/flavors_spec.rb Thu Jul  8 23:16:20 2010
@@ -5,7 +5,18 @@ describe "flavors" do
 
   it "should allow retrieval of all flavors" do
     DeltaCloud.new( "name", "password", API_URL ) do |client|
-      puts client.flavors.inspect
+      flavors = client.flavors
+      flavors.should_not be_empty
+      flavors.each do |flavor|
+        flavor[:id].should_not be_nil
+        flavor[:id].should be_a(String)
+        flavor[:architecture].should_not be_nil
+        flavor[:architecture].should be_a(String)
+        flavor[:storage].should_not be_nil
+        flavor[:storage].should be_a(Float)
+        flavor[:memory].should_not be_nil
+        flavor[:memory].should be_a(Float)
+      end
     end
   end 
 end

Added: incubator/deltacloud/trunk/client-ruby/specs/images_spec.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/client-ruby/specs/images_spec.rb?rev=962005&view=auto
==============================================================================
--- incubator/deltacloud/trunk/client-ruby/specs/images_spec.rb (added)
+++ incubator/deltacloud/trunk/client-ruby/specs/images_spec.rb Thu Jul  8 23:16:20 2010
@@ -0,0 +1,41 @@
+
+require 'specs/spec_helper'
+
+describe "images" do
+
+  it "should allow retrieval of all images" do
+    DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
+      images = client.images
+      images.should_not be_empty
+      images.size.should be > 100
+      images.each do |image|
+        image[:id].should_not be_nil
+        image[:id].should be_a(String)
+        image[:description].should_not be_nil
+        image[:description].should be_a(String)
+        image[:architecture].should_not be_nil
+        image[:architecture].should be_a(String)
+        image[:owner_id].should_not be_nil
+        image[:owner_id].should be_a(String)
+      end
+    end
+  end 
+
+  it "should allow retrieval of my own images" do
+    DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
+      images = client.images( :owner=>:self )
+      images.should_not be_empty
+      images.size.should be < 100
+      images.each do |image|
+        image[:id].should_not be_nil
+        image[:id].should be_a(String)
+        image[:description].should_not be_nil
+        image[:description].should be_a(String)
+        image[:architecture].should_not be_nil
+        image[:architecture].should be_a(String)
+        image[:owner_id].should_not be_nil
+        image[:owner_id].should be_a(String)
+      end
+    end
+  end
+end

Modified: incubator/deltacloud/trunk/client-ruby/specs/spec_helper.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/client-ruby/specs/spec_helper.rb?rev=962005&r1=962004&r2=962005&view=diff
==============================================================================
--- incubator/deltacloud/trunk/client-ruby/specs/spec_helper.rb (original)
+++ incubator/deltacloud/trunk/client-ruby/specs/spec_helper.rb Thu Jul  8 23:16:20 2010
@@ -1,3 +1,4 @@
+require 'yaml'
 
 API_HOST = 'localhost'
 API_PORT = 3000
@@ -5,5 +6,10 @@ API_PATH = '/api'
 
 API_URL = "http://#{API_HOST}:#{API_PORT}#{API_PATH}"
 
+credentials = YAML.load( File.read( File.dirname( __FILE__ ) + '/../credentials.yml' ) )
+
+API_NAME     = credentials['name']
+API_PASSWORD = credentials['password']
+
 $: << File.dirname( __FILE__ ) + '/../lib'
 require 'deltacloud'



Mime
View raw message