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 2011/11/18 14:32:29 UTC

CIMI: Fixed properties vs property problem

Hi,

This patch should make our RSpec tests green for CIMI. After David suggestion I
added new type to schema called 'hash' to deal with the incostistency between
property and properties. Both should now serialize/deserialize correctly.

  -- Michal


Re: [PATCH core 4/4] CIMI: Added new type 'hash' to deal with properties

Posted by Tong Li <li...@us.ibm.com>.
OK
Tong Li
Emerging Technologies & Standards
Building 501/B205
litong01@us.ibm.com

mfojtik@redhat.com wrote on 11/18/2011 08:32:33 AM:

> From: mfojtik@redhat.com
> To: deltacloud-dev@incubator.apache.org
> Date: 11/18/2011 08:32 AM
> Subject: [PATCH core 4/4] CIMI: Added new type 'hash' to deal with
properties
>
> From: Michal Fojtik <mf...@redhat.com>
>
>
> Signed-off-by: Michal fojtik <mf...@redhat.com>
> ---
>  server/lib/cimi/model/base.rb   |    2 +-
>  server/lib/cimi/model/schema.rb |   33 +++++++++++++++++++++++++++++++++
>  2 files changed, 34 insertions(+), 1 deletions(-)
>
> diff --git a/server/lib/cimi/model/base.rb
b/server/lib/cimi/model/base.rb
> index de6529b..7cc3f49 100644
> --- a/server/lib/cimi/model/base.rb
> +++ b/server/lib/cimi/model/base.rb
> @@ -163,7 +163,7 @@ class CIMI::Model::Base
>    text :uri, :name, :description, :created
>
>    # FIXME: this doesn't match with JSON
> -  array :property, :content => :value do
> +  hash :property, :content => :value do
>      scalar :name
>    end
>  end
> diff --git a/server/lib/cimi/model/schema.rb
b/server/lib/cimi/model/schema.rb
> index 946c1a2..46d537b 100644
> --- a/server/lib/cimi/model/schema.rb
> +++ b/server/lib/cimi/model/schema.rb
> @@ -170,6 +170,35 @@ class CIMI::Model::Schema
>      end
>    end
>
> +  class Hash < Attribute
> +
> +    def initialize(name, opts = {}, &block)
> +      opts[:json_name] = name.to_s.pluralize unless opts[:json_name]
> +      super(name, opts)
> +      @struct = Struct.new(name, opts, &block)
> +    end
> +
> +    def from_xml(xml, model)
> +      model[name] = (xml[xml_name] || []).map { |elt|
> @struct.convert_from_xml(elt) }
> +    end
> +
> +    def from_json(json, model)
> +      model[name] = (json[json_name] || {}).inject([]) do |result,item|
> +        result << @struct.convert_from_json({ 'name' => item[0],
> 'value' => item[1] })
> +      end
> +    end
> +
> +    def to_xml(model, xml)
> +      ary = model[name].map { |elt| @struct.convert_to_xml(elt) }
> +      xml[xml_name] = ary unless ary.empty?
> +    end
> +
> +    def to_json(model, json)
> +      ary = model[name].map { |elt| @struct.convert_to_json(elt) }
> +      json[json_name] = ary.inject({}) { |result, item| result[item
> ['name']] = item['value']; result } unless ary.empty?
> +    end
> +  end
> +
>    #
>    # The actual Schema class
>    #
> @@ -231,6 +260,10 @@ class CIMI::Model::Schema
>      def struct(name, opts={}, &block)
>        add_attributes!([name, opts], Struct, &block)
>      end
> +
> +    def hash(name, opts={}, &block)
> +      add_attributes!([name, opts], Hash, &block)
> +    end
>    end
>
>    include DSL
> --
> 1.7.4.4
>

[PATCH core 4/4] CIMI: Added new type 'hash' to deal with properties

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


Signed-off-by: Michal fojtik <mf...@redhat.com>
---
 server/lib/cimi/model/base.rb   |    2 +-
 server/lib/cimi/model/schema.rb |   33 +++++++++++++++++++++++++++++++++
 2 files changed, 34 insertions(+), 1 deletions(-)

diff --git a/server/lib/cimi/model/base.rb b/server/lib/cimi/model/base.rb
index de6529b..7cc3f49 100644
--- a/server/lib/cimi/model/base.rb
+++ b/server/lib/cimi/model/base.rb
@@ -163,7 +163,7 @@ class CIMI::Model::Base
   text :uri, :name, :description, :created
 
   # FIXME: this doesn't match with JSON
-  array :property, :content => :value do
+  hash :property, :content => :value do
     scalar :name
   end
 end
diff --git a/server/lib/cimi/model/schema.rb b/server/lib/cimi/model/schema.rb
index 946c1a2..46d537b 100644
--- a/server/lib/cimi/model/schema.rb
+++ b/server/lib/cimi/model/schema.rb
@@ -170,6 +170,35 @@ class CIMI::Model::Schema
     end
   end
 
