deltacloud-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mar...@apache.org
Subject [2/11] git commit: CIMI: Adds VSP create and delete for CIMI server and mock driver
Date Wed, 11 Apr 2012 15:33:08 GMT
CIMI: Adds VSP create and delete for CIMI server and mock driver


Project: http://git-wip-us.apache.org/repos/asf/deltacloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltacloud/commit/3242e16b
Tree: http://git-wip-us.apache.org/repos/asf/deltacloud/tree/3242e16b
Diff: http://git-wip-us.apache.org/repos/asf/deltacloud/diff/3242e16b

Branch: refs/heads/master
Commit: 3242e16bf9049fa3c1fe83f7dee31602df428017
Parents: e318fdd
Author: marios <marios@redhat.com>
Authored: Fri Apr 6 12:23:37 2012 +0300
Committer: marios <marios@redhat.com>
Committed: Wed Apr 11 18:32:08 2012 +0300

----------------------------------------------------------------------
 server/lib/cimi/model/vsp.rb                       |   39 +++++++++++++++
 server/lib/cimi/server.rb                          |   24 +++++++++
 server/lib/deltacloud/drivers/mock/mock_client.rb  |    3 +-
 .../drivers/mock/mock_driver_cimi_methods.rb       |   28 ++++++++++
 4 files changed, 93 insertions(+), 1 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltacloud/blob/3242e16b/server/lib/cimi/model/vsp.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/model/vsp.rb b/server/lib/cimi/model/vsp.rb
index 33e5d5b..9507dd5 100644
--- a/server/lib/cimi/model/vsp.rb
+++ b/server/lib/cimi/model/vsp.rb
@@ -47,4 +47,43 @@ class CIMI::Model::VSP < CIMI::Model::Base
     end
   end
 
+  def self.create(request_body, context, type)
+    input = (type == :xml)? XmlSimple.xml_in(request_body, {"ForceArray"=>false, "NormaliseSpace"=>2})
: JSON.parse(request_body)
+    if input["vspTemplate"]["href"] #template by reference
+      vsp_config, network = get_by_reference(input, context)
+    else
+      if input["vspTemplate"]["vspConfig"]["href"] # configuration by reference
+        vsp_config = VSPConfiguration.find(context.href_id(input["vspTemplate"]["vspConfig"]["href"],:vsp_configurations),
context)
+      else #configuration by value
+        vsp_config = get_by_value(request_body, type)
+      end
+      network = Network.find(context.href_id(input["vspTemplate"]["network"]["href"], :networks),
context)
+    end
+    params = {:vsp_config => vsp_config, :network => network, :name=>input["name"],
:description=>input["description"], :env=>context}
+    raise CIMI::Model::BadRequest.new("Bad request - missing required parameters. Client
sent: #{request_body} which produced #{params.inspect}")  if params.has_value?(nil)
+    context.driver.create_vsp(context.credentials, params)
+  end
+
+  def self.delete!(id, context)
+    context.driver.delete_vsp(context.credentials, id)
+  end
+
+  private
+
+  def self.get_by_reference(input, context)
+    vsp_template = VSPTemplate.find(context.href_id(input["vspTemplate"]["href"], :vsp_templates),
context)
+    vsp_config = VSPConfiguration.find(context.href_id(vsp_template.vsp_config.href, :vsp_configurations),
context)
+    network = Network.find(context.href_id(vsp_template.network.href, :networks), context)
+    return vsp_config, network
+  end
+
+  def self.get_by_value(request_body, type)
+    if type == :xml
+      xml_arrays = XmlSimple.xml_in(request_body, {"NormaliseSpace"=>2})
+      vsp_config = VSPConfiguration.from_xml(XmlSimple.xml_out(xml_arrays["vspTemplate"][0]["vspConfig"][0]))
+    else
+     json = JSON.parse(request_body)
+      vsp_config = VSPConfiguration.from_json(JSON.generate(json["vspTemplate"]["vspConfig"]))
+    end
+  end
 end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/3242e16b/server/lib/cimi/server.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/server.rb b/server/lib/cimi/server.rb
index 2891df6..63523ba 100644
--- a/server/lib/cimi/server.rb
+++ b/server/lib/cimi/server.rb
@@ -750,6 +750,30 @@ global_collection :vsps do
     end
   end
 
+  operation :create do
+    description "Create a new VSP"
+    control do
+      if request.content_type.end_with?("json")
+        vsp = CIMI::Model::VSP.create(request.body.read, self, :json)
+      else
+        vsp = CIMI::Model::VSP.create(request.body.read, self, :xml)
+      end
+      respond_to do |format|
+        format.xml { vsp.to_xml }
+        format.json { vsp.to_json }
+      end
+    end
+  end
+
+  operation :destroy do
+    description "Delete a specified VSP"
+    param :id, :string, :required
+    control do
+      CIMI::Model::VSP.delete!(params[:id], self)
+      no_content_with_status(200)
+    end
+  end
+
 end
 
 global_collection :vsp_configurations do

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/3242e16b/server/lib/deltacloud/drivers/mock/mock_client.rb
----------------------------------------------------------------------
diff --git a/server/lib/deltacloud/drivers/mock/mock_client.rb b/server/lib/deltacloud/drivers/mock/mock_client.rb
index 3de8254..94c56af 100644
--- a/server/lib/deltacloud/drivers/mock/mock_client.rb
+++ b/server/lib/deltacloud/drivers/mock/mock_client.rb
@@ -98,7 +98,8 @@ module Deltacloud::Drivers::Mock
 
     def destroy_cimi(collection, id)
       fname = cimi_file(collection, id)
-      FileUtils.rm(fname) if File::exists?(fname)
+      raise "No such object: #{id} in #{collection} collection" unless File::exists?(fname)
+      FileUtils.rm(fname)
     end
 
     def load_all_cimi(model_name)

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/3242e16b/server/lib/deltacloud/drivers/mock/mock_driver_cimi_methods.rb
----------------------------------------------------------------------
diff --git a/server/lib/deltacloud/drivers/mock/mock_driver_cimi_methods.rb b/server/lib/deltacloud/drivers/mock/mock_driver_cimi_methods.rb
index 28b74a0..fdd501a 100644
--- a/server/lib/deltacloud/drivers/mock/mock_driver_cimi_methods.rb
+++ b/server/lib/deltacloud/drivers/mock/mock_driver_cimi_methods.rb
@@ -130,6 +130,33 @@ module Deltacloud::Drivers::Mock
       end
     end
 
+    def create_vsp(credentials, opts={})
+      check_credentials(credentials)
+      id = "#{opts[:env].send("vsps_url")}/#{opts[:name]}"
+      vsp_hash = { "id"    => id,
+                    "name"  => opts[:name],
+                    "description" => opts[:description],
+                    "state" => "STARTED",
+                    "created" => Time.now,
+                    "bandwidthReservation"=>opts[:vsp_config].bandwidth_reservation,
+                    "trafficPriority"=>opts[:vsp_config].traffic_priority,
+                    "maxTrafficDelay"=>opts[:vsp_config].max_traffic_delay,
+                    "maxTrafficLoss"=>opts[:vsp_config].max_traffic_loss,
+                    "maxTrafficJitter"=>opts[:vsp_config].max_traffic_jitter,
+                    "network" => {"href" => opts[:network].id},
+                    "operations" => [{"rel"=>"edit", "href"=> id},
+                                     {"rel"=>"delete", "href"=> id}]
+                   }
+      vsp = CIMI::Model::VSP.from_json(JSON.generate(vsp_hash))
+      @client.store_cimi(:vsp, vsp)
+      vsp
+    end
+
+    def delete_vsp(credentials, id)
+      check_credentials(credentials)
+      @client.destroy_cimi(:vsp, id)
+    end
+
     def vsp_configurations(credentials, opts={})
       check_credentials(credentials)
       if opts[:id].nil?
@@ -169,6 +196,7 @@ module Deltacloud::Drivers::Mock
       addr_hash = { "id"    => id,
                     "name"  => opts[:name],
                     "description" => opts[:description],
+                    "created" => Time.now,
                     "hostName" => opts[:address_template].hostname,
                     "allocation" => opts[:address_template].allocation,
                     "defaultGateway" => opts[:address_template].default_gateway,


Mime
View raw message