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/23 17:23:20 UTC

CIMI: Initial import of Mock driver for CIMI frontend

Hi,

This is just first draft for the Mock driver with CIMI frontend.
The Mock driver will currently only respond to:

curl -H 'Accept:application/xml' \
  "http://localhost:3001/cimi/machine_configurations/m1-small"

if you start the server using:

./bin/deltacloudd --cimi -i mock

You should get both XML and JSON output back in browser.
Remember this is just a draft how the driver code could looks like.

 -- Michal


Re: [PATCH core 5/5] CIMI: Added ability to convert to JSON/XML when certain properties are not set

Posted by David Lutterkort <lu...@redhat.com>.
On Wed, 2011-11-23 at 17:23 +0100, mfojtik@redhat.com wrote:
> From: Michal Fojtik <mf...@redhat.com>
> 
> 
> Signed-off-by: Michal fojtik <mf...@redhat.com>
> ---
>  server/lib/cimi/model/schema.rb |    9 +++++----
>  1 files changed, 5 insertions(+), 4 deletions(-)

ACK.

David



[PATCH core 5/5] CIMI: Added ability to convert to JSON/XML when certain properties are not set

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


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

diff --git a/server/lib/cimi/model/schema.rb b/server/lib/cimi/model/schema.rb
index 46d537b..b6237e3 100644
--- a/server/lib/cimi/model/schema.rb
+++ b/server/lib/cimi/model/schema.rb
@@ -68,6 +68,7 @@ class CIMI::Model::Schema
     end
 
     def to_xml(model, xml)
+      return unless model
       return unless model[@name]
       case @text
         when :nested then xml[@xml_name] = [{ "content" => model[@name] }]
@@ -160,12 +161,12 @@ class CIMI::Model::Schema
     end
 
     def to_xml(model, xml)
-      ary = model[name].map { |elt| @struct.convert_to_xml(elt) }
+      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) }
+      ary = (model[name] || []).map { |elt| @struct.convert_to_json(elt) }
       json[json_name] = ary unless ary.empty?
     end
   end
@@ -189,12 +190,12 @@ class CIMI::Model::Schema
     end
 
     def to_xml(model, xml)
-      ary = model[name].map { |elt| @struct.convert_to_xml(elt) }
+      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) }
+      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
-- 
1.7.4.4


Re: [PATCH core 3/5] Core: Make 'features' optional for driver

Posted by David Lutterkort <lu...@redhat.com>.
On Wed, 2011-11-23 at 17:23 +0100, mfojtik@redhat.com wrote:
> From: Michal Fojtik <mf...@redhat.com>
> 
> 
> Signed-off-by: Michal fojtik <mf...@redhat.com>
> ---
>  server/lib/sinatra/rabbit.rb |    4 ++--
>  1 files changed, 2 insertions(+), 2 deletions(-)

ACK; though wouldn't it be simpler to have a base class for
CIMI::Drivers with a features method that always returns [] ?

David



[PATCH core 3/5] Core: Make 'features' optional for driver

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


Signed-off-by: Michal fojtik <mf...@redhat.com>
---
 server/lib/sinatra/rabbit.rb |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/server/lib/sinatra/rabbit.rb b/server/lib/sinatra/rabbit.rb
index 5c63fd9..21f0645 100644
--- a/server/lib/sinatra/rabbit.rb
+++ b/server/lib/sinatra/rabbit.rb
@@ -168,9 +168,9 @@ module Sinatra
       # operation plus the params defined by any features in the +driver+
       # that might modify this operation
       def effective_params(driver)
-        driver.features(@collection.name).collect do |f|
+        (driver.respond_to?(:features) ? driver.features(@collection.name).collect do |f|
           f.decl.operation(@name)
-        end.flatten.select { |op| op }.inject(params.dup) do |result, fop|
+        end : []).flatten.select { |op| op }.inject(params.dup) do |result, fop|
           fop.params.each_key do |k|
             if result.has_key?(k)
               raise DuplicateParamException, "Parameter '#{k}' for operation #{fop.name} in collection #{@collection.name}"
