You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@deltacloud.apache.org by lu...@apache.org on 2012/11/08 18:13:51 UTC

[7/15] git commit: CIMI: initial support for $expand

CIMI: initial support for $expand

Works for disks and volumes in machines now


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

Branch: refs/heads/master
Commit: 029ad9304cea6f3f5b2d6fe72c06550711233b07
Parents: f18a366
Author: David Lutterkort <lu...@redhat.com>
Authored: Wed Nov 7 20:00:33 2012 -0800
Committer: David Lutterkort <lu...@redhat.com>
Committed: Thu Nov 8 09:11:18 2012 -0800

----------------------------------------------------------------------
 server/lib/cimi/helpers/cimi_helper.rb         |    5 ++
 server/lib/cimi/models/base.rb                 |    1 +
 server/lib/cimi/models/collection.rb           |    1 +
 server/lib/cimi/models/machine.rb              |   16 ++++++--
 server/tests/cimi/collections/machines_test.rb |   39 +++++++++++++++++++
 5 files changed, 58 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltacloud/blob/029ad930/server/lib/cimi/helpers/cimi_helper.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/helpers/cimi_helper.rb b/server/lib/cimi/helpers/cimi_helper.rb
index a7f76cf..b61c7d2 100644
--- a/server/lib/cimi/helpers/cimi_helper.rb
+++ b/server/lib/cimi/helpers/cimi_helper.rb
@@ -16,6 +16,11 @@
 module CIMI
   module Helper
 
+    def expand?(collection)
+      params['$expand'] == '*' ||
+        (params['$expand'] || '').split(',').include?(collection.to_s)
+    end
+
     def no_content_with_status(code=200)
       body ''
       status code

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/029ad930/server/lib/cimi/models/base.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/models/base.rb b/server/lib/cimi/models/base.rb
index bdaa860..63b24ae 100644
--- a/server/lib/cimi/models/base.rb
+++ b/server/lib/cimi/models/base.rb
@@ -173,6 +173,7 @@ class CIMI::Model::Resource
   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
 

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/029ad930/server/lib/cimi/models/collection.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/models/collection.rb b/server/lib/cimi/models/collection.rb
index 49c36ff..70692ca 100644
--- a/server/lib/cimi/models/collection.rb
+++ b/server/lib/cimi/models/collection.rb
@@ -82,6 +82,7 @@ module CIMI::Model
       entry_schema = model_class.schema
       coll_class.instance_eval do
         text :id
+        scalar :href
         text :count
         scalar :href if opts[:embedded]
         array self.entry_name, :schema => entry_schema, :xml_name => model_name

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/029ad930/server/lib/cimi/models/machine.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/models/machine.rb b/server/lib/cimi/models/machine.rb
index 9358c18..63520a8 100644
--- a/server/lib/cimi/models/machine.rb
+++ b/server/lib/cimi/models/machine.rb
@@ -114,7 +114,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.nil? ? Time.now.xmlschema : Time.parse(instance.launch_time).xmlschema,
@@ -122,11 +122,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.expand? :disks
+      machine_spec[:disks] = CIMI::Model::Disk.find(instance, machine_conf, context, :all)
+    end
+    if context.expand? :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'

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/029ad930/server/tests/cimi/collections/machines_test.rb
----------------------------------------------------------------------
diff --git a/server/tests/cimi/collections/machines_test.rb b/server/tests/cimi/collections/machines_test.rb
index 8d891c5..db49cab 100644
--- a/server/tests/cimi/collections/machines_test.rb
+++ b/server/tests/cimi/collections/machines_test.rb
@@ -5,6 +5,8 @@ require_relative './common.rb'
 
 describe CIMI::Collections::Machines do
 
+  NS = { "c" => "http://schemas.dmtf.org/cimi/1" }
+
   before do
     def app; run_frontend(:cimi) end
     authorize 'mockuser', 'mockpassword'
@@ -33,6 +35,43 @@ describe CIMI::Collections::Machines do
     xml.root.name.must_equal 'Machine'
   end
 
+  describe "$expand" do
+    def machine(*expand)
+      url = '/machines/inst1'
+      url += "?$expand=#{expand.join(",")}" unless expand.empty?
+      get root_url url
+      status.must_equal 200
+    end
+
+    def ids(coll)
+      xml.xpath("/c:Machine/c:#{coll}/c:id", NS)
+    end
+
+    it "should not expand collections when missing" do
+      machine
+      ids(:disks).must_be_empty
+      ids(:volumes).must_be_empty
+    end
+
+    it "should expand named collections" do
+      machine :disks
+      ids(:disks).size.must_equal 1
+      ids(:volumes).must_be_empty
+    end
+
+    it "should expand multiple named collections" do
+      machine :disks, :volumes
+      ids(:disks).size.must_equal 1
+      ids(:volumes).size.must_equal 1
+    end
+
+    it "should expand all collections with *" do
+      machine "*"
+      ids(:disks).size.must_equal 1
+      ids(:volumes).size.must_equal 1
+    end
+  end
+
   it 'should not return non-existing machine' do
     get root_url '/machines/unknown-machine'
     status.must_equal 404