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