From 5183ef0e2f2c3df390970ea4e578f3ebc7fe5d10 Mon Sep 17 00:00:00 2001 From: Bill Wei Date: Tue, 14 Mar 2017 13:49:27 -0400 Subject: [PATCH] catalog item accepts remove_resources option --- .../service_template_ansible_playbook.rb | 23 +++++++++-- .../service_template_ansible_playbook_spec.rb | 41 ++++++++++++++++++- 2 files changed, 59 insertions(+), 5 deletions(-) diff --git a/app/models/service_template_ansible_playbook.rb b/app/models/service_template_ansible_playbook.rb index 2dc4b975fb5..29521f47acc 100644 --- a/app/models/service_template_ansible_playbook.rb +++ b/app/models/service_template_ansible_playbook.rb @@ -1,10 +1,19 @@ class ServiceTemplateAnsiblePlaybook < ServiceTemplateGeneric + RETIREMENT_ENTRY_POINTS = { + 'yes_without_playbook' => '/Service/Generic/StateMachines/GenericLifecycle/Retire_Basic_Resource', + 'no_without_playbook' => '/Service/Generic/StateMachines/GenericLifecycle/Retire_Basic_Resource_None', + 'no_with_playbook' => '/Service/Generic/StateMachines/GenericLifecycle/Retire_Advanced_Resource_None', + 'pre_with_playbook' => '/Service/Generic/StateMachines/GenericLifecycle/Retire_Advanced_Resource_Pre', + 'post_with_playbook' => '/Service/Generic/StateMachines/GenericLifecycle/Retire_Advanced_Resource_Post' + }.freeze + private_constant :RETIREMENT_ENTRY_POINTS + def self.default_provisioning_entry_point(_service_type) '/Service/Generic/StateMachines/GenericLifecycle/provision' end def self.default_retirement_entry_point - '/Service/Generic/StateMachines/GenericLifecycle/retire' + RETIREMENT_ENTRY_POINTS['yes_without_playbook'] end def job_template(action) @@ -66,7 +75,7 @@ def self.prepare_job_template_and_dialog(action, service_name, description, conf def self.create_job_templates(service_name, description, config_info, auth_user) [:provision, :retirement, :reconfigure].each_with_object({}) do |action, hash| - next unless config_info[action] + next unless config_info[action] && config_info[action].key?(:playbook_id) hash[action] = { :configuration_template => create_job_template("miq_#{service_name}_#{action}", description, config_info[action], auth_user) } end end @@ -108,10 +117,16 @@ def self.build_parameter_list(name, description, info) private_class_method :build_parameter_list def self.validate_config_info(info) - info[:provision][:fqname] ||= default_provisioning_entry_point('atomic') if info.key?(:provision) - info[:retirement][:fqname] ||= default_retirement_entry_point if info.key?(:retirement) + info[:provision][:fqname] ||= default_provisioning_entry_point('atomic') if info.key?(:provision) info[:reconfigure][:fqname] ||= default_reconfiguration_entry_point if info.key?(:reconfigure) + if info.key?(:retirement) + info[:retirement][:fqname] ||= RETIREMENT_ENTRY_POINTS[info[:retirement][:remove_resources]] + info[:retirement][:fqname] ||= default_retirement_entry_point + else + info[:retirement] = {:fqname => default_retirement_entry_point} + end + # TODO: Add more validation for required fields info diff --git a/spec/models/service_template_ansible_playbook_spec.rb b/spec/models/service_template_ansible_playbook_spec.rb index fea29e7a477..1c8381a0f0d 100644 --- a/spec/models/service_template_ansible_playbook_spec.rb +++ b/spec/models/service_template_ansible_playbook_spec.rb @@ -127,7 +127,7 @@ end end - describe '#create_catalog_item' do + describe '.create_catalog_item' do it 'creates and returns a catalog item' do expect(described_class) .to receive(:create_job_templates).and_return(:provision => {:configuration_template => job_template}) @@ -147,4 +147,43 @@ 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 + + context 'retirement entry point is given' do + it 'keeps the given entry point' do + opts = described_class.send(:validate_config_info, :retirement => {:fqname => 'a/b/c'}) + expect(opts[:retirement][:fqname]).to eq('a/b/c') + end + end + + context 'retirement entry point is not given' do + it 'sets the default entry point' do + opts = described_class.send(:validate_config_info, {}) + expect(opts[:retirement][:fqname]).to eq(described_class.default_retirement_entry_point) + end + end + + context 'with remove_resources in retirement option' do + it 'sets the corresponding entry point' do + %w(yes_without_playbook no_without_playbook no_with_playbook pre_with_playbook post_with_playbook).each do |opt| + opts = described_class.send(:validate_config_info, :retirement => {:remove_resources => opt}) + expect(opts[:retirement][:fqname]).to eq(described_class.const_get(:RETIREMENT_ENTRY_POINTS)[opt]) + end + end + end + end end