Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add create_catalog_item to ServiceTemplateOrchestration #13628

Merged
merged 2 commits into from
Jan 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions app/models/service_template_orchestration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may simply put config_info[:template] || OrchestrationTemplate.find(config_info[:template_id]) since we already validate either one must exist.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bzwei I wanted to, but the line was too long so rubocop wasn't happy, and this was more readable than breaking that up onto two lines imo

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 }
Expand All @@ -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])
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bzwei should I check for combinations of ids / objects?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally we should allow the combination. What you have here is likely real. I am ok with it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jntullo I would suggest allowing for the different combinations since that would better align with the logic in the create_catalog_item method. This can be done in a followup PR.

raise _('Must provide both template_id and manager_id or manager and template')
end
config_info
end
private_class_method :validate_config_info
end
64 changes: 64 additions & 0 deletions spec/models/service_template_orchestration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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