-- 
1.7.4.4


Re: [PATCH core 4/5] CIMI: Implemented :show operation for :machine_configuration model in Rabbit

Posted by David Lutterkort <lu...@redhat.com>.
On Wed, 2011-11-23 at 17:23 +0100, mfojtik@redhat.com wrote:
> From: Michal Fojtik <mf...@redhat.com>
> 
> 
> Signed-off-by: Michal fojtik <mf...@redhat.com>
> ---
>  server/lib/cimi/server.rb |   15 +++++++++++----
>  1 files changed, 11 insertions(+), 4 deletions(-)

ACK



[PATCH core 4/5] CIMI: Implemented :show operation for :machine_configuration model in Rabbit

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


Signed-off-by: Michal fojtik <mf...@redhat.com>
---
 server/lib/cimi/server.rb |   15 +++++++++++----
 1 files changed, 11 insertions(+), 4 deletions(-)

diff --git a/server/lib/cimi/server.rb b/server/lib/cimi/server.rb
index 39f72f5..5767d1b 100644
--- a/server/lib/cimi/server.rb
+++ b/server/lib/cimi/server.rb
@@ -16,10 +16,13 @@
 
 require 'cimi/helpers/dmtfdep'
 require 'cimi/helpers/cmwgapp_helper'
+require 'cimi/drivers/drivers'
+require 'deltacloud/core_ext'
+require 'cimi/model'
 
 set :version, '0.1.0'
 
-include Deltacloud::Drivers
+include CIMI::Drivers
 set :drivers, Proc.new { driver_config }
 
 STOREROOT = File.join($top_srcdir, 'lib', 'cimi', 'data')
@@ -108,9 +111,12 @@ global_collection :machine_configurations do
   end
 
   operation :show do
-    description "Show specific machine configuration."
-    with_capability :hardware_profile
-    param :id,          :string,    :required
+
+    description "The Machine Configuration entity represents the set of configuration values "+
+      "that define the (virtual) hardware resources of a to-be-realized Machine Instance.."
+
+    param :id, :string, :required
+
     control do
       machine_conf = driver.machine_configuration(credentials, :id => params[:id])
       machine_conf.uri = machine_configuration_url(params[:id])
@@ -119,6 +125,7 @@ global_collection :machine_configurations do
         format.json { machine_conf.to_json }
       end
     end
+
   end
 end
 
-- 
1.7.4.4


Re: [PATCH core 1/5] CIMI: Added 'should_properly_serialize_model' helper to RSpec tests

Posted by "marios@redhat.com" <ma...@redhat.com>.
ACK, pushed

On 24/11/11 02:34, David Lutterkort wrote:
> This fails in two places: (1) ruby-debug19 is not found (it shouldn't
> look for it since I am on mri_18, but who knows); is there a good reason
> why we even mention ruby-debug in the Gemfile ? FWIW, you can do
> development perfectly fine without a debugger installed. (2) no rspec ~>
> 2.0.0; I have 2.7.0. Is there a good reason why we can't just require
> rspec >= 2.0.0 ?
> 
> TL;DR: can we commit this patch (and whoever ACK's this should just
> commit it):
> 
> diff --git a/server/Gemfile b/server/Gemfile
> index f453f4a..b3180e3 100644
> --- a/server/Gemfile
> +++ b/server/Gemfile
> @@ -3,8 +3,6 @@ source "http://rubygems.org"
>  gemspec
>  
>  group :development do
> -  gem 'ruby-debug', :platforms => :mri_18
> -  gem 'ruby-debug19', :platforms => :mri_19, :require => 'ruby-debug'
>    gem "compass", ">= 0.8.17"
>    gem "vcr"
>    gem "webmock"
> @@ -12,6 +10,6 @@ group :development do
>    gem "ci_reporter"
>    gem "cucumber", ">= 0.6.3"
>    gem "rcov", ">= 0.9.8"
> -  gem "rspec", "~> 2.0.0"
> +  gem "rspec", ">= 2.0.0"
>  end
> 
> David
> 
> 


