deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mar...@redhat.com
Subject [PATCH 4/5] Adds mocking of CIMI::Network to deltacloud mock driver/client
Date Fri, 13 Jan 2012 17:58:25 GMT
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)}
+    end
+
+    def load_cimi(model_name, id)
+        fname=cimi_file(model_name, id)
+        IO::read(fname)
+    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}"
+      network.uri=uri
+      network.operations.each{|op| op.href=uri}
+      network
+    end
+
     exceptions do
 
       on /AuthFailure/ do
-- 
1.7.6.5


Mime
View raw message