From: marios <[email protected]>

Signed-off-by: marios <[email protected]>
---
 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(-)

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
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
 
 
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?
-- 
1.7.6.5

Reply via email to