deltacloud-commits mailing list archives

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


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

Branch: refs/heads/master
Commit: f4ea118cf3504a9b739e3bd53be858d947e64c23
Parents: 124b2d0
Author: marios <marios@redhat.com>
Authored: Tue Mar 27 18:12:13 2012 +0300
Committer: marios <marios@redhat.com>
Committed: Wed Apr 11 18:32:07 2012 +0300

----------------------------------------------------------------------
 server/lib/cimi/helpers/cimi_helper.rb |    6 ++++
 server/lib/cimi/model/errors.rb        |    8 +++++
 server/lib/cimi/model/network.rb       |   41 ++++++++++++++++++++++----
 server/lib/cimi/server.rb              |    6 ++--
 4 files changed, 51 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltacloud/blob/f4ea118c/server/lib/cimi/helpers/cimi_helper.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/helpers/cimi_helper.rb b/server/lib/cimi/helpers/cimi_helper.rb
index f67460b..b3f3493 100644
--- a/server/lib/cimi/helpers/cimi_helper.rb
+++ b/server/lib/cimi/helpers/cimi_helper.rb
@@ -20,6 +20,12 @@ module CIMIHelper
     status code
   end
 
+
+  def href_id(href, entity)
+    split_on = self.send(:"#{entity.to_s}_url")
+    href.split("#{split_on}/").last
+  end
+
 end
 
 helpers CIMIHelper

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/f4ea118c/server/lib/cimi/model/errors.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/model/errors.rb b/server/lib/cimi/model/errors.rb
index 9fb82f1..7c090ed 100644
--- a/server/lib/cimi/model/errors.rb
+++ b/server/lib/cimi/model/errors.rb
@@ -25,6 +25,14 @@ module CIMI::Model
 
   end
 
+  class BadRequest < StandardError
+    attr_accessor :code
+    def initialize(msg="")
+      super(msg)
+      self.code=400
+    end
+  end
+
   class NotImplemented < StandardError
     attr_accessor :code
 

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/f4ea118c/server/lib/cimi/model/network.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/model/network.rb b/server/lib/cimi/model/network.rb
index 08fcd97..5d95919 100644
--- a/server/lib/cimi/model/network.rb
+++ b/server/lib/cimi/model/network.rb
@@ -51,19 +51,46 @@ class CIMI::Model::Network < CIMI::Model::Base
     networks
   end
 
-  def self.create_from_xml(request_body, context)
-#FIXME
-  end
-
-  def self.create_from_json(request_body,context)
-#FIXME
+  def self.create(request_body, context, type)
+    input = (type == :xml)? XmlSimple.xml_in(request_body, 'ForceArray'=>false) : JSON.parse(request_body)
+    if input["networkTemplate"]["href"] #template by reference
+      network_config, routing_group = get_by_reference(input, context)
+    else
+      if input["networkTemplate"]["networkConfig"]["href"] # configuration by reference
+        network_config = NetworkConfiguration.find(context.href_id(input["networkTemplate"]["networkConfig"]["href"],:network_configurations),
context)
+      else #configuration by value
+        network_config = get_by_value(request_body, type)
+      end
+      routing_group = RoutingGroup.find(context.href_id(input["networkTemplate"]["routingGroup"]["href"],
:routing_groups), context)
+    end
+    params = {:network_config => network_config, :routing_group => routing_group, :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_network(context.credentials, params)
   end
 
   def self.delete!(id, context)
-  #FIXME
   end
 
 #FIXME
 #actions - needs method(s) to facilitate stop/start/suspend/delete
 
+  private
+
+  def self.get_by_reference(input, context)
+    network_template = NetworkTemplate.find(context.href_id(input["networkTemplate"]["href"],
:network_templates), context)
+    network_config = NetworkConfiguration.find(context.href_id(network_template.network_config.href,
:network_configurations), context)
+    routing_group = RoutingGroup.find(context.href_id(network_template.routing_group.href,
:routing_groups), context)
+    return network_config, routing_group
+  end
+
+  def self.get_by_value(request_body, type)
+    if type == :xml
+      xml_arrays = XmlSimple.xml_in(request_body)
+      network_config = NetworkConfiguration.from_xml(XmlSimple.xml_out(xml_arrays["networkTemplate"][0]["networkConfig"][0]))
+    else
+     json = JSON.parse(request_body)
+      network_config = NetworkConfiguration.from_json(JSON.generate(json["networkTemplate"]["networkConfig"]))
+    end
+  end
+
 end

http://git-wip-us.apache.org/repos/asf/deltacloud/blob/f4ea118c/server/lib/cimi/server.rb
----------------------------------------------------------------------
diff --git a/server/lib/cimi/server.rb b/server/lib/cimi/server.rb
index 0f62951..c58bf87 100644
--- a/server/lib/cimi/server.rb
+++ b/server/lib/cimi/server.rb
@@ -523,10 +523,10 @@ global_collection :networks do
   operation :create do
     description "Create a new Network"
     control do
-      if request.content_type.end_with("+json")
-        network = Network.create_from_json(request.body.read, self)
+      if request.content_type.end_with?("+json")
+        network = Network.create(request.body.read, self, :json)
       else
-        network = Network.create_from_xml(request.body.read, self)
+        network = Network.create(request.body.read, self, :xml)
       end
       respond_to do |format|
         format.xml { network.to_xml}


Mime
View raw message