incubator-deltacloud-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lut...@apache.org
Subject svn commit: r962030 - in /incubator/deltacloud/trunk/client-ruby: lib/deltacloud.rb lib/models/base_model.rb specs/instances_spec.rb specs/shared/resources.rb
Date Thu, 08 Jul 2010 23:18:06 GMT
Author: lutter
Date: Thu Jul  8 23:18:05 2010
New Revision: 962030

URL: http://svn.apache.org/viewvc?rev=962030&view=rev
Log:
Further testing, allowing launching of images.

Modified:
    incubator/deltacloud/trunk/client-ruby/lib/deltacloud.rb
    incubator/deltacloud/trunk/client-ruby/lib/models/base_model.rb
    incubator/deltacloud/trunk/client-ruby/specs/instances_spec.rb
    incubator/deltacloud/trunk/client-ruby/specs/shared/resources.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=962030&r1=962029&r2=962030&view=diff
==============================================================================
--- incubator/deltacloud/trunk/client-ruby/lib/deltacloud.rb (original)
+++ incubator/deltacloud/trunk/client-ruby/lib/deltacloud.rb Thu Jul  8 23:18:05 2010
@@ -46,7 +46,7 @@ class DeltaCloud
     images = []
     request_path = entry_points[:images]
     if ( opts[:owner] )
-      request_path += "?owner=#{opts[:owner]}"
+      request_path += "?owner_id=#{opts[:owner]}"
     end
     request( request_path ) do |response|
       if ( response.is_a?( Net::HTTPSuccess ) )
@@ -60,7 +60,7 @@ class DeltaCloud
     images
   end
 
-  def instances(opts={})
+  def instances()
     instances = []
     request( entry_points[:instances] ) do |response|
       if ( response.is_a?( Net::HTTPSuccess ) )
@@ -74,6 +74,34 @@ class DeltaCloud
     instances
   end
 
+  def instance(id)
+    request( entry_points[:instances], :get, {'id'=>id} ) do |response|
+      if ( response.is_a?( Net::HTTPSuccess ) )
+        doc = REXML::Document.new( response.body )
+        doc.get_elements( 'instances/instance' ).each do |instance|
+          uri = instance.attributes['href']
+          return Instance.new( self, uri, build_hash( instance ) )
+        end
+      end
+    end
+    nil
+  end
+
+  def fetch_instance(uri)
+    return Instance.new( self, uri, fetch( uri ) )
+  end
+
+  def create_instance(image_id, flavor_id)
+    request( entry_points[:instances], :post, { 'image_id'=>image_id, 'flavor_id'=>flavor_id}
) do |response|
+      if ( response.is_a?( Net::HTTPSuccess ) )
+        doc = REXML::Document.new( response.body )
+        instance = doc.root
+        uri = instance.attributes['href']
+        return Instance.new( self, uri, build_hash( instance ) )
+      end
+    end  
+  end
+
   def fetch(uri)
     request( uri ) do |response|
       doc = REXML::Document.new( response.body )
@@ -122,15 +150,18 @@ class DeltaCloud
     end
   end
 
-  def request(path='', method=:get, &block)
+  def request(path='', method=:get, form_data={}, &block)
     if ( path =~ /^http/ ) 
       request_path = path
     else
       request_path = "#{api_path}#{path}"
     end
-    logger << "Request [#{request_path}]\n"
+    logger << "Request [#{method.to_s.upcase} #{request_path}]\n"
     request = eval( "Net::HTTP::#{method.to_s.capitalize}" ).new( request_path )
     request.basic_auth( @name, @password )
+    if ( method == :post )
+      request.set_form_data( form_data )
+    end
     request['Accept'] = 'text/xml'
     http.request( request, &block )
   end

Modified: incubator/deltacloud/trunk/client-ruby/lib/models/base_model.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/client-ruby/lib/models/base_model.rb?rev=962030&r1=962029&r2=962030&view=diff
==============================================================================
--- incubator/deltacloud/trunk/client-ruby/lib/models/base_model.rb (original)
+++ incubator/deltacloud/trunk/client-ruby/lib/models/base_model.rb Thu Jul  8 23:18:05 2010
@@ -43,6 +43,7 @@ class BaseModel
   end
 
   attr_reader :uri
+  attr_reader :resource_id
 
   def initialize(client, uri=nil, init=nil)
     @client      = client
@@ -60,6 +61,7 @@ class BaseModel
   def load_payload(init=nil)
     unless ( init.nil? )
       @loaded = true
+      @resource_id = init[:id] 
       self.class.attributes.each{|attr,type| 
         value = convert( init[attr], type )
         self.send( "#{attr}=", value )

Modified: incubator/deltacloud/trunk/client-ruby/specs/instances_spec.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/client-ruby/specs/instances_spec.rb?rev=962030&r1=962029&r2=962030&view=diff
==============================================================================
--- incubator/deltacloud/trunk/client-ruby/specs/instances_spec.rb (original)
+++ incubator/deltacloud/trunk/client-ruby/specs/instances_spec.rb Thu Jul  8 23:18:05 2010
@@ -38,4 +38,43 @@ describe "images" do
       instance.image.description.should be_a(String)
     end
   end
+
+  it "should allow retrieval of a single instance" do
+    DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
+      instance = client.instance( "inst1" )
+      instance.should_not be_nil
+      instance.uri.should_not be_nil
+      instance.uri.should be_a( String )
+      instance.owner_id.should eql( "mockuser" )
+      instance.public_address.should eql( "img3.inst1.public.com" )
+    end
+  
+  end
+
+  it "should allow creation of new instances" do
+    DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
+      instance = client.create_instance( 'img1', 'm1-small' )
+      instance.should_not be_nil
+      instance.uri.should_not be_nil
+      instance.uri.should be_a( String )
+    end
+  end
+
+  it "should allow fetching of instances by id" do
+    DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
+      instance = client.instance( 'inst1' )
+      instance.should_not be_nil
+      instance.uri.should_not be_nil
+      instance.uri.should be_a( String )
+    end
+  end
+
+  it "should allow fetching of instances by URI" do
+    DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
+      instance = client.fetch_instance( API_URL + '/instances/inst1' )
+      instance.should_not be_nil
+      instance.uri.should eql( API_URL + '/instances/inst1' )
+      instance.resource_id.should eql( 'inst1' )
+    end
+  end
 end

Modified: incubator/deltacloud/trunk/client-ruby/specs/shared/resources.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/client-ruby/specs/shared/resources.rb?rev=962030&r1=962029&r2=962030&view=diff
==============================================================================
--- incubator/deltacloud/trunk/client-ruby/specs/shared/resources.rb (original)
+++ incubator/deltacloud/trunk/client-ruby/specs/shared/resources.rb Thu Jul  8 23:18:05 2010
@@ -6,7 +6,7 @@ shared_examples_for "all resources" do
   end
 
   after(:each) do
-    clean_fixtures
+    #clean_fixtures
   end
 
 end



Mime
View raw message