You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltacloud.apache.org by mf...@redhat.com on 2012/10/11 10:47:18 UTC

[PATCH 2/2] CIMI: Initial implementation of $expand

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

* This PoC works only for MachineCollection now (disks, volumes)

Signed-off-by: Michal fojtik <mf...@redhat.com>
---
 server/lib/cimi/collections/networks.rb           |  2 +-
 server/lib/cimi/helpers/cimi_helper.rb            |  4 ++++
 server/lib/cimi/models.rb                         | 10 +++++-----
 server/lib/cimi/models/base.rb                    |  1 +
 server/lib/cimi/models/collection.rb              |  2 ++
 server/lib/cimi/models/machine.rb                 | 19 ++++++++++++++-----
 server/lib/cimi/models/network.rb                 |  7 ++++++-
 server/lib/cimi/models/network_port.rb            | 13 +++++++++++++
 server/lib/cimi/models/network_port_collection.rb | 14 --------------
 server/lib/cimi/models/schema.rb                  |  1 +
 10 files changed, 47 insertions(+), 26 deletions(-)

diff --git a/server/lib/cimi/collections/networks.rb b/server/lib/cimi/collections/networks.rb
index e006503..3b8b864 100644
--- a/server/lib/cimi/collections/networks.rb
+++ b/server/lib/cimi/collections/networks.rb
@@ -125,7 +125,7 @@ module CIMI::Collections
         description "Retrieve the Network's NetworkPortCollection"
         param :id, :string, :required
         control do
-          network_ports = NetworkPortCollection.for_network(params[:id], self)
+          network_ports = NetworkPort.collection_for_network(params[:id], self)
           respond_to do |format|
             format.json {network_ports.to_json}
             format.xml  {network_ports.to_xml}
diff --git a/server/lib/cimi/helpers/cimi_helper.rb b/server/lib/cimi/helpers/cimi_helper.rb
index a7f76cf..3e84898 100644
--- a/server/lib/cimi/helpers/cimi_helper.rb
+++ b/server/lib/cimi/helpers/cimi_helper.rb
@@ -16,6 +16,10 @@
 module CIMI
   module Helper
 
+    def cimi_expand
+      (params['$expand'] || '').split(',')
+    end
+
     def no_content_with_status(code=200)
       body ''
       status code
diff --git a/server/lib/cimi/models.rb b/server/lib/cimi/models.rb
index 1eae2c9..68611c6 100644
--- a/server/lib/cimi/models.rb
+++ b/server/lib/cimi/models.rb
@@ -31,19 +31,19 @@ require_relative './models/machine_volume'
 # in which the entities appear in the CEP
 require_relative './models/cloud_entry_point'
 require_relative './models/resource_metadata'
+require_relative './models/volume'
+require_relative './models/volume_template'
+require_relative './models/volume_configuration'
+require_relative './models/volume_image'
 require_relative './models/machine'
 require_relative './models/machine_template'
 require_relative './models/machine_configuration'
 require_relative './models/machine_image'
 require_relative './models/credential'
-require_relative './models/volume'
-require_relative './models/volume_template'
-require_relative './models/volume_configuration'
-require_relative './models/volume_image'
+require_relative './models/network_port'
 require_relative './models/network'
 require_relative './models/network_template'
 require_relative './models/network_configuration'
-require_relative './models/network_port'
 require_relative './models/network_port_template'
 require_relative './models/network_port_configuration'
 require_relative './models/address'
diff --git a/server/lib/cimi/models/base.rb b/server/lib/cimi/models/base.rb
index fe6016d..4613e91 100644
--- a/server/lib/cimi/models/base.rb
+++ b/server/lib/cimi/models/base.rb
@@ -171,6 +171,7 @@ class CIMI::Model::Base
   def prepare
     self.class.schema.collections.map { |coll| coll.name }.each do |n|
       self[n].href = "#{self.id}/#{n}" unless self[n].href
+      self[n].id = "#{self.id}/#{n}" if !self[n].entries.empty?
     end
   end
 
diff --git a/server/lib/cimi/models/collection.rb b/server/lib/cimi/models/collection.rb
index db08047..857034e 100644
--- a/server/lib/cimi/models/collection.rb
+++ b/server/lib/cimi/models/collection.rb
@@ -75,6 +75,7 @@ module CIMI::Model
       coll_class.embedded = opts[:embedded]
       entry_schema = model_class.schema
       coll_class.instance_eval do
+        scalar :href
         text :count
         scalar :href if opts[:embedded]
         array self.entry_name, :schema => entry_schema, :xml_name => model_name
@@ -84,6 +85,7 @@ module CIMI::Model
       end
       coll_class
     end
+
   end
 
   #
diff --git a/server/lib/cimi/models/machine.rb b/server/lib/cimi/models/machine.rb
index e61f631..e6fb565 100644
--- a/server/lib/cimi/models/machine.rb
+++ b/server/lib/cimi/models/machine.rb
@@ -25,6 +25,8 @@ class CIMI::Model::Machine < CIMI::Model::Base
   collection :disks, :class => CIMI::Model::Disk
   collection :volumes, :class => CIMI::Model::MachineVolume
 
+  href :network_interfaces
+
   array :meters do
     scalar :href
   end
@@ -46,7 +48,6 @@ class CIMI::Model::Machine < CIMI::Model::Base
   end
 
   def self.create_from_json(body, context)
-    json = JSON.parse(body)
     hardware_profile_id = xml['machineTemplate']['machineConfig']["href"].split('/').last
     image_id = xml['machineTemplate']['machineImage']["href"].split('/').last
     instance = context.create_instance(context.credentials, image_id, { :hwp_id => hardware_profile_id })
@@ -114,7 +115,7 @@ class CIMI::Model::Machine < CIMI::Model::Base
   def self.from_instance(instance, context)
     cpu =  memory = (instance.instance_profile.id == "opaque")? "n/a" : nil
     machine_conf = CIMI::Model::MachineConfiguration.find(instance.instance_profile.name, context)
-    self.new(
+    machine_spec = {
       :name => instance.id,
       :description => instance.name,
       :created => instance.launch_time,
@@ -122,11 +123,19 @@ 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 => CIMI::Model::Disk.find(instance, machine_conf, context, :all),
+      :disks => { :href => context.machine_url(instance.id)+"/disks"},
+      :volumes => { :href=>context.machine_url(instance.id)+"/volumes"},
       :operations => convert_instance_actions(instance, context),
-      :volumes => CIMI::Model::MachineVolume.find(instance.id, context, :all),
       :property => convert_instance_properties(instance, context)
-    )
+    }
+    if context.cimi_expand.include? 'disks'
+      machine_spec[:disks] = CIMI::Model::Disk.find(instance, machine_conf, context, :all)
+    end
+    if context.cimi_expand.include? 'volumes'
+      machine_spec[:volumes] = CIMI::Model::MachineVolume.find(instance.id, context, :all)
+    end
+    machine = self.new(machine_spec)
+    machine
   end
 
   # FIXME: This will convert 'RUNNING' state to 'STARTED'
diff --git a/server/lib/cimi/models/network.rb b/server/lib/cimi/models/network.rb
index b08984a..a7f1ad7 100644
--- a/server/lib/cimi/models/network.rb
+++ b/server/lib/cimi/models/network.rb
@@ -25,7 +25,7 @@ class CIMI::Model::Network < CIMI::Model::Base
 
   text :class_of_service
 
-  href :network_ports
+  collection :network_ports, :class => CIMI::Model::NetworkPort
 
   href :forwarding_group
 
@@ -44,6 +44,11 @@ class CIMI::Model::Network < CIMI::Model::Base
     else
       networks = context.driver.networks(context.credentials, {:id=>id, :env=>context})
     end
+    if context.cimi_expand.include? 'networkPorts'
+      networks.each do |network|
+        network.network_ports = CIMI::Model::NetworkPort.collection_for_network(network.id, context)
+      end
+    end
     networks
   end
 
diff --git a/server/lib/cimi/models/network_port.rb b/server/lib/cimi/models/network_port.rb
index 2951ffd..d69dc3f 100644
--- a/server/lib/cimi/models/network_port.rb
+++ b/server/lib/cimi/models/network_port.rb
@@ -74,6 +74,19 @@ class CIMI::Model::NetworkPort < CIMI::Model::Base
     end
   end
 
+  def self.collection_for_network(network_id, context)
+    net_url = context.network_url(network_id)
+    network_ports = CIMI::Model::NetworkPort.all(context)
+    ports_collection = network_ports.inject([]){|res, current| res << current if current.network.href == net_url ; res}
+    CIMI::Model::NetworkPortCollection.new(
+      :id => net_url+"/network_ports",
+      :name => 'default',
+      :created => Time.now,
+      :description => "#{context.driver.name.capitalize} NetworkPortCollection",
+      :count => ports_collection.size,
+      :network_ports => ports_collection
+    )
+  end
 
   private
 
diff --git a/server/lib/cimi/models/network_port_collection.rb b/server/lib/cimi/models/network_port_collection.rb
index 32642cf..8b4f62e 100644
--- a/server/lib/cimi/models/network_port_collection.rb
+++ b/server/lib/cimi/models/network_port_collection.rb
@@ -34,18 +34,4 @@ class CIMI::Model::NetworkPortCollection < CIMI::Model::Base
     )
   end
 
-  def self.for_network(network_id, context)
-    net_url = context.network_url(network_id)
-    network_ports = CIMI::Model::NetworkPort.all(context)
-    ports_collection = network_ports.inject([]){|res, current| res << current if current.network.href == net_url ; res}
-    self.new(
-      :id => net_url+"/network_ports",
-      :name => 'default',
-      :created => Time.now,
-      :description => "#{context.driver.name.capitalize} NetworkPortCollection",
-      :count => ports_collection.size,
-      :network_ports => ports_collection
-    )
-  end
-
 end
diff --git a/server/lib/cimi/models/schema.rb b/server/lib/cimi/models/schema.rb
index e9504e9..077d663 100644
--- a/server/lib/cimi/models/schema.rb
+++ b/server/lib/cimi/models/schema.rb
@@ -327,6 +327,7 @@ class CIMI::Model::Schema
   # Requires that the class into which this is included has a
   # +add_attributes!+ method
   module DSL
+
     def href(*args)
       opts = args.extract_opts!
       args.each { |arg| struct(arg, opts) { scalar :href } }
-- 
1.7.12.1