Re: [PATCH core 1/5] CIMI: Added 'should_properly_serialize_model' helper to RSpec tests

Posted by David Lutterkort <lu...@redhat.com>.
On Wed, 2011-11-23 at 17:23 +0100, mfojtik@redhat.com wrote:
> From: Michal Fojtik <mf...@redhat.com>
> 
> 
> Signed-off-by: Michal fojtik <mf...@redhat.com>
> ---
>  server/spec/cimi/model/machine_admin_spec.rb       |   14 ++------------
>  .../spec/cimi/model/machine_configuration_spec.rb  |   12 ++----------
>  server/spec/cimi/model/machine_image_spec.rb       |   11 ++---------
>  server/spec/cimi/model/machine_spec.rb             |   17 ++---------------
>  server/spec/cimi/model/machine_template_spec.rb    |   12 ++----------
>  server/spec/cimi/model/volume_spec.rb              |   12 ++----------
>  server/spec/spec_helper.rb                         |   15 +++++++--------
>  7 files changed, 19 insertions(+), 74 deletions(-)

This gives me a bunch of spec failures since should_serialize_from_json!
and should_serialize_from_xml! are still used in some specs.

ACK after updating those specs.

Incidentally, on my Fedora 15 machine, I also run into trouble with the
Gemfile:

        source "http://rubygems.org"
        
        gemspec
        
        group :development do
          gem 'ruby-debug', :platforms => :mri_18
          gem 'ruby-debug19', :platforms => :mri_19, :require => 'ruby-debug'
          gem "compass", ">= 0.8.17"
          gem "vcr"
          gem "webmock"
          gem "rack-test", ">= 0.5.3"
          gem "ci_reporter"
          gem "cucumber", ">= 0.6.3"
          gem "rcov", ">= 0.9.8"
          gem "rspec", "~> 2.0.0"
        end

This fails in two places: (1) ruby-debug19 is not found (it shouldn't
look for it since I am on mri_18, but who knows); is there a good reason
why we even mention ruby-debug in the Gemfile ? FWIW, you can do
development perfectly fine without a debugger installed. (2) no rspec ~>
2.0.0; I have 2.7.0. Is there a good reason why we can't just require
rspec >= 2.0.0 ?

TL;DR: can we commit this patch (and whoever ACK's this should just
commit it):

diff --git a/server/Gemfile b/server/Gemfile
index f453f4a..b3180e3 100644
--- a/server/Gemfile
+++ b/server/Gemfile
@@ -3,8 +3,6 @@ source "http://rubygems.org"
 gemspec
 
 group :development do
-  gem 'ruby-debug', :platforms => :mri_18
-  gem 'ruby-debug19', :platforms => :mri_19, :require => 'ruby-debug'
   gem "compass", ">= 0.8.17"
   gem "vcr"
   gem "webmock"
@@ -12,6 +10,6 @@ group :development do
   gem "ci_reporter"
   gem "cucumber", ">= 0.6.3"
   gem "rcov", ">= 0.9.8"
-  gem "rspec", "~> 2.0.0"
+  gem "rspec", ">= 2.0.0"
 end

David



[PATCH core 1/5] CIMI: Added 'should_properly_serialize_model' helper to RSpec tests

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


