Class: ForemanAnsible::VariablesImporter
- Inherits:
-
Object
- Object
- ForemanAnsible::VariablesImporter
- Includes:
- ProxyAPI
- Defined in:
- app/services/foreman_ansible/variables_importer.rb
Overview
Methods to transform variable names coming from the proxy into Foreman AnsibleVariable objects
Constant Summary collapse
- VARIABLE_TYPES =
{ 'TrueClass' => 'boolean', 'FalseClass' => 'boolean', 'Integer' => 'integer', 'Fixnum' => 'integer', 'Float' => 'real', 'Array' => 'array', 'Hash' => 'hash', 'String' => 'string' }.freeze
Instance Method Summary collapse
- #create_new_variables(variables) ⇒ Object
- #delete_old_variables(variables) ⇒ Object
- #detect_changes(imported) ⇒ Object
- #finish_import(new, obsolete, update) ⇒ Object
- #import_new_role(role_name, new_roles) ⇒ Object
- #import_variable_names(new_roles) ⇒ Object
- #import_variables(role_variables, new_roles) ⇒ Object
-
#initialize(proxy = nil) ⇒ VariablesImporter
constructor
A new instance of VariablesImporter.
- #initialize_variables(variables, role) ⇒ Object
- #update_variables(variables) ⇒ Object
Constructor Details
#initialize(proxy = nil) ⇒ VariablesImporter
Returns a new instance of VariablesImporter.
20 21 22 |
# File 'app/services/foreman_ansible/variables_importer.rb', line 20 def initialize(proxy = nil) @ansible_proxy = proxy end |
Instance Method Details
#create_new_variables(variables) ⇒ Object
78 79 80 81 82 83 84 85 86 87 |
# File 'app/services/foreman_ansible/variables_importer.rb', line 78 def create_new_variables(variables) iterate_over_variables(variables) do |role, memo, attrs| variable = AnsibleVariable.new( JSON.parse(attrs)['ansible_variable'] ) variable.ansible_role = ::AnsibleRole.find_by(:name => role) variable.save memo << variable end end |
#delete_old_variables(variables) ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'app/services/foreman_ansible/variables_importer.rb', line 98 def delete_old_variables(variables) iterate_over_variables(variables) do |_role, memo, attrs| variable = AnsibleVariable.find( JSON.parse(attrs)['ansible_variable']['id'] ) memo << variable.key variable.destroy end end |
#detect_changes(imported) ⇒ Object
62 63 64 65 66 67 68 |
# File 'app/services/foreman_ansible/variables_importer.rb', line 62 def detect_changes(imported) changes = {}.with_indifferent_access persisted, changes[:new] = imported.partition { |role| role.id.present? } changes[:update], _old = persisted.partition(&:changed?) changes[:obsolete] = AnsibleVariable.where.not(:id => persisted.pluck(:id), :imported => false) changes end |
#finish_import(new, obsolete, update) ⇒ Object
70 71 72 73 74 75 76 |
# File 'app/services/foreman_ansible/variables_importer.rb', line 70 def finish_import(new, obsolete, update) results = { :added => [], :obsolete => [], :updated => [] } results[:added] = create_new_variables(new) if new.present? results[:obsolete] = delete_old_variables(obsolete) if obsolete.present? results[:updated] = update_variables(update) if update.present? results end |
#import_new_role(role_name, new_roles) ⇒ Object
39 40 41 42 43 44 45 |
# File 'app/services/foreman_ansible/variables_importer.rb', line 39 def import_new_role(role_name, new_roles) role = AnsibleRole.find_by(:name => role_name) if role.blank? && new_roles.map(&:name).include?(role_name) role = new_roles.select { |r| r.name == role_name }.first end role end |
#import_variable_names(new_roles) ⇒ Object
24 25 26 27 |
# File 'app/services/foreman_ansible/variables_importer.rb', line 24 def import_variable_names(new_roles) return import_variables(remote_variables, new_roles) if @ansible_proxy import_variables(local_variables, new_roles) end |
#import_variables(role_variables, new_roles) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'app/services/foreman_ansible/variables_importer.rb', line 29 def import_variables(role_variables, new_roles) detect_changes( role_variables.map do |role_name, variables| role = import_new_role(role_name, new_roles) next if role.blank? initialize_variables(variables, role) end.select(&:present?).flatten.compact ) end |
#initialize_variables(variables, role) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'app/services/foreman_ansible/variables_importer.rb', line 47 def initialize_variables(variables, role) variables.map do |variable_name, variable_default| variable = AnsibleVariable.find_or_initialize_by( :key => variable_name ) if variable.new_record? variable.assign_attributes(:default_value => variable_default, :key_type => infer_key_type(variable_default), :imported => true) end variable.ansible_role = role variable.valid? ? variable : nil end end |
#update_variables(variables) ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'app/services/foreman_ansible/variables_importer.rb', line 89 def update_variables(variables) iterate_over_variables(variables) do |_role, memo, attrs| attributes = JSON.parse(attrs)['ansible_variable'] var = AnsibleVariable.find attributes['id'] var.update(attributes) memo << var end end |