Skip to content

Commit

Permalink
Refactor ServiceAnsiblePlaybook#save_job_options into methods
Browse files Browse the repository at this point in the history
  • Loading branch information
carbonin committed Jul 15, 2019
1 parent 5020b43 commit f3c7231
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions app/models/service_ansible_playbook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,27 +82,37 @@ def get_job_options(action)
options[job_option_key(action)].deep_dup
end

def save_job_options(action, overrides)
option_keys = %i[
hosts
extra_vars
credential_id
vault_credential_id
network_credential_id
cloud_credential_id
]
job_options = options.fetch_path(:config_info, action.downcase.to_sym).slice(*option_keys).with_indifferent_access
job_options[:extra_vars].try(:transform_values!) do |val|
val.kind_of?(String) ? val : val[:default] # TODO: support Hash only
end
job_options.deep_merge!(parse_dialog_options) unless action == ResourceAction::RETIREMENT
job_options.deep_merge!(overrides)
CONFIG_OPTIONS_WHITELIST = %i[
hosts
extra_vars
credential_id
vault_credential_id
network_credential_id
cloud_credential_id
].freeze

def config_options(action)
options.fetch_path(:config_info, action.downcase.to_sym).slice(*CONFIG_OPTIONS_WHITELIST).with_indifferent_access
end

def translate_credentials!(job_options)
%i[credential vault_credential network_credential cloud_credential].each do |cred|
cred_sym = "#{cred}_id".to_sym
credential_id = job_options.delete(cred_sym)
job_options[cred] = Authentication.find(credential_id).native_ref if credential_id.present?
end
end

def save_job_options(action, overrides)
job_options = config_options(action)

job_options[:extra_vars].try(:transform_values!) do |val|
val.kind_of?(String) ? val : val[:default] # TODO: support Hash only
end

job_options.deep_merge!(parse_dialog_options) unless action == ResourceAction::RETIREMENT
job_options.deep_merge!(overrides)
translate_credentials!(job_options)

options[job_option_key(action)] = job_options
save!
Expand Down

0 comments on commit f3c7231

Please sign in to comment.