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:10 UTC

svn commit: r962088 - in /incubator/deltacloud/trunk/client-ruby: lib/deltacloud.rb lib/models/state.rb lib/models/transition.rb specs/instance_states_spec.rb

Author: lutter
Date: Thu Jul  8 23:22:09 2010
New Revision: 962088

URL: http://svn.apache.org/viewvc?rev=962088&view=rev
Log:
Integrate instance-state interrogation into client.

Added:
    incubator/deltacloud/trunk/client-ruby/lib/models/state.rb
    incubator/deltacloud/trunk/client-ruby/lib/models/transition.rb
    incubator/deltacloud/trunk/client-ruby/specs/instance_states_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=962088&r1=962087&r2=962088&view=diff
==============================================================================
--- incubator/deltacloud/trunk/client-ruby/lib/deltacloud.rb (original)
+++ incubator/deltacloud/trunk/client-ruby/lib/deltacloud.rb Thu Jul  8 23:22:09 2010
@@ -10,6 +10,9 @@ require 'models/instance'
 require 'models/storage_volume'
 require 'models/storage_snapshot'
 
+require 'models/state'
+require 'models/transition'
+
 class DeltaCloud
 
   attr_accessor :logger
@@ -64,6 +67,31 @@ class DeltaCloud
     nil
   end
 
+  def instance_states()
+    states = []
+    request( entry_points[:instance_states] ) do |response|
+      if ( response.is_a?( Net::HTTPSuccess ) )
+        doc = REXML::Document.new( response.body )
+        doc.get_elements( 'states/state' ).each do |state_elem|
+          state = State.new( state_elem.attributes['name'] )
+          state_elem.get_elements( 'transition' ).each do |transition_elem|
+            state.transitions << Transition.new( 
+                                   transition_elem.attributes['to'],
+                                   transition_elem.attributes['action']
+                                 )
+          end
+          states << state
+        end
+      end
+    end
+    states
+  end
+
+  def instance_state(name)
+    found = instance_states.find{|e| e.name.to_s == name.to_s}
+    found
+  end
+
   def realms(opts={})
     realms = []
     request( entry_points[:realms], :get, opts ) do |response|

Added: incubator/deltacloud/trunk/client-ruby/lib/models/state.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/client-ruby/lib/models/state.rb?rev=962088&view=auto
==============================================================================
--- incubator/deltacloud/trunk/client-ruby/lib/models/state.rb (added)
+++ incubator/deltacloud/trunk/client-ruby/lib/models/state.rb Thu Jul  8 23:22:09 2010
@@ -0,0 +1,12 @@
+
+class State
+
+  attr_accessor :name
+  attr_accessor :transitions
+
+  def initialize(name)
+    @name = name
+    @transitions = []
+  end
+
+end

Added: incubator/deltacloud/trunk/client-ruby/lib/models/transition.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/client-ruby/lib/models/transition.rb?rev=962088&view=auto
==============================================================================
--- incubator/deltacloud/trunk/client-ruby/lib/models/transition.rb (added)
+++ incubator/deltacloud/trunk/client-ruby/lib/models/transition.rb Thu Jul  8 23:22:09 2010
@@ -0,0 +1,16 @@
+
+class Transition
+
+  attr_accessor :to
+  attr_accessor :action
+
+  def initialize(to, action)
+    @to = to
+    @action = action
+  end
+
+  def auto?()
+    @action.nil?
+  end
+
+end

Added: incubator/deltacloud/trunk/client-ruby/specs/instance_states_spec.rb
URL: http://svn.apache.org/viewvc/incubator/deltacloud/trunk/client-ruby/specs/instance_states_spec.rb?rev=962088&view=auto
==============================================================================
--- incubator/deltacloud/trunk/client-ruby/specs/instance_states_spec.rb (added)
+++ incubator/deltacloud/trunk/client-ruby/specs/instance_states_spec.rb Thu Jul  8 23:22:09 2010
@@ -0,0 +1,49 @@
+
+require 'specs/spec_helper'
+
+Spec::Matchers.define :include_transition do |action,to|
+  match do |transitions|
+    found = transitions.find{|e| e.action.to_s == action.to_s && e.to.to_s == to.to_s }
+    ! found.nil?
+  end
+end
+
+describe "instance-states" do
+
+  it_should_behave_like "all resources"
+
+  it "should allow retrieval of instance-state information" do
+    DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
+      instance_states = client.instance_states
+      instance_states.should_not be_nil
+      instance_states.should_not be_empty
+
+      instance_states[0].name.should eql( 'pending' )
+      instance_states[0].transitions.size.should eql( 1 )
+      instance_states[0].transitions[0].should be_auto
+
+      instance_states[1].name.should eql( 'running' )
+      instance_states[1].transitions.size.should eql( 2 )
+      instance_states[1].transitions.should include_transition( :reboot, :running )
+      instance_states[1].transitions.should include_transition( :stop, :terminated )
+    end
+  end 
+
+  it "should allow retrieval of a single instance-state blob" do
+    DeltaCloud.new( API_NAME, API_PASSWORD, API_URL ) do |client|
+      instance_state = client.instance_state( :pending )
+      instance_state.should_not be_nil
+      instance_state.name.should eql( 'pending' )
+      instance_state.transitions.size.should eql( 1 )
+      instance_state.transitions[0].should be_auto
+
+      instance_state = client.instance_state( :running )
+      instance_state.name.should eql( 'running' )
+      instance_state.transitions.size.should eql( 2 )
+      instance_state.transitions.should include_transition( :reboot, :running )
+      instance_state.transitions.should include_transition( :stop, :terminated )
+    end
+  end
+
+
+end