You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltacloud.apache.org by mf...@redhat.com on 2011/11/29 14:45:01 UTC

[PATCH core 09/10] CIMI: Added create and destroy operations for Machine entity

From: Michal Fojtik <mf...@redhat.com>


Signed-off-by: Michal fojtik <mf...@redhat.com>
---
 server/lib/cimi/model/machine.rb |   21 +++++++++++++++++++++
 server/lib/cimi/server.rb        |   33 +++++++++++++++++++--------------
 2 files changed, 40 insertions(+), 14 deletions(-)

diff --git a/server/lib/cimi/model/machine.rb b/server/lib/cimi/model/machine.rb
index 74741e6..19c7f2c 100644
--- a/server/lib/cimi/model/machine.rb
+++ b/server/lib/cimi/model/machine.rb
@@ -63,10 +63,27 @@ class CIMI::Model::Machine < CIMI::Model::Base
       instances.map { |instance| from_instance(instance, _self) }.compact
     else
       instance = _self.driver.instance(_self.credentials, :id => id)
+      raise CIMI::Model::NotFound unless instance
       from_instance(instance, _self)
     end
   end
 
+  def self.create_from_json(body, _self)
+    json = JSON.parse(body)
+    hardware_profile_id = xml['MachineTemplate']['MachineConfig']["href"].split('/').last
+    image_id = xml['MachineTemplate']['MachineImage']["href"].split('/').last
+    instance = _self.create_instance(_self.credentials, image_id, { :hwp_id => hardware_profile_id })
+    from_instance(instance, _self)
+  end
+
+  def self.create_from_xml(body, _self)
+    xml = XmlSimple.xml_in(body)
+    hardware_profile_id = xml['MachineTemplate'][0]['MachineConfig'][0]["href"].split('/').last
+    image_id = xml['MachineTemplate'][0]['MachineImage'][0]["href"].split('/').last
+    instance = _self.driver.create_instance(_self.credentials, image_id, { :hwp_id => hardware_profile_id })
+    from_instance(instance, _self)
+  end
+
   def perform(action, _self, &block)
     begin
       if _self.driver.send(:"#{action.name}_instance", _self.credentials, self.name)
@@ -79,6 +96,10 @@ class CIMI::Model::Machine < CIMI::Model::Base
     end
   end
 
+  def self.delete!(id, _self)
+    _self.driver.destroy_instance(_self.credentials, id)
+  end
+
   private
 
   def self.from_instance(instance, _self)
diff --git a/server/lib/cimi/server.rb b/server/lib/cimi/server.rb
index e6279b9..afee932 100644
--- a/server/lib/cimi/server.rb
+++ b/server/lib/cimi/server.rb
@@ -180,23 +180,28 @@ global_collection :machines do
     end
   end
 
-  operation :delete, :method => :post, :member => true do
+  operation :create do
+    description "Create a new Machine entity."
+    control do
+      if request.content_type.end_with?("+json")
+        new_machine = Machine.create_from_json(request.body.read, self)
+      else
+        new_machine = Machine.create_from_xml(request.body.read, self)
+      end
+      status 201 # Created
+      respond_to do |format|
+        format.json { new_machine.to_json }
+        format.xml { new_machine.to_xml }
+      end
+    end
+  end
+
+  operation :delete, :method => :delete, :member => true do
     description "Reboot specific machine."
     param :id,          :string,    :required
     control do
-      machine = Machine.find(params[:id], self)
-      machine.perform :destroy do |operation|
-        operation.body = request.body.read
-        operation.content_type = params[:content_type]
-        operation.on :success do
-          # We *should* return 202 - Accepted because the 'reboot' operation will not be processed
-          # immediately
-          no_content_with_status 202
-        end
-        operation.on :failure do
-          # error...
-        end
-      end
+      Machine.delete!(params[:id], self)
+      no_content_with_status(200)
     end
   end
 
-- 
1.7.4.4