Signed-off-by: Michal fojtik <mf...@redhat.com>
---
 server/spec/cimi/model/machine_admin_spec.rb       |   14 ++------------
 .../spec/cimi/model/machine_configuration_spec.rb  |   12 ++----------
 server/spec/cimi/model/machine_image_spec.rb       |   11 ++---------
 server/spec/cimi/model/machine_spec.rb             |   17 ++---------------
 server/spec/cimi/model/machine_template_spec.rb    |   12 ++----------
 server/spec/cimi/model/volume_spec.rb              |   12 ++----------
 server/spec/spec_helper.rb                         |   15 +++++++--------
 7 files changed, 19 insertions(+), 74 deletions(-)

diff --git a/server/spec/cimi/model/machine_admin_spec.rb b/server/spec/cimi/model/machine_admin_spec.rb
index e9cabc4..036a66d 100644
--- a/server/spec/cimi/model/machine_admin_spec.rb
+++ b/server/spec/cimi/model/machine_admin_spec.rb
@@ -21,18 +21,8 @@ describe "MachineAdmin model" do
     @json = IO::read(File::join(DATA_DIR, "machine_admin.json"))
   end
 
-  it "can be constructed from XML" do
-    conf = CIMI::Model::MachineAdmin.from_xml(@xml)
-    conf.should_not be_nil
-    should_serialize_from_xml! conf, @xml, @json
+  it "can be constructed from XML and JSON" do
+    should_properly_serialize_model CIMI::Model::MachineAdmin, @xml, @json
   end
 
-  it "can be constructed from JSON" do
-    conf = CIMI::Model::MachineAdmin.from_json(@json)
-    conf.should_not be_nil
-    should_serialize_from_json! conf, @xml, @json
-  end
-
-
-
 end
diff --git a/server/spec/cimi/model/machine_configuration_spec.rb b/server/spec/cimi/model/machine_configuration_spec.rb
index 454e8c4..2a57269 100644
--- a/server/spec/cimi/model/machine_configuration_spec.rb
+++ b/server/spec/cimi/model/machine_configuration_spec.rb
@@ -20,16 +20,8 @@ describe "MachineConfiguration model" do
     @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
-    should_serialize_from_xml! conf, @xml, @json
-  end
-
-  it "can be constructed from JSON" do
-    conf = CIMI::Model::MachineConfiguration.from_json(@json)
-    conf.should_not be_nil
-    should_serialize_from_json! conf, @xml, @json
+  it "can be constructed from XML and JSON" do
+    should_properly_serialize_model CIMI::Model::MachineConfiguration, @xml, @json
   end
 
 end
diff --git a/server/spec/cimi/model/machine_image_spec.rb b/server/spec/cimi/model/machine_image_spec.rb
index d4af9af..f284952 100644
--- a/server/spec/cimi/model/machine_image_spec.rb
+++ b/server/spec/cimi/model/machine_image_spec.rb
@@ -21,16 +21,9 @@ describe "MachineImage model" do
     @json = IO::read(File::join(DATA_DIR, "machine_image.json"))
   end
 
-  it "can be constructed from XML" do
-    img = CIMI::Model::MachineImage.from_xml(@xml)
-    img.should_not be_nil
-    should_serialize_from_xml! img, @xml, @json
+  it "can be constructed from XML and JSON" do
+    should_properly_serialize_model CIMI::Model::MachineImage, @xml, @json
   end
 
-  it "can be constructed from JSON" do
-    img = CIMI::Model::MachineImage.from_json(@json)
-    img.should_not be_nil
-    should_serialize_from_json! img, @xml, @json
-  end
 
 end
diff --git a/server/spec/cimi/model/machine_spec.rb b/server/spec/cimi/model/machine_spec.rb
index f1ee296..59b5d6f 100644
--- a/server/spec/cimi/model/machine_spec.rb
+++ b/server/spec/cimi/model/machine_spec.rb
@@ -20,21 +20,8 @@ describe "Machine model" do
     @json = IO::read(File::join(DATA_DIR, "machine.json"))
   end
 
