deltacloud-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@apache.org
Subject [4/12] git commit: Move the logic from Machine.create_from_json and Machine.create_from_xml into MachineCreate
Date Fri, 22 Feb 2013 10:32:24 GMT
Move the logic from Machine.create_from_json and Machine.create_from_xml
into MachineCreate


Project: http://git-wip-us.apache.org/repos/asf/deltacloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltacloud/commit/0ffdf176
Tree: http://git-wip-us.apache.org/repos/asf/deltacloud/tree/0ffdf176
Diff: http://git-wip-us.apache.org/repos/asf/deltacloud/diff/0ffdf176

Branch: refs/heads/master
Commit: 0ffdf1760223f1c07b3c547988a01f8710d56404
Parents: 91e0b38
Author: David Lutterkort <lutter@redhat.com>
Authored: Sat Feb 9 21:54:42 2013 +0100
Committer: Michal fojtik <mfojtik@redhat.com>
Committed: Thu Feb 21 15:31:31 2013 +0100

----------------------------------------------------------------------
 server/lib/cimi/collections/machines.rb  |    7 +--
 server/lib/cimi/models/machine.rb        |   71 -------------------------
 server/lib/cimi/models/machine_create.rb |   29 ++++++++++
 3 files changed, 31 insertions(+), 76 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltacloud/blob/0ffdf176/server/lib/cimi/collections/machines.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/collections/machines.rb b/server/lib/cimi/collections/machines.rb
index 9a71bb1..5025e58 100644
--- a/server/lib/cimi/collections/machines.rb
+++ b/server/lib/cimi/collections/machines.rb
@@ -46,11 +46,8 @@ module CIMI::Collections
       operation :create, :with_capability => :create_instance do
         description "Create a new Machine entity."
         control do
-          if grab_content_type(request.content_type, request.body) == :json
-            new_machine = Machine.create_from_json(request.body.read, self)
-          else
-            new_machine = Machine.create_from_xml(request.body.read, self)
-          end
+          mc = MachineCreate.parse(request.body, request.content_type)
+          new_machine = mc.create(self)
           headers_for_create new_machine
           respond_to do |format|
             format.json { new_machine.to_json }

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/0ffdf176/server/lib/cimi/models/machine.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/models/machine.rb b/server/lib/cimi/models/machine.rb
index 1b8e28a..f32bdfb 100644
--- a/server/lib/cimi/models/machine.rb
+++ b/server/lib/cimi/models/machine.rb
@@ -50,77 +50,6 @@ class CIMI::Model::Machine < CIMI::Model::Base
     end
   end
 
-  def self.create_from_json(body, context)
-    json = JSON.parse(body)
-    additional_params={}
-    machine_template = json['machineTemplate']
-    if !machine_template['href'].nil?
-      template = current_db.machine_templates_dataset.first(:id => machine_template['href'].split('/').last)
-      raise 'Could not find the MachineTemplate' if template.nil?
-      hardware_profile_id = template.machine_config.split('/').last
-      image_id = template.machine_image.split('/').last
-      json['realm'] = template.realm
-    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
-    if machine_template.has_key? "initialState"
-      additional_params[:initial_state] = machine_template["initialState"].strip
-    end
-    additional_params[:name] = json['name'] if json['name']
-    additional_params[:realm_id] = json['realm'] if json['realm']
-    instance = context.driver.create_instance(context.credentials, image_id, {
-      :hwp_id => hardware_profile_id
-    }.merge(additional_params))
-
-    # Store attributes that are not supported by the backend cloud to local
-    # database:
-    machine = from_instance(instance, context)
-    machine.name = json['name'] || machine.name
-    machine.description = json['description']
-    machine.extract_properties!(json)
-    machine.save
-    machine
-  end
-
-  def self.create_from_xml(body, context)
-    xml = XmlSimple.xml_in(body)
-    additional_params = {}
-    if xml['machineTemplate'][0]['href']
-      template = current_db.machine_templates_dataset.first(:id => xml['machineTemplate'][0]['href'].split('/').last)
-      hardware_profile_id = template.machine_config.split('/').last
-      image_id = template.machine_image.split('/').last
-      xml['realm'] = [ template.realm ]
-    else
-      machine_template = xml['machineTemplate'][0]
-      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
-    if xml["machineTemplate"][0].has_key? "initialState"
-      additional_params[:initial_state] = xml["machineTemplate"][0]["initialState"].first.strip
-    end
-    additional_params[:name] = xml['name'][0] if xml['name']
-    additional_params[:realm_id] = xml['realm'][0] if xml['realm']
-    instance = context.driver.create_instance(context.credentials, image_id, {
-      :hwp_id => hardware_profile_id
-    }.merge(additional_params))
-
-    # Store attributes that are not supported by the backend cloud to local
-    # database:
-    machine = from_instance(instance, context)
-    machine.name = xml['name'] || machine.name
-    machine.description = xml['description']
-    machine.extract_properties!(xml)
-    machine.save
-    machine
-  end
-
   def perform(action, context, &block)
     begin
       if context.driver.send(:"#{action.name}_instance", context.credentials, self.id.split("/").last)

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/0ffdf176/server/lib/cimi/models/machine_create.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/models/machine_create.rb b/server/lib/cimi/models/machine_create.rb
index 8f9fc89..c0b107c 100644
--- a/server/lib/cimi/models/machine_create.rb
+++ b/server/lib/cimi/models/machine_create.rb
@@ -15,4 +15,33 @@
 
 class CIMI::Model::MachineCreate < CIMI::Model::Base
   ref :machine_template
+  text :realm
+
+  def create(ctx)
+    params = {}
+    if machine_template.href
+      template = machine_template.find(ctx)
+      params[:hwp_id] = template.machine_config.ref_id(ctx)
+      image_id = template.machine_image.ref_id(ctx)
+    else
+      # FIXME: What if either of these href's isn't there ? What if the user
+      # tries to override some aspect of the machine_config/machine_image ?
+      params[:hwp_id] = machine_template.machine_config.href.split('/').last
+      image_id = machine_template.machine_image.href.split('/').last
+      if machine_template.credential.href
+        params[:keyname] = machine_template.credential.href.split('/').last
+      end
+    end
+
+    params[:name] = name if name
+    params[:realm_id] = realm if realm
+    instance = ctx.driver.create_instance(ctx.credentials, image_id, params)
+
+    result = CIMI::Model::Machine::from_instance(instance, ctx)
+    result.name = name if name
+    result.description = description if description
+    result.property = property if property
+    result.save
+    result
+  end
 end


Mime
View raw message