deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michal Fojtik <mfoj...@redhat.com>
Subject Re: [PATCH 4/5] Adds mocking of CIMI::Network to deltacloud mock driver/client
Date Mon, 16 Jan 2012 12:44:27 GMT
On Jan 13, 2012, at 6:58 PM, marios@redhat.com wrote:

ACK.

Minor nits inline.

  -- Michal


> From: marios <marios@redhat.com>
> 
> 
> Signed-off-by: marios <marios@redhat.com>
> ---
> server/lib/cimi/model/network.rb                  |    4 ++--
> server/lib/deltacloud/drivers/mock/mock_client.rb |   18 ++++++++++++++++++
> server/lib/deltacloud/drivers/mock/mock_driver.rb |   18 ++++++++++++++++++
> 3 files changed, 38 insertions(+), 2 deletions(-)
> 
> diff --git a/server/lib/cimi/model/network.rb b/server/lib/cimi/model/network.rb
> index d46f244..08fcd97 100644
> --- a/server/lib/cimi/model/network.rb
> +++ b/server/lib/cimi/model/network.rb
> @@ -44,9 +44,9 @@ class CIMI::Model::Network < CIMI::Model::Base
>   def self.find(id, context)
>     networks=[]
>     if id==:all
> -#FIXME
> +      networks = context.driver.networks(context.credentials, {:env=>context})
>     else
> -#FIXME
> +      networks = context.driver.networks(context.credentials, {:id=>id, :env=>context})
>     end
>     networks
>   end
> diff --git a/server/lib/deltacloud/drivers/mock/mock_client.rb b/server/lib/deltacloud/drivers/mock/mock_client.rb
> index 96157cd..325caab 100644
> --- a/server/lib/deltacloud/drivers/mock/mock_client.rb
> +++ b/server/lib/deltacloud/drivers/mock/mock_client.rb
> @@ -91,6 +91,24 @@ module Deltacloud::Drivers::Mock
>       FileUtils.rm(fname) if File::exists?(fname)
>     end
> 
> +    def load_all_cimi(model_name)
> +        model_files = Dir[File::join(cimi_dir(model_name), "*.json")]
> +        model_files.map{|f| IO.read(f)}

You can also use: File.read(f). I think it would be better to return an
'File' exception when reading fails than IO error :)

> +    end
> +
> +    def load_cimi(model_name, id)
> +        fname=cimi_file(model_name, id)
> +        IO::read(fname)

I would rather use:

IO::read(cimi_file(model_name, id))

to avoid creating a temp variable here (like bellow).

> +    end
> +
> +    def cimi_file(collection, id)
> +      File::join(cimi_dir(collection), "#{id}.json")
> +    end
> +
> +    def cimi_dir(collection)
> +      File::join(@storage_root, "cimi", collection.to_s)
> +    end
> +
>     private
> 
>     def collection_name(klass)
> diff --git a/server/lib/deltacloud/drivers/mock/mock_driver.rb b/server/lib/deltacloud/drivers/mock/mock_driver.rb
> index bee75a0..acabee4 100644
> --- a/server/lib/deltacloud/drivers/mock/mock_driver.rb
> +++ b/server/lib/deltacloud/drivers/mock/mock_driver.rb
> @@ -441,6 +441,17 @@ module Deltacloud::Drivers::Mock
>       return false
>     end
> 
> +    def networks(credentials, opts={})
> +      check_credentials(credentials)
> +      if opts[:id].nil?
> +        networks = @client.load_all_cimi(:network).map{|net| CIMI::Model::Network.from_json(net)}
> +        networks.map{|net|convert_cimi_network(net,opts[:env])}.flatten
> +      else
> +        network = CIMI::Model::Network.from_json(@client.load_cimi(:network, opts[:id]))
> +        convert_cimi_network(network, opts[:env])
> +      end
> +    end
> +
>     private
> 
>     def check_credentials(credentials)
> @@ -477,6 +488,13 @@ module Deltacloud::Drivers::Mock
>       StorageVolume.new(volume)
>     end
> 
> +    def convert_cimi_network(network, context)
> +      uri="#{context.env["rack.url_scheme"]}://#{context.env["HTTP_HOST"]}#{settings.root_url}/networks/#{network.name}"

If there is a global_collection for :networks then we should have a helper:

network_url(network.name)

I think we also have the :absolute option in url_for, if not we should consider to add it
there.

> +      network.uri=uri
> +      network.operations.each{|op| op.href=uri}
> +      network
> +    end
> +
>     exceptions do
> 
>       on /AuthFailure/ do
> -- 
> 1.7.6.5
> 

------------------------------------------------------
Michal Fojtik, mfojtik@redhat.com
Deltacloud API: http://deltacloud.org


Mime
View raw message