deltacloud-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@apache.org
Subject [6/12] git commit: CIMI: Initial inclusion of CIMI action models
Date Fri, 22 Feb 2013 10:32:24 GMT
CIMI: Initial inclusion of CIMI action models

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.

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)


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

Branch: refs/heads/master
Commit: 73fb6a71be7989d681be984bcf758350442f03af
Parents: 44957a9
Author: Michal Fojtik <mfojtik@redhat.com>
Authored: Thu Feb 7 13:37:35 2013 +0100
Committer: Michal fojtik <mfojtik@redhat.com>
Committed: Fri Feb 22 11:31:44 2013 +0100

----------------------------------------------------------------------
 server/lib/cimi/models.rb                         |    2 +
 server/lib/cimi/models/machine_template.rb        |   27 ++++++++-------
 server/lib/cimi/models/machine_template_create.rb |   21 ++++++++++++
 3 files changed, 37 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltacloud/blob/73fb6a71/server/lib/cimi/models.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/models.rb b/server/lib/cimi/models.rb
index 8b8512a..3787a8a 100644
--- a/server/lib/cimi/models.rb
+++ b/server/lib/cimi/models.rb
@@ -82,3 +82,5 @@ require_relative './models/forwarding_group'
 require_relative './models/forwarding_group_template'
 require_relative './models/system_template'
 require_relative './models/system'
+require_relative './models/network_template'
+require_relative './models/network_create'

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/73fb6a71/server/lib/cimi/models/machine_template.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/models/machine_template.rb b/server/lib/cimi/models/machine_template.rb
index 43df953..920bab3 100644
--- a/server/lib/cimi/models/machine_template.rb
+++ b/server/lib/cimi/models/machine_template.rb
@@ -55,8 +55,7 @@ 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'],
@@ -68,17 +67,16 @@ class CIMI::Model::MachineTemplate < CIMI::Model::Base
       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'],
-        :realm => xml['realm'].first,
-        :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)
@@ -98,7 +96,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

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/73fb6a71/server/lib/cimi/models/machine_template_create.rb
----------------------------------------------------------------------
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


Mime
View raw message