diff --git a/app/models/service_template_orchestration.rb b/app/models/service_template_orchestration.rb index 5f16cca5486..e8ab095e193 100644 --- a/app/models/service_template_orchestration.rb +++ b/app/models/service_template_orchestration.rb @@ -3,6 +3,27 @@ class ServiceTemplateOrchestration < ServiceTemplate before_save :remove_invalid_resource + def self.create_catalog_item(options, _auth_user = nil) + transaction do + create(options.except(:config_info)).tap do |service_template| + config_info = validate_config_info(options) + + service_template.orchestration_template = if config_info[:template_id] + OrchestrationTemplate.find(config_info[:template_id]) + else + config_info[:template] + end + service_template.orchestration_manager = if config_info[:manager_id] + ExtManagementSystem.find(config_info[:manager_id]) + else + config_info[:manager] + end + + service_template.create_resource_actions(config_info) + end + end + end + def remove_invalid_resource # remove the resource from both memory and table service_resources.to_a.delete_if { |r| r.destroy unless r.resource } @@ -24,4 +45,13 @@ def self.default_reconfiguration_entry_point def my_zone orchestration_manager.try(:my_zone) || MiqServer.my_zone end + + def self.validate_config_info(options) + config_info = options[:config_info] + unless (config_info[:template_id] && config_info[:manager_id]) || (config_info[:template] && config_info[:manager]) + raise _('Must provide both template_id and manager_id or manager and template') + end + config_info + end + private_class_method :validate_config_info end diff --git a/spec/models/service_template_orchestration_spec.rb b/spec/models/service_template_orchestration_spec.rb index 36880c288a9..c27e7bcb415 100644 --- a/spec/models/service_template_orchestration_spec.rb +++ b/spec/models/service_template_orchestration_spec.rb @@ -100,4 +100,68 @@ end end end + + describe '#create_catalog_item' do + let(:ra1) { FactoryGirl.create(:resource_action, :action => 'Provision') } + let(:ra2) { FactoryGirl.create(:resource_action, :action => 'Retirement') } + let(:service_dialog) { FactoryGirl.create(:dialog) } + let(:template) { FactoryGirl.create(:orchestration_template) } + let(:manager) { FactoryGirl.create(:ext_management_system) } + let(:catalog_item_options) do + { + :name => 'Orchestration Template', + :service_type => 'atomic', + :prov_type => 'generic_orchestration', + :display => 'false', + :description => 'a description', + :config_info => { + :template_id => template.id, + :manager_id => manager.id, + :provision => { + :fqname => ra1.fqname, + :dialog_id => service_dialog.id + }, + :retirement => { + :fqname => ra2.fqname, + :dialog_id => service_dialog.id + } + } + } + end + + it 'creates and returns an orchestration service template' do + service_template = ServiceTemplateOrchestration.create_catalog_item(catalog_item_options) + service_template.reload + + expect(service_template.name).to eq('Orchestration Template') + expect(service_template.dialogs.first).to eq(service_dialog) + expect(service_template.orchestration_template).to eq(template) + expect(service_template.orchestration_manager).to eq(manager) + expect(service_template.resource_actions.pluck(:action)).to include('Provision', 'Retirement') + end + + it 'requires both a template_id and manager_id' do + catalog_item_options[:config_info].delete(:template_id) + + expect do + ServiceTemplateOrchestration.create_catalog_item(catalog_item_options) + end.to raise_error(StandardError, 'Must provide both template_id and manager_id or manager and template') + end + + it 'requires both a template and a manager' do + catalog_item_options[:config_info] = { :manager => manager } + + expect do + ServiceTemplateOrchestration.create_catalog_item(catalog_item_options) + end.to raise_error(StandardError, 'Must provide both template_id and manager_id or manager and template') + end + + it 'accepts a manager and a template' do + catalog_item_options[:config_info] = { :manager => manager, :template => template } + + service_template = ServiceTemplateOrchestration.create_catalog_item(catalog_item_options) + expect(service_template.orchestration_template).to eq(template) + expect(service_template.orchestration_manager).to eq(manager) + end + end end