deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@redhat.com
Subject [PATCH core 06/11] CIMI: Initial inclusion of CIMI action models
Date Mon, 11 Feb 2013 12:35:47 GMT
From: Michal Fojtik <mfojtik@redhat.com>

The way of how we currently create CIMI entities is not
really nice. We parse the raw JSON/XML and then we are
trying to create the CIMI entity.

Besides the code looks ugly, there is no way to add validation
for required attributes without make it even more uglier.

This patch introduce the 'models/actions/' directory where
we can store the CIMI 'actions' model, like 'MachineTemplateCreate'
model.

Then in MachineTemplate model, we can leverage the '.from_xml' and
the '.from_json' methods to create the action model and then use it
to create the CIMI entity (MachineTemplate in this case)

Signed-off-by: Michal fojtik <mfojtik@redhat.com>
---
 server/lib/cimi/models.rb                         |  2 ++
 server/lib/cimi/models/machine_template.rb        | 36 ++++++++++++-----------
 server/lib/cimi/models/machine_template_create.rb | 21 +++++++++++++
 3 files changed, 42 insertions(+), 17 deletions(-)
 create mode 100644 server/lib/cimi/models/machine_template_create.rb

diff --git a/server/lib/cimi/models.rb b/server/lib/cimi/models.rb
index dd19a8a..892637d 100644
--- a/server/lib/cimi/models.rb
+++ b/server/lib/cimi/models.rb
@@ -82,3 +82,5 @@ require_relative './models/address'
 require_relative './models/address_template'
 require_relative './models/forwarding_group'
 require_relative './models/forwarding_group_template'
+
+require_relative './models/actions/machine_template_create'
diff --git a/server/lib/cimi/models/machine_template.rb b/server/lib/cimi/models/machine_template.rb
index 9e3f198..5967250 100644
--- a/server/lib/cimi/models/machine_template.rb
+++ b/server/lib/cimi/models/machine_template.rb
@@ -52,28 +52,27 @@ class CIMI::Model::MachineTemplate < CIMI::Model::Base
       end
     end
 
-    def create_from_json(body, context)
-      json = JSON.parse(body)
+    def create(template, context)
       new_template = current_db.add_machine_template(
-        :name => json['name'],
-        :description => json['description'],
-        :machine_config => json['machineConfig']['href'],
-        :machine_image => json['machineImage']['href'],
-        :ent_properties => json['properties'] ? json['properties'].to_json : {}
+        :name => template.name,
+        :description => template.description,
+        :machine_config => template.machine_config.href,
+        :machine_image => template.machine_image.href,
+        :ent_properties => template.property.to_json
       )
       from_db(new_template, context)
     end
 
+    def create_from_json(body, context)
+      template = CIMI::Model::MachineTemplateCreate.from_json(body)
+      template.validate!(:json)
+      create(template, context)
+    end
+
     def create_from_xml(body, context)
-      xml = XmlSimple.xml_in(body)
-      new_template = current_db.add_machine_template(
-        :name => xml['name'].first,
-        :description => xml['description'].first,
-        :machine_config => xml['machineConfig'].first['href'],
-        :machine_image => xml['machineImage'].first['href'],
-        :ent_properties => xml['property'] ? JSON::dump(xml['property'].inject({}) { |r,
p| r[p['key']]=p['content']; r }) : {}
-      )
-      from_db(new_template, context)
+      template = CIMI::Model::MachineTemplateCreate.from_xml(body)
+      template.validate!(:xml)
+      create(template, context)
     end
 
     def delete!(id, context)
@@ -92,7 +91,10 @@ class CIMI::Model::MachineTemplate < CIMI::Model::Base
         :property => (model.ent_properties ? JSON::parse(model.ent_properties) :  nil),
         :created => Time.parse(model.created_at.to_s).xmlschema,
         :operations => [
-          { :href => context.destroy_machine_template_url(model.id), :rel => 'http://schemas.dmtf.org/cimi/1/action/delete'
}
+          {
+            :href => context.destroy_machine_template_url(model.id),
+            :rel => 'http://schemas.dmtf.org/cimi/1/action/delete'
+          }
         ]
       )
     end
diff --git a/server/lib/cimi/models/machine_template_create.rb b/server/lib/cimi/models/machine_template_create.rb
new file mode 100644
index 0000000..4d46b79
--- /dev/null
+++ b/server/lib/cimi/models/machine_template_create.rb
@@ -0,0 +1,21 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License.  You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+class CIMI::Model::MachineTemplateCreate < CIMI::Model::Base
+
+  href :machine_config, :required => true
+  href :machine_image, :required => true
+
+end
-- 
1.8.1.2


Mime
View raw message