deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@redhat.com
Subject [PATCH core 1/3] CIMI: Fixed namespace error in MachineTemplate collection
Date Fri, 30 Nov 2012 09:49:51 GMT
From: Michal Fojtik <mfojtik@redhat.com>

* Fixed bugs in parsing MachineTemplate from XML

Signed-off-by: Michal fojtik <mfojtik@redhat.com>
---
 server/lib/cimi/collections/machine_templates.rb | 10 +++++-----
 server/lib/cimi/models/machine.rb                | 23 +++++++++++------------
 server/lib/cimi/models/machine_template.rb       |  3 ++-
 3 files changed, 18 insertions(+), 18 deletions(-)

diff --git a/server/lib/cimi/collections/machine_templates.rb b/server/lib/cimi/collections/machine_templates.rb
index 17dfbd1..bda57a0 100644
--- a/server/lib/cimi/collections/machine_templates.rb
+++ b/server/lib/cimi/collections/machine_templates.rb
@@ -23,7 +23,7 @@ module CIMI::Collections
       operation :index do
         description "List all machine templates"
         control do
-          machine_templates = MachineTemplate.list(self).filter_by(params['$select'])
+          machine_templates = CIMI::Model::MachineTemplate.list(self).filter_by(params['$select'])
           respond_to do |format|
             format.xml { machine_templates.to_xml }
             format.json { machine_templates.to_json }
@@ -34,7 +34,7 @@ module CIMI::Collections
       operation :show do
         description "Show specific machine template"
         control do
-          machine_template = MachineTemplate.find(params[:id], self)
+          machine_template = CIMI::Model::MachineTemplate.find(params[:id], self)
           respond_to do |format|
             format.xml { machine_template.to_xml }
             format.json { machine_template.to_json }
@@ -46,9 +46,9 @@ module CIMI::Collections
         description "Create new machine template"
         control do
           if request.content_type.end_with?("json")
-            new_machine_template = MachineTemplate.create_from_json(request.body.read, self)
+            new_machine_template = CIMI::Model::MachineTemplate.create_from_json(request.body.read,
self)
           else
-            new_machine_template = MachineTemplate.create_from_xml(request.body.read, self)
+            new_machine_template = CIMI::Model::MachineTemplate.create_from_xml(request.body.read,
self)
           end
           status 201 # Created
           headers 'Location' => new_machine_template.id
@@ -62,7 +62,7 @@ module CIMI::Collections
       operation :destroy do
         description "Delete a specified machine template"
         control do
-          MachineTemplate.delete!(params[:id], self)
+          CIMI::Model::MachineTemplate.delete!(params[:id], self)
           no_content_with_status(200)
         end
       end
diff --git a/server/lib/cimi/models/machine.rb b/server/lib/cimi/models/machine.rb
index 4b5e958..b55d7e3 100644
--- a/server/lib/cimi/models/machine.rb
+++ b/server/lib/cimi/models/machine.rb
@@ -55,12 +55,13 @@ class CIMI::Model::Machine < CIMI::Model::Base
     else
       hardware_profile_id = machine_template['machineConfig']["href"].split('/').last
       image_id = machine_template['machineImage']["href"].split('/').last
+      if machine_template.has_key? 'credential'
+        additional_params[:keyname] = machine_template['credential']["href"].split('/').last
+      end
     end
+
     additional_params = {}
     additional_params[:name] = json['name'] if json['name']
-    if machine_template.has_key? 'credential'
-      additional_params[:keyname] = machine_template['credential']["href"].split('/').last
-    end
     instance = context.driver.create_instance(context.credentials, image_id, {
       :hwp_id => hardware_profile_id
     }.merge(additional_params))
@@ -73,22 +74,20 @@ class CIMI::Model::Machine < CIMI::Model::Base
 
   def self.create_from_xml(body, context)
     xml = XmlSimple.xml_in(body)
-    if !xml['machineTemplate']['href'].nil?
-      template = context.current_db.machine_templates.first(:id => xml['machineTemplate']['href'].split('/').last)
+    if xml['machineTemplate'][0]['href']
+      template = context.current_db.machine_templates.first(:id => xml['machineTemplate'][0]['href'].split('/').last)
       hardware_profile_id = template.machine_config.split('/').last
       image_id = template.machine_image.split('/').last
     else
       machine_template = xml['machineTemplate'][0]
-      hardware_profile_id = machine_template['machineConfig']["href"].split('/').last
-      image_id = machine_template['machineImage']["href"].split('/').last
+      hardware_profile_id = machine_template['machineConfig'].first["href"].split('/').last
+      image_id = machine_template['machineImage'].first["href"].split('/').last
+      if machine_template.has_key? 'credential'
+        additional_params[:keyname] = machine_template['credential'][0]["href"].split('/').last
+      end
     end
-    hardware_profile_id = machine_template['machineConfig'][0]["href"].split('/').last
-    image_id = machine_template['machineImage'][0]["href"].split('/').last
     additional_params = {}
     additional_params[:name] = xml['name'][0] if xml['name']
-    if machine_template.has_key? 'credential'
-      additional_params[:keyname] = machine_template['credential'][0]["href"].split('/').last
-    end
     instance = context.driver.create_instance(context.credentials, image_id, {
       :hwp_id => hardware_profile_id
     }.merge(additional_params))
diff --git a/server/lib/cimi/models/machine_template.rb b/server/lib/cimi/models/machine_template.rb
index 5236ebf..b0a0aad 100644
--- a/server/lib/cimi/models/machine_template.rb
+++ b/server/lib/cimi/models/machine_template.rb
@@ -69,12 +69,13 @@ class CIMI::Model::MachineTemplate < CIMI::Model::Base
 
     def create_from_xml(body, context)
       xml = XmlSimple.xml_in(body)
+      puts xml.inspect
       new_template = context.current_db.machine_templates.new(
         :name => xml['name'].first,
         :description => xml['description'].first,
         :machine_config => xml['machineConfig'].first['href'],
         :machine_image => xml['machineImage'].first['href'],
-        :ent_properties => xml['properties'].first.to_json,
+        :ent_properties => xml['property'].inject({}) { |r, p| r[p['name']]=p['content'];
r },
         :be_kind => 'machine_template',
         :be_id => ''
       )
-- 
1.8.0


Mime
View raw message