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

Signed-off-by: Michal fojtik <mfoj...@redhat.com>
---
 server/lib/cimi/helpers/cmwg_helper.rb |  102 ++++++++++++++++++++++++++++++++
 1 files changed, 102 insertions(+), 0 deletions(-)
 create mode 100644 server/lib/cimi/helpers/cmwg_helper.rb

diff --git a/server/lib/cimi/helpers/cmwg_helper.rb 
b/server/lib/cimi/helpers/cmwg_helper.rb
new file mode 100644
index 0000000..9dae5a1
--- /dev/null
+++ b/server/lib/cimi/helpers/cmwg_helper.rb
@@ -0,0 +1,102 @@
+# 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.
+
+module ApplicationHelper
+  include Deltacloud
+
+  def render_resource(object_name, data)
+    respond_to do |format|
+      format.xml do
+        report_error 404 unless data
+        @object = data
+        content_type cimi_content_type_for(object_name.to_s.camelize, :xml)
+        haml :"#{object_name.to_s.pluralize}/show", :layout => false
+      end
+      format.json do
+        report_error 404 unless data
+        content_type cimi_content_type_for(object_name.to_s.camelize, :json)
+        resource_to_json(object_name, data)
+      end
+    end
+  end
+
+  def render_collection(collection_name)
+    name = (collection_name == :cloud_entry_point) ? collection_name : 
collection_name.to_s.pluralize
+    respond_to do |format|
+      format.html do
+        haml :"#{name}/index"
+      end
+      format.xml do
+        content_type cimi_content_type_for([collection_name.to_s.camelize, 
'Collection'].join, :xml)
+        haml :"#{name}/index", :layout => false
+      end
+      format.json do
+        content_type cimi_content_type_for([collection_name.to_s.camelize, 
'Collection'].join, :json)
+        collection_to_json collection_name, @resources
+      end
+    end
+  end
+
+  def cimi_content_type_for(coll_type, format="html")
+    case format
+    when :xml
+      "application/CIMI-%s+xml" % coll_type
+    when :json
+      "application/CIMI-%s+json" % coll_type
+    end
+  end
+
+  def collection_to_json(collection_name, resources=[])
+    name = collection_name == :cloud_entry_point ? collection_name.to_s : 
collection_name.to_s.pluralize
+    engine = Tilt::HamlTemplate.new(File.join(settings.views, name, 
'index.xml.haml'))
+    hash_response = XmlSimple.xml_in(engine.render(self, :'@resources' => 
resources))
+    hash_response.delete('xmlns')
+    hash_response.to_json
+  end
+
+  def resource_to_json(resource_name, object)
+    engine = Tilt::HamlTemplate.new(File.join(settings.views, 
resource_name.to_s.pluralize, 'show.xml.haml'))
+    hash_response = XmlSimple.xml_in(engine.render(self, :'@object' => object))
+    hash_response.delete('xmlns')
+    rename_key_fields!(hash_response, replacement_keys_for(resource_name))
+    hash_response.to_json
+  end
+
+  private
+
+  def replacement_keys_for(resource)
+    case resource
+    when :machine_image then {"property" => "properties", "operation" => 
"operations"}
+    when :machine then {"property" => "properties", "disk" => "disks", 
"operation" => "operations"}
+    when :colume then {"property" => "properties", "operation" => "operations"}
+    end
+  end
+
+  def rename_key_fields!(an_object, key_maps = nil)
+    if an_object.kind_of?(Hash)
+      key_maps.each do |key, value|
+        an_object[value] = an_object.delete(key) if an_object.key?(key)
+      end
+      an_object.each do |key, value|
+        rename_key_fields!(value, key_maps)
+      end
+    elsif an_object.kind_of?(Array)
+      an_object.each do |value|
+        rename_key_fields!(value, key_maps)
+      end
+    end
+  end
+
+end
-- 
1.7.4.4

Reply via email to