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/14 16:09:29 UTC

[PATCH core 3/5] CIMI: Added MachineImage model

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


Signed-off-by: Michal fojtik <mf...@redhat.com>
---
 server/lib/cimi/model/machine_image.rb       |   23 +++++++++
 server/spec/cimi/data/machine_image.json     |   13 +++++
 server/spec/cimi/data/machine_image.xml      |   11 ++++
 server/spec/cimi/model/machine_image_spec.rb |   66 ++++++++++++++++++++++++++
 4 files changed, 113 insertions(+), 0 deletions(-)
 create mode 100644 server/lib/cimi/model/machine_image.rb
 create mode 100644 server/spec/cimi/data/machine_image.json
 create mode 100644 server/spec/cimi/data/machine_image.xml
 create mode 100644 server/spec/cimi/model/machine_image_spec.rb

diff --git a/server/lib/cimi/model/machine_image.rb b/server/lib/cimi/model/machine_image.rb
new file mode 100644
index 0000000..dd3c960
--- /dev/null
+++ b/server/lib/cimi/model/machine_image.rb
@@ -0,0 +1,23 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License.  You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+class CIMI::Model::MachineImage < CIMI::Model::Base
+
+  scalar :image_location
+
+  array :operations do
+    scalar :rel, :href
+  end
+end
diff --git a/server/spec/cimi/data/machine_image.json b/server/spec/cimi/data/machine_image.json
new file mode 100644
index 0000000..e037144
--- /dev/null
+++ b/server/spec/cimi/data/machine_image.json
@@ -0,0 +1,13 @@
+{
+  "uri": "http://cimi.example.org/machine_images/1",
+  "name": "My First image",
+  "description": "A image for testing",
+  "created": "2011-11-14",
+  "imageLocation": { "href": "nfs://cimi.example.com/images/1.img"}
+  "properties": [ {"name": "status", "value": "build"}, { "name": "locked", "value": "true" } ]
+  "operations": [
+    { "rel": "edit",
+      "href": "http://cimi.example.org/machine_images/1/edit" },
+    { "rel": "delete",
+      "href": "http://cimi.example.org/machine_images/1/delete" }]
+}
diff --git a/server/spec/cimi/data/machine_image.xml b/server/spec/cimi/data/machine_image.xml
new file mode 100644
index 0000000..a02bc36
--- /dev/null
+++ b/server/spec/cimi/data/machine_image.xml
@@ -0,0 +1,11 @@
+<MachineImage xmlns="http://www.dmtf.org/cimi">
+  <uri>http://cimi.example.org/machine_image/1</uri>
+  <name>img1</name>
+  <description>Machine Image One</description>
+  <created>2011-11-14</created>
+  <property name="status">BUILD</property>
+  <property name="locked">true</property>
+  <imageLocation href="nfs://cimi.example.com/images/1.img"/>
+  <operation rel="edit" href="http://cimi.example.org/machine_image/1/edit"/>
+  <operation rel="delete" href="http://cimi.example.org/machine_image/1/delete"/>
+</MachineImage>
diff --git a/server/spec/cimi/model/machine_image_spec.rb b/server/spec/cimi/model/machine_image_spec.rb
new file mode 100644
index 0000000..6336660
--- /dev/null
+++ b/server/spec/cimi/model/machine_image_spec.rb
@@ -0,0 +1,66 @@
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License.  You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations
+# under the License.
+#
+
+describe "MachineImage model" do
+
+  before(:all) do
+    @xml = IO::read(File::join(DATA_DIR, "machine_image.xml"))
+    @json = IO::read(File::join(DATA_DIR, "machine_template.json"))
+  end
+
+  it "can be constructed from XML" do
+    img = CIMI::Model::MachineImage.from_xml(@xml)
+    img.should_not be_nil
+    img.created.should == "2011-11-14"
+    img.name.should == "img1"
+    img.description.should == "Machine Image One"
+    img.uri.should == "http://cimi.example.org/machine_image/1"
+    img.image_location.size == 1
+    img.image_location[0]['href'].should == 'nfs://cimi.example.com/images/1.img'
+    img.operations.any? { |operation| operation.rel == 'edit' }.should be_true
+    img.operations.any? { |operation| operation.rel == 'delete' }.should be_true
+    img.operations.each { |operation| operation.href.should =~ /^http:\/\/.*\/(#{operation.rel})$/ }
+    img.should serialize_to @xml, :fmt => :xml
+  end
+
+  it "should parse properties correctly in XML" do
+    img = CIMI::Model::MachineImage.from_xml(@xml)
+    img.property.any? { |p| p.name == 'status' }.should be_true
+    img.property.any? { |p| p.name == 'locked' }.should be_true
+    img.property.size.should == 2
+  end
+
+  it "should convert strings in keys to symbols when contructed from XML" do
+    imgl = CIMI::Model::MachineImage.from_xml(@xml)
+    imgl.should_not be_nil
+    imgl.attribute_values.keys.each { |key| key.should be_a(Symbol) }
+  end
+
+  it "can be constructed from JSON" do
+    templ = CIMI::Model::MachineTemplate.from_json(@json)
+    templ.should_not be_nil
+    templ.created.should == "2011-11-01"
+    templ.should serialize_to @json, :fmt => :json
+  end
+
+  it "should parse properties correctly in JSON" do
+    img = CIMI::Model::MachineImage.from_json(@json)
+    # TODO: Fix this
+    # img.property.any? { |p| p.name == 'status' }.should be_true
+    # img.property.any? { |p| p.name == 'locked' }.should be_true
+    # img.property.size.should == 2
+  end
+end
-- 
1.7.4.4


Re: [PATCH core 3/5] CIMI: Added MachineImage model

Posted by Michal Fojtik <mf...@redhat.com>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

marios@redhat.com wrote:

> minor comments inline (otherwise ACK):

Thanks!

> 
> On 14/11/11 17:09, mfojtik@redhat.com wrote:
>> From: Michal Fojtik <mf...@redhat.com> +# Unless required by
>> applicable law or agreed to in writing, software +# distributed
>> under the License is distributed on an "AS IS" BASIS, WITHOUT +#
>> WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
>> See the +# License for the specific language governing permissions
>> and limitations +# under the License. + +class
>> CIMI::Model::MachineImage < CIMI::Model::Base + +  scalar
>> :image_location
> 
> could also have :image_data instead of :image_location (either-or,
> but not both)... :image_data is a byte array, but represented as
> string in json and xml, so scalar?
> 
> JSON: "imageData": string, ? XML: <imageData> xs:string </imageData>
> ?

Yes, that's right. I'll add this before push. Thanks!

>> + +  array :operations do +    scalar :rel, :href +  end +end diff
>> --git a/server/spec/cimi/data/machine_image.json
>> b/server/spec/cimi/data/machine_image.json new file mode 100644 
>> index 0000000..e037144 --- /dev/null +++
>> b/server/spec/cimi/data/machine_image.json @@ -0,0 +1,13 @@ +{ +
>> "uri": "http://cimi.example.org/machine_images/1", +  "name": "My
>> First image", +  "description": "A image for testing", +
>> "created": "2011-11-14", +  "imageLocation": { "href":
>> "nfs://cimi.example.com/images/1.img"} +  "properties": [ {"name":
>> "status", "value": "build"}, { "name": "locked", "value": "true" }
>> ]
> 
> I think this is gonna be more like
> 
> +  "properties": [ {"status": "build", "locked": "true" } ]

Right, I misinterpreted the example from spec. Will fix it before commit.


>> +  "operations": [ +    { "rel": "edit", +      "href":
>> "http://cimi.example.org/machine_images/1/edit" }, +    { "rel":
>> "delete", +      "href":
>> "http://cimi.example.org/machine_images/1/delete" }] +} diff --git
>> a/server/spec/cimi/data/machine_image.xml
>> b/server/spec/cimi/data/machine_image.xml new file mode 100644 
>> index 0000000..a02bc36 --- /dev/null +++
>> b/server/spec/cimi/data/machine_image.xml @@ -0,0 +1,11 @@ 
>> +<MachineImage xmlns="http://www.dmtf.org/cimi"> +
>> <uri>http://cimi.example.org/machine_image/1</uri> +
>> <name>img1</name> +  <description>Machine Image One</description> +
>> <created>2011-11-14</created> +  <property
>> name="status">BUILD</property> +  <property
>> name="locked">true</property>
> 
> properties are (for now :) ) :key="status" instead of :name
> 
> <property key="xs:string"> xs:string </property> *

I hope this is the same thing as above.

> 
>> +  <imageLocation href="nfs://cimi.example.com/images/1.img"/> +
>> <operation rel="edit"
>> href="http://cimi.example.org/machine_image/1/edit"/> +  <operation
>> rel="delete"
>> href="http://cimi.example.org/machine_image/1/delete"/> 
>> +</MachineImage>

- -- 
- --
Michal Fojtik, mfojtik@redhat.com
Deltacloud API: http://deltacloud.org
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iQEcBAEBAgAGBQJOw6MsAAoJEEnFpmY+LvFRocEH/AnxwD16ksTv6DoGfYKr3Ahd
8eE5YUMCw5w1PMkiDlmQmkXqsfAQ1GFf4Uj55EFNqvhOdyCgVJG0Et1TLN7blZtW
Q3iW/DKktLLS7Us3zGlx+DT+LEaWUrYKKMn2E8mHsL9lS5mjVHI0GoGCNg/pUR3w
LCS4q1rfBsYm9M3bokAQs/SXMIUjYzW7SidIy+dUQS+D9WBpjkgiyuubRuhd98XV
D0u6Z9BWJWM2eGELSOKvBmCue+ZEepronFrshxhyIxnX1ZTHVtr/U6rJ0u2v5o64
sidzUeLZPXG3TsNFS5rprO0gl6i7tLq4HvrCc2AzA2bVGVKffGwCXGkvE2GkR3k=
=nhcC
-----END PGP SIGNATURE-----

Re: [PATCH core 3/5] CIMI: Added MachineImage model

Posted by David Lutterkort <lu...@redhat.com>.
On Wed, 2011-11-16 at 13:21 +0200, marios@redhat.com wrote:
> minor comments inline (otherwise ACK):
> 
> On 14/11/11 17:09, mfojtik@redhat.com wrote:
> > From: Michal Fojtik <mf...@redhat.com>
> > +# Unless required by applicable law or agreed to in writing, software
> > +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
> > +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
> > +# License for the specific language governing permissions and limitations
> > +# under the License.
> > +
> > +class CIMI::Model::MachineImage < CIMI::Model::Base
> > +
> > +  scalar :image_location
> 
> could also have :image_data instead of :image_location (either-or, but
> not both)... :image_data is a byte array, but represented as string in
> json and xml, so scalar?

We can't express 'either or' in the DSL (yet) Adding Rails-style
validation is one of the things I'd like to do. In terms of lifecycle, I
think validations should run right after from_{xml,json} and just before
to_{xml,json}

But we can hold off on that for a little bit, I'd rather get to start
machines first ;)

