deltacloud-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dko...@apache.org
Subject git commit: Mock driver: convert all stub URL's
Date Wed, 03 Apr 2013 10:19:21 GMT
Updated Branches:
  refs/heads/master b114f2e96 -> 61122c833


Mock driver: convert all stub URL's

There were cases where we didn't catch stubbed out URL's and reported
invalid references with the mock driver. This fixes that and simplifies the
URL conversion code.


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

Branch: refs/heads/master
Commit: 61122c833d9b1e779f53c8a6b8fda30272c79276
Parents: b114f2e
Author: David Lutterkort <lutter@redhat.com>
Authored: Fri Mar 15 15:53:41 2013 -0700
Committer: Dies Koper <diesk@fast.au.fujitsu.com>
Committed: Wed Apr 3 21:18:50 2013 +1100

----------------------------------------------------------------------
 .../mock/data/cimi/forwarding_group/group1.json    |    6 +-
 .../drivers/mock/mock_driver_cimi_methods.rb       |  111 ++++++++-------
 2 files changed, 60 insertions(+), 57 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltacloud/blob/61122c83/server/lib/deltacloud/drivers/mock/data/cimi/forwarding_group/group1.json
----------------------------------------------------------------------
diff --git a/server/lib/deltacloud/drivers/mock/data/cimi/forwarding_group/group1.json b/server/lib/deltacloud/drivers/mock/data/cimi/forwarding_group/group1.json
index e10e0bd..04f9266 100644
--- a/server/lib/deltacloud/drivers/mock/data/cimi/forwarding_group/group1.json
+++ b/server/lib/deltacloud/drivers/mock/data/cimi/forwarding_group/group1.json
@@ -1,4 +1,4 @@
-{ "id": "http://cimi.example.org/routing_groups/group1",
+{ "id": "http://cimi.example.org/forwarding_groups/group1",
   "name": "group1",
   "description": "a mock routing group",
   "created": "Thu Jan 12 16:02:56 EET 2012",
@@ -7,7 +7,7 @@
     { "href": "http://cimi.example.org/networks/network2"}
   ],
   "operations": [
-    { "rel": "edit", "href": "http://cimi.example.org/routing_groups/group1" },
-    { "rel": "delete", "href": "http://cimi.example.org/routing_groups/group1" }
+    { "rel": "edit", "href": "http://cimi.example.org/forwarding_groups/group1" },
+    { "rel": "delete", "href": "http://cimi.example.org/forwarding_groups/group1" }
   ]
 }

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/61122c83/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
index 9a6daa2..b0d4f9a 100644
--- a/server/lib/deltacloud/drivers/mock/mock_driver_cimi_methods.rb
+++ b/server/lib/deltacloud/drivers/mock/mock_driver_cimi_methods.rb
@@ -32,7 +32,7 @@ module Deltacloud::Drivers::Mock
           return []
         end
       end
-      systems.map{|sys|convert_cimi_mock_urls(:system, sys ,opts[:env])}.flatten
+      systems.map{|sys| convert_urls(sys, opts[:env])}.flatten
     end
 
     def create_system(credentials, opts={})
@@ -155,17 +155,17 @@ module Deltacloud::Drivers::Mock
           return []
         end
       end
-      system_templates.map{|sys_templ|convert_cimi_mock_urls(:system_template, sys_templ,
opts[:env])}.flatten
+      system_templates.map{|sys_templ| convert_urls(sys_templ, opts[:env])}.flatten
     end
 
     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
+        networks.map{|net| convert_urls(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])
+        convert_urls(network, opts[:env])
       end
     end
 
@@ -215,10 +215,10 @@ module Deltacloud::Drivers::Mock
       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
+        network_configs.map{|net_config| convert_urls(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])
+        convert_urls(network_config, opts[:env])
       end
     end
 
@@ -226,10 +226,10 @@ module Deltacloud::Drivers::Mock
       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
+        network_templates.map{|net_templ| convert_urls(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])
+        convert_urls(network_template, opts[:env])
       end
     end
 
@@ -237,10 +237,10 @@ module Deltacloud::Drivers::Mock
       check_credentials(credentials)
       if opts[:id].nil?
         forwarding_groups = @client.load_all_cimi(:forwarding_group).map{|fg| CIMI::Model::ForwardingGroup.from_json(fg)}
-        forwarding_groups.map{|fg|convert_cimi_mock_urls(:forwarding_group, fg, opts[:env])}.flatten
+        forwarding_groups.map{|fg| convert_urls(fg, opts[:env])}.flatten
       else
         forwarding_group = CIMI::Model::ForwardingGroup.from_json(@client.load_cimi(:forwarding_group,
opts[:id]))
-        convert_cimi_mock_urls(:forwarding_group, forwarding_group, opts[:env])
+        convert_urls(forwarding_group, opts[:env])
       end
     end
 
@@ -248,10 +248,10 @@ module Deltacloud::Drivers::Mock
       check_credentials(credentials)
       if opts[:id].nil?
         forwarding_group_templates = @client.load_all_cimi(:forwarding_group_template).map{|fg_templ|
CIMI::Model::ForwardingGroupTemplate.from_json(fg_templ)}
-        forwarding_group_templates.map{|fg_templ|convert_cimi_mock_urls(:forwarding_group_template,
fg_templ, opts[:env])}.flatten
+        forwarding_group_templates.map{|fg_templ| convert_urls(fg_templ, opts[:env])}.flatten
       else
         forwarding_group_template = CIMI::Model::ForwardingGroupTemplate.from_json(@client.load_cimi(:forwarding_group_template,
opts[:id]))
-        convert_cimi_mock_urls(:forwarding_group_template, forwarding_group_template, opts[:env])
+        convert_urls(forwarding_group_template, opts[:env])
       end
     end
 
@@ -259,10 +259,10 @@ module Deltacloud::Drivers::Mock
       check_credentials(credentials)
       if opts[:id].nil?
         ports = @client.load_all_cimi(:network_port).map{|net_port| CIMI::Model::NetworkPort.from_json(net_port)}
-        ports.map{|net_port|convert_cimi_mock_urls(:network_port, net_port, opts[:env])}.flatten
+        ports.map{|net_port| convert_urls(net_port, opts[:env])}.flatten
       else
         port = CIMI::Model::NetworkPort.from_json(@client.load_cimi(:network_port, opts[:id]))
-        convert_cimi_mock_urls(:network_port, port, opts[:env])
+        convert_urls(port, opts[:env])
       end
     end
 
@@ -270,10 +270,10 @@ module Deltacloud::Drivers::Mock
       check_credentials(credentials)
       if opts[:id].nil?
         network_port_configurations = @client.load_all_cimi(:network_port_configuration).map{|network_port_config|
CIMI::Model::NetworkPortConfiguration.from_json(network_port_config)}
-        network_port_configurations.map{|network_port_config|convert_cimi_mock_urls(:network_port_configuration,
network_port_config, opts[:env])}.flatten
+        network_port_configurations.map{|network_port_config| convert_urls(network_port_config,
opts[:env])}.flatten
       else
         network_port_configuration = CIMI::Model::NetworkPortConfiguration.from_json(@client.load_cimi(:network_port_configuration,
opts[:id]))
-        convert_cimi_mock_urls(:network_port_configuration, network_port_configuration, opts[:env])
+        convert_urls(network_port_configuration, opts[:env])
       end
     end
 
@@ -281,10 +281,10 @@ module Deltacloud::Drivers::Mock
       check_credentials(credentials)
       if opts[:id].nil?
         network_port_templates = @client.load_all_cimi(:network_port_template).map{|net_port_templ|
CIMI::Model::NetworkPortTemplate.from_json(net_port_templ)}
-        network_port_templates.map{|net_port_templ|convert_cimi_mock_urls(:network_port_template,
net_port_templ, opts[:env])}.flatten
+        network_port_templates.map{|net_port_templ| convert_urls(net_port_templ, opts[:env])}.flatten
       else
         network_port_template = CIMI::Model::NetworkPortTemplate.from_json(@client.load_cimi(:network_port_template,
opts[:id]))
-        convert_cimi_mock_urls(:network_port_template, network_port_template, opts[:env])
+        convert_urls(network_port_template, opts[:env])
       end
     end
 
@@ -292,54 +292,57 @@ module Deltacloud::Drivers::Mock
       check_credentials(credentials)
       if opts[:id].nil?
         address_templates = @client.load_all_cimi(:address_template).map{|addr_templ| CIMI::Model::AddressTemplate.from_json(addr_templ)}
-        address_templates.map{|addr_templ|convert_cimi_mock_urls(:address_template, addr_templ,
opts[:env])}.flatten
+        address_templates.map{|addr_templ| convert_urls(addr_templ, opts[:env])}.flatten
       else
         address_template = CIMI::Model::AddressTemplate.from_json(@client.load_cimi(:address_template,
opts[:id]))
-        convert_cimi_mock_urls(:address_template, address_template, opts[:env])
+        convert_urls(address_template, 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
-              opts = nil
-              if is_subcollection?(v, cimi_object.id)
-                opts = {:parent_model_name => model_name, :parent_item_name => cimi_object.name}
-              end
-              convert_struct_urls(v, k, context, opts)
-            end
+    # Convert all attributes that have values of the form
+    #   http://cimi.example.org/COLL/ID
+    def convert_urls(val, context)
+      if val.nil? || val.is_a?(Fixnum)
+        # Nothing to do
+      elsif val.is_a?(Struct)
+        val.members.each { |m| val[m] = convert_urls(val[m], context) }
+      elsif val.is_a?(Hash)
+        val.keys.each { |m| val[m] = convert_urls(val[m], context) }
+      elsif val.is_a?(Array)
+        val.each_index { |i| val[i] = convert_urls(val[i], context) }
+      elsif val.is_a?(String)
+        val = rewrite_url(val, context)
+      elsif val.is_a?(CIMI::Model::Resource)
+        val.attribute_values.each do |k, v|
+          val[k] = convert_urls(val[k], context)
         end
+      else
+        # Need to add a branch for val.class
+        raise "Whoa ! #{val.inspect}"
       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 is_subcollection?(struct, cimi_object_id)
-      return false if struct.href.nil?
-      struct.href.include?(cimi_object_id)
+      val
     end
 
-    def convert_struct_urls(struct, cimi_name, context, opts = nil)
-      return unless (struct.respond_to?(:href) && (not struct.href.nil?) &&
(not cimi_name == :operation ))
-      if opts
-        struct.href = context.send(:"#{opts[:parent_model_name]}_url", opts[:parent_item_name])
+ "/#{cimi_name}"
+    # Rewrite URL assuming it points at a valid resource; if that's not
+    # possible, return +s+
+    #
+    # URLs that should be rewritten need to be in the form
+    #   http://cimi.example.org/COLLECTION/ID
+    def rewrite_url(s, context)
+      begin
+        u = URI.parse(s)
+      rescue URI::InvalidURIError
+        return s
+      end
+      return s unless u.scheme == 'http' && u.host == 'cimi.example.org'
+      _, coll, id = u.path.split("/")
+      method = "#{coll.singularize}_url"
+      if context.respond_to?(method)
+        context.send(method, id)
       else
-        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
+        s
       end
     end
 


Mime
View raw message