From: Michal Fojtik <mfoj...@redhat.com>

Signed-off-by: Michal fojtik <mfoj...@redhat.com>
---
 server/lib/cimi/collections/volume_templates.rb  |  6 ++---
 server/lib/cimi/models.rb                        |  3 ++-
 server/lib/cimi/models/volume_template.rb        | 25 +++++-------------
 server/lib/cimi/models/volume_template_create.rb | 33 ++++++++++++++++++++++++
 server/support/cimi/volume_template.xml          |  7 +++++
 5 files changed, 51 insertions(+), 23 deletions(-)
 create mode 100644 server/lib/cimi/models/volume_template_create.rb
 create mode 100644 server/support/cimi/volume_template.xml

diff --git a/server/lib/cimi/collections/volume_templates.rb 
b/server/lib/cimi/collections/volume_templates.rb
index 720e73b..b844ed8 100644
--- a/server/lib/cimi/collections/volume_templates.rb
+++ b/server/lib/cimi/collections/volume_templates.rb
@@ -45,9 +45,9 @@ module CIMI::Collections
       operation :create, :with_capability => :create_storage_volume do
         description "Create new VolumeTemplate"
         control do
-          new_template = CIMI::Model::VolumeTemplate.create(
-            request.body.read, self, current_content_type
-          )
+          puts request.body
+          vol = CIMI::Model::VolumeTemplateCreate.parse(request.body, 
request.content_type)
+          new_template = vol.create(self)
           headers_for_create new_template
           respond_to do |format|
             format.json { new_template.to_json }
diff --git a/server/lib/cimi/models.rb b/server/lib/cimi/models.rb
index a86d6a9..6c89a93 100644
--- a/server/lib/cimi/models.rb
+++ b/server/lib/cimi/models.rb
@@ -64,9 +64,10 @@ require_relative './models/credential'
 require_relative './models/credential_template'
 require_relative './models/credential_create'
 require_relative './models/volume'
-require_relative './models/volume_template'
 require_relative './models/volume_configuration'
 require_relative './models/volume_image'
+require_relative './models/volume_template'
+require_relative './models/volume_template_create'
 require_relative './models/machine'
 require_relative './models/machine_configuration'
 require_relative './models/machine_image'
diff --git a/server/lib/cimi/models/volume_template.rb 
b/server/lib/cimi/models/volume_template.rb
index c00ffc8..15cbcd0 100644
--- a/server/lib/cimi/models/volume_template.rb
+++ b/server/lib/cimi/models/volume_template.rb
@@ -17,8 +17,8 @@ class CIMI::Model::VolumeTemplate < CIMI::Model::Base
 
   acts_as_root_entity
 
-  href :volume_config
-  href :volume_image
+  ref :volume_config, :required => true
+  ref :volume_image, :required => true
 
   array :meter_templates do
   end
@@ -47,26 +47,10 @@ class CIMI::Model::VolumeTemplate < CIMI::Model::Base
     end
   end
 
-  def self.create(body, context, type)
-    input = (type == :xml)? XmlSimple.xml_in(body, 
{"ForceArray"=>false,"NormaliseSpace"=>2}) : JSON.parse(body)
-    input['property'] ||= []
-    vol_image = input['volumeImage']['href'] if input['volumeImage']
-    new_template = current_db.add_volume_template(
-      :name => input['name'],
-      :description => input['description'],
-      :volume_config => input['volumeConfig']['href'],
-      :volume_image => vol_image,
-      :ent_properties => type == :xml ? JSON::dump((xml['property'] || 
{}).inject({}){ |r, p| r[p['key']]=p['content']; r })  : (json['properties'] || 
{}).to_json
-    )
-    from_db(new_template, context)
-  end
-
   def self.delete!(id, context)
     current_db.volume_templates.first(:id => id).destroy
   end
 
-private
-
   def self.from_db(model, context)
     self.new(
       :id => context.volume_template_url(model.id),
@@ -76,7 +60,10 @@ private
       :volume_image => {:href => model.volume_image},
       :property => (model.ent_properties ? JSON::parse(model.ent_properties) : 
 nil),
       :operations => [
-        { :href => context.destroy_volume_template_url(model.id), :rel => 
'http://schemas.dmtf.org/cimi/1/action/delete' }
+        {
+          :href => context.destroy_volume_template_url(model.id),
+          :rel => 'http://schemas.dmtf.org/cimi/1/action/delete'
+        }
       ]
     )
   end
diff --git a/server/lib/cimi/models/volume_template_create.rb 
b/server/lib/cimi/models/volume_template_create.rb
new file mode 100644
index 0000000..b6dbec2
--- /dev/null
+++ b/server/lib/cimi/models/volume_template_create.rb
@@ -0,0 +1,33 @@
+# 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::VolumeTemplateCreate < CIMI::Model::Base
+
+  ref :volume_config, :required => true
+  ref :volume_image, :required => true
+
+  def create(context)
+    validate!
+    new_template = self.class.current_db.add_volume_template(
+      :name => name,
+      :description => description,
+      :volume_config => volume_config.href,
+      :volume_image => volume_image.href,
+      :ent_properties => property.to_json
+    )
+    CIMI::Model::VolumeTemplate.from_db(new_template, context)
+  end
+
+end
diff --git a/server/support/cimi/volume_template.xml 
b/server/support/cimi/volume_template.xml
new file mode 100644
index 0000000..0e1d4fa
--- /dev/null
+++ b/server/support/cimi/volume_template.xml
@@ -0,0 +1,7 @@
+<VolumeTemplateCreate>
+  <name>myXMLVolumeTemplate</name>
+  <description>Description of VolumeTemplate</description>
+  <property key="test">value</property>
+  <volumeConfig href="http://localhost:3001/cimi/volume_configurations/1"/>
+  <volumeImage href="http://localhost:3001/cimi/volume_images/snap1"/>
+</VolumeTemplateCreate>
-- 
1.8.1.2

Reply via email to