Return-Path: X-Original-To: apmail-deltacloud-commits-archive@www.apache.org Delivered-To: apmail-deltacloud-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 2740F9681 for ; Wed, 11 Apr 2012 15:33:09 +0000 (UTC) Received: (qmail 93660 invoked by uid 500); 11 Apr 2012 15:33:09 -0000 Delivered-To: apmail-deltacloud-commits-archive@deltacloud.apache.org Received: (qmail 93612 invoked by uid 500); 11 Apr 2012 15:33:09 -0000 Mailing-List: contact commits-help@deltacloud.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@deltacloud.apache.org Delivered-To: mailing list commits@deltacloud.apache.org Received: (qmail 93426 invoked by uid 99); 11 Apr 2012 15:33:08 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 11 Apr 2012 15:33:08 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id AD4A5C55E; Wed, 11 Apr 2012 15:33:08 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: marios@apache.org To: commits@deltacloud.apache.org X-Mailer: ASF-Git Admin Mailer Subject: [8/11] git commit: CIMI: Adds Network create for server and Network model Message-Id: <20120411153308.AD4A5C55E@tyr.zones.apache.org> Date: Wed, 11 Apr 2012 15:33:08 +0000 (UTC) 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 Authored: Tue Mar 27 18:12:13 2012 +0300 Committer: marios 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}