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:22:49 UTC

svn commit: r962099 - in /incubator/deltacloud/trunk/client-ruby: lib/deltacloud.rb lib/models/base_model.rb lib/models/instance.rb specs/instances_spec.rb

Author: lutter
Date: Thu Jul  8 23:22:48 2010
New Revision: 962099

URL: http://svn.apache.org/viewvc?rev=962099&view=rev
Log:
Add start!(), stop!(), and reboot!()

Modified:
    incubator/deltacloud/trunk/client-ruby/lib/deltacloud.rb
    incubator/deltacloud/trunk/client-ruby/lib/models/base_model.rb
    incubator/deltacloud/trunk/client-ruby/lib/models/instance.rb
    incubator/deltacloud/trunk/client-ruby/specs/instances_spec.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=962099&r1=962098&r2=962099&view=diff
==============================================================================
--- incubator/deltacloud/trunk/client-ruby/lib/deltacloud.rb (original)
+++ incubator/deltacloud/trunk/client-ruby/lib/deltacloud.rb Thu Jul  8 23:22:48 2010
@@ -188,6 +188,15 @@ class DeltaCloud
     nil
   end
 
+  def post_instance(uri)
+    request( uri, :post ) do |response|
+      if ( response.is_a?( Net::HTTPSuccess ) )
+        return true
+      end
+    end
+    return false
+  end
+
   def fetch_instance(uri)
     xml = fetch_resource( :instance, uri )
     return Instance.new( self, uri, xml ) if xml

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=962099&r1=962098&r2=962099&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:22:48 2010
@@ -36,6 +36,11 @@ class BaseModel
     @id
   end
 
+ 
+  protected
+
+  attr_reader :client
+
   def check_load_payload()
     return if @loaded
     xml = @client.fetch_resource( self.class.xml_tag_name.to_sym, @uri )
@@ -49,4 +54,8 @@ class BaseModel
     end
   end
 
+  def unload
+    @loaded = false
+  end
+
 end

Modified: incubator/deltacloud/trunk/client-ruby/lib/models/instance.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/client-ruby/lib/models/instance.rb?rev=962099&r1=962098&r2=962099&view=diff
==============================================================================
--- incubator/deltacloud/trunk/client-ruby/lib/models/instance.rb (original)
+++ incubator/deltacloud/trunk/client-ruby/lib/models/instance.rb Thu Jul  8 23:22:48 2010
@@ -13,11 +13,34 @@ class Instance < BaseModel
   attribute :image
   attribute :flavor
   attribute :realm
+  attribute :action_urls
 
   def initialize(client, uri, xml=nil)
+    @action_urls = {}
     super( client, uri, xml )
   end
 
+  def start!()
+    url = action_urls['start']
+    throw Exception.new( "Unable to start" ) unless url
+    client.post_instance( url )
+    unload
+  end
+
+  def reboot!()
+    url = action_urls['reboot']
+    throw Exception.new( "Unable to reboot" ) unless url
+    client.post_instance( url )
+    unload
+  end
+
+  def stop!()
+    url = action_urls['stop']
+    throw Exception.new( "Unable to stop" ) unless url
+    client.post_instance( url )
+    unload
+  end
+
   def load_payload(xml=nil)
     super(xml)
     unless xml.nil?
@@ -39,7 +62,11 @@ class Instance < BaseModel
       @state = xml.text( 'state' )
       @actions = []
       xml.get_elements( 'actions/link' ).each do |link|
-        @actions << link.attributes['rel']
+        action_name = link.attributes['rel']
+        if ( action_name )
+          @actions << link.attributes['rel']
+          @action_urls[ link.attributes['rel'] ] = link.attributes['href']
+        end
       end
     end
   end

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=962099&r1=962098&r2=962099&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:22:48 2010
@@ -123,4 +123,29 @@ describe "instances" do
       instance.id.should eql( 'inst1' )
     end
   end
+
+  describe "performing actions on instances" do
+    it "should allow actions that are valid" do
+      DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
+        instance = client.instance( 'inst1' )
+        instance.should_not be_nil
+        instance.state.should eql( "RUNNING" )
+        instance.uri.should eql( API_URL + '/instances/inst1' )
+        instance.id.should eql( 'inst1' )
+        instance.stop!
+        instance.state.should eql( "STOPPED" )
+        instance.start!
+        instance.state.should eql( "RUNNING" )
+      end
+    end
+
+    it "should not allow actions that are invalid" do
+      DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
+        instance = client.instance( 'inst1' )
+        instance.should_not be_nil
+        instance.state.should eql( "RUNNING" )
+        lambda{instance.start}.should raise_error
+      end
+    end
+  end
 end