You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltacloud.apache.org by ma...@redhat.com on 2012/04/06 18:01:25 UTC

[PATCH 10/11] CIMI: Adds VSP status change methods and mock driver implementation

From: marios <ma...@redhat.com>


Signed-off-by: marios <ma...@redhat.com>
---
 server/lib/cimi/model/vsp.rb                       |   13 +++++++
 server/lib/cimi/server.rb                          |   36 ++++++++++++++++++++
 .../drivers/mock/mock_driver_cimi_methods.rb       |   28 +++++++++++----
 3 files changed, 69 insertions(+), 8 deletions(-)

diff --git a/server/lib/cimi/model/vsp.rb b/server/lib/cimi/model/vsp.rb
index 9507dd5..40a526f 100644
--- a/server/lib/cimi/model/vsp.rb
+++ b/server/lib/cimi/model/vsp.rb
@@ -68,6 +68,19 @@ class CIMI::Model::VSP < CIMI::Model::Base
     context.driver.delete_vsp(context.credentials, id)
   end
 
+  def perform(action, context, &block)
+    begin
+      if context.driver.send(:"#{action.name}_vsp", context.credentials, self.name)
+        block.callback :success
+      else
+        raise "Operation #{action.name} failed to execute on the VSP #{self.name} "
+      end
+    rescue => e
+      block.callback :failure, e.message
+    end
+  end
+
+
   private
 
   def self.get_by_reference(input, context)
diff --git a/server/lib/cimi/server.rb b/server/lib/cimi/server.rb
index 63523ba..60f9a3b 100644
--- a/server/lib/cimi/server.rb
+++ b/server/lib/cimi/server.rb
@@ -774,6 +774,42 @@ global_collection :vsps do
     end
   end
 
+  operation :start, :method => :post, :member => true do
+    description "Start specific VSP."
+    param :id,          :string,    :required
+    control do
+      vsp = VSP.find(params[:id], self)
+      report_error(404) unless vsp
+      if request.content_type.end_with?("json")
+        action = Action.from_json(request.body.read)
+      else
+        action = Action.from_xml(request.body.read)
+      end
+      vsp.perform(action, self) do |operation|
+        no_content_with_status(202) if operation.success?
+        # Handle errors using operation.failure?
+      end
+    end
+  end
+
+  operation :stop, :method => :post, :member => true do
+    description "Stop specific VSP."
+    param :id,          :string,    :required
+    control do
+      vsp = VSP.find(params[:id], self)
+      report_error(404) unless vsp
+      if request.content_type.end_with?("json")
+        action = Action.from_json(request.body.read)
+      else
+        action = Action.from_xml(request.body.read)
+      end
+      vsp.perform(action, self) do |operation|
+        no_content_with_status(202) if operation.success?
+        # Handle errors using operation.failure?
+      end
+    end
+  end
+
 end
 
 global_collection :vsp_configurations do
diff --git a/server/lib/deltacloud/drivers/mock/mock_driver_cimi_methods.rb b/server/lib/deltacloud/drivers/mock/mock_driver_cimi_methods.rb
index fdd501a..2dec66b 100644
--- a/server/lib/deltacloud/drivers/mock/mock_driver_cimi_methods.rb
+++ b/server/lib/deltacloud/drivers/mock/mock_driver_cimi_methods.rb
@@ -62,17 +62,17 @@ module Deltacloud::Drivers::Mock
 
     def start_network(credentials, id)
       check_credentials(credentials)
-      update_network_state(id, "STARTED")
+      update_object_state(id, "Network", "STARTED")
     end
 
     def stop_network(credentials, id)
       check_credentials(credentials)
-      update_network_state(id, "STOPPED")
+      update_object_state(id, "Network", "STOPPED")
     end
 
     def suspend_network(credentials, id)
       check_credentials(credentials)
-      update_network_state(id, "SUSPENDED")
+      update_object_state(id, "Network", "SUSPENDED")
     end
 
     def network_configurations(credentials, opts={})
@@ -152,6 +152,16 @@ module Deltacloud::Drivers::Mock
       vsp
     end
 
+    def start_vsp(credentials, id)
+      check_credentials(credentials)
+      update_object_state(id, "VSP", "STARTED")
+    end
+
+    def stop_vsp(credentials, id)
+      check_credentials(credentials)
+      update_object_state(id, "VSP", "STOPPED")
+    end
+
     def delete_vsp(credentials, id)
       check_credentials(credentials)
       @client.destroy_cimi(:vsp, id)
@@ -260,11 +270,13 @@ module Deltacloud::Drivers::Mock
       end
     end
 
-    def update_network_state(id, new_state)
-      network = CIMI::Model::Network.from_json(@client.load_cimi(:network, id))
-      network.state = new_state
-      @client.store_cimi(:network, network)
-      network
+    def update_object_state(id, object, new_state)
+      klass = CIMI::Model.const_get("#{object}")
+      symbol = object.to_s.downcase.singularize.intern
+      obj = klass.from_json(@client.load_cimi(symbol, id))
+      obj.state = new_state
+      @client.store_cimi(symbol, obj)
+      obj
     end
 
   end
-- 
1.7.6.5