+  class Hash < Attribute
+
+    def initialize(name, opts = {}, &block)
+      opts[:json_name] = name.to_s.pluralize unless opts[:json_name]
+      super(name, opts)
+      @struct = Struct.new(name, opts, &block)
+    end
+
+    def from_xml(xml, model)
+      model[name] = (xml[xml_name] || []).map { |elt| @struct.convert_from_xml(elt) }
+    end
+
+    def from_json(json, model)
+      model[name] = (json[json_name] || {}).inject([]) do |result,item|
+        result << @struct.convert_from_json({ 'name' => item[0], 'value' => item[1] })
+      end
+    end
+
+    def to_xml(model, xml)
+      ary = model[name].map { |elt| @struct.convert_to_xml(elt) }
+      xml[xml_name] = ary unless ary.empty?
+    end
+
+    def to_json(model, json)
+      ary = model[name].map { |elt| @struct.convert_to_json(elt) }
+      json[json_name] = ary.inject({}) { |result, item| result[item['name']] = item['value']; result } unless ary.empty?
+    end
+  end
+
   #
   # The actual Schema class
   #
@@ -231,6 +260,10 @@ class CIMI::Model::Schema
     def struct(name, opts={}, &block)
       add_attributes!([name, opts], Struct, &block)
     end
+
+    def hash(name, opts={}, &block)
+      add_attributes!([name, opts], Hash, &block)
+    end
   end
 
   include DSL
-- 
1.7.4.4


Re: [PATCH core 1/4] CIMI: Added possibility to run single spec for specified model in Rakefile

Posted by Tong Li <li...@us.ibm.com>.
OK.
Tong Li
Emerging Technologies & Standards
Building 501/B205
litong01@us.ibm.com



From:	mfojtik@redhat.com
To:	deltacloud-dev@incubator.apache.org
Date:	11/18/2011 08:32 AM
Subject:	[PATCH core 1/4] CIMI: Added possibility to run single spec for
            specified model in Rakefile



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


Signed-off-by: Michal fojtik <mf...@redhat.com>
---
 server/Rakefile |    9 +++++++++
 1 files changed, 9 insertions(+), 0 deletions(-)

diff --git a/server/Rakefile b/server/Rakefile
index 9436518..53fb7bf 100644
--- a/server/Rakefile
+++ b/server/Rakefile
@@ -95,6 +95,15 @@ RSpec::Core::RakeTask.new do |t|
   t.rspec_opts = [ "--format", "nested", "--color",
"-r ./spec/spec_helper.rb"]
 end

+Dir['spec/**/*_spec.rb'].each do |file|
+  RSpec::Core::RakeTask.new("spec:#{File.basename(file).gsub(/_spec\.rb$/,
'')}") do |t|
+    t.pattern = FileList[file]
+    t.rspec_opts = [ "--format", "nested", "--color",
"-r ./spec/spec_helper.rb"]
+  end
+end
+
+
+
 begin
   require 'yard'
   YARD::Rake::YardocTask.new do |t|
--
1.7.4.4


[PATCH core 1/4] CIMI: Added possibility to run single spec for specified model in Rakefile

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


Signed-off-by: Michal fojtik <mf...@redhat.com>
---
 server/Rakefile |    9 +++++++++
 1 files changed, 9 insertions(+), 0 deletions(-)

diff --git a/server/Rakefile b/server/Rakefile
index 9436518..53fb7bf 100644
--- a/server/Rakefile
+++ b/server/Rakefile
@@ -95,6 +95,15 @@ RSpec::Core::RakeTask.new do |t|
   t.rspec_opts = [ "--format", "nested", "--color", "-r ./spec/spec_helper.rb"]
 end
 
+Dir['spec/**/*_spec.rb'].each do |file|
+  RSpec::Core::RakeTask.new("spec:#{File.basename(file).gsub(/_spec\.rb$/, '')}") do |t|
+    t.pattern = FileList[file]
+    t.rspec_opts = [ "--format", "nested", "--color", "-r ./spec/spec_helper.rb"]
+  end
+end
+
+
+
 begin
   require 'yard'
   YARD::Rake::YardocTask.new do |t|
-- 
1.7.4.4


Re: [PATCH core 3/4] CIMI: Fixed properties syntax for JSON to be Hash

Posted by Tong Li <li...@us.ibm.com>.
OK
Tong Li
Emerging Technologies & Standards
Building 501/B205
litong01@us.ibm.com

mfojtik@redhat.com wrote on 11/18/2011 08:32:32 AM:

> From: mfojtik@redhat.com
> To: deltacloud-dev@incubator.apache.org
> Date: 11/18/2011 08:32 AM
> Subject: [PATCH core 3/4] CIMI: Fixed properties syntax for JSON to be
Hash
>
> From: Michal Fojtik <mf...@redhat.com>
>
>
> Signed-off-by: Michal fojtik <mf...@redhat.com>
> ---
>  server/spec/cimi/data/machine_configuration.json |    2 +-
>  server/spec/cimi/data/machine_image.json         |    2 +-
>  2 files changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/server/spec/cimi/data/machine_configuration.json b/
> server/spec/cimi/data/machine_configuration.json
> index 7dbd0ae..f106a2d 100644
> --- a/server/spec/cimi/data/machine_configuration.json
> +++ b/server/spec/cimi/data/machine_configuration.json
> @@ -8,7 +8,7 @@
>    "disks" : [
>      { "capacity": { "quantity": "1", "units": "terabyte" } }
>    ],
> -  "properties": [ { "architectore": "i386" } ],
> +  "properties": { "architecture": "i386" },
>    "operations": [
>      { "rel": "edit",
>        "href": "http://cimi.example.org/machine_configurations/1/edit" },
> diff --git a/server/spec/cimi/data/machine_image.json b/server/spec/
> cimi/data/machine_image.json
> index dbd7218..d338517 100644
> --- a/server/spec/cimi/data/machine_image.json
> +++ b/server/spec/cimi/data/machine_image.json
> @@ -4,7 +4,7 @@
>    "description": "Machine Image One",
>    "created": "2011-11-14",
>    "imageLocation": { "href": "nfs://cimi.example.com/images/1.img" },
> -  "properties": [ { "status": "build"}, { "locked": "true" } ],
> +  "properties": { "status": "BUILD", "locked": "true" },
>    "operations": [
>    { "rel": "edit",
>      "href": "http://cimi.example.org/machine_images/1/edit" },
> --
> 1.7.4.4
>

[PATCH core 3/4] CIMI: Fixed properties syntax for JSON to be Hash

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


Signed-off-by: Michal fojtik <mf...@redhat.com>
---
 server/spec/cimi/data/machine_configuration.json |    2 +-
 server/spec/cimi/data/machine_image.json         |    2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/server/spec/cimi/data/machine_configuration.json b/server/spec/cimi/data/machine_configuration.json
index 7dbd0ae..f106a2d 100644
--- a/server/spec/cimi/data/machine_configuration.json
+++ b/server/spec/cimi/data/machine_configuration.json
@@ -8,7 +8,7 @@
   "disks" : [
     { "capacity": { "quantity": "1", "units": "terabyte" } }
   ],
-  "properties": [ { "architectore": "i386" } ],
+  "properties": { "architecture": "i386" },
   "operations": [
     { "rel": "edit",
       "href": "http://cimi.example.org/machine_configurations/1/edit" },
diff --git a/server/spec/cimi/data/machine_image.json b/server/spec/cimi/data/machine_image.json
index dbd7218..d338517 100644
--- a/server/spec/cimi/data/machine_image.json
+++ b/server/spec/cimi/data/machine_image.json
@@ -4,7 +4,7 @@
   "description": "Machine Image One",
   "created": "2011-11-14",
   "imageLocation": { "href": "nfs://cimi.example.com/images/1.img" },
-  "properties": [ { "status": "build"}, { "locked": "true" } ],
+  "properties": { "status": "BUILD", "locked": "true" },
   "operations": [
   { "rel": "edit",
     "href": "http://cimi.example.org/machine_images/1/edit" },
-- 
1.7.4.4


Re: [PATCH core 2/4] Core: Fixed .pluralize to deal with values like 'property' => 'properties'

Posted by Tong Li <li...@us.ibm.com>.
OK
Tong Li
Emerging Technologies & Standards
Building 501/B205
litong01@us.ibm.com

mfojtik@redhat.com wrote on 11/18/2011 08:32:31 AM:

> From: mfojtik@redhat.com
> To: deltacloud-dev@incubator.apache.org
> Date: 11/18/2011 08:32 AM
> Subject: [PATCH core 2/4] Core: Fixed .pluralize to deal with values
> like 'property' => 'properties'
>
> From: Michal Fojtik <mf...@redhat.com>
>
>
> Signed-off-by: Michal fojtik <mf...@redhat.com>
> ---
>  server/lib/deltacloud/core_ext/string.rb |    1 +
>  1 files changed, 1 insertions(+), 0 deletions(-)
>
> diff --git a/server/lib/deltacloud/core_ext/string.rb b/server/lib/
> deltacloud/core_ext/string.rb
> index 53b3984..de56c99 100644
> --- a/server/lib/deltacloud/core_ext/string.rb
> +++ b/server/lib/deltacloud/core_ext/string.rb
> @@ -34,6 +34,7 @@ class String
>
>    def pluralize
>      return self + 'es' if self =~ /ess$/
> +    return self[0, self.length-1] + "ies" if self =~ /ty$/
>      self + "s"
>    end
>
> --
> 1.7.4.4
>

[PATCH core 2/4] Core: Fixed .pluralize to deal with values like 'property' => 'properties'

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


Signed-off-by: Michal fojtik <mf...@redhat.com>
---
 server/lib/deltacloud/core_ext/string.rb |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)

diff --git a/server/lib/deltacloud/core_ext/string.rb b/server/lib/deltacloud/core_ext/string.rb
index 53b3984..de56c99 100644
--- a/server/lib/deltacloud/core_ext/string.rb
+++ b/server/lib/deltacloud/core_ext/string.rb
@@ -34,6 +34,7 @@ class String
 
   def pluralize
     return self + 'es' if self =~ /ess$/
+    return self[0, self.length-1] + "ies" if self =~ /ty$/
     self + "s"
   end
 
-- 
1.7.4.4