-  it "should hold just machine schema" do
-    CIMI::Model::Machine.schema.attribute_names.should_not include(:image_location)
-    CIMI::Model::Machine.schema.attribute_names.should include(:meters)
-  end
-
-  it "can be constructed from XML" do
-    machine = CIMI::Model::Machine.from_xml(@xml)
-    machine.should_not be_nil
-    should_serialize_from_xml! machine, @xml, @json
-  end
-
-  it "can be constructed from JSON" do
-    machine = CIMI::Model::Machine.from_json(@json)
-    machine.should_not be_nil
-    should_serialize_from_json! machine, @xml, @json
+  it "can be constructed from XML and JSON" do
+    should_properly_serialize_model CIMI::Model::Machine, @xml, @json
   end
 
 end
diff --git a/server/spec/cimi/model/machine_template_spec.rb b/server/spec/cimi/model/machine_template_spec.rb
index 0c6f50a..3ca2c9a 100644
--- a/server/spec/cimi/model/machine_template_spec.rb
+++ b/server/spec/cimi/model/machine_template_spec.rb
@@ -20,16 +20,8 @@ describe "MachineTemplate model" do
     @json = IO::read(File::join(DATA_DIR, "machine_template.json"))
   end
 
-  it "can be constructed from XML" do
-    conf = CIMI::Model::MachineTemplate.from_xml(@xml)
-    conf.should_not be_nil
-    should_serialize_from_xml! conf, @xml, @json
-  end
-
-  it "can be constructed from JSON" do
-    conf = CIMI::Model::MachineTemplate.from_json(@json)
-    conf.should_not be_nil
-    should_serialize_from_json! conf, @xml, @json
+  it "can be constructed from XML and JSON" do
+    should_properly_serialize_model CIMI::Model::MachineTemplate, @xml, @json
   end
 
 end
diff --git a/server/spec/cimi/model/volume_spec.rb b/server/spec/cimi/model/volume_spec.rb
index 829dc57..d208e15 100644
--- a/server/spec/cimi/model/volume_spec.rb
+++ b/server/spec/cimi/model/volume_spec.rb
@@ -21,16 +21,8 @@ describe "Volume model" do
     @json = IO::read(File::join(DATA_DIR, "volume.json"))
   end
 
-  it "can be constructed from XML" do
-    conf = CIMI::Model::Volume.from_xml(@xml)
-    conf.should_not be_nil
-    should_serialize_from_xml! conf, @xml, @json
-  end
-
-  it "can be constructed from JSON" do
-    conf = CIMI::Model::Volume.from_json(@json)
-    conf.should_not be_nil
-    should_serialize_from_json! conf, @xml, @json
+  it "can be constructed from XML and JSON" do
+    should_properly_serialize_model CIMI::Model::Volume, @xml, @json
   end
 
 end
diff --git a/server/spec/spec_helper.rb b/server/spec/spec_helper.rb
index c8b41fc..69d4797 100644
--- a/server/spec/spec_helper.rb
+++ b/server/spec/spec_helper.rb
@@ -90,14 +90,13 @@ class HashCmp
   end
 end
 
-def should_serialize_from_xml!(model, xml, json)
-  model.to_xml.should serialize_to xml, :fmt => :xml
-  model.to_json.should serialize_to json, :fmt => :json
-end
-
-def should_serialize_from_json!(model, xml, json)
-  model.to_xml.should serialize_to xml, :fmt => :xml
-  model.to_json.should serialize_to json, :fmt => :json
+def should_properly_serialize_model(model_class, xml, json)
+  # Roundtrip in same format
+  model_class.from_xml(xml).should serialize_to xml, :fmt => :xml
+  model_class.from_json(json).should serialize_to json, :fmt => :json
+  # Roundtrip crossing format
+  model_class.from_xml(xml).should serialize_to json, :fmt => :json
+  model_class.from_json(json).should serialize_to xml, :fmt => :xml
 end
 
 RSpec::Matchers.define :serialize_to do |exp, opts|
