incubator-deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Chris Lalancette <clala...@redhat.com>
Subject [PATCH 06/10] Canonicalize instance states.
Date Thu, 05 Aug 2010 19:57:20 GMT
For some drivers, we were passing the raw string we got from
the cloud provider through as the instance state.  However,
that doesn't fit in with trying to make the drivers act consistent,
so make sure all of the drivers have a translation layer between
the cloud provider's states and the deltcloud API states.  After
this series, there are only 3 valid deltacloud states: "RUNNING",
"STOPPED", and "PENDING".  We may want to think about adding
additional states in the future, but these cover most of the
current use-cases.

Signed-off-by: Chris Lalancette <clalance@redhat.com>
---
 server/lib/deltacloud/drivers/ec2/ec2_driver.rb    |   18 +++++++++++++++---
 .../lib/deltacloud/drivers/gogrid/gogrid_driver.rb |    5 +++--
 .../drivers/opennebula/opennebula_driver.rb        |    2 +-
 .../drivers/rackspace/rackspace_driver.rb          |    8 ++++----
 .../lib/deltacloud/drivers/rhevm/rhevm_driver.rb   |   11 +++++++----
 5 files changed, 30 insertions(+), 14 deletions(-)

diff --git a/server/lib/deltacloud/drivers/ec2/ec2_driver.rb b/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
index 909eca3..7e5e9ec 100644
--- a/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
+++ b/server/lib/deltacloud/drivers/ec2/ec2_driver.rb
@@ -201,7 +201,7 @@ class EC2Driver < Deltacloud::BaseDriver
       # at this point, the action has succeeded but our follow-up
       # "describe_instances" failed for some reason.  Create a simple Instance
       # object with only the ID and new state in place
-      state = backup.instancesSet.item.first.currentState.name
+      state = convert_state(backup.instancesSet.item.first.currentState.name)
       Instance.new( {
         :id => id,
         :state => state,
@@ -351,9 +351,21 @@ class EC2Driver < Deltacloud::BaseDriver
     } )
   end
 
+  def convert_state(ec2_state)
+    case ec2_state
+    when :terminated
+      :STOPPED
+    when :running
+      :RUNNING
+    when :pending
+      :PENDING
+    when :'shutting-down'
+      :STOPPED
+    end
+  end
+
   def convert_instance(ec2_instance, owner_id)
-    state = ec2_instance['instanceState']['name'].upcase
-    state_key = state.downcase.underscore.to_sym
+    state = convert_state(ec2_instance['instanceState']['name'])
     realm_id = ec2_instance['placement']['availabilityZone']
     (realm_id = nil ) if ( realm_id == '' )
     hwp_name = ec2_instance['instanceType']
diff --git a/server/lib/deltacloud/drivers/gogrid/gogrid_driver.rb b/server/lib/deltacloud/drivers/gogrid/gogrid_driver.rb
index 750e1cd..e204cbe 100644
--- a/server/lib/deltacloud/drivers/gogrid/gogrid_driver.rb
+++ b/server/lib/deltacloud/drivers/gogrid/gogrid_driver.rb
@@ -282,6 +282,7 @@ class GogridDriver < Deltacloud::BaseDriver
 
   def convert_instance(instance, owner_id)
     hwp_name = instance['image']['name']
+    state = convert_server_state(instance['state']['name'], instance['id'])
 
     Instance.new(
        # note that we use 'name' as the id here, because newly created instances
@@ -294,8 +295,8 @@ class GogridDriver < Deltacloud::BaseDriver
       :instance_profile => InstanceProfile.new(hwp_name),
       :name => instance['name'],
       :realm_id => instance['type']['id'],
-      :state => convert_server_state(instance['state']['name'], instance['id']),
-      :actions => instance_actions_for(convert_server_state(instance['state']['name'],
instance['id'])),
+      :state => state,
+      :actions => instance_actions_for(state),
       :public_addresses => [ instance['ip']['ip'] ],
       :private_addresses => [],
       :username => instance['username'],
diff --git a/server/lib/deltacloud/drivers/opennebula/opennebula_driver.rb b/server/lib/deltacloud/drivers/opennebula/opennebula_driver.rb
index c5e1408..daa9a34 100644
--- a/server/lib/deltacloud/drivers/opennebula/opennebula_driver.rb
+++ b/server/lib/deltacloud/drivers/opennebula/opennebula_driver.rb
@@ -180,7 +180,7 @@ class OpennebulaDriver < Deltacloud::BaseDriver
 
 	imageid = computehash['STORAGE/DISK[@type="disk"]'].attributes['href'].split("/").last
 
-	state = (computehash['STATE'].text == 'ACTIVE') ? 'RUNNING' : computehash['STATE'].text
+	state = (computehash['STATE'].text == :ACTIVE) ? :RUNNING : :STOPPED
 
 	hwp_name = computehash['INSTANCE_TYPE'] || 'small'
 
diff --git a/server/lib/deltacloud/drivers/rackspace/rackspace_driver.rb b/server/lib/deltacloud/drivers/rackspace/rackspace_driver.rb
index 6d6ba0b..a182c04 100644
--- a/server/lib/deltacloud/drivers/rackspace/rackspace_driver.rb
+++ b/server/lib/deltacloud/drivers/rackspace/rackspace_driver.rb
@@ -75,8 +75,8 @@ class RackspaceDriver < Deltacloud::BaseDriver
     end
     Instance.new( {
       :id => id,
-      :state => "REBOOT",
-      :actions => instance_actions_for( state ),
+      :state => :RUNNING,
+      :actions => instance_actions_for( :RUNNING ),
     } )
   end
 
@@ -91,8 +91,8 @@ class RackspaceDriver < Deltacloud::BaseDriver
     end
     Instance.new( {
       :id => id,
-      :state => "STOPPED",
-      :actions => instance_actions_for( "STOPPED" ),
+      :state => :STOPPED,
+      :actions => instance_actions_for( :STOPPED ),
     } )
   end
 
diff --git a/server/lib/deltacloud/drivers/rhevm/rhevm_driver.rb b/server/lib/deltacloud/drivers/rhevm/rhevm_driver.rb
index 18ba0fa..beac58f 100644
--- a/server/lib/deltacloud/drivers/rhevm/rhevm_driver.rb
+++ b/server/lib/deltacloud/drivers/rhevm/rhevm_driver.rb
@@ -78,10 +78,13 @@ class RHEVMDriver < Deltacloud::BaseDriver
 
   def statify(state)
     st = state.nil? ? "" : state.upcase()
-    return "running" if st == "UP"
-    return "stopped" if st == "DOWN"
-    return "pending" if st == "POWERING UP"
-    st
+    case st
+    when :UP
+      :RUNNING
+    when :DOWN
+      :STOPPED
+    when :'POWERING UP'
+      :PENDING
   end
 
   define_hardware_profile 'rhevm'
-- 
1.7.2


Mime
View raw message