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

Use extra_vars to create a new dialog when editing Ansible playbook service template. #15120

Merged
merged 1 commit into from
May 19, 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
14 changes: 7 additions & 7 deletions app/models/dialog/ansible_playbook_service_dialog.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
class Dialog
class AnsiblePlaybookServiceDialog
def self.create_dialog(label, job_template, hosts = 'localhost')
new.create_dialog(label, job_template, hosts)
def self.create_dialog(label, extra_vars, hosts = 'localhost')
new.create_dialog(label, extra_vars, hosts)
end

# This dialog is to be used by a playbook service
# The job_template contains the playbook
def create_dialog(label, job_template, hosts = 'localhost')
def create_dialog(label, extra_vars, hosts = 'localhost')
Dialog.new(:label => label, :buttons => "submit,cancel").tap do |dialog|
tab = dialog.dialog_tabs.build(:display => "edit", :label => "Basic Information", :position => 0)
add_options_group(tab, 0, hosts)
unless job_template.variables.blank?
add_variables_group(tab, 1, job_template)
unless extra_vars.blank?
add_variables_group(tab, 1, extra_vars)
end
dialog.save!
end
Expand Down Expand Up @@ -65,13 +65,13 @@ def add_inventory_field(group, position, hosts)
)
end

def add_variables_group(tab, position, template)
def add_variables_group(tab, position, extra_vars)
tab.dialog_groups.build(
:display => "edit",
:label => "Variables",
:position => position
).tap do |dialog_group|
template.variables.each_with_index do |(key, value), index|
extra_vars.transform_values { |val| val[:default] }.each_with_index do |(key, value), index|
value = value.to_json if [Hash, Array].include?(value.class)
add_variable_field(key, value, dialog_group, index)
end
Expand Down
6 changes: 3 additions & 3 deletions app/models/service_template_ansible_playbook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ def self.create_catalog_item(options, auth_user)
end
end

def create_new_dialog(dialog_name, job_template, hosts)
Dialog::AnsiblePlaybookServiceDialog.create_dialog(dialog_name, job_template, hosts)
def create_new_dialog(dialog_name, extra_vars, hosts)
Dialog::AnsiblePlaybookServiceDialog.create_dialog(dialog_name, extra_vars, hosts)
end
private :create_new_dialog

Expand Down Expand Up @@ -185,7 +185,7 @@ def create_dialogs(config_info)
[:provision, :retirement, :reconfigure].each_with_object({}) do |action, hash|
info = config_info[action]
next unless new_dialog_required?(info)
hash[action] = {:dialog_id => create_new_dialog(info[:new_dialog_name], info[:configuration_template], info[:hosts]).id}
hash[action] = {:dialog_id => create_new_dialog(info[:new_dialog_name], info[:extra_vars], info[:hosts]).id}
end
end

Expand Down
14 changes: 7 additions & 7 deletions spec/models/dialog/ansible_playbook_service_dialog_spec.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
describe Dialog::AnsiblePlaybookServiceDialog do
let(:playbook) { FactoryGirl.create(:configuration_script, :variables => nil) }

describe "#create_dialog" do
it "creates a dialog for a playbook with variables" do
allow(playbook).to receive(:variables).and_return('some_extra_var' => 'blah',
'other_extra_var' => {'name' => 'some_value'},
'array_extra_var' => [{'name' => 'some_value'}])
extra_vars = {
'some_extra_var' => {:default => 'blah'},
'other_extra_var' => {:default => {'name' => 'some_value'}},
'array_extra_var' => {:default => [{'name' => 'some_value'}]}
}

dialog = subject.create_dialog("mydialog1", playbook)
dialog = subject.create_dialog("mydialog1", extra_vars)
expect(dialog).to have_attributes(:label => 'mydialog1', :buttons => "submit,cancel")

tabs = dialog.dialog_tabs
Expand All @@ -16,7 +16,7 @@
end

it "creates a dialog for a playbook with no variables" do
dialog = described_class.create_dialog("mydialog2", playbook)
dialog = described_class.create_dialog("mydialog2", {})
expect(dialog.dialog_tabs[0].dialog_groups.size).to eq(1)
end
end
Expand Down