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:30 UTC
[PATCH core 4/5] CIMI: Added MachineConfiguration model
From: Michal Fojtik <mf...@redhat.com>
Signed-off-by: Michal fojtik <mf...@redhat.com>
---
server/lib/cimi/model/machine_configuration.rb | 28 +++++++
server/spec/cimi/data/machine_configuration.json | 17 ++++
server/spec/cimi/data/machine_configuration.xml | 14 ++++
.../spec/cimi/model/machine_configuration_spec.rb | 78 ++++++++++++++++++++
4 files changed, 137 insertions(+), 0 deletions(-)
create mode 100644 server/lib/cimi/model/machine_configuration.rb
create mode 100644 server/spec/cimi/data/machine_configuration.json
create mode 100644 server/spec/cimi/data/machine_configuration.xml
create mode 100644 server/spec/cimi/model/machine_configuration_spec.rb
diff --git a/server/lib/cimi/model/machine_configuration.rb b/server/lib/cimi/model/machine_configuration.rb
new file mode 100644
index 0000000..221236c
--- /dev/null
+++ b/server/lib/cimi/model/machine_configuration.rb
@@ -0,0 +1,28 @@
+# 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::MachineConfiguration < CIMI::Model::Base
+
+ scalar :memory
+ text :cpu
+
+ array :disk do
+ scalar :capacity
+ end
+
+ array :operations do
+ scalar :rel, :href
+ end
+end
diff --git a/server/spec/cimi/data/machine_configuration.json b/server/spec/cimi/data/machine_configuration.json
new file mode 100644
index 0000000..8864340
--- /dev/null
+++ b/server/spec/cimi/data/machine_configuration.json
@@ -0,0 +1,17 @@
+{
+ "uri": "http://cimi.example.org/machine_configurations/1",
+ "name": "MachineConfiguration1",
+ "description": "Example MachineConfiguration One",
+ "created": "2011-11-14",
+ "cpu": "2",
+ "memory" : { "quantity": "1", "units": "gigabyte" },
+ "disks" : [
+ { "capacity": { "quantity": "1", "units": "terabyte", "format": "ext4", "attachmentPoint": "/" } }
+ ],
+ "properties": [ { "name": "architectore", "value" : "i386" } ],
+ "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_configuration.xml b/server/spec/cimi/data/machine_configuration.xml
new file mode 100644
index 0000000..788a185
--- /dev/null
+++ b/server/spec/cimi/data/machine_configuration.xml
@@ -0,0 +1,14 @@
+<MachineConfiguration xmlns="http://www.dmtf.org/cimi">
+ <uri>http://cimi.example.org/machine_configurations/1</uri>
+ <name>MachineConfiguration1</name>
+ <description>Example MachineConfiguration One</description>
+ <created>2011-11-14</created>
+ <property name="architecture">i386</property>
+ <cpu>2</cpu>
+ <memory quantity="1" units="gigabyte"/>
+ <disk>
+ <capacity quantity="1" units="terabyte" format="ext4" attachmentPoint="/"/>
+ </disk>
+ <operation rel="edit" href="http://cimi.example.org/machine_configurations/1/edit"/>
+ <operation rel="delete" href="http://cimi.example.org/machine_configurations/1/delete"/>
+</MachineConfiguration>
diff --git a/server/spec/cimi/model/machine_configuration_spec.rb b/server/spec/cimi/model/machine_configuration_spec.rb
new file mode 100644
index 0000000..f17c69b
--- /dev/null
+++ b/server/spec/cimi/model/machine_configuration_spec.rb
@@ -0,0 +1,78 @@
+# 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 "MachineConfiguration model" do
+
+ before(:all) do
+ @xml = IO::read(File::join(DATA_DIR, "machine_configuration.xml"))
+ @json = IO::read(File::join(DATA_DIR, "machine_configuration.json"))
+ end
+
+ it "can be constructed from XML" do
+ conf = CIMI::Model::MachineConfiguration.from_xml(@xml)
+ conf.should_not be_nil
+ conf.name.should == 'MachineConfiguration1'
+ conf.uri == 'http://cimi.example.org/machine_configurations/1'
+ conf.description == 'Example MachineConfiguration One'
+ conf.created.should == "2011-11-14"
+ conf.cpu.should == "2"
+ conf.memory.size.should == 1
+ conf.memory[0]['quantity'].should == '1'
+ conf.memory[0]['units'].should == 'gigabyte'
+ conf.disk.size.should == 1
+ conf.disk[0].respond_to?(:capacity).should be_true
+ conf.disk[0].capacity.size.should == 1
+ conf.disk[0].capacity[0]['format'] == 'ext4'
+ conf.disk[0].capacity[0]['quantity'] == '1'
+ conf.disk[0].capacity[0]['attachmentPoint'] == '/'
+ conf.disk[0].capacity[0]['units'] == 'terabyte'
+ conf.operations.size.should == 2
+ conf.operations.any? { |operation| operation.rel == 'edit' }.should be_true
+ conf.operations.any? { |operation| operation.rel == 'delete' }.should be_true
+ conf.operations.each { |operation| operation.href.should =~ /^http:\/\/.*\/(#{operation.rel})$/ }
+ conf.should serialize_to @xml, :fmt => :xml
+ end
+
+ it "should convert strings in keys to symbols when contructed from XML" do
+ conf = CIMI::Model::MachineConfiguration.from_xml(@xml)
+ conf.should_not be_nil
+ conf.attribute_values.keys.each { |key| key.should be_a(Symbol) }
+ end
+
+ it "can be constructed from JSON" do
+ conf = CIMI::Model::MachineConfiguration.from_json(@json)
+ conf.should_not be_nil
+ conf.name.should == 'MachineConfiguration1'
+ conf.uri == 'http://cimi.example.org/machine_configurations/1'
+ conf.description == 'Example MachineConfiguration One'
+ conf.created.should == "2011-11-14"
+ conf.cpu.should == "2"
+ conf.memory.size.should == 1
+ conf.memory[0]['quantity'].should == '1'
+ conf.memory[0]['units'].should == 'gigabyte'
+ conf.disk.size.should == 1
+ conf.disk[0].respond_to?(:capacity).should be_true
+ conf.disk[0].capacity.size.should == 1
+ conf.disk[0].capacity[0]['format'] == 'ext4'
+ conf.disk[0].capacity[0]['quantity'] == '1'
+ conf.disk[0].capacity[0]['attachmentPoint'] == '/'
+ conf.disk[0].capacity[0]['units'] == 'terabyte'
+ conf.operations.size.should == 2
+ conf.operations.any? { |operation| operation.rel == 'edit' }.should be_true
+ conf.operations.any? { |operation| operation.rel == 'delete' }.should be_true
+ conf.operations.each { |operation| operation.href.should =~ /^http:\/\/.*\/(#{operation.rel})$/ }
+ conf.should serialize_to @json, :fmt => :json
+ end
+end
--
1.7.4.4
Re: [PATCH core 4/5] CIMI: Added MachineConfiguration model
Posted by David Lutterkort <lu...@redhat.com>.
On Mon, 2011-11-14 at 16:09 +0100, mfojtik@redhat.com wrote:
> From: Michal Fojtik <mf...@redhat.com>
>
>
> Signed-off-by: Michal fojtik <mf...@redhat.com>
> ---
> diff --git a/server/lib/cimi/model/machine_configuration.rb b/server/lib/cimi/model/machine_configuration.rb
> new file mode 100644
> index 0000000..221236c
> --- /dev/null
> +++ b/server/lib/cimi/model/machine_configuration.rb
> @@ -0,0 +1,28 @@
> +class CIMI::Model::MachineConfiguration < CIMI::Model::Base
> +
> + scalar :memory
Model that as
struct :memory do
scalar :quantity
scalar :units
end
> + text :cpu
> +
> + array :disk do
> + scalar :capacity
> + end
That has to be _disks_; the convention is that arrays always use
pluralized names, i.e. the name that is used in JSON. In fact, the whole
exercise of the model layer is to create Ruby objects that you can talk
to as if you were in JavaScript and had just deserialized the JSON form
of the object.
One general remark about specs: we should have the following roundtrip
test for everything
Model.from_xml(xml).to_xml.should serialize_to xml
Model.from_json(json).to_json.should serialize_to json
Model.from_xml(xml).to_json.should serialize_to json
Model.from_json(json).to_xml.should serialize_to xml
The last two should help us catch inconsistencies in how we treat XML
vs. JSON (and I know that you have been writing the first two kinds of
tests)
The above four tests can be hidden in a helper
def check_serialization(model_class, xml, json)
David
Re: [PATCH core 4/5] CIMI: Added MachineConfiguration model
Posted by "marios@redhat.com" <ma...@redhat.com>.
minor comment inline (otherwise ACK):
On 14/11/11 17:09, mfojtik@redhat.com wrote:
> From: Michal Fojtik <mf...@redhat.com>
>
> +# 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::MachineConfiguration < CIMI::Model::Base
> +
> + scalar :memory
> + text :cpu
> +
> + array :disk do
> + scalar :capacity
also, :format and :attachment_point
> + end
> +
> + array :operations do
> + scalar :rel, :href
> + end
> +end
> diff --git a/server/spec/cimi/data/machine_configuration.json b/server/spec/cimi/data/machine_configuration.json
> new file mode 100644
> index 0000000..8864340
> --- /dev/null
> +++ b/server/spec/cimi/data/machine_configuration.json
> @@ -0,0 +1,17 @@
> +{
> + "uri": "http://cimi.example.org/machine_configurations/1",
> + "name": "MachineConfiguration1",
> + "description": "Example MachineConfiguration One",
> + "created": "2011-11-14",
> + "cpu": "2",
> + "memory" : { "quantity": "1", "units": "gigabyte" },
> + "disks" : [
> + { "capacity": { "quantity": "1", "units": "terabyte", "format": "ext4", "attachmentPoint": "/" } }
> + ],
I think this will be:
"disks" : [
{ "capacity": { "quantity": "1", "units": "terabyte"}},
{"format": "ext4"}, {"attachmentPoint": "/" }
> + ],
> + "properties": [ { "name": "architectore", "value" : "i386" } ],
> + "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_configuration.xml b/server/spec/cimi/data/machine_configuration.xml
> new file mode 100644
> index 0000000..788a185
> --- /dev/null
> +++ b/server/spec/cimi/data/machine_configuration.xml
> @@ -0,0 +1,14 @@
> +<MachineConfiguration xmlns="http://www.dmtf.org/cimi">
> + <uri>http://cimi.example.org/machine_configurations/1</uri>
> + <name>MachineConfiguration1</name>
> + <description>Example MachineConfiguration One</description>
> + <created>2011-11-14</created>
> + <property name="architecture">i386</property>
> + <cpu>2</cpu>
> + <memory quantity="1" units="gigabyte"/>
> + <disk>
> + <capacity quantity="1" units="terabyte" format="ext4" attachmentPoint="/"/>
> + </disk>
again, I think its:
<disk>
<capacity quantity="1" units="terabyte"/>
<format="ext4"/>
<attachmentPoint="/"/>
</disk>
I should raise a mantis issue since I can't see this in any of the
examples, even though :format and :attachmentPoint are mandatory.