-- 
1.7.4.4


Re: [PATCH core 2/5] CIMI: Initial import of the Mock driver

Posted by David Lutterkort <lu...@redhat.com>.
Forgot something in my last reply:

> diff --git a/server/lib/cimi/drivers/mock/mock_driver.rb b/server/lib/cimi/drivers/mock/mock_driver.rb
> new file mode 100644
> index 0000000..bcb1e2c
> --- /dev/null
> +++ b/server/lib/cimi/drivers/mock/mock_driver.rb

> +class CIMI::Drivers::Mock
> +  include Deltacloud::Drivers::Mock
> +  include CIMI::Model
> +
> +  def initialize
> +    @deltacloud_driver = MockDriver.new
> +  end

At first, I thought CIMI drives should subclass Deltacloud drivers. But
seeing this, I actually like the idea of them wrapping the Deltacloud
drives - that makes it much clearer where CIMI uses Deltacloud
functionality.

David



Re: [PATCH core 2/5] CIMI: Initial import of the Mock driver

Posted by David Lutterkort <lu...@redhat.com>.
On Wed, 2011-11-23 at 17:23 +0100, mfojtik@redhat.com wrote:
> From: Michal Fojtik <mf...@redhat.com>
> 
> 
> Signed-off-by: Michal fojtik <mf...@redhat.com>
> ---
>  server/lib/cimi/drivers/drivers.rb          |   55 +++++++++++++++++++++++++++
>  server/lib/cimi/drivers/mock/mock_driver.rb |   53 ++++++++++++++++++++++++++
>  server/lib/cimi/server.rb                   |   18 ++------
>  3 files changed, 113 insertions(+), 13 deletions(-)
>  create mode 100644 server/lib/cimi/drivers/drivers.rb
>  create mode 100644 server/lib/cimi/drivers/mock/mock_driver.rb

ACK; some comments:

> diff --git a/server/lib/cimi/drivers/drivers.rb b/server/lib/cimi/drivers/drivers.rb
> new file mode 100644
> index 0000000..4d1dd2e
> --- /dev/null
> +++ b/server/lib/cimi/drivers/drivers.rb

Short of copying this file wholesale, is their something more clever we
can do ? How about we start a new lib/common directory, which also has a
drivers.rb containing everything from the current
lib/deltacloud/drivers.rb except for driver_class and
driver_source_name, and make lib/cimi/drivers.rb

        module CIMI::Drivers
          include Common::Drivers
        
          def driver_class ... same as in current drivers.rb ... end
        
          def driver_source_name ... same as in current drivers.rb ...
        end
        end

and similar for Deltacloud::Drivers. Looking at how these two methods
differ between CIMI and Deltacloud, this could be refactored further, so
that CIMI::Drives and Deltacloud::Drivers only add two methods, one
returning "cimi"/"deltacloud", the other returning
CIMI::Drivers/Deltacloud::Drivers (could that be replaced by self ?)

> diff --git a/server/lib/cimi/drivers/mock/mock_driver.rb b/server/lib/cimi/drivers/mock/mock_driver.rb
> new file mode 100644
> index 0000000..bcb1e2c
> --- /dev/null
> +++ b/server/lib/cimi/drivers/mock/mock_driver.rb
> @@ -0,0 +1,53 @@

> +  private
> +
> +  def convert_machine_description(hwp)
> +    MachineConfiguration.new(
> +      :name => hwp.name,
> +      :description => machine_conf_desc(hwp), 
> +      :cpu => hwp.cpu.value,
> +      :created => Time.now.to_s,
> +      :memory => { :quantity => hwp.memory.value, :units => hwp.memory.unit },
> +      :disks => [ { :capacity => { :quantity => hwp.storage.value, :units => hwp.storage.unit } } ]
> +    )
> +  end

This would be nicer as a class method on MachineConfiguration, say
MachineConfiguration.from_hwp or MachineConfiguration.convert_hwp
(and I wonder if we should alias *Configuration classes to *Config
classes)

