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/08/31 21:32:38 UTC

[PATCH 1/3] CIMI: sorts out collections - include :count and each member of the collection inline as an element

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


Signed-off-by: marios <ma...@redhat.com>
---
 server/lib/cimi/models/machine_collection.rb           | 11 +++++++----
 .../cimi/models/machine_configuration_collection.rb    | 10 ++++++----
 server/lib/cimi/models/machine_image_collection.rb     | 10 ++++++----
 server/lib/cimi/models/machine_template_collection.rb  | 10 ++++++----
 server/lib/cimi/models/network_collection.rb           | 11 ++++++-----
 server/lib/cimi/models/schema.rb                       | 18 ++++++++++++++++++
 server/lib/cimi/models/volume_collection.rb            | 10 ++++++----
 .../lib/cimi/models/volume_configuration_collection.rb | 10 ++++++----
 server/lib/cimi/models/volume_image_collection.rb      | 10 ++++++----
 server/lib/cimi/models/volume_template_collection.rb   | 10 ++++++----
 10 files changed, 73 insertions(+), 37 deletions(-)

diff --git a/server/lib/cimi/models/machine_collection.rb b/server/lib/cimi/models/machine_collection.rb
index 9154aa9..27c721d 100644
--- a/server/lib/cimi/models/machine_collection.rb
+++ b/server/lib/cimi/models/machine_collection.rb
@@ -17,17 +17,20 @@ class CIMI::Model::MachineCollection < CIMI::Model::Base
 
   act_as_root_entity :machine
 
-  array :machines do
-    scalar :href
-  end
+  text :count
+
+  #add machines array:
+  self.schema.add_collection_member_array(CIMI::Model::Machine)
 
   def self.default(context)
+    machines = CIMI::Model::Machine.all(context)
     self.new(
       :id => context.machines_url,
       :name => 'default',
       :created => Time.now,
       :description => "#{context.driver.name.capitalize} MachineCollection",
-      :machines => CIMI::Model::Machine.all_uri(context)
+      :count => machines.size,
+      :machines => machines
     )
   end
 
diff --git a/server/lib/cimi/models/machine_configuration_collection.rb b/server/lib/cimi/models/machine_configuration_collection.rb
index 6d7ebad..e75bbb4 100644
--- a/server/lib/cimi/models/machine_configuration_collection.rb
+++ b/server/lib/cimi/models/machine_configuration_collection.rb
@@ -17,17 +17,19 @@ class CIMI::Model::MachineConfigurationCollection < CIMI::Model::Base
 
   act_as_root_entity :machine_configuration
 
-  array :machine_configurations do
-    scalar :href
-  end
+  text :count
+
+  self.schema.add_collection_member_array(CIMI::Model::MachineConfiguration)
 
   def self.default(context)
+    machine_configurations = CIMI::Model::MachineConfiguration.all(context)
     self.new(
       :id => context.machine_configurations_url,
       :name => 'default',
       :created => Time.now,
       :description => "#{context.driver.name.capitalize} MachineConfigurationCollection",
-      :machine_configurations => CIMI::Model::MachineConfiguration.all_uri(context)
+      :count => machine_configurations.count,
+      :machine_configurations => machine_configurations
     )
   end
 
diff --git a/server/lib/cimi/models/machine_image_collection.rb b/server/lib/cimi/models/machine_image_collection.rb
index 70bdc7b..703e515 100644
--- a/server/lib/cimi/models/machine_image_collection.rb
+++ b/server/lib/cimi/models/machine_image_collection.rb
@@ -17,17 +17,19 @@ class CIMI::Model::MachineImageCollection < CIMI::Model::Base
 
   act_as_root_entity :machine_image
 
-  array :machine_images do
-    scalar :href
-  end
+  text :count
+
+  self.schema.add_collection_member_array(CIMI::Model::MachineImage)
 
   def self.default(context)
+    machine_images = CIMI::Model::MachineImage.all(context)
     self.new(
       :id => context.machine_images_url,
       :name => 'default',
       :created => Time.now,
       :description => "#{context.driver.name.capitalize} MachineImageCollection",
-      :machine_images => CIMI::Model::MachineImage.all_uri(context)
+      :count => machine_images.count,
+      :machine_images => machine_images
     )
   end
 
diff --git a/server/lib/cimi/models/machine_template_collection.rb b/server/lib/cimi/models/machine_template_collection.rb
index d2a3f23..bef9aaf 100644
--- a/server/lib/cimi/models/machine_template_collection.rb
+++ b/server/lib/cimi/models/machine_template_collection.rb
@@ -17,17 +17,19 @@ class CIMI::Model::MachineTemplateCollection < CIMI::Model::Base
 
   act_as_root_entity :machine_template
 
-  array :machine_templates do
-    scalar :href
-  end
+  text :count
+
+  self.schema.add_collection_member_array(CIMI::Model::MachineTemplate)
 
   def self.default(context)
+    machine_templates = CIMI::Model::MachineTemplate.all(context)
     self.new(
       :id => context.machine_template_url,
       :name => 'default',
       :created => Time.now,
       :description => "#{context.driver.name.capitalize} MachineTemplateCollection",
-      :machine_templates => CIMI::Model::MachineTemplate.all_uri(context)
+      :count => machine_templates.size,
+      :machine_templates => machine_templates
     )
   end
 
diff --git a/server/lib/cimi/models/network_collection.rb b/server/lib/cimi/models/network_collection.rb
index d76a3dd..c17c3f7 100644
--- a/server/lib/cimi/models/network_collection.rb
+++ b/server/lib/cimi/models/network_collection.rb
@@ -12,22 +12,23 @@
 # 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::NetworkCollection < CIMI::Model::Base
 
   act_as_root_entity :network
+  text :count
 
-  array :networks do
-    scalar :href
-  end
+#add networks Array:
+  self.schema.add_collection_member_array(CIMI::Model::Network)
 
   def self.default(context)
+    networks = CIMI::Model::Network.all(context)
     self.new(
       :id => context.networks_url,
       :name => 'default',
       :created => Time.now,
       :description => "#{context.driver.name.capitalize} NetworkCollection",
-      :networks => CIMI::Model::Network.all(context).map { |c| { :href => c.id } }
+      :count => networks.size,
+      :networks => networks
     )
   end
 
diff --git a/server/lib/cimi/models/schema.rb b/server/lib/cimi/models/schema.rb
index 57a190d..088e111 100644
--- a/server/lib/cimi/models/schema.rb
+++ b/server/lib/cimi/models/schema.rb
@@ -81,6 +81,9 @@ class CIMI::Model::Schema
   end
 
   class Struct < Attribute
+
+    attr_accessor :schema
+
     def initialize(name, opts, &block)
       content = opts[:content]
       super(name, opts)
@@ -146,6 +149,9 @@ class CIMI::Model::Schema
   end
 
   class Array < Attribute
+
+    attr_accessor :struct
+
     # For an array :funThings, we collect all <funThing/> elements (XmlSimple
     # actually does the collecting)
     def initialize(name, opts = {}, &block)
@@ -208,6 +214,9 @@ class CIMI::Model::Schema
   #
   # The actual Schema class
   #
+
+  attr_accessor :attributes
+
   def initialize
     @attributes = []
   end
@@ -230,6 +239,15 @@ class CIMI::Model::Schema
     xml
   end
 
+  #For MachineCollection, copy over the schema of Machine to hold
+  #each member of the collection - avoid duplicating the schemas
+  def add_collection_member_array(model)
+    member_symbol = model.name.split("::").last.underscore.pluralize.to_sym
+    members = CIMI::Model::Schema::Array.new(member_symbol)
+    members.struct.schema.attributes = model.schema.attributes
+    self.attributes << members
+  end
+
   def to_json(model, json = {})
     @attributes.freeze
     @attributes.each { |attr| attr.to_json(model, json) }
diff --git a/server/lib/cimi/models/volume_collection.rb b/server/lib/cimi/models/volume_collection.rb
index 1f4152e..6bec78f 100644
--- a/server/lib/cimi/models/volume_collection.rb
+++ b/server/lib/cimi/models/volume_collection.rb
@@ -17,17 +17,19 @@ class CIMI::Model::VolumeCollection < CIMI::Model::Base
 
   act_as_root_entity :volume
 
-  array :volumes do
-    scalar :href
-  end
+  text :count
+
+  self.schema.add_collection_member_array(CIMI::Model::Volume)
 
   def self.default(context)
+    volumes = CIMI::Model::Volume.all(context)
     self.new(
       :id => context.volumes_url,
       :name => 'default',
       :created => Time.now,
       :description => "#{context.driver.name.capitalize} VolumeCollection",
-      :volumes => CIMI::Model::Volume.all_uri(context)
+      :count => volumes.count,
+      :volumes => volumes
     )
   end
 
diff --git a/server/lib/cimi/models/volume_configuration_collection.rb b/server/lib/cimi/models/volume_configuration_collection.rb
index 2120ae6..8800e35 100644
--- a/server/lib/cimi/models/volume_configuration_collection.rb
+++ b/server/lib/cimi/models/volume_configuration_collection.rb
@@ -17,17 +17,19 @@ class CIMI::Model::VolumeConfigurationCollection < CIMI::Model::Base
 
   act_as_root_entity :volume_configuration
 
-  array :volume_configurations do
-    scalar :href
-  end
+  text :count
+
+  self.schema.add_collection_member_array(CIMI::Model::VolumeConfiguration)
 
   def self.default(context)
+    volume_configurations = CIMI::Model::VolumeConfiguration.all(context)
     self.new(
       :id => context.volume_configurations_url,
       :name => 'default',
       :created => Time.now,
       :description => "#{context.driver.name.capitalize} VolumeConfigurationCollection",
-      :volume_configurations => CIMI::Model::VolumeConfiguration.all_uri(context)
+      :count => volume_configurations.size,
+      :volume_configurations => volume_configurations
     )
   end
 
diff --git a/server/lib/cimi/models/volume_image_collection.rb b/server/lib/cimi/models/volume_image_collection.rb
index f3da877..78881d5 100644
--- a/server/lib/cimi/models/volume_image_collection.rb
+++ b/server/lib/cimi/models/volume_image_collection.rb
@@ -17,17 +17,19 @@ class CIMI::Model::VolumeImageCollection < CIMI::Model::Base
 
   act_as_root_entity :volume_image
 
-  array :volume_images do
-    scalar :href
-  end
+  text :count 
+
+  self.schema.add_collection_member_array(CIMI::Model::VolumeImage)
 
   def self.default(context)
+      volume_images = CIMI::Model::VolumeImage.all(context)
     self.new(
       :id => context.volume_images_url,
       :name => 'default',
       :created => Time.now,
       :description => "#{context.driver.name.capitalize} VolumeImageCollection",
-      :volume_images => CIMI::Model::VolumeImage.all_uri(context)
+      :count => volume_images.count,
+      :volume_images => volume_images
     )
   end
 
diff --git a/server/lib/cimi/models/volume_template_collection.rb b/server/lib/cimi/models/volume_template_collection.rb
index f53547c..3f2ae02 100644
--- a/server/lib/cimi/models/volume_template_collection.rb
+++ b/server/lib/cimi/models/volume_template_collection.rb
@@ -17,17 +17,19 @@ class CIMI::Model::VolumeTemplateCollection < CIMI::Model::Base
 
   act_as_root_entity :volume_template
 
-  array :volume_templates do
-    scalar :href
-  end
+  text :count
+
+  self.schema.add_collection_member_array(CIMI::Model::VolumeTemplate)
 
   def self.default(context)
+    volume_templates = CIMI::Model::VolumeTemplate.all(context)
     self.new(
       :id => context.volume_template_url,
       :name => 'default',
       :created => Time.now,
       :description => "#{context.driver.name.capitalize} VolumeTemplateCollection",
-      :volume_templates => CIMI::Model::VolumeTemplate.all_uri(context)
+      :count => volume_templates.size,
+      :volume_templates => volume_templates
     )
   end
 
-- 
1.7.11.4


[PATCH 3/3] CIMI: starts fixing Networks (mocking)

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


Signed-off-by: marios <ma...@redhat.com>
---
 server/lib/cimi/collections/networks.rb                    |  2 +-
 server/lib/cimi/models/network.rb                          | 14 +++++---------
 .../drivers/mock/data/cimi/network/network1.json           | 12 +++++-------
 .../drivers/mock/data/cimi/network/network2.json           | 12 +++++-------
 4 files changed, 16 insertions(+), 24 deletions(-)

diff --git a/server/lib/cimi/collections/networks.rb b/server/lib/cimi/collections/networks.rb
index 4f26b0b..154bb77 100644
--- a/server/lib/cimi/collections/networks.rb
+++ b/server/lib/cimi/collections/networks.rb
@@ -33,7 +33,7 @@ module CIMI::Collections
         end
       end
 
-      operation :show, :with_capability => :network do
+      operation :show, :with_capability => :networks do
         description "Show a specific Network"
         control do
           network = Network.find(params[:id], self)
diff --git a/server/lib/cimi/models/network.rb b/server/lib/cimi/models/network.rb
index 3d9c7ed..cc8e4df 100644
--- a/server/lib/cimi/models/network.rb
+++ b/server/lib/cimi/models/network.rb
@@ -17,19 +17,15 @@ class CIMI::Model::Network < CIMI::Model::Base
 
   text :state
 
-  text :access
+  text :network_type
 
-  text :bandwidth_limit
+  text :mtu
 
-  text :traffic_priority
+  text :class_of_service
 
-  text :max_traffic_delay
+  href :network_ports
 
-  text :max_traffic_loss
-
-  text :max_traffic_jitter
-
-  href :routing_group
+  href :forwarding_group
 
   href :event_log
 
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 28cf767..7a0f78b 100644
--- a/server/lib/deltacloud/drivers/mock/data/cimi/network/network1.json
+++ b/server/lib/deltacloud/drivers/mock/data/cimi/network/network1.json
@@ -3,13 +3,11 @@
   "description": "the first network",
   "created": "Thu Jan 12 15:25:41 EET 2012",
   "state": "STARTED",
-  "access": "Public",
-  "bandwidthLimit": 2,
-  "trafficPriority": 1,
-  "maxTrafficDelay": 500000,
-  "maxTrafficLoss": 100,
-  "maxTrafficJitter": 100000,
-  "routingGroup": { "href": "http://cimi.example.org/routinggroups/group1" },
+  "networkType": "PUBLIC",
+  "classOfService": "GOLD",
+  "mtu": 1500,
+  "networkPorts": { "href": "http://cimi.example.org/networks/network1/networkports"},
+  "forwardingGroup": { "href": "http://cimi.example.org/forwarding_groups/group1" },
   "operations": [
     { "rel": "edit", "href": "http://cimi.example.org/networks/network1" },
     { "rel": "delete", "href": "http://cimi.example.org/networks/network1" }
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 370db99..283d242 100644
--- a/server/lib/deltacloud/drivers/mock/data/cimi/network/network2.json
+++ b/server/lib/deltacloud/drivers/mock/data/cimi/network/network2.json
@@ -3,13 +3,11 @@
   "description": "another network",
   "created": "Fri Jan 13 17:30:34 EET 2012",
   "state": "STARTED",
-  "access": "Public",
-  "bandwidthLimit": 3,
-  "trafficPriority": 2,
-  "maxTrafficDelay": 600000,
-  "maxTrafficLoss": 100,
-  "maxTrafficJitter": 200000,
-  "routingGroup": { "href": "http://cimi.example.org/routinggroups/group1" },
+  "networkType": "PUBLIC",
+  "classOfService": "SILVER",
+  "mtu": 1500,
+  "networkPorts": { "href": "http://cimi.example.org/networks/network2/networkports"},
+  "forwardingGroup": { "href": "http://cimi.example.org/forwarding_groups/group1" },
   "operations": [
     { "rel": "edit", "href": "http://cimi.example.org/networks/network2" },
     { "rel": "delete", "href": "http://cimi.example.org/networks/network2" }
-- 
1.7.11.4


Re: [PATCH 1/3] CIMI: sorts out collections - include :count and each member of the collection inline as an element

Posted by Michal Fojtik <mf...@redhat.com>.
On Aug 31, 2012, at 9:32 PM, marios@redhat.com wrote:

ACK to serie, but please fix the tests before push:

http://tracker.deltacloud.org/build/39/build

Also in future we should consider using Rabbit subcollections
for things like 'operation :disks'.

It would be uber-cool if we can do things like:

collection :machines do
  subcollection :disks
end

and Rabbit then find that collection and 'mount' it into
the '/machine/:id/disks' automagically.

  -- Michal

> From: marios <ma...@redhat.com>
> 
> 
> Signed-off-by: marios <ma...@redhat.com>
> ---
> server/lib/cimi/models/machine_collection.rb           | 11 +++++++----
> .../cimi/models/machine_configuration_collection.rb    | 10 ++++++----
> server/lib/cimi/models/machine_image_collection.rb     | 10 ++++++----
> server/lib/cimi/models/machine_template_collection.rb  | 10 ++++++----
> server/lib/cimi/models/network_collection.rb           | 11 ++++++-----
> server/lib/cimi/models/schema.rb                       | 18 ++++++++++++++++++
> server/lib/cimi/models/volume_collection.rb            | 10 ++++++----
> .../lib/cimi/models/volume_configuration_collection.rb | 10 ++++++----
> server/lib/cimi/models/volume_image_collection.rb      | 10 ++++++----
> server/lib/cimi/models/volume_template_collection.rb   | 10 ++++++----
> 10 files changed, 73 insertions(+), 37 deletions(-)
> 
> diff --git a/server/lib/cimi/models/machine_collection.rb b/server/lib/cimi/models/machine_collection.rb
> index 9154aa9..27c721d 100644
> --- a/server/lib/cimi/models/machine_collection.rb
> +++ b/server/lib/cimi/models/machine_collection.rb
> @@ -17,17 +17,20 @@ class CIMI::Model::MachineCollection < CIMI::Model::Base
> 
>   act_as_root_entity :machine
> 
> -  array :machines do
> -    scalar :href
> -  end
> +  text :count
> +
> +  #add machines array:
> +  self.schema.add_collection_member_array(CIMI::Model::Machine)
> 
>   def self.default(context)
> +    machines = CIMI::Model::Machine.all(context)
>     self.new(
>       :id => context.machines_url,
>       :name => 'default',
>       :created => Time.now,
>       :description => "#{context.driver.name.capitalize} MachineCollection",
> -      :machines => CIMI::Model::Machine.all_uri(context)
> +      :count => machines.size,
> +      :machines => machines
>     )
>   end
> 
> diff --git a/server/lib/cimi/models/machine_configuration_collection.rb b/server/lib/cimi/models/machine_configuration_collection.rb
> index 6d7ebad..e75bbb4 100644
> --- a/server/lib/cimi/models/machine_configuration_collection.rb
> +++ b/server/lib/cimi/models/machine_configuration_collection.rb
> @@ -17,17 +17,19 @@ class CIMI::Model::MachineConfigurationCollection < CIMI::Model::Base
> 
>   act_as_root_entity :machine_configuration
> 
> -  array :machine_configurations do
> -    scalar :href
> -  end
> +  text :count
> +
> +  self.schema.add_collection_member_array(CIMI::Model::MachineConfiguration)
> 
>   def self.default(context)
> +    machine_configurations = CIMI::Model::MachineConfiguration.all(context)
>     self.new(
>       :id => context.machine_configurations_url,
>       :name => 'default',
>       :created => Time.now,
>       :description => "#{context.driver.name.capitalize} MachineConfigurationCollection",
> -      :machine_configurations => CIMI::Model::MachineConfiguration.all_uri(context)
> +      :count => machine_configurations.count,
> +      :machine_configurations => machine_configurations
>     )
>   end
> 
> diff --git a/server/lib/cimi/models/machine_image_collection.rb b/server/lib/cimi/models/machine_image_collection.rb
> index 70bdc7b..703e515 100644
> --- a/server/lib/cimi/models/machine_image_collection.rb
> +++ b/server/lib/cimi/models/machine_image_collection.rb
> @@ -17,17 +17,19 @@ class CIMI::Model::MachineImageCollection < CIMI::Model::Base
> 
>   act_as_root_entity :machine_image
> 
> -  array :machine_images do
> -    scalar :href
> -  end
> +  text :count
> +
> +  self.schema.add_collection_member_array(CIMI::Model::MachineImage)
> 
>   def self.default(context)
> +    machine_images = CIMI::Model::MachineImage.all(context)
>     self.new(
>       :id => context.machine_images_url,
>       :name => 'default',
>       :created => Time.now,
>       :description => "#{context.driver.name.capitalize} MachineImageCollection",
> -      :machine_images => CIMI::Model::MachineImage.all_uri(context)
> +      :count => machine_images.count,
> +      :machine_images => machine_images
>     )
>   end
> 
> diff --git a/server/lib/cimi/models/machine_template_collection.rb b/server/lib/cimi/models/machine_template_collection.rb
> index d2a3f23..bef9aaf 100644
> --- a/server/lib/cimi/models/machine_template_collection.rb
> +++ b/server/lib/cimi/models/machine_template_collection.rb
> @@ -17,17 +17,19 @@ class CIMI::Model::MachineTemplateCollection < CIMI::Model::Base
> 
>   act_as_root_entity :machine_template
> 
> -  array :machine_templates do
> -    scalar :href
> -  end
> +  text :count
> +
> +  self.schema.add_collection_member_array(CIMI::Model::MachineTemplate)
> 
>   def self.default(context)
> +    machine_templates = CIMI::Model::MachineTemplate.all(context)
>     self.new(
>       :id => context.machine_template_url,
>       :name => 'default',
>       :created => Time.now,
>       :description => "#{context.driver.name.capitalize} MachineTemplateCollection",
> -      :machine_templates => CIMI::Model::MachineTemplate.all_uri(context)
> +      :count => machine_templates.size,
> +      :machine_templates => machine_templates
>     )
>   end
> 
> diff --git a/server/lib/cimi/models/network_collection.rb b/server/lib/cimi/models/network_collection.rb
> index d76a3dd..c17c3f7 100644
> --- a/server/lib/cimi/models/network_collection.rb
> +++ b/server/lib/cimi/models/network_collection.rb
> @@ -12,22 +12,23 @@
> # 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::NetworkCollection < CIMI::Model::Base
> 
>   act_as_root_entity :network
> +  text :count
> 
> -  array :networks do
> -    scalar :href
> -  end
> +#add networks Array:
> +  self.schema.add_collection_member_array(CIMI::Model::Network)
> 
>   def self.default(context)
> +    networks = CIMI::Model::Network.all(context)
>     self.new(
>       :id => context.networks_url,
>       :name => 'default',
>       :created => Time.now,
>       :description => "#{context.driver.name.capitalize} NetworkCollection",
> -      :networks => CIMI::Model::Network.all(context).map { |c| { :href => c.id } }
> +      :count => networks.size,
> +      :networks => networks
>     )
>   end
> 
> diff --git a/server/lib/cimi/models/schema.rb b/server/lib/cimi/models/schema.rb
> index 57a190d..088e111 100644
> --- a/server/lib/cimi/models/schema.rb
> +++ b/server/lib/cimi/models/schema.rb
> @@ -81,6 +81,9 @@ class CIMI::Model::Schema
>   end
> 
>   class Struct < Attribute
> +
> +    attr_accessor :schema
> +
>     def initialize(name, opts, &block)
>       content = opts[:content]
>       super(name, opts)
> @@ -146,6 +149,9 @@ class CIMI::Model::Schema
>   end
> 
>   class Array < Attribute
> +
> +    attr_accessor :struct
> +
>     # For an array :funThings, we collect all <funThing/> elements (XmlSimple
>     # actually does the collecting)
>     def initialize(name, opts = {}, &block)
> @@ -208,6 +214,9 @@ class CIMI::Model::Schema
>   #
>   # The actual Schema class
>   #
> +
> +  attr_accessor :attributes
> +
>   def initialize
>     @attributes = []
>   end
> @@ -230,6 +239,15 @@ class CIMI::Model::Schema
>     xml
>   end
> 
> +  #For MachineCollection, copy over the schema of Machine to hold
> +  #each member of the collection - avoid duplicating the schemas
> +  def add_collection_member_array(model)
> +    member_symbol = model.name.split("::").last.underscore.pluralize.to_sym
> +    members = CIMI::Model::Schema::Array.new(member_symbol)
> +    members.struct.schema.attributes = model.schema.attributes
> +    self.attributes << members
> +  end
> +
>   def to_json(model, json = {})
>     @attributes.freeze
>     @attributes.each { |attr| attr.to_json(model, json) }
> diff --git a/server/lib/cimi/models/volume_collection.rb b/server/lib/cimi/models/volume_collection.rb
> index 1f4152e..6bec78f 100644
> --- a/server/lib/cimi/models/volume_collection.rb
> +++ b/server/lib/cimi/models/volume_collection.rb
> @@ -17,17 +17,19 @@ class CIMI::Model::VolumeCollection < CIMI::Model::Base
> 
>   act_as_root_entity :volume
> 
> -  array :volumes do
> -    scalar :href
> -  end
> +  text :count
> +
> +  self.schema.add_collection_member_array(CIMI::Model::Volume)
> 
>   def self.default(context)
> +    volumes = CIMI::Model::Volume.all(context)
>     self.new(
>       :id => context.volumes_url,
>       :name => 'default',
>       :created => Time.now,
>       :description => "#{context.driver.name.capitalize} VolumeCollection",
> -      :volumes => CIMI::Model::Volume.all_uri(context)
> +      :count => volumes.count,
> +      :volumes => volumes
>     )
>   end
> 
> diff --git a/server/lib/cimi/models/volume_configuration_collection.rb b/server/lib/cimi/models/volume_configuration_collection.rb
> index 2120ae6..8800e35 100644
> --- a/server/lib/cimi/models/volume_configuration_collection.rb
> +++ b/server/lib/cimi/models/volume_configuration_collection.rb
> @@ -17,17 +17,19 @@ class CIMI::Model::VolumeConfigurationCollection < CIMI::Model::Base
> 
>   act_as_root_entity :volume_configuration
> 
> -  array :volume_configurations do
> -    scalar :href
> -  end
> +  text :count
> +
> +  self.schema.add_collection_member_array(CIMI::Model::VolumeConfiguration)
> 
>   def self.default(context)
> +    volume_configurations = CIMI::Model::VolumeConfiguration.all(context)
>     self.new(
>       :id => context.volume_configurations_url,
>       :name => 'default',
>       :created => Time.now,
>       :description => "#{context.driver.name.capitalize} VolumeConfigurationCollection",
> -      :volume_configurations => CIMI::Model::VolumeConfiguration.all_uri(context)
> +      :count => volume_configurations.size,
> +      :volume_configurations => volume_configurations
>     )
>   end
> 
> diff --git a/server/lib/cimi/models/volume_image_collection.rb b/server/lib/cimi/models/volume_image_collection.rb
> index f3da877..78881d5 100644
> --- a/server/lib/cimi/models/volume_image_collection.rb
> +++ b/server/lib/cimi/models/volume_image_collection.rb
> @@ -17,17 +17,19 @@ class CIMI::Model::VolumeImageCollection < CIMI::Model::Base
> 
>   act_as_root_entity :volume_image
> 
> -  array :volume_images do
> -    scalar :href
> -  end
> +  text :count 
> +
> +  self.schema.add_collection_member_array(CIMI::Model::VolumeImage)
> 
>   def self.default(context)
> +      volume_images = CIMI::Model::VolumeImage.all(context)
>     self.new(
>       :id => context.volume_images_url,
>       :name => 'default',
>       :created => Time.now,
>       :description => "#{context.driver.name.capitalize} VolumeImageCollection",
> -      :volume_images => CIMI::Model::VolumeImage.all_uri(context)
> +      :count => volume_images.count,
> +      :volume_images => volume_images
>     )
>   end
> 
> diff --git a/server/lib/cimi/models/volume_template_collection.rb b/server/lib/cimi/models/volume_template_collection.rb
> index f53547c..3f2ae02 100644
> --- a/server/lib/cimi/models/volume_template_collection.rb
> +++ b/server/lib/cimi/models/volume_template_collection.rb
> @@ -17,17 +17,19 @@ class CIMI::Model::VolumeTemplateCollection < CIMI::Model::Base
> 
>   act_as_root_entity :volume_template
> 
> -  array :volume_templates do
> -    scalar :href
> -  end
> +  text :count
> +
> +  self.schema.add_collection_member_array(CIMI::Model::VolumeTemplate)
> 
>   def self.default(context)
> +    volume_templates = CIMI::Model::VolumeTemplate.all(context)
>     self.new(
>       :id => context.volume_template_url,
>       :name => 'default',
>       :created => Time.now,
>       :description => "#{context.driver.name.capitalize} VolumeTemplateCollection",
> -      :volume_templates => CIMI::Model::VolumeTemplate.all_uri(context)
> +      :count => volume_templates.size,
> +      :volume_templates => volume_templates
>     )
>   end
> 
> -- 
> 1.7.11.4
> 

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




[PATCH 2/3] CIMI: fixes Machine with subcollections for disk, machine_volume

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


Signed-off-by: marios <ma...@redhat.com>
---
 server/lib/cimi/collections/machines.rb            | 22 +++++++++
 server/lib/cimi/helpers/cimi_helper.rb             | 12 ++++-
 server/lib/cimi/models.rb                          |  4 ++
 server/lib/cimi/models/disk.rb                     | 40 +++++++++++++++++
 server/lib/cimi/models/disk_collection.rb          | 38 ++++++++++++++++
 server/lib/cimi/models/machine.rb                  | 52 ++++------------------
 server/lib/cimi/models/machine_configuration.rb    | 23 ++++------
 server/lib/cimi/models/machine_volume.rb           | 42 +++++++++++++++++
 .../lib/cimi/models/machine_volume_collection.rb   | 34 ++++++++++++++
 9 files changed, 208 insertions(+), 59 deletions(-)
 create mode 100644 server/lib/cimi/models/disk.rb
 create mode 100644 server/lib/cimi/models/disk_collection.rb
 create mode 100644 server/lib/cimi/models/machine_volume.rb
 create mode 100644 server/lib/cimi/models/machine_volume_collection.rb

diff --git a/server/lib/cimi/collections/machines.rb b/server/lib/cimi/collections/machines.rb
index 6084506..74a31dc 100644
--- a/server/lib/cimi/collections/machines.rb
+++ b/server/lib/cimi/collections/machines.rb
@@ -117,6 +117,28 @@ module CIMI::Collections
         end
       end
 
+      operation :disks, :with_capability => :hardware_profiles do
+        description "Retrieve the Machine's DiskCollection"
+        control do
+          disks = DiskCollection.default(params[:id], self)
+          respond_to do |format|
+            format.json {disks.to_json}
+            format.xml  {disks.to_xml}
+          end
+        end
+      end
+
+      operation :volumes, :with_capability => :storage_volumes do
+        description "Retrieve the Machine's MachineVolumeCollection"
+        control do
+          volumes = MachineVolumeCollection.default(params[:id], self)
+          respond_to do |format|
+            format.json {volumes.to_json}
+            format.xml  {volumes.to_xml}
+          end
+        end
+      end
+
       #NOTE: The routes for attach/detach used here are NOT as specified by CIMI
       #will likely move later. CIMI specifies PUT of the whole Machine description
       #with inclusion/ommission of the volumes you want [att|det]ached
diff --git a/server/lib/cimi/helpers/cimi_helper.rb b/server/lib/cimi/helpers/cimi_helper.rb
index e48fa8e..be64dbd 100644
--- a/server/lib/cimi/helpers/cimi_helper.rb
+++ b/server/lib/cimi/helpers/cimi_helper.rb
@@ -20,12 +20,22 @@ module CIMIHelper
     status code
   end
 
-
   def href_id(href, entity)
     split_on = self.send(:"#{entity.to_s}_url")
     href.split("#{split_on}/").last
   end
 
+  def to_kibibyte(value, unit)
+    case unit
+      when "GB"
+        value*1024*1024
+      when "MB"
+        value*1024
+      else
+        nil # should probably be exploding something here...
+    end
+  end
+
 end
 
 class Array
diff --git a/server/lib/cimi/models.rb b/server/lib/cimi/models.rb
index 2eaf345..b447e50 100644
--- a/server/lib/cimi/models.rb
+++ b/server/lib/cimi/models.rb
@@ -64,3 +64,7 @@ require_relative './models/address'
 require_relative './models/address_collection'
 require_relative './models/address_template'
 require_relative './models/address_template_collection'
+require_relative './models/disk'
+require_relative './models/disk_collection'
+require_relative './models/machine_volume'
+require_relative './models/machine_volume_collection'
diff --git a/server/lib/cimi/models/disk.rb b/server/lib/cimi/models/disk.rb
new file mode 100644
index 0000000..d74ec54
--- /dev/null
+++ b/server/lib/cimi/models/disk.rb
@@ -0,0 +1,40 @@
+# 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::Disk < CIMI::Model::Base
+
+  text :capacity
+  text :initial_location
+
+  array :operations do
+    scalar :rel, :href
+  end
+
+  def self.find(instance, machine_config, context, id=:all)
+    if id == :all
+      storage_override = instance.instance_profile.overrides.find { |p, v| p == :storage }
+      capacity = storage_override.nil? ? machine_config.disks[0][:capacity] : context.to_kibibyte(storage_override[1].to_i, "MB")
+      name = instance.id+"_disk_#{capacity}" #assuming one disk for now...
+     [ self.new(
+       :id => context.machine_url(instance.id)+"/disks/#{name}",
+       :name => name,
+       :description => "DiskCollection for Machine #{instance.id}",
+       :created => instance.launch_time,
+       :capacity => capacity
+      ) ]
+    else
+    end
+  end
+end
diff --git a/server/lib/cimi/models/disk_collection.rb b/server/lib/cimi/models/disk_collection.rb
new file mode 100644
index 0000000..b86a3c9
--- /dev/null
+++ b/server/lib/cimi/models/disk_collection.rb
@@ -0,0 +1,38 @@
+# 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::DiskCollection < CIMI::Model::Base
+  text :count
+
+  #add disks array:
+  self.schema.add_collection_member_array(CIMI::Model::Disk)
+
+  array :operations do
+    scalar :rel, :href
+  end
+
+  def self.default(instance_id, context)
+    instance = context.driver.instance(context.credentials, :id=>instance_id)
+    machine_conf = CIMI::Model::MachineConfiguration.find(instance.instance_profile.name, context)
+    disks = CIMI::Model::Disk.find(instance, machine_conf, context, :all)
+    storage_override = instance.instance_profile.overrides.find { |p, v| p == :storage }
+    self.new(
+      :id => context.machine_url(instance_id)+"/disks",
+      :description => "DiskCollection for Machine #{instance_id}",
+      :created => instance.launch_time,
+      :count => disks.size,
+      :disks => disks
+    )
+  end
+end
diff --git a/server/lib/cimi/models/machine.rb b/server/lib/cimi/models/machine.rb
index 415b0b5..7cc659a 100644
--- a/server/lib/cimi/models/machine.rb
+++ b/server/lib/cimi/models/machine.rb
@@ -18,33 +18,15 @@ class CIMI::Model::Machine < CIMI::Model::Base
   text :state
   text :cpu
 
-  struct :memory do
-    scalar :quantity
-    scalar :units
-  end
+  text :memory
 
   href :event_log
 
-  array :disks do
-    struct :capacity do
-      scalar :quantity
-      scalar :units
-    end
-    scalar :format
-    scalar :attachment_point
-  end
+  href :disks
 
-  array :volumes do
-    scalar :href
-    scalar :protocol
-    scalar :attachment_point
-  end
+  href :volumes
 
-  array :network_interfaces do
-    href :vsp
-    text :hostname, :mac_address, :state, :protocol, :allocation
-    text :address, :default_gateway, :dns, :max_transmission_unit
-  end
+  href :network_interfaces
 
   array :meters do
     scalar :href
@@ -133,7 +115,7 @@ class CIMI::Model::Machine < CIMI::Model::Base
 
   private
   def self.from_instance(instance, context)
-    cpu =  memory = disks = (instance.instance_profile.id == "opaque")? "n/a" : nil
+    cpu =  memory = (instance.instance_profile.id == "opaque")? "n/a" : nil
     self.new(
       :name => instance.id,
       :description => instance.name,
@@ -142,10 +124,10 @@ class CIMI::Model::Machine < CIMI::Model::Base
       :state => convert_instance_state(instance.state),
       :cpu => cpu || convert_instance_cpu(instance.instance_profile, context),
       :memory => memory || convert_instance_memory(instance.instance_profile, context),
-      :disks => disks || convert_instance_storage(instance.instance_profile, context),
-      :network_interfaces => convert_instance_addresses(instance),
+      :disks => {:href => context.machine_url(instance.id)+"/disks"},
+      :network_interfaces => {:href => context.machine_url(instance.id+"/network_interfaces")},
       :operations => convert_instance_actions(instance, context),
-      :volumes=>convert_storage_volumes(instance, context),
+      :volumes=>{:href=>context.machine_url(instance.id)+"/volumes"},
       :property => convert_instance_properties(instance, context)
     )
   end
@@ -178,23 +160,7 @@ class CIMI::Model::Machine < CIMI::Model::Base
   def self.convert_instance_memory(profile, context)
     machine_conf = CIMI::Model::MachineConfiguration.find(profile.name, context)
     memory_override = profile.overrides.find { |p, v| p == :memory }
-    {
-      :quantity => memory_override.nil? ? machine_conf.memory[:quantity] : memory_override[1],
-      :units => machine_conf.memory[:units]
-    }
-  end
-
-  def self.convert_instance_storage(profile, context)
-    machine_conf = CIMI::Model::MachineConfiguration.find(profile.name, context)
-    return nil unless machine_conf.disks
-    storage_override = profile.overrides.find { |p, v| p == :storage }
-    [
-      { :capacity => {
-          :quantity => storage_override.nil? ? machine_conf.disks.first[:capacity][:quantity] : storage_override[1],
-          :units => machine_conf.disks.first[:capacity][:units]
-        }
-      }
-    ]
+    memory_override.nil? ? machine_conf.memory : context.to_kibibyte(memory_override[1].to_i,"MB")
   end
 
   def self.convert_instance_addresses(instance)
diff --git a/server/lib/cimi/models/machine_configuration.rb b/server/lib/cimi/models/machine_configuration.rb
index 28bf3ab..6ea92dd 100644
--- a/server/lib/cimi/models/machine_configuration.rb
+++ b/server/lib/cimi/models/machine_configuration.rb
@@ -15,20 +15,13 @@
 
 class CIMI::Model::MachineConfiguration < CIMI::Model::Base
 
-  struct :memory do
-    scalar :quantity
-    scalar :units
-  end
-
+  text :memory
   text :cpu
 
   array :disks do
-    struct :capacity do
-      scalar :quantity
-      scalar :units
-    end
-    scalar :format
-    scalar :attachment_point
+    text :capacity
+    text :format
+    text :attachment_point
   end
 
   array :operations do
@@ -51,17 +44,17 @@ class CIMI::Model::MachineConfiguration < CIMI::Model::Base
   def self.from_hardware_profile(profile, context)
     # We accept just profiles with all properties set
     return unless profile.memory or profile.cpu or profile.storage
-    memory = profile.memory.value || profile.memory.default
+    memory = profile.memory ? context.to_kibibyte((profile.memory.value || profile.memory.default), profile.memory.unit) : nil
     cpu = (profile.cpu ? (profile.cpu.value || profile.cpu.default) : nil )
-    storage = (profile.storage ? (profile.storage.value || profile.storage.default) :  nil )
+    storage = profile.storage ? context.to_kibibyte((profile.storage.value || profile.storage.default), profile.storage.unit) :  nil
     machine_hash = {
       :name => profile.name,
       :description => "Machine Configuration with #{memory} #{profile.memory.unit} "+
         "of memory and #{cpu} CPU",
       :cpu => ( cpu if cpu ) ,
       :created => Time.now.to_s,  # FIXME: DC hardware_profile has no mention about created_at
-      :memory => { :quantity => profile.memory.value || profile.memory.default, :units => profile.memory.unit },
-      :disks => (  [ { :capacity => { :quantity => profile.storage.value || profile.storage.default, :units => profile.storage.unit } } ] if storage ),
+      :memory => (memory if memory),
+      :disks => (  [ { :capacity => storage  } ] if storage ),
       :id => context.machine_configuration_url(profile.name)
     }
     self.new(machine_hash)
diff --git a/server/lib/cimi/models/machine_volume.rb b/server/lib/cimi/models/machine_volume.rb
new file mode 100644
index 0000000..2337bdf
--- /dev/null
+++ b/server/lib/cimi/models/machine_volume.rb
@@ -0,0 +1,42 @@
+# 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::MachineVolume < CIMI::Model::Base
+
+  text :initial_location
+  href :volume
+
+  array :operations do
+    scalar :rel, :href
+  end
+
+  def self.find(instance_id, context, id=:all)
+    if id == :all
+      volumes = context.driver.storage_volumes(context.credentials)
+      volumes.inject([]) do |attached, vol|
+        attached <<  self.new(
+          :id => context.machine_url(instance_id)+"/volumes/#{vol.id}",
+          :name => vol.id,
+          :description => "MachineVolume #{vol.id} for Machine #{instance_id}",
+          :created => vol.created,
+          :initial_location => vol.device,
+          :volume => {:href=>context.volume_url(vol.id)}
+          ) if vol.instance_id == instance_id
+        attached
+      end
+    else
+    end
+  end
+end
diff --git a/server/lib/cimi/models/machine_volume_collection.rb b/server/lib/cimi/models/machine_volume_collection.rb
new file mode 100644
index 0000000..85483ff
--- /dev/null
+++ b/server/lib/cimi/models/machine_volume_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::MachineVolumeCollection < CIMI::Model::Base
+
+  text :count
+
+  self.schema.add_collection_member_array(CIMI::Model::MachineVolume)
+
+  array :operations do
+    scalar :rel, :href
+  end
+
+  def self.default(instance_id, context)
+    volumes = CIMI::Model::MachineVolume.find(instance_id, context)
+    self.new(
+      :id => context.machine_url(instance_id)+"/volumes",
+      :description => "MachineVolumeCollection for Machine #{instance_id}",
+      :count => volumes.size,
+      :machine_volumes => volumes
+    )
+  end
+end
-- 
1.7.11.4