David



Re: [PATCH core 3/5] CIMI: Added MachineImage model

Posted by "marios@redhat.com" <ma...@redhat.com>.
minor comments inline (otherwise ACK):

On 14/11/11 17:09, mfojtik@redhat.com wrote:
> From: Michal Fojtik <mf...@redhat.com>
> +# Unless required by applicable law or agreed to in writing, software
> +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
> +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
> +# License for the specific language governing permissions and limitations
> +# under the License.
> +
> +class CIMI::Model::MachineImage < CIMI::Model::Base
> +
> +  scalar :image_location

could also have :image_data instead of :image_location (either-or, but
not both)... :image_data is a byte array, but represented as string in
json and xml, so scalar?

JSON: "imageData": string, ?
XML: <imageData> xs:string </imageData> ?

> +
> +  array :operations do
> +    scalar :rel, :href
> +  end
> +end
> diff --git a/server/spec/cimi/data/machine_image.json b/server/spec/cimi/data/machine_image.json
> new file mode 100644
> index 0000000..e037144
> --- /dev/null
> +++ b/server/spec/cimi/data/machine_image.json
> @@ -0,0 +1,13 @@
> +{
> +  "uri": "http://cimi.example.org/machine_images/1",
> +  "name": "My First image",
> +  "description": "A image for testing",
> +  "created": "2011-11-14",
> +  "imageLocation": { "href": "nfs://cimi.example.com/images/1.img"}
> +  "properties": [ {"name": "status", "value": "build"}, { "name": "locked", "value": "true" } ]

I think this is gonna be more like

 +  "properties": [ {"status": "build", "locked": "true" } ]

> +  "operations": [
> +    { "rel": "edit",
> +      "href": "http://cimi.example.org/machine_images/1/edit" },
> +    { "rel": "delete",
> +      "href": "http://cimi.example.org/machine_images/1/delete" }]
> +}
> diff --git a/server/spec/cimi/data/machine_image.xml b/server/spec/cimi/data/machine_image.xml
> new file mode 100644
> index 0000000..a02bc36
> --- /dev/null
> +++ b/server/spec/cimi/data/machine_image.xml
> @@ -0,0 +1,11 @@
> +<MachineImage xmlns="http://www.dmtf.org/cimi">
> +  <uri>http://cimi.example.org/machine_image/1</uri>
> +  <name>img1</name>
> +  <description>Machine Image One</description>
> +  <created>2011-11-14</created>
> +  <property name="status">BUILD</property>
> +  <property name="locked">true</property>

properties are (for now :) ) :key="status" instead of :name

<property key="xs:string"> xs:string </property> *

> +  <imageLocation href="nfs://cimi.example.com/images/1.img"/>
> +  <operation rel="edit" href="http://cimi.example.org/machine_image/1/edit"/>
> +  <operation rel="delete" href="http://cimi.example.org/machine_image/1/delete"/>
> +</MachineImage>

Re: [PATCH core 3/5] CIMI: Added MachineImage model

Posted by David Lutterkort <lu...@redhat.com>.
On Mon, 2011-11-14 at 16:09 +0100, mfojtik@redhat.com wrote:
> diff --git a/server/lib/cimi/model/machine_image.rb b/server/lib/cimi/model/machine_image.rb
> new file mode 100644
> index 0000000..dd3c960
> --- /dev/null
> +++ b/server/lib/cimi/model/machine_image.rb
> @@ -0,0 +1,23 @@

> +class CIMI::Model::MachineImage < CIMI::Model::Base
> +
> +  scalar :image_location

This should be 'href', not 'scalar'. 'href' is syntactic sugar for
  struct :image_location do
    scalar :href
  end

David