> diff --git a/server/lib/cimi/server.rb b/server/lib/cimi/server.rb
> index ba925fa..39f72f5 100644
> --- a/server/lib/cimi/server.rb
> +++ b/server/lib/cimi/server.rb
> @@ -112,19 +112,11 @@ global_collection :machine_configurations do
>      with_capability :hardware_profile
>      param :id,          :string,    :required
>      control do
> -      @profile =  driver.hardware_profile(credentials, params[:id])
> -      if @profile
> -        #setup the default values for a machine configuration
> -        resource_default = get_resource_default "machine_configuration"
> -        #get the actual values from profile
> -        resource_value = { "name" => @profile.name,"uri" => @profile.name,
> -              "href" => machine_configuration_url(@profile.name) }
> -        #mixin actual values get from profile
> -        @dmtfitem = resource_default["dmtfitem"].merge resource_value
> -        show_resource "machine_configurations/show", "MachineConfiguration",
> -          {"property" => "properties", "disk" => "disks", "operation" => "operations"}
> -      else
> -        report_error(404)
> +      machine_conf = driver.machine_configuration(credentials, :id => params[:id])
> +      machine_conf.uri = machine_configuration_url(params[:id])

Just to sound this out in my head: if we made this a method on MC, the
code would look like

                machine_conf = MachineConfiguration.find(params[:id], self)

with

                class MachineConfiguration
                    ...
                    def self.find(id, ctx)
                      hwps = nil
                      if id == :all
                        hwps = ctx.driver.hardware_profiles(ctx.credentials)
                      else
                        hwps = ctx.driver.hardware_profile(ctx.credentials, id)
                      end
                      confs = hwps.map { |hwp| self.convert_hwp(hwp, ctx) }
                      id == :all ? confs : confs.first
                    end
                
                    private
                    def self.convert_hwp(hwp, ctx)
                      new(:name => hwp.name , :uri => ctx.machine_configuration_url(hwp.id), ...)
                    end
                    ...
                end
                
With that, pretty much all the methods in the current
CIMI::Drivers::MockDriver would just become methods in
MachineConfiguration (it would need a hardware_profiles method that
delegates to Deltacloud::Drivers::MockDriver).

I am not against having a CIMI::MockDriver, but I would like to try to
keep logic that is not driver specific in the model classes.
Essentially, the drivers are solely our internal cloud abstraction
layer.

David



[PATCH core 2/5] CIMI: Initial import of the Mock driver

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


Signed-off-by: Michal fojtik <mf...@redhat.com>
---
 server/lib/cimi/drivers/drivers.rb          |   55 +++++++++++++++++++++++++++
 server/lib/cimi/drivers/mock/mock_driver.rb |   53 ++++++++++++++++++++++++++
 server/lib/cimi/server.rb                   |   18 ++------
 3 files changed, 113 insertions(+), 13 deletions(-)
 create mode 100644 server/lib/cimi/drivers/drivers.rb
 create mode 100644 server/lib/cimi/drivers/mock/mock_driver.rb

