deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@redhat.com
Subject [PATCH core] CIMI: Initial implementation of UPDATE method for MachineTemplate
Date Thu, 03 Jan 2013 12:40:54 GMT
From: Michal Fojtik <mfojtik@redhat.com>

This patch will allow partial update of MachineTemplate entity:

PUT /cimi/machine_template/1?$select=name,description

{
  'name' : 'updatedName',
  'description' : 'updatedDescription'
}

Signed-off-by: Michal fojtik <mfojtik@redhat.com>
---
 server/lib/cimi/collections/machine_templates.rb |  7 +++++++
 server/lib/cimi/helpers/cimi_helper.rb           | 19 +++++++++++++++++
 server/lib/cimi/models/machine_template.rb       | 26 ++++++++++++++++++++++++
 server/support/cimi/machine_template_update.json |  4 ++++
 4 files changed, 56 insertions(+)
 create mode 100644 server/support/cimi/machine_template_update.json

diff --git a/server/lib/cimi/collections/machine_templates.rb b/server/lib/cimi/collections/machine_templates.rb
index 9a12915..bf25886 100644
--- a/server/lib/cimi/collections/machine_templates.rb
+++ b/server/lib/cimi/collections/machine_templates.rb
@@ -66,6 +66,13 @@ module CIMI::Collections
         end
       end
 
+      operation :update, :http_method => :put do
+        description "Update attributes for specified machine template"
+        control do
+          update_operation(CIMI::Model::MachineTemplate)
+        end
+      end
+
     end
 
   end
diff --git a/server/lib/cimi/helpers/cimi_helper.rb b/server/lib/cimi/helpers/cimi_helper.rb
index b062af7..fff430b 100644
--- a/server/lib/cimi/helpers/cimi_helper.rb
+++ b/server/lib/cimi/helpers/cimi_helper.rb
@@ -21,6 +21,25 @@ module CIMI
         (params['$expand'] || '').split(',').include?(collection.to_s)
     end
 
+    def select_attrs
+      return [] if params['$select'].nil?
+      return [] if params['$select'].empty?
+      params['$select'].split(',').map { |p| p.strip }
+    end
+
+    def update_operation(model_klass)
+      if grab_content_type(request.content_type, request.body) == :json
+        model_klass.update_from_json!(params[:id], select_attrs, request.body.read, self)
+      else
+        model_klass.update_from_xml!(params[:id], select_attrs, request.body.read, self)
+      end
+      entity = model_klass.find(params[:id], self)
+      respond_to do |format|
+        format.xml { entity.to_xml }
+        format.json { entity.to_json }
+      end
+    end
+
     def no_content_with_status(code=200)
       body ''
       status code
diff --git a/server/lib/cimi/models/machine_template.rb b/server/lib/cimi/models/machine_template.rb
index 90ed025..648eee7 100644
--- a/server/lib/cimi/models/machine_template.rb
+++ b/server/lib/cimi/models/machine_template.rb
@@ -93,6 +93,32 @@ class CIMI::Model::MachineTemplate < CIMI::Model::Base
       current_db.machine_templates.first(:id => id).destroy
     end
 
+    def update_from_json!(id, attrs, body, context)
+      return if attrs.empty?
+      input = JSON::parse(body)
+      template = current_db.machine_templates.first(:id => id)
+      update_hash = attrs.inject({}) { |r,v|
+        next unless input.has_key?(v)
+        next unless template.respond_to?(v)
+        r[v] = input[v]
+        r
+      }
+      template.update!(update_hash)
+    end
+
+    def update_from_xml!(id, attrs, body, context)
+      return if attrs.empty?
+      input = XmlSimple.xml_in(body)
+      template = current_db.machine_templates.first(:id => id)
+      update_hash = attrs.inject({}) { |r,v|
+        next unless input.has_key?(v)
+        next unless template.respond_to?(v)
+        r[v] = input[v].first
+        r
+      }
+      template.update!(update_hash)
+    end
+
     private
 
     def from_db(model, context)
diff --git a/server/support/cimi/machine_template_update.json b/server/support/cimi/machine_template_update.json
new file mode 100644
index 0000000..afcf734
--- /dev/null
+++ b/server/support/cimi/machine_template_update.json
@@ -0,0 +1,4 @@
+{
+  "name": "myUpdatedDatabaseMachine",
+  "description": "This is a update sample"
+}
-- 
1.8.0.2


Mime
View raw message