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

Service Playbook updates fqname and configuration_template #15007

Merged
merged 4 commits into from
May 8, 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
1 change: 1 addition & 0 deletions app/models/service_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class ServiceTemplate < ApplicationRecord

RESOURCE_ACTION_UPDATE_ATTRS = [:dialog,
:dialog_id,
:fqname,
:configuration_template,
:configuration_template_id,
:configuration_template_type].freeze
Expand Down
51 changes: 29 additions & 22 deletions app/models/service_template_ansible_playbook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,9 @@ def self.create_catalog_item(options, auth_user)

transaction do
create_from_options(options).tap do |service_template|
[:provision, :retirement, :reconfigure].each do |action|
action_info = enhanced_config[action]
next unless service_template.send(:new_dialog_required?, action_info)

dialog_name = action_info[:new_dialog_name]
job_template = action_info[:configuration_template]
hosts = action_info[:hosts]

new_dialog = service_template.send(:create_new_dialog, dialog_name, job_template, hosts)
action_info[:dialog] = new_dialog
service_template.options[:config_info][action][:dialog_id] = new_dialog.id
end
dialog_ids = service_template.send(:create_dialogs, enhanced_config)
enhanced_config.deep_merge!(dialog_ids)
service_template.options[:config_info].deep_merge!(dialog_ids)
service_template.create_resource_actions(enhanced_config)
end
end
Expand Down Expand Up @@ -141,7 +132,7 @@ def self.validate_config_info(info)


def job_template(action)
resource_actions.find_by(:action => action.to_s.capitalize).try(:configuration_template)
resource_actions.find_by(:action => action.capitalize).try(:configuration_template)
Copy link
Contributor

@syncrou syncrou May 5, 2017

Choose a reason for hiding this comment

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

I believe this will fail without the to_s. There may be more.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tested and proved to_s can be dropped.

Copy link
Contributor

Choose a reason for hiding this comment

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

With the latest changes I'm not able to get as far as before.

Using the following scenario:

  1. Create new Ansible Playbook catalog item with no retirement playbook.
  2. Edit the catalog item and add a new retirement playbook.
  3. Edit the catalog item and remove the retirement playbook.
  4. Delete the entire catalog item.

I am failing at 2. The retirement resource_action is created:

screen shot 2017-05-05 at 3 26 13 pm

But the retirement playbook is not created in Tower:

screen shot 2017-05-05 at 3 27 35 pm

end

def update_catalog_item(options, auth_user = nil)
Expand All @@ -151,10 +142,15 @@ def update_catalog_item(options, auth_user = nil)

update_job_templates(name, description, config_info, auth_user)

updated_config = config_info.deep_merge(self.class.send(:create_job_templates, name, description, config_info, auth_user, self))
config_info.deep_merge!(self.class.send(:create_job_templates, name, description, config_info, auth_user, self))

create_dialogs(updated_config)
options[:config_info] = updated_config
[:provision, :retirement, :reconfigure].each do |action|
next unless config_info.key?(action)
config_info[action][:configuration_template] ||= job_template(action)
end
config_info.deep_merge!(create_dialogs(config_info))

options[:config_info] = config_info

super
end
Expand Down Expand Up @@ -186,19 +182,19 @@ def check_retirement_potential
end

def create_dialogs(config_info)
[:provision, :retirement, :reconfigure].each do |action|
[:provision, :retirement, :reconfigure].each_with_object({}) do |action, hash|
info = config_info[action]
if new_dialog_required?(info)
info[:dialog_id] = create_new_dialog(info[:new_dialog_name], job_template(action), info[:hosts]).id
end
next unless new_dialog_required?(info)
hash[action] = {:dialog_id => create_new_dialog(info[:new_dialog_name], info[:configuration_template], info[:hosts]).id}
end
end

def delete_job_templates(job_templates)
def delete_job_templates(job_templates, action = nil)
auth_user = User.current_userid || 'system'
job_templates.each do |job_template|
ManageIQ::Providers::EmbeddedAnsible::AutomationManager::ConfigurationScript
.delete_in_provider_queue(job_template.manager.id, { :manager_ref => job_template.manager_ref }, auth_user)
resource_actions.find_by(:action => action.capitalize).update_attributes(:configuration_template => nil) if action
end
end

Expand All @@ -224,7 +220,7 @@ def update_job_templates(name, description, config_info, auth_user)
params[:manager_ref] = job_template(action).manager_ref
ManageIQ::Providers::EmbeddedAnsible::AutomationManager::ConfigurationScript.update_in_provider_queue(tower.id, params, auth_user)
else
delete_job_templates([job_template])
delete_job_templates([job_template], action)
end
end
end
Expand All @@ -233,4 +229,15 @@ def validate_update_config_info(options)
opts = super
self.class.send(:validate_config_info, opts)
end

# override
def update_service_resources(_config_info, _auth_user = nil)
# do nothing since no service resources for this template
end

# override
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
1 change: 1 addition & 0 deletions spec/models/service_template_ansible_playbook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@
new_dialog_record = Dialog.where(:label => new_dialog_label).first
expect(new_dialog_record).to be_truthy
expect(service_template.resource_actions.first.dialog.id).to eq new_dialog_record.id
expect(service_template.options[:config_info][:provision]).not_to have_key(:configuration_template)
end

it 'uses the existing dialog if :dialog_id is passed in' do
Expand Down
6 changes: 4 additions & 2 deletions spec/models/service_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -586,11 +586,11 @@
:instance_type => [flavor.id, flavor.name],
:src_ems_id => [ems.id, ems.name],
:provision => {
:fqname => ra1.fqname,
:fqname => 'a1/b1/c1',
:dialog_id => nil
},
:reconfigure => {
:fqname => ra3.fqname,
:fqname => 'x1/y1/z1',
:dialog_id => service_dialog.id
}
}
Expand All @@ -608,7 +608,9 @@
# Removes Retirement / Adds Reconfigure
expect(updated.resource_actions.pluck(:action)).to match_array(%w(Provision Reconfigure))
expect(updated.resource_actions.first.dialog_id).to be_nil # Removes the dialog from Provision
expect(updated.resource_actions.first.fqname).to eq('/a1/b1/c1')
expect(updated.resource_actions.last.dialog).to eq(service_dialog)
expect(updated.resource_actions.last.fqname).to eq('/x1/y1/z1')
expect(updated.name).to eq('Updated Template Name')
expect(updated.service_resources.first.resource.source_id).to eq(new_vm.id) # Validate request update
expect(updated.config_info).to eq(updated_catalog_item_options[:config_info])
Expand Down