diff --git a/server/lib/cimi/drivers/drivers.rb b/server/lib/cimi/drivers/drivers.rb
new file mode 100644
index 0000000..4d1dd2e
--- /dev/null
+++ b/server/lib/cimi/drivers/drivers.rb
@@ -0,0 +1,55 @@
+# 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.
+
+module CIMI
+
+  module Drivers
+
+    DRIVER=ENV['API_DRIVER'] ? ENV['API_DRIVER'].to_sym : :mock
+
+    def driver_config
+      if Thread::current[:drivers].nil?
+        Thread::current[:drivers] = {}
+        top_srcdir = File.join(File.dirname(__FILE__), '..', '..', '..')
+        Dir[File.join(top_srcdir, 'config', 'drivers', '*.yaml')].each do |driver_file|
+          Thread::current[:drivers].merge!(YAML::load(File::read(driver_file)))
+        end
+      end
+      Thread::current[:drivers]
+    end
+
+    def driver_name
+      driver_config[:"#{driver_symbol}"][:name]
+    end
+
+    def driver_symbol
+      (Thread.current[:driver] || DRIVER).to_sym
+    end
+
+    def driver_class
+      basename = driver_config[:"#{driver_symbol}"][:class] || "#{driver_name}"
+      CIMI::Drivers.const_get(driver_name).const_get(basename)
+    end
+
+    def driver_source_name
+      File.join("cimi", "drivers", "#{driver_symbol}", "#{driver_symbol}_driver.rb")
+    end
+
+    def driver
+      require driver_source_name
+      @driver ||= driver_class.new
+    end
+  end
+end
diff --git a/server/lib/cimi/drivers/mock/mock_driver.rb b/server/lib/cimi/drivers/mock/mock_driver.rb
new file mode 100644
index 0000000..bcb1e2c
--- /dev/null
+++ b/server/lib/cimi/drivers/mock/mock_driver.rb
@@ -0,0 +1,53 @@
+# 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.
+
+require 'deltacloud/drivers/mock/mock_driver'
+
+class CIMI::Drivers::Mock
+  include Deltacloud::Drivers::Mock
+  include CIMI::Model
+
+  def initialize
+    @deltacloud_driver = MockDriver.new
+  end
+
+  def machine_configuration(credentials, opts={})
+    hwp = @deltacloud_driver.hardware_profile(credentials, opts[:id])
+    convert_machine_description(hwp)
+  end
+
+  def machine_configurations(credentials, opts={})
+    hwp_list = @deltacloud_driver.hardware_profiles(credentials)
+    hwp_list.map { |hwp| convert_machine_description(hwp) }
+  end
+
+  private
+
+  def convert_machine_description(hwp)
+    MachineConfiguration.new(
+      :name => hwp.name,
+      :description => machine_conf_desc(hwp), 
+      :cpu => hwp.cpu.value,
+      :created => Time.now.to_s,
+      :memory => { :quantity => hwp.memory.value, :units => hwp.memory.unit },
+      :disks => [ { :capacity => { :quantity => hwp.storage.value, :units => hwp.storage.unit } } ]
+    )
+  end
+
+  def machine_conf_desc(hwp)
+    "Machine Configuration with #{hwp.memory.value} #{hwp.memory.unit} of memory and #{hwp.cpu.value} CPU"
+  end
+
+end
diff --git a/server/lib/cimi/server.rb b/server/lib/cimi/server.rb
index ba925fa..39f72f5 100644
--- a/server/lib/cimi/server.rb
+++ b/server/lib/cimi/server.rb
@@ -112,19 +112,11 @@ global_collection :machine_configurations do
     with_capability :hardware_profile
     param :id,          :string,    :required
     control do
-      @profile =  driver.hardware_profile(credentials, params[:id])
-      if @profile
-        #setup the default values for a machine configuration
-        resource_default = get_resource_default "machine_configuration"
-        #get the actual values from profile
-        resource_value = { "name" => @profile.name,"uri" => @profile.name,
-              "href" => machine_configuration_url(@profile.name) }
-        #mixin actual values get from profile
-        @dmtfitem = resource_default["dmtfitem"].merge resource_value
-        show_resource "machine_configurations/show", "MachineConfiguration",
-          {"property" => "properties", "disk" => "disks", "operation" => "operations"}
-      else
-        report_error(404)
+      machine_conf = driver.machine_configuration(credentials, :id => params[:id])
+      machine_conf.uri = machine_configuration_url(params[:id])
+      respond_to do |format|
+        format.xml { machine_conf.to_xml }
+        format.json { machine_conf.to_json }
       end
     end
   end
-- 
1.7.4.4