deltacloud-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mar...@apache.org
Subject [11/11] git commit: CIMI:: Adds create and destroy for CIMI::Address entity (+ deltacloud mock implementation)
Date Wed, 11 Apr 2012 15:33:08 GMT
CIMI:: Adds create and destroy for CIMI::Address entity (+ deltacloud mock implementation)


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

Branch: refs/heads/master
Commit: e318fdd6b1b901aa1ea9e47783ec5518a7c81cc1
Parents: 1e76228
Author: marios <marios@redhat.com>
Authored: Thu Apr 5 19:27:30 2012 +0300
Committer: marios <marios@redhat.com>
Committed: Wed Apr 11 18:32:07 2012 +0300

----------------------------------------------------------------------
 server/lib/cimi/model/address.rb                   |   16 +++++++++
 server/lib/cimi/server.rb                          |   24 +++++++++++++
 .../drivers/mock/mock_driver_cimi_methods.rb       |   27 +++++++++++++++
 3 files changed, 67 insertions(+), 0 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltacloud/blob/e318fdd6/server/lib/cimi/model/address.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/model/address.rb b/server/lib/cimi/model/address.rb
index c16b0c6..8838331 100644
--- a/server/lib/cimi/model/address.rb
+++ b/server/lib/cimi/model/address.rb
@@ -48,9 +48,25 @@ class CIMI::Model::Address < CIMI::Model::Base
   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["addressTemplate"]["href"] #by reference
+      address_template = AddressTemplate.find(context.href_id(input["addressTemplate"]["href"],
:address_templates), context)
+    else
+      case type
+        when :json
+          address_template = AddressTemplate.from_json(JSON.generate(input["addressTemplate"]))
+        when :xml
+          xml = XmlSimple.xml_in(request_body, {"NormaliseSpace"=>2})
+          address_template = AddressTemplate.from_xml(XmlSimple.xml_out(xml["addressTemplate"][0]))
+      end
+    end
+    params = {:name=>input["name"], :description=>input["description"], :address_template=>address_template,
: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_address(context.credentials, params)
   end
 
   def self.delete!(id, context)
+    context.driver.delete_address(context.credentials, id)
   end
 
 end

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

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/e318fdd6/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 781be1e..28b74a0 100644
--- a/server/lib/deltacloud/drivers/mock/mock_driver_cimi_methods.rb
+++ b/server/lib/deltacloud/drivers/mock/mock_driver_cimi_methods.rb
@@ -163,6 +163,33 @@ module Deltacloud::Drivers::Mock
       end
     end
 
+    def create_address(credentials, opts={})
+      check_credentials(credentials)
+      id = "#{opts[:env].send("addresses_url")}/#{opts[:name]}"
+      addr_hash = { "id"    => id,
+                    "name"  => opts[:name],
+                    "description" => opts[:description],
+                    "hostName" => opts[:address_template].hostname,
+                    "allocation" => opts[:address_template].allocation,
+                    "defaultGateway" => opts[:address_template].default_gateway,
+                    "dns" => opts[:address_template].dns,
+                    "macAddress" => opts[:address_template].mac_address,
+                    "protocol" => opts[:address_template].protocol,
+                    "mask" => opts[:address_template].mask,
+                    "network" => {"href" => opts[:address_template].network.href},
+                    "operations" => [{"rel"=>"edit", "href"=> id},
+                                     {"rel"=>"delete", "href"=> id}]
+                   }
+      address = CIMI::Model::Address.from_json(JSON.generate(addr_hash))
+      @client.store_cimi(:address, address)
+      address
+    end
+
+    def delete_address(credentials, id)
+      check_credentials(credentials)
+      @client.destroy_cimi(:address, id)
+    end
+
     def address_templates(credentials, opts={})
       check_credentials(credentials)
       if opts[:id].nil?


Mime
View raw message