You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltacloud.apache.org by ma...@redhat.com on 2012/03/15 16:59:55 UTC

More CIMI Networking

Continuation of the CIMI Network entities.

1/3 adds the network_template and routing_group (+collection) entities and routes in server.rb
2/3 adds appropriate methods to mock driver and mocking data in json format for new entities
3/3 moves the CIMI mocking methods out of mock_driver for clean separation

Re: [PATCH 2/3] CIMI: adds network_templates and routing_groups to deltacloud mock driver and mocking data (json)

Posted by Michal Fojtik <mf...@redhat.com>.
On Mar 15, 2012, at 4:59 PM, marios@redhat.com wrote:

ACK.

> From: marios <ma...@redhat.com>
> 
> 
> Signed-off-by: marios <ma...@redhat.com>
> ---
> .../drivers/mock/data/cimi/network/network1.json   |    2 +-
> .../drivers/mock/data/cimi/network/network2.json   |    2 +-
> .../network_configuration/network_config1.json     |    2 +-
> .../network_configuration/network_config2.json     |    2 +-
> .../mock/data/cimi/network_template/template1.json |   10 ++++
> .../mock/data/cimi/network_template/template2.json |   10 ++++
> .../mock/data/cimi/routing_group/group1.json       |   13 +++++
> server/lib/deltacloud/drivers/mock/mock_driver.rb  |   50 ++++++++++++++++++-
> 8 files changed, 84 insertions(+), 7 deletions(-)
> create mode 100644 server/lib/deltacloud/drivers/mock/data/cimi/network_template/template1.json
> create mode 100644 server/lib/deltacloud/drivers/mock/data/cimi/network_template/template2.json
> create mode 100644 server/lib/deltacloud/drivers/mock/data/cimi/routing_group/group1.json
> 
> diff --git a/server/lib/deltacloud/drivers/mock/data/cimi/network/network1.json b/server/lib/deltacloud/drivers/mock/data/cimi/network/network1.json
> index 165ef8a..28cf767 100644
> --- a/server/lib/deltacloud/drivers/mock/data/cimi/network/network1.json
> +++ b/server/lib/deltacloud/drivers/mock/data/cimi/network/network1.json
> @@ -1,4 +1,4 @@
> -{ "uri": "http://cimi.example.org/networks/network1",
> +{ "id": "http://cimi.example.org/networks/network1",
>   "name": "network1",
>   "description": "the first network",
>   "created": "Thu Jan 12 15:25:41 EET 2012",
> diff --git a/server/lib/deltacloud/drivers/mock/data/cimi/network/network2.json b/server/lib/deltacloud/drivers/mock/data/cimi/network/network2.json
> index 3bd264e..370db99 100644
> --- a/server/lib/deltacloud/drivers/mock/data/cimi/network/network2.json
> +++ b/server/lib/deltacloud/drivers/mock/data/cimi/network/network2.json
> @@ -1,4 +1,4 @@
> -{ "uri": "http://cimi.example.org/networks/network2",
> +{ "id": "http://cimi.example.org/networks/network2",
>   "name": "network2",
>   "description": "another network",
>   "created": "Fri Jan 13 17:30:34 EET 2012",
> diff --git a/server/lib/deltacloud/drivers/mock/data/cimi/network_configuration/network_config1.json b/server/lib/deltacloud/drivers/mock/data/cimi/network_configuration/network_config1.json
> index f767de9..8fad5d3 100644
> --- a/server/lib/deltacloud/drivers/mock/data/cimi/network_configuration/network_config1.json
> +++ b/server/lib/deltacloud/drivers/mock/data/cimi/network_configuration/network_config1.json
> @@ -1,4 +1,4 @@
> -{ "uri": "http://cimi.example.org/network_configurations/network_config1",
> +{ "id": "http://cimi.example.org/network_configurations/network_config1",
>   "name": "network_config1",
>   "description": "a network configuration",
>   "created": "Fri Mar 9 11:57:39 EET 2012",
> diff --git a/server/lib/deltacloud/drivers/mock/data/cimi/network_configuration/network_config2.json b/server/lib/deltacloud/drivers/mock/data/cimi/network_configuration/network_config2.json
> index b22d837..e489bf5 100644
> --- a/server/lib/deltacloud/drivers/mock/data/cimi/network_configuration/network_config2.json
> +++ b/server/lib/deltacloud/drivers/mock/data/cimi/network_configuration/network_config2.json
> @@ -1,4 +1,4 @@
> -{ "uri": "http://cimi.example.org/network_configurations/network_config2",
> +{ "id": "http://cimi.example.org/network_configurations/network_config2",
>   "name": "network_config2",
>   "description": "a network configuration",
>   "created": "Fri Mar 9 12:01:53 EET 2012",
> diff --git a/server/lib/deltacloud/drivers/mock/data/cimi/network_template/template1.json b/server/lib/deltacloud/drivers/mock/data/cimi/network_template/template1.json
> new file mode 100644
> index 0000000..682d39b
> --- /dev/null
> +++ b/server/lib/deltacloud/drivers/mock/data/cimi/network_template/template1.json
> @@ -0,0 +1,10 @@
> +{ "id": "http://cimi.example.org/network_templates/template1",
> +  "name": "template1",
> +  "description": "A mock network template",
> +  "created": "Thu Mar 15 12:15:15 EET 2012",
> +  "networkConfig": {"href": "http://cimi.example.org/network_configurations/network_config1"},
> +  "routingGroup": {"href": "http://cimi.example.org/routing_groups/group1"},
> +  "operations": [
> +    { "rel": "edit", "href": "http://cimi.example.org/network_templates/template1" },
> +    { "rel": "delete", "href": "http://cimi.example.org/network_templates/template1" }]
> +}
> diff --git a/server/lib/deltacloud/drivers/mock/data/cimi/network_template/template2.json b/server/lib/deltacloud/drivers/mock/data/cimi/network_template/template2.json
> new file mode 100644
> index 0000000..b7222cc
> --- /dev/null
> +++ b/server/lib/deltacloud/drivers/mock/data/cimi/network_template/template2.json
> @@ -0,0 +1,10 @@
> +{ "id": "http://cimi.example.org/network_templates/template2",
> +  "name": "template2",
> +  "description": "Another mock network template",
> +  "created": "Thu Mar 15 13:51:12 EET 2012",
> +  "networkConfig": {"href": "http://cimi.example.org/network_configurations/network_config2"},
> +  "routingGroup": {"href": "http://cimi.example.org/routing_groups/group1"},
> +  "operations": [
> +    { "rel": "edit", "href": "http://cimi.example.org/network_templates/template2" },
> +    { "rel": "delete", "href": "http://cimi.example.org/network_templates/template2" }]
> +}
> diff --git a/server/lib/deltacloud/drivers/mock/data/cimi/routing_group/group1.json b/server/lib/deltacloud/drivers/mock/data/cimi/routing_group/group1.json
> new file mode 100644
> index 0000000..e10e0bd
> --- /dev/null
> +++ b/server/lib/deltacloud/drivers/mock/data/cimi/routing_group/group1.json
> @@ -0,0 +1,13 @@
> +{ "id": "http://cimi.example.org/routing_groups/group1",
> +  "name": "group1",
> +  "description": "a mock routing group",
> +  "created": "Thu Jan 12 16:02:56 EET 2012",
> +  "networks": [
> +    { "href": "http://cimi.example.org/networks/network1"},
> +    { "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" }
> +  ]
> +}
> diff --git a/server/lib/deltacloud/drivers/mock/mock_driver.rb b/server/lib/deltacloud/drivers/mock/mock_driver.rb
> index 1725538..83dae09 100644
> --- a/server/lib/deltacloud/drivers/mock/mock_driver.rb
> +++ b/server/lib/deltacloud/drivers/mock/mock_driver.rb
> @@ -475,6 +475,28 @@ module Deltacloud::Drivers::Mock
>       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 check_credentials(credentials)
> @@ -512,12 +534,34 @@ module Deltacloud::Drivers::Mock
>     end
> 
>     def convert_cimi_mock_urls(model_name, cimi_object, context)
> -      uri=context.send(:"#{model_name}_url", cimi_object.name)
> -      cimi_object.id=uri
> -      cimi_object.operations.each{|op| op.href=uri}
> +      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
> +
>     exceptions do
> 
>       on /AuthFailure/ do
> -- 
> 1.7.6.5
> 


[PATCH 2/3] CIMI: adds network_templates and routing_groups to deltacloud mock driver and mocking data (json)

Posted by ma...@redhat.com.
From: marios <ma...@redhat.com>


Signed-off-by: marios <ma...@redhat.com>
---
 .../drivers/mock/data/cimi/network/network1.json   |    2 +-
 .../drivers/mock/data/cimi/network/network2.json   |    2 +-
 .../network_configuration/network_config1.json     |    2 +-
 .../network_configuration/network_config2.json     |    2 +-
 .../mock/data/cimi/network_template/template1.json |   10 ++++
 .../mock/data/cimi/network_template/template2.json |   10 ++++
 .../mock/data/cimi/routing_group/group1.json       |   13 +++++
 server/lib/deltacloud/drivers/mock/mock_driver.rb  |   50 ++++++++++++++++++-
 8 files changed, 84 insertions(+), 7 deletions(-)
 create mode 100644 server/lib/deltacloud/drivers/mock/data/cimi/network_template/template1.json
 create mode 100644 server/lib/deltacloud/drivers/mock/data/cimi/network_template/template2.json
 create mode 100644 server/lib/deltacloud/drivers/mock/data/cimi/routing_group/group1.json

diff --git a/server/lib/deltacloud/drivers/mock/data/cimi/network/network1.json b/server/lib/deltacloud/drivers/mock/data/cimi/network/network1.json
index 165ef8a..28cf767 100644
--- a/server/lib/deltacloud/drivers/mock/data/cimi/network/network1.json
+++ b/server/lib/deltacloud/drivers/mock/data/cimi/network/network1.json
@@ -1,4 +1,4 @@
-{ "uri": "http://cimi.example.org/networks/network1",
+{ "id": "http://cimi.example.org/networks/network1",
   "name": "network1",
   "description": "the first network",
   "created": "Thu Jan 12 15:25:41 EET 2012",
diff --git a/server/lib/deltacloud/drivers/mock/data/cimi/network/network2.json b/server/lib/deltacloud/drivers/mock/data/cimi/network/network2.json
index 3bd264e..370db99 100644
--- a/server/lib/deltacloud/drivers/mock/data/cimi/network/network2.json
+++ b/server/lib/deltacloud/drivers/mock/data/cimi/network/network2.json
@@ -1,4 +1,4 @@
-{ "uri": "http://cimi.example.org/networks/network2",
+{ "id": "http://cimi.example.org/networks/network2",
   "name": "network2",
   "description": "another network",
   "created": "Fri Jan 13 17:30:34 EET 2012",
diff --git a/server/lib/deltacloud/drivers/mock/data/cimi/network_configuration/network_config1.json b/server/lib/deltacloud/drivers/mock/data/cimi/network_configuration/network_config1.json
index f767de9..8fad5d3 100644
--- a/server/lib/deltacloud/drivers/mock/data/cimi/network_configuration/network_config1.json
+++ b/server/lib/deltacloud/drivers/mock/data/cimi/network_configuration/network_config1.json
@@ -1,4 +1,4 @@
-{ "uri": "http://cimi.example.org/network_configurations/network_config1",
+{ "id": "http://cimi.example.org/network_configurations/network_config1",
   "name": "network_config1",
   "description": "a network configuration",
   "created": "Fri Mar 9 11:57:39 EET 2012",
diff --git a/server/lib/deltacloud/drivers/mock/data/cimi/network_configuration/network_config2.json b/server/lib/deltacloud/drivers/mock/data/cimi/network_configuration/network_config2.json
index b22d837..e489bf5 100644
--- a/server/lib/deltacloud/drivers/mock/data/cimi/network_configuration/network_config2.json
+++ b/server/lib/deltacloud/drivers/mock/data/cimi/network_configuration/network_config2.json
@@ -1,4 +1,4 @@
-{ "uri": "http://cimi.example.org/network_configurations/network_config2",
+{ "id": "http://cimi.example.org/network_configurations/network_config2",
   "name": "network_config2",
   "description": "a network configuration",
   "created": "Fri Mar 9 12:01:53 EET 2012",
diff --git a/server/lib/deltacloud/drivers/mock/data/cimi/network_template/template1.json b/server/lib/deltacloud/drivers/mock/data/cimi/network_template/template1.json
new file mode 100644
index 0000000..682d39b
--- /dev/null
+++ b/server/lib/deltacloud/drivers/mock/data/cimi/network_template/template1.json
@@ -0,0 +1,10 @@
+{ "id": "http://cimi.example.org/network_templates/template1",
+  "name": "template1",
+  "description": "A mock network template",
+  "created": "Thu Mar 15 12:15:15 EET 2012",
+  "networkConfig": {"href": "http://cimi.example.org/network_configurations/network_config1"},
+  "routingGroup": {"href": "http://cimi.example.org/routing_groups/group1"},
+  "operations": [
+    { "rel": "edit", "href": "http://cimi.example.org/network_templates/template1" },
+    { "rel": "delete", "href": "http://cimi.example.org/network_templates/template1" }]
+}
diff --git a/server/lib/deltacloud/drivers/mock/data/cimi/network_template/template2.json b/server/lib/deltacloud/drivers/mock/data/cimi/network_template/template2.json
new file mode 100644
index 0000000..b7222cc
--- /dev/null
+++ b/server/lib/deltacloud/drivers/mock/data/cimi/network_template/template2.json
@@ -0,0 +1,10 @@
+{ "id": "http://cimi.example.org/network_templates/template2",
+  "name": "template2",
+  "description": "Another mock network template",
+  "created": "Thu Mar 15 13:51:12 EET 2012",
+  "networkConfig": {"href": "http://cimi.example.org/network_configurations/network_config2"},
+  "routingGroup": {"href": "http://cimi.example.org/routing_groups/group1"},
+  "operations": [
+    { "rel": "edit", "href": "http://cimi.example.org/network_templates/template2" },
+    { "rel": "delete", "href": "http://cimi.example.org/network_templates/template2" }]
+}
diff --git a/server/lib/deltacloud/drivers/mock/data/cimi/routing_group/group1.json b/server/lib/deltacloud/drivers/mock/data/cimi/routing_group/group1.json
new file mode 100644
index 0000000..e10e0bd
--- /dev/null
+++ b/server/lib/deltacloud/drivers/mock/data/cimi/routing_group/group1.json
@@ -0,0 +1,13 @@
+{ "id": "http://cimi.example.org/routing_groups/group1",
+  "name": "group1",
+  "description": "a mock routing group",
+  "created": "Thu Jan 12 16:02:56 EET 2012",
+  "networks": [
+    { "href": "http://cimi.example.org/networks/network1"},
+    { "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" }
+  ]
+}
diff --git a/server/lib/deltacloud/drivers/mock/mock_driver.rb b/server/lib/deltacloud/drivers/mock/mock_driver.rb
index 1725538..83dae09 100644
--- a/server/lib/deltacloud/drivers/mock/mock_driver.rb
+++ b/server/lib/deltacloud/drivers/mock/mock_driver.rb
@@ -475,6 +475,28 @@ module Deltacloud::Drivers::Mock
       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 check_credentials(credentials)
@@ -512,12 +534,34 @@ module Deltacloud::Drivers::Mock
     end
 
     def convert_cimi_mock_urls(model_name, cimi_object, context)
-      uri=context.send(:"#{model_name}_url", cimi_object.name)
-      cimi_object.id=uri
-      cimi_object.operations.each{|op| op.href=uri}
+      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
+
     exceptions do
 
       on /AuthFailure/ do
-- 
1.7.6.5


Re: [PATCH 1/3] CIMI: Adds NetworkTemplates and RoutingGroup (+collections) to CIMI frontend server

Posted by Michal Fojtik <mf...@redhat.com>.
Hi,

ACK. Few comments bellow.

Michal Fojtik
http://deltacloud.org
mfojtik@redhat.com


On Mar 15, 2012, at 4:59 PM, marios@redhat.com wrote:

> From: marios <ma...@redhat.com>
> 
> 
> Signed-off-by: marios <ma...@redhat.com>
> ---
> server/lib/cimi/model.rb                           |    3 +
> server/lib/cimi/model/network_template.rb          |   10 +++
> .../lib/cimi/model/network_template_collection.rb  |   35 +++++++++++
> server/lib/cimi/model/routing_group.rb             |   36 ++++++++++++
> server/lib/cimi/model/routing_group_collection.rb  |   34 +++++++++++
> server/lib/cimi/server.rb                          |   61 ++++++++++++++++++++
> 6 files changed, 179 insertions(+), 0 deletions(-)
> create mode 100644 server/lib/cimi/model/network_template_collection.rb
> create mode 100644 server/lib/cimi/model/routing_group.rb
> create mode 100644 server/lib/cimi/model/routing_group_collection.rb
> 
> diff --git a/server/lib/cimi/model.rb b/server/lib/cimi/model.rb
> index 1ecf848..6b3359b 100644
> --- a/server/lib/cimi/model.rb
> +++ b/server/lib/cimi/model.rb
> @@ -50,3 +50,6 @@ require 'cimi/model/network_collection'
> require 'cimi/model/network_configuration'
> require 'cimi/model/network_configuration_collection'
> require 'cimi/model/network_template'
> +require 'cimi/model/network_template_collection'
> +require 'cimi/model/routing_group.rb'
> +require 'cimi/model/routing_group_collection.rb'

It's necessary to require those files with '.rb' extension?

> diff --git a/server/lib/cimi/model/network_template.rb b/server/lib/cimi/model/network_template.rb
> index 7b6b125..ce3b990 100644
> --- a/server/lib/cimi/model/network_template.rb
> +++ b/server/lib/cimi/model/network_template.rb
> @@ -23,4 +23,14 @@ class CIMI::Model::NetworkTemplate < CIMI::Model::Base
>     scalar :rel, :href
>   end
> 
> +  def self.find(id, context)
> +    network_templates = []
> +    if id==:all
> +      network_templates = context.driver.network_templates(context.credentials, {:env=>context})
> +    else
> +      network_templates = context.driver.network_templates(context.credentials, {:env=>context, :id=>id})
> +    end
> +    network_templates
> +  end
> +
> end
> diff --git a/server/lib/cimi/model/network_template_collection.rb b/server/lib/cimi/model/network_template_collection.rb
> new file mode 100644
> index 0000000..4235b19
> --- /dev/null
> +++ b/server/lib/cimi/model/network_template_collection.rb
> @@ -0,0 +1,35 @@
> +# 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::NetworkTemplateCollection < CIMI::Model::Base
> +
> +  act_as_root_entity :network_template
> +
> +  array :network_templates do
> +    scalar :href
> +  end
> +
> +  def self.default(context)
> +    self.new(
> +      :id => context.network_templates_url,
> +      :name => 'default',
> +      :created => Time.now,
> +      :description => "#{context.driver.name.capitalize} NetworkTemplateCollection",
> +      :network_templates => NetworkTemplate.all(context).map {|t| {:href=> t.id}}

I think the ':network_templates => NetworkTemplate.all_uri(context)' helper will work
as well.

> +    )
> +  end
> +
> +end
> diff --git a/server/lib/cimi/model/routing_group.rb b/server/lib/cimi/model/routing_group.rb
> new file mode 100644
> index 0000000..2399974
> --- /dev/null
> +++ b/server/lib/cimi/model/routing_group.rb
> @@ -0,0 +1,36 @@
> +# 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::RoutingGroup < CIMI::Model::Base
> +
> +  array :networks do
> +    scalar :href
> +  end
> +
> +  array :operations do
> +    scalar :rel, :href
> +  end
> +
> +  def self.find(id, context)
> +    routing_groups = []
> +    if id==:all
> +      routing_groups = context.driver.routing_groups(context.credentials, {:env=>context})
> +    else
> +      routing_groups = context.driver.routing_groups(context.credentials, {:env=>context, :id=>id})
> +    end
> +    routing_groups
> +  end

In fact you don't need to store anything:

def self.find(id, context)
  if id == :all
    context.driver.routing_groups(context.credentials, :env=>context)
  else
    context.driver.routing_groups(context.credentials, :env=>context, :id=>id)
   end
end

> +
> +end
> diff --git a/server/lib/cimi/model/routing_group_collection.rb b/server/lib/cimi/model/routing_group_collection.rb
> new file mode 100644
> index 0000000..945e0ca
> --- /dev/null
> +++ b/server/lib/cimi/model/routing_group_collection.rb
> @@ -0,0 +1,34 @@
> +# 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::RoutingGroupCollection < CIMI::Model::Base
> +
> +  act_as_root_entity :routing_group
> +
> +  array :routing_groups do
> +    scalar :href
> +  end
> +
> +  def self.default(context)
> +    self.new(
> +      :id => context.routing_groups_url,
> +      :name => 'default',
> +      :created => Time.now,
> +      :description => "#{context.driver.name.capitalize} RoutingGroupCollection",
> +      :routing_groups => RoutingGroup.all(context).map {|rg| {:href=> rg.id}}

The same suggestion like above (.all_uri helper will do this as well).

> +    )
> +  end
> +
> +end
> diff --git a/server/lib/cimi/server.rb b/server/lib/cimi/server.rb
> index 6cd845e..06ad758 100644
> --- a/server/lib/cimi/server.rb
> +++ b/server/lib/cimi/server.rb
> @@ -574,3 +574,64 @@ global_collection :network_configurations do
>   end
> end
> end
> +
> +global_collection :network_templates do
> +
> +  description 'Network Template is a set of configuration values for realizing a Network. An instance of Network Template may be used to create multiple Networks'
> +
> +  operation :index do
> +    description 'List all Network Templates in the NetworkTemplateCollection'
> +    param :CIMISelect, :string, :optional
> +    control do
> +      network_templates = NetworkTemplateCollection.default(self).filter_by(params[:CIMISelect])
> +      respond_to do |format|
> +        format.xml {network_templates.to_xml}
> +        format.json {network_templates.to_json}
> +      end
> +    end
> +  end
> +
> +  operation :show do
> +    description 'Show a specific Network Template'
> +    param :id, :string, :required
> +    control do
> +      network_template = NetworkTemplate.find(params[:id], self)
> +      respond_to do |format|
> +        format.xml {network_template.to_xml}
> +        format.json {network_template.to_json}
> +      end
> +    end
> +  end
> +
> +end
> +
> +
> +global_collection :routing_groups do
> +
> +  description 'Routing Groups represent a collection of Networks that route to each other. Providers shall not allow two Networks to be routable to each other unless they are explicitly connected by being part of a common RoutingGroup.'
> +
> +  operation :index do
> +    description 'List all RoutingGroups in the RoutingGroupsCollection'
> +    param :CIMISelect, :string, :optional
> +    control do
> +      routing_groups = RoutingGroupCollection.default(self).filter_by(params[:CIMISelect])
> +      respond_to do |format|
> +        format.xml {routing_groups.to_xml}
> +        format.json {routing_groups.to_json}
> +      end
> +    end
> +  end
> +
> +  operation :show do
> +    description 'Show a specific RoutingGroup'
> +    param :id, :string, :required
> +    control do
> +      routing_group = RoutingGroup.find(params[:id], self)
> +      respond_to do |format|
> +        format.xml {routing_group.to_xml}
> +        format.json {routing_group.to_json}
> +      end
> +    end
> +  end
> +
> +end
> -- 
> 1.7.6.5
> 


[PATCH 1/3] CIMI: Adds NetworkTemplates and RoutingGroup (+collections) to CIMI frontend server

Posted by ma...@redhat.com.
From: marios <ma...@redhat.com>


Signed-off-by: marios <ma...@redhat.com>
---
 server/lib/cimi/model.rb                           |    3 +
 server/lib/cimi/model/network_template.rb          |   10 +++
 .../lib/cimi/model/network_template_collection.rb  |   35 +++++++++++
 server/lib/cimi/model/routing_group.rb             |   36 ++++++++++++
 server/lib/cimi/model/routing_group_collection.rb  |   34 +++++++++++
 server/lib/cimi/server.rb                          |   61 ++++++++++++++++++++
 6 files changed, 179 insertions(+), 0 deletions(-)
 create mode 100644 server/lib/cimi/model/network_template_collection.rb
 create mode 100644 server/lib/cimi/model/routing_group.rb
 create mode 100644 server/lib/cimi/model/routing_group_collection.rb

diff --git a/server/lib/cimi/model.rb b/server/lib/cimi/model.rb
index 1ecf848..6b3359b 100644
--- a/server/lib/cimi/model.rb
+++ b/server/lib/cimi/model.rb
@@ -50,3 +50,6 @@ require 'cimi/model/network_collection'
 require 'cimi/model/network_configuration'
 require 'cimi/model/network_configuration_collection'
 require 'cimi/model/network_template'
+require 'cimi/model/network_template_collection'
+require 'cimi/model/routing_group.rb'
+require 'cimi/model/routing_group_collection.rb'
diff --git a/server/lib/cimi/model/network_template.rb b/server/lib/cimi/model/network_template.rb
index 7b6b125..ce3b990 100644
--- a/server/lib/cimi/model/network_template.rb
+++ b/server/lib/cimi/model/network_template.rb
@@ -23,4 +23,14 @@ class CIMI::Model::NetworkTemplate < CIMI::Model::Base
     scalar :rel, :href
   end
 
+  def self.find(id, context)
+    network_templates = []
+    if id==:all
+      network_templates = context.driver.network_templates(context.credentials, {:env=>context})
+    else
+      network_templates = context.driver.network_templates(context.credentials, {:env=>context, :id=>id})
+    end
+    network_templates
+  end
+
 end
diff --git a/server/lib/cimi/model/network_template_collection.rb b/server/lib/cimi/model/network_template_collection.rb
new file mode 100644
index 0000000..4235b19
--- /dev/null
+++ b/server/lib/cimi/model/network_template_collection.rb
@@ -0,0 +1,35 @@
+# 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::NetworkTemplateCollection < CIMI::Model::Base
+
+  act_as_root_entity :network_template
+
+  array :network_templates do
+    scalar :href
+  end
+
+  def self.default(context)
+    self.new(
+      :id => context.network_templates_url,
+      :name => 'default',
+      :created => Time.now,
+      :description => "#{context.driver.name.capitalize} NetworkTemplateCollection",
+      :network_templates => NetworkTemplate.all(context).map {|t| {:href=> t.id}}
+    )
+  end
+
+end
diff --git a/server/lib/cimi/model/routing_group.rb b/server/lib/cimi/model/routing_group.rb
new file mode 100644
index 0000000..2399974
--- /dev/null
+++ b/server/lib/cimi/model/routing_group.rb
@@ -0,0 +1,36 @@
+# 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::RoutingGroup < CIMI::Model::Base
+
+  array :networks do
+    scalar :href
+  end
+
+  array :operations do
+    scalar :rel, :href
+  end
+
+  def self.find(id, context)
+    routing_groups = []
+    if id==:all
+      routing_groups = context.driver.routing_groups(context.credentials, {:env=>context})
+    else
+      routing_groups = context.driver.routing_groups(context.credentials, {:env=>context, :id=>id})
+    end
+    routing_groups
+  end
+
+end
diff --git a/server/lib/cimi/model/routing_group_collection.rb b/server/lib/cimi/model/routing_group_collection.rb
new file mode 100644
index 0000000..945e0ca
--- /dev/null
+++ b/server/lib/cimi/model/routing_group_collection.rb
@@ -0,0 +1,34 @@
+# 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::RoutingGroupCollection < CIMI::Model::Base
+
+  act_as_root_entity :routing_group
+
+  array :routing_groups do
+    scalar :href
+  end
+
+  def self.default(context)
+    self.new(
+      :id => context.routing_groups_url,
+      :name => 'default',
+      :created => Time.now,
+      :description => "#{context.driver.name.capitalize} RoutingGroupCollection",
+      :routing_groups => RoutingGroup.all(context).map {|rg| {:href=> rg.id}}
+    )
+  end
+
+end
diff --git a/server/lib/cimi/server.rb b/server/lib/cimi/server.rb
index 6cd845e..06ad758 100644
--- a/server/lib/cimi/server.rb
+++ b/server/lib/cimi/server.rb
@@ -574,3 +574,64 @@ global_collection :network_configurations do
   end
 end
 end
+
+global_collection :network_templates do
+
+  description 'Network Template is a set of configuration values for realizing a Network. An instance of Network Template may be used to create multiple Networks'
+
+  operation :index do
+    description 'List all Network Templates in the NetworkTemplateCollection'
+    param :CIMISelect, :string, :optional
+    control do
+      network_templates = NetworkTemplateCollection.default(self).filter_by(params[:CIMISelect])
+      respond_to do |format|
+        format.xml {network_templates.to_xml}
+        format.json {network_templates.to_json}
+      end
+    end
+  end
+
+  operation :show do
+    description 'Show a specific Network Template'
+    param :id, :string, :required
+    control do
+      network_template = NetworkTemplate.find(params[:id], self)
+      respond_to do |format|
+        format.xml {network_template.to_xml}
+        format.json {network_template.to_json}
+      end
+    end
+  end
+
+end
+
+
+global_collection :routing_groups do
+
+  description 'Routing Groups represent a collection of Networks that route to each other. Providers shall not allow two Networks to be routable to each other unless they are explicitly connected by being part of a common RoutingGroup.'
+
+  operation :index do
+    description 'List all RoutingGroups in the RoutingGroupsCollection'
+    param :CIMISelect, :string, :optional
+    control do
+      routing_groups = RoutingGroupCollection.default(self).filter_by(params[:CIMISelect])
+      respond_to do |format|
+        format.xml {routing_groups.to_xml}
+        format.json {routing_groups.to_json}
+      end
+    end
+  end
+
+  operation :show do
+    description 'Show a specific RoutingGroup'
+    param :id, :string, :required
+    control do
+      routing_group = RoutingGroup.find(params[:id], self)
+      respond_to do |format|
+        format.xml {routing_group.to_xml}
+        format.json {routing_group.to_json}
+      end
+    end
+  end
+
+end
-- 
1.7.6.5


Re: [PATCH 3/3] CIMI: separates the CIMI mocking methods to a new file for cleanness

Posted by "marios@redhat.com" <ma...@redhat.com>.
On 16/03/12 14:55, Michal Fojtik wrote:
> ACK.
> 

thanks for keeping it all tidy :) I implemented the nits in 1/3 and pushed.

> Btw. does this stuff got some test:unit coverage as well? :-)

sure ... watch this space (_real_soon_now_) - i want to finish all the
networking stuff (at least for mock driver) first -still vsp and then
all the create/delete ops

> 
>  -- michal
> 
> Michal Fojtik
> http://deltacloud.org
> mfojtik@redhat.com
> 
> On Mar 15, 2012, at 4:59 PM, marios@redhat.com wrote:
> 
>> From: marios <ma...@redhat.com>
>>
>>
>> Signed-off-by: marios <ma...@redhat.com>
>> ---
>> server/lib/deltacloud/drivers/mock/mock_driver.rb  |   74 +--------------
>> .../drivers/mock/mock_driver_cimi_methods.rb       |  101 ++++++++++++++++++++
>> 2 files changed, 102 insertions(+), 73 deletions(-)
>> create mode 100644 server/lib/deltacloud/drivers/mock/mock_driver_cimi_methods.rb
>>
>> diff --git a/server/lib/deltacloud/drivers/mock/mock_driver.rb b/server/lib/deltacloud/drivers/mock/mock_driver.rb
>> index 83dae09..67b4990 100644
>> --- a/server/lib/deltacloud/drivers/mock/mock_driver.rb
>> +++ b/server/lib/deltacloud/drivers/mock/mock_driver.rb
>> @@ -20,6 +20,7 @@ require 'base64'
>> require 'etc'
>> require 'deltacloud/base_driver'
>> require 'deltacloud/drivers/mock/mock_client'
>> +require 'deltacloud/drivers/mock/mock_driver_cimi_methods'
>>
>> module Deltacloud::Drivers::Mock
>>
>> @@ -453,50 +454,6 @@ module Deltacloud::Drivers::Mock
>>       return false
>>     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
>> -      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 check_credentials(credentials)
>> @@ -533,35 +490,6 @@ module Deltacloud::Drivers::Mock
>>       StorageVolume.new(volume)
>>     end
>>
>> -    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
>> -
>>     exceptions do
>>
>>       on /AuthFailure/ do
>> 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
>> -- 
>> 1.7.6.5
>>
> 


Re: [PATCH 3/3] CIMI: separates the CIMI mocking methods to a new file for cleanness

Posted by Michal Fojtik <mf...@redhat.com>.
ACK.

Btw. does this stuff got some test:unit coverage as well? :-)

 -- michal

Michal Fojtik
http://deltacloud.org
mfojtik@redhat.com

On Mar 15, 2012, at 4:59 PM, marios@redhat.com wrote:

> From: marios <ma...@redhat.com>
> 
> 
> Signed-off-by: marios <ma...@redhat.com>
> ---
> server/lib/deltacloud/drivers/mock/mock_driver.rb  |   74 +--------------
> .../drivers/mock/mock_driver_cimi_methods.rb       |  101 ++++++++++++++++++++
> 2 files changed, 102 insertions(+), 73 deletions(-)
> create mode 100644 server/lib/deltacloud/drivers/mock/mock_driver_cimi_methods.rb
> 
> diff --git a/server/lib/deltacloud/drivers/mock/mock_driver.rb b/server/lib/deltacloud/drivers/mock/mock_driver.rb
> index 83dae09..67b4990 100644
> --- a/server/lib/deltacloud/drivers/mock/mock_driver.rb
> +++ b/server/lib/deltacloud/drivers/mock/mock_driver.rb
> @@ -20,6 +20,7 @@ require 'base64'
> require 'etc'
> require 'deltacloud/base_driver'
> require 'deltacloud/drivers/mock/mock_client'
> +require 'deltacloud/drivers/mock/mock_driver_cimi_methods'
> 
> module Deltacloud::Drivers::Mock
> 
> @@ -453,50 +454,6 @@ module Deltacloud::Drivers::Mock
>       return false
>     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
> -      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 check_credentials(credentials)
> @@ -533,35 +490,6 @@ module Deltacloud::Drivers::Mock
>       StorageVolume.new(volume)
>     end
> 
> -    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
> -
>     exceptions do
> 
>       on /AuthFailure/ do
> 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
> -- 
> 1.7.6.5
> 


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

Posted by ma...@redhat.com.
From: marios <ma...@redhat.com>


Signed-off-by: marios <ma...@redhat.com>
---
 server/lib/deltacloud/drivers/mock/mock_driver.rb  |   74 +--------------
 .../drivers/mock/mock_driver_cimi_methods.rb       |  101 ++++++++++++++++++++
 2 files changed, 102 insertions(+), 73 deletions(-)
 create mode 100644 server/lib/deltacloud/drivers/mock/mock_driver_cimi_methods.rb

diff --git a/server/lib/deltacloud/drivers/mock/mock_driver.rb b/server/lib/deltacloud/drivers/mock/mock_driver.rb
index 83dae09..67b4990 100644
--- a/server/lib/deltacloud/drivers/mock/mock_driver.rb
+++ b/server/lib/deltacloud/drivers/mock/mock_driver.rb
@@ -20,6 +20,7 @@ require 'base64'
 require 'etc'
 require 'deltacloud/base_driver'
 require 'deltacloud/drivers/mock/mock_client'
+require 'deltacloud/drivers/mock/mock_driver_cimi_methods'
 
 module Deltacloud::Drivers::Mock
 
@@ -453,50 +454,6 @@ module Deltacloud::Drivers::Mock
       return false
     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
-      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 check_credentials(credentials)
@@ -533,35 +490,6 @@ module Deltacloud::Drivers::Mock
       StorageVolume.new(volume)
     end
 
-    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
-
     exceptions do
 
       on /AuthFailure/ do
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
-- 
1.7.6.5