You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltacloud.apache.org by ma...@apache.org on 2012/03/16 14:42:53 UTC

[1/3] git commit: CIMI: separates the CIMI mocking methods to a new file for cleanness

Updated Branches:
  refs/heads/master 86cb93952 -> 644c31a72


CIMI: separates the CIMI mocking methods to a new file for cleanness


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

Branch: refs/heads/master
Commit: 644c31a72cacea735bb90990f017c70ecd60b968
Parents: d4f3106
Author: marios <ma...@redhat.com>
Authored: Fri Mar 16 15:41:28 2012 +0200
Committer: marios <ma...@redhat.com>
Committed: Fri Mar 16 15:41:28 2012 +0200

----------------------------------------------------------------------
 .../drivers/mock/mock_driver_cimi_methods.rb       |  101 +++++++++++++++
 1 files changed, 101 insertions(+), 0 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltacloud/blob/644c31a7/server/lib/deltacloud/drivers/mock/mock_driver_cimi_methods.rb
----------------------------------------------------------------------
diff --git a/server/lib/deltacloud/drivers/mock/mock_driver_cimi_methods.rb b/server/lib/deltacloud/drivers/mock/mock_driver_cimi_methods.rb
new file mode 100644
index 0000000..1e7b3c2
--- /dev/null
+++ b/server/lib/deltacloud/drivers/mock/mock_driver_cimi_methods.rb
@@ -0,0 +1,101 @@
+#
+# 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.
+#
+#Definition of CIMI methods for the mock driver - seperation from deltacloud
+#API mock driver methods
+
+module Deltacloud::Drivers::Mock
+
+  class MockDriver < Deltacloud::BaseDriver
+
+    def networks(credentials, opts={})
+      check_credentials(credentials)
+      if opts[:id].nil?
+        networks = @client.load_all_cimi(:network).map{|net| CIMI::Model::Network.from_json(net)}
+        networks.map{|net|convert_cimi_mock_urls(:network, net ,opts[:env])}.flatten
+      else
+        network = CIMI::Model::Network.from_json(@client.load_cimi(:network, opts[:id]))
+        convert_cimi_mock_urls(:network, network, opts[:env])
+      end
+    end
+
+    def network_configurations(credentials, opts={})
+      check_credentials(credentials)
+      if opts[:id].nil?
+        network_configs = @client.load_all_cimi(:network_configuration).map{|net_config| CIMI::Model::NetworkConfiguration.from_json(net_config)}
+        network_configs.map{|net_config|convert_cimi_mock_urls(:network_configuration, net_config, opts[:env])}.flatten
+      else
+        network_config = CIMI::Model::NetworkConfiguration.from_json(@client.load_cimi(:network_configuration, opts[:id]))
+        convert_cimi_mock_urls(:network_configuration, network_config, opts[:env])
+      end
+    end
+
+    def network_templates(credentials, opts={})
+      check_credentials(credentials)
+      if opts[:id].nil?
+        network_templates = @client.load_all_cimi(:network_template).map{|net_templ| CIMI::Model::NetworkTemplate.from_json(net_templ)}
+        network_templates.map{|net_templ|convert_cimi_mock_urls(:network_template, net_templ, opts[:env])}.flatten
+      else
+        network_template = CIMI::Model::NetworkTemplate.from_json(@client.load_cimi(:network_template, opts[:id]))
+        convert_cimi_mock_urls(:network_template, network_template, opts[:env])
+      end
+    end
+
+    def routing_groups(credentials, opts={})
+      check_credentials(credentials)
+      if opts[:id].nil?
+        routing_groups = @client.load_all_cimi(:routing_group).map{|rg| CIMI::Model::RoutingGroup.from_json(rg)}
+        routing_groups.map{|rg|convert_cimi_mock_urls(:routing_group, rg, opts[:env])}.flatten
+      else
+        routing_group = CIMI::Model::RoutingGroup.from_json(@client.load_cimi(:routing_group, opts[:id]))
+        convert_cimi_mock_urls(:routing_group, routing_group, opts[:env])
+      end
+    end
+
+    private
+
+    def convert_cimi_mock_urls(model_name, cimi_object, context)
+      cimi_object.attribute_values.each do |k,v|
+        if ( v.is_a?(Struct) || ( v.is_a?(Array) && v.first.is_a?(Struct)))
+          case v
+            when Array
+              v.each do |item|
+                convert_struct_urls(item, k.to_s.singularize.to_sym, context)
+              end
+            else
+              convert_struct_urls(v, k, context)
+            end
+        end
+      end
+      object_url = context.send(:"#{model_name}_url", cimi_object.name)
+      cimi_object.id=object_url
+      cimi_object.operations.each{|op| op.href=object_url  }
+      cimi_object
+    end
+
+    def convert_struct_urls(struct, cimi_name, context)
+      return unless (struct.respond_to?(:href) && (not struct.href.nil?) && (not cimi_name == :operation ))
+      obj_name = struct.href.split("/").last
+      if cimi_name.to_s.end_with?("config")
+        struct.href = context.send(:"#{cimi_name}uration_url", obj_name)
+      else
+        struct.href = context.send(:"#{cimi_name}_url", obj_name)
+      end
+    end
+
+  end
+
+end