-
Notifications
You must be signed in to change notification settings - Fork 900
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ServiceTemplateContainerTemplate.
- Loading branch information
Showing
4 changed files
with
184 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
class ServiceTemplateContainerTemplate < ServiceTemplateGeneric | ||
def self.default_provisioning_entry_point(_service_type) | ||
'/Service/Generic/StateMachines/GenericLifecycle/provision' | ||
end | ||
|
||
def self.default_retirement_entry_point | ||
nil | ||
end | ||
|
||
# create ServiceTemplate and supporting ServiceResources and ResourceActions | ||
# options | ||
# :name | ||
# :description | ||
# :service_template_catalog_id | ||
# :config_info | ||
# :provision | ||
# :dialog_id or :dialog | ||
# :container_template_id or :container_template | ||
# | ||
def self.create_catalog_item(options) | ||
options = options.merge(:service_type => 'atomic', :prov_type => 'generic_container_template') | ||
config_info = validate_config_info(options[:config_info]) | ||
enhanced_config = config_info.deep_merge( | ||
:provision => { | ||
:configuration_template => container_template_from_config_info(config_info) | ||
} | ||
) | ||
|
||
transaction do | ||
create_from_options(options).tap do |service_template| | ||
service_template.create_resource_actions(enhanced_config) | ||
end | ||
end | ||
end | ||
|
||
def self.validate_config_info(info) | ||
info[:provision][:fqname] ||= default_provisioning_entry_point('atomic') if info.key?(:provision) | ||
|
||
# TODO: Add more validation for required fields | ||
info | ||
end | ||
private_class_method :validate_config_info | ||
|
||
def self.container_template_from_config_info(info) | ||
template_id = info[:provision][:container_template_id] | ||
template_id ? ContainerTemplate.find(template_id) : info[:provision][:container_template] | ||
end | ||
private_class_method :container_template_from_config_info | ||
|
||
def container_template | ||
resource_actions.find_by(:action => "Provision").try(:configuration_template) | ||
end | ||
|
||
def container_manager | ||
container_template.try(:ext_management_system) | ||
end | ||
|
||
def update_catalog_item(options) | ||
config_info = validate_update_config_info(options) | ||
config_info[:provision][:configuration_template] ||= container_template_from_config_info(config_info) if config_info.key?(:provision) | ||
|
||
options[:config_info] = config_info | ||
|
||
super | ||
end | ||
|
||
private | ||
|
||
def container_template_from_config_info(info) | ||
self.class.send(:container_template_from_config_info, info) | ||
end | ||
|
||
def validate_update_config_info(options) | ||
opts = super | ||
self.class.send(:validate_config_info, opts) | ||
end | ||
|
||
def update_service_resources(_config_info, _auth_user = nil) | ||
# do nothing since no service resources for this template | ||
end | ||
|
||
def update_from_options(params) | ||
options[:config_info] = Hash[params[:config_info].collect { |k, v| [k, v.except(:configuration_template)] }] | ||
update_attributes!(params.except(:config_info)) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
describe ServiceTemplateContainerTemplate do | ||
let(:service_template_catalog) { FactoryGirl.create(:service_template_catalog) } | ||
let(:container_template) { FactoryGirl.create(:container_template, :ems_id => ems.id) } | ||
let(:ems) { FactoryGirl.create(:ems_openshift) } | ||
let(:dialog) { FactoryGirl.create(:dialog) } | ||
let(:dialog2) { FactoryGirl.create(:dialog) } | ||
|
||
let(:catalog_item_options) do | ||
{ | ||
:name => 'container_template_catalog_item', | ||
:description => 'test container template', | ||
:service_template_catalog_id => service_template_catalog.id, | ||
:display => true, | ||
:config_info => { | ||
:provision => { | ||
:dialog_id => dialog.id, | ||
:container_template_id => container_template.id | ||
}, | ||
} | ||
} | ||
end | ||
|
||
let(:catalog_item_options_2) do | ||
changed_items = { | ||
:name => 'test_update_item', | ||
:description => 'test updated container template item', | ||
:config_info => { | ||
:provision => { | ||
:dialog_id => dialog2.id | ||
} | ||
} | ||
} | ||
catalog_item_options.deep_merge(changed_items) | ||
end | ||
|
||
describe '.create_catalog_item' do | ||
it 'creates and returns a catalog item' do | ||
service_template = described_class.create_catalog_item(catalog_item_options) | ||
|
||
expect(service_template.name).to eq(catalog_item_options[:name]) | ||
expect(service_template.config_info[:provision]).to include(catalog_item_options[:config_info][:provision]) | ||
expect(service_template.container_template).to eq(container_template) | ||
expect(service_template.resource_actions.first).to have_attributes( | ||
:action => 'Provision', | ||
:fqname => described_class.default_provisioning_entry_point('atomic'), | ||
:configuration_template => container_template, | ||
:dialog_id => dialog.id | ||
) | ||
|
||
saved_options = catalog_item_options[:config_info] | ||
expect(service_template.options[:config_info]).to include(saved_options) | ||
end | ||
end | ||
|
||
describe '.validate_config_info' do | ||
context 'provisioning entry point is given' do | ||
it 'keeps the given entry point' do | ||
opts = described_class.send(:validate_config_info, :provision => {:fqname => 'a/b/c'}) | ||
expect(opts[:provision][:fqname]).to eq('a/b/c') | ||
end | ||
end | ||
|
||
context 'provisioning entry point is not given' do | ||
it 'sets the default entry point' do | ||
opts = described_class.send(:validate_config_info, :provision => {}) | ||
expect(opts[:provision][:fqname]).to eq(described_class.default_provisioning_entry_point('atomic')) | ||
end | ||
end | ||
end | ||
|
||
describe '#update_catalog_item' do | ||
subject(:service_template) { described_class.create_catalog_item(catalog_item_options) } | ||
|
||
it 'updates and returns the modified catalog item' do | ||
service_template.update_catalog_item(catalog_item_options_2) | ||
|
||
expect(service_template.name).to eq(catalog_item_options_2[:name]) | ||
expect(service_template.description).to eq(catalog_item_options_2[:description]) | ||
expect(service_template.dialogs.first.id).to eq(dialog2.id) | ||
expect(service_template.options[:config_info][:provision]).not_to have_key(:configuration_template) | ||
expect(service_template.options[:config_info][:provision][:dialog_id]).to eq(dialog2.id) | ||
end | ||
end | ||
end |