deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@redhat.com
Subject [PATCH core 2/2] CIMI: Initial implementation of $expand
Date Wed, 03 Oct 2012 14:40:58 GMT
From: Michal Fojtik <mfojtik@redhat.com>

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

Signed-off-by: Michal fojtik <mfojtik@redhat.com>
---
 server/lib/cimi/helpers/cimi_helper.rb |    4 ++++
 server/lib/cimi/models.rb              |    8 ++++----
 server/lib/cimi/models/machine.rb      |   20 +++++++++++++-------
 server/lib/cimi/models/schema.rb       |   19 ++++++++++++++++++-
 4 files changed, 39 insertions(+), 12 deletions(-)

diff --git a/server/lib/cimi/helpers/cimi_helper.rb b/server/lib/cimi/helpers/cimi_helper.rb
index ea1e89e..2da684f 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 df328f5..f5ae48b 100644
--- a/server/lib/cimi/models.rb
+++ b/server/lib/cimi/models.rb
@@ -33,15 +33,15 @@ require_relative './models/machine_volume_collection'
 # 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'
 require_relative './models/network_template'
 require_relative './models/network_configuration'
diff --git a/server/lib/cimi/models/machine.rb b/server/lib/cimi/models/machine.rb
index da67eb7..35a0354 100644
--- a/server/lib/cimi/models/machine.rb
+++ b/server/lib/cimi/models/machine.rb
@@ -24,9 +24,8 @@ class CIMI::Model::Machine < CIMI::Model::Base
 
   href :event_log
 
-  href :disks
-
-  href :volumes
+  subcollection :disks
+  subcollection :volumes, :use => :machine_volumes
 
   href :network_interfaces
 
@@ -51,7 +50,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
})
@@ -118,7 +116,7 @@ class CIMI::Model::Machine < CIMI::Model::Base
   private
   def self.from_instance(instance, context)
     cpu =  memory = (instance.instance_profile.id == "opaque")? "n/a" : nil
-    self.new(
+    machine_spec = {
       :name => instance.id,
       :description => instance.name,
       :created => instance.launch_time,
@@ -129,9 +127,17 @@ class CIMI::Model::Machine < CIMI::Model::Base
       :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=>{:href=>context.machine_url(instance.id)+"/volumes"},
+      :volumes=> { :href=>context.machine_url(instance.id)+"/volumes" },
       :property => convert_instance_properties(instance, context)
-    )
+    }
+    if context.cimi_expand.include? 'disks'
+      machine_spec[:disks].merge!(CIMI::Model::DiskCollection.default(instance.id, context).attribute_values)
+    end
+    if context.cimi_expand.include? 'volumes'
+      machine_spec[:volumes].merge!(CIMI::Model::MachineVolumeCollection.default(instance.id,
context).attribute_values)
+    end
+    machine = self.new(machine_spec)
+    machine
   end
 
   # FIXME: This will convert 'RUNNING' state to 'STARTED'
diff --git a/server/lib/cimi/models/schema.rb b/server/lib/cimi/models/schema.rb
index ed3541a..ba8946c 100644
--- a/server/lib/cimi/models/schema.rb
+++ b/server/lib/cimi/models/schema.rb
@@ -237,7 +237,9 @@ class CIMI::Model::Schema
   def to_xml(model, xml = nil)
     xml ||= OrderedHash.new
     @attributes.freeze
-    @attributes.each { |attr| attr.to_xml(model, xml) }
+    @attributes.each do |attr|
+      attr.to_xml(model, xml)
+    end
     xml
   end
 
@@ -266,11 +268,26 @@ 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 } }
     end
 
+    def subcollection(name, opts={})
+      collection_model_name = "#{(opts[:use] || name).to_s.singularize.camelize}Collection"
+      begin
+        collection_model = CIMI::Model::const_get(collection_model_name)
+        add_attributes!([name, {}], Struct, &Proc.new{
+          scalar :href
+          @attributes += collection_model.schema.attributes
+        })
+      rescue NameError
+        warn "WARNING: You need to require '#{collection_model_name}' first to use "+
+          "it as a subcollection for the #{self.name} model."
+      end
+    end
+
     def text(*args)
       args.expand_opts!(:text => :nested)
       scalar(*args)
-- 
1.7.10.2


Mime
View raw message