David:

Still a problem: an empty collection is not expanded and remains closed (e.g. 
when retrieving a machine with no attached volumes with $expand=volumes). 

Frederic


On Nov 8, 2012, at 5:05 AM, <[email protected]> <[email protected]> wrote:

> From: David Lutterkort <[email protected]>
> 
> Works for disks and volumes in machines now
> ---
> 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(-)
> 
> 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
> 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
> 
> 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
> 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'
> 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
> -- 
> 1.7.7.6
> 

Reply via email to