From: David Lutterkort <lut...@redhat.com> Move the logic from Machine.create_from_json and Machine.create_from_xml into MachineCreate --- server/lib/cimi/collections/machines.rb | 7 +--- server/lib/cimi/models/machine.rb | 69 -------------------------------- server/lib/cimi/models/machine_create.rb | 29 ++++++++++++++ 3 files changed, 31 insertions(+), 74 deletions(-)
diff --git a/server/lib/cimi/collections/machines.rb b/server/lib/cimi/collections/machines.rb index 5718f46..88b3c99 100644 --- a/server/lib/cimi/collections/machines.rb +++ b/server/lib/cimi/collections/machines.rb @@ -46,11 +46,8 @@ module CIMI::Collections operation :create, :with_capability => :create_instance do description "Create a new Machine entity." control do - if grab_content_type(request.content_type, request.body) == :json - new_machine = Machine.create_from_json(request.body.read, self) - else - new_machine = Machine.create_from_xml(request.body.read, self) - end + mc = MachineCreate.parse(request.body, request.content_type) + new_machine = mc.create(self) headers_for_create new_machine respond_to do |format| format.json { new_machine.to_json } diff --git a/server/lib/cimi/models/machine.rb b/server/lib/cimi/models/machine.rb index 0e180ce..4010aa0 100644 --- a/server/lib/cimi/models/machine.rb +++ b/server/lib/cimi/models/machine.rb @@ -50,75 +50,6 @@ class CIMI::Model::Machine < CIMI::Model::Base end end - def self.create_from_json(body, context) - json = JSON.parse(body) - additional_params={} - machine_template = json['machineTemplate'] - if !machine_template['href'].nil? - template = current_db.machine_templates.first(:id => machine_template['href'].split('/').last) - raise 'Could not find the MachineTemplate' if template.nil? - hardware_profile_id = template.machine_config.split('/').last - image_id = template.machine_image.split('/').last - else - hardware_profile_id = machine_template['machineConfig']["href"].split('/').last - image_id = machine_template['machineImage']["href"].split('/').last - if machine_template.has_key? 'credential' - additional_params[:keyname] = machine_template['credential']["href"].split('/').last - end - end - if machine_template.has_key? "initialState" - additional_params[:initial_state] = machine_template["initialState"].strip - end - additional_params[:name] = json['name'] if json['name'] - additional_params[:realm_id] = json['realm'] if json['realm'] - instance = context.driver.create_instance(context.credentials, image_id, { - :hwp_id => hardware_profile_id - }.merge(additional_params)) - - # Store attributes that are not supported by the backend cloud to local - # database: - machine = from_instance(instance, context) - machine.name = json['name'] || machine.name - machine.description = json['description'] - machine.extract_properties!(json) - machine.save - machine - end - - def self.create_from_xml(body, context) - xml = XmlSimple.xml_in(body) - additional_params = {} - if xml['machineTemplate'][0]['href'] - template = current_db.machine_templates_dataset.first(:id => xml['machineTemplate'][0]['href'].split('/').last) - hardware_profile_id = template.machine_config.split('/').last - image_id = template.machine_image.split('/').last - else - machine_template = xml['machineTemplate'][0] - hardware_profile_id = machine_template['machineConfig'].first["href"].split('/').last - image_id = machine_template['machineImage'].first["href"].split('/').last - if machine_template.has_key? 'credential' - additional_params[:keyname] = machine_template['credential'][0]["href"].split('/').last - end - end - if xml["machineTemplate"][0].has_key? "initialState" - additional_params[:initial_state] = xml["machineTemplate"][0]["initialState"].first.strip - end - additional_params[:name] = xml['name'][0] if xml['name'] - additional_params[:realm_id] = xml['realm'][0] if xml['realm'] - instance = context.driver.create_instance(context.credentials, image_id, { - :hwp_id => hardware_profile_id - }.merge(additional_params)) - - # Store attributes that are not supported by the backend cloud to local - # database: - machine = from_instance(instance, context) - machine.name = xml['name'] || machine.name - machine.description = xml['description'] - machine.extract_properties!(xml) - machine.save - machine - end - def perform(action, context, &block) begin if context.driver.send(:"#{action.name}_instance", context.credentials, self.id.split("/").last) diff --git a/server/lib/cimi/models/machine_create.rb b/server/lib/cimi/models/machine_create.rb index 8f9fc89..c0b107c 100644 --- a/server/lib/cimi/models/machine_create.rb +++ b/server/lib/cimi/models/machine_create.rb @@ -15,4 +15,33 @@ class CIMI::Model::MachineCreate < CIMI::Model::Base ref :machine_template + text :realm + + def create(ctx) + params = {} + if machine_template.href + template = machine_template.find(ctx) + params[:hwp_id] = template.machine_config.ref_id(ctx) + image_id = template.machine_image.ref_id(ctx) + else + # FIXME: What if either of these href's isn't there ? What if the user + # tries to override some aspect of the machine_config/machine_image ? + params[:hwp_id] = machine_template.machine_config.href.split('/').last + image_id = machine_template.machine_image.href.split('/').last + if machine_template.credential.href + params[:keyname] = machine_template.credential.href.split('/').last + end + end + + params[:name] = name if name + params[:realm_id] = realm if realm + instance = ctx.driver.create_instance(ctx.credentials, image_id, params) + + result = CIMI::Model::Machine::from_instance(instance, ctx) + result.name = name if name + result.description = description if description + result.property = property if property + result.save + result + end end -- 1.8.1