Module: ForemanAnsible::AnsibleRolesDataPreparations

Included in:
AnsibleRolesController, Api::V2::AnsibleRolesController, ApiRolesImporter
Defined in:
app/helpers/foreman_ansible/ansible_roles_data_preparations.rb

Constant Summary collapse

VARIABLE_ACTION_NAMES =
{ 'new' => _('Add'), 'obsolete' => _('Remove'), 'update' => _('Update') }.freeze
ROLE_ACTION_NAMES =
{ 'new' => _('Import Role'), 'obsolete' => _('Remove Role'), 'old' => _('Update Role Variables') }.freeze

Instance Method Summary collapse

Instance Method Details

#excluded_rolesObject



45
46
47
# File 'app/helpers/foreman_ansible/ansible_roles_data_preparations.rb', line 45

def excluded_roles
  Setting.convert_array_to_regexp(Setting[:ansible_roles_to_ignore])
end

#get_old_roles_variables(imported_variables, role) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'app/helpers/foreman_ansible/ansible_roles_data_preparations.rb', line 16

def get_old_roles_variables(imported_variables, role)
  variables = { 'new' => [], 'obsolete' => [], 'update' => [] }
  imported_variables.each do |kind, temp_variables|
    temp_variables.each do |temp_variable|
      variables[kind].append(temp_variable.key) if temp_variable.ansible_role_id == role.id
    end
  end
  variables
end

#get_roles_variables(imported_variables, variables_importer, kind, role) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'app/helpers/foreman_ansible/ansible_roles_data_preparations.rb', line 34

def get_roles_variables(imported_variables, variables_importer, kind, role)
  if kind == 'new'
    variables = { 'new' => variables_importer.get_variables_names(role.name) }
  elsif kind == 'obsolete'
    variables = { 'obsolete' => role.ansible_variables.map(&:key) }
  elsif kind == 'old'
    variables = get_old_roles_variables(imported_variables, role)
  end
  variables_to_s(variables)
end

#prepare_ansible_import_rows(changed, variables_importer, is_ui = true) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/helpers/foreman_ansible/ansible_roles_data_preparations.rb', line 77

def prepare_ansible_import_rows(changed, variables_importer, is_ui = true)
  rows = []
  changed.each do |kind, roles|
    imported_variables = variables_importer.import_variable_names(roles)
    roles.each do |role|
      next if role_match_excluded_roles(role.name)
      variables = get_roles_variables(imported_variables, variables_importer, kind, role)
      next if variables.empty? && kind['old']
      if is_ui
        rows.append(prepare_ui_row(role, kind, variables))
      else
        rows.append(prepare_api_row(role, kind, variables))
      end
    end
  end
  rows
end

#prepare_api_row(role, kind, variables) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'app/helpers/foreman_ansible/ansible_roles_data_preparations.rb', line 54

def prepare_api_row(role, kind, variables)
  {
    name: role.name,
    id: role.id,
    role_action: role_action_name(kind),
    variables: variables,
    hosts_count: kind == 'obsolete' ? role.hosts.count : '',
    hostgroup_count: kind == 'obsolete' ? role.hostgroups.count : '',
    kind: kind
  }
end

#prepare_ui_row(role, kind, variables) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'app/helpers/foreman_ansible/ansible_roles_data_preparations.rb', line 66

def prepare_ui_row(role, kind, variables)
  { cells: [
    role.name,
    role_action_name(kind),
    variables,
    kind == 'obsolete' ? role.hosts.count : '',
    kind == 'obsolete' ? role.hostgroups.count : ''
  ],
    role: role, kind: kind, id: role.name }
end

#role_action_name(kind) ⇒ Object



12
13
14
# File 'app/helpers/foreman_ansible/ansible_roles_data_preparations.rb', line 12

def role_action_name(kind)
  ROLE_ACTION_NAMES[kind]
end

#role_match_excluded_roles(role_name) ⇒ Object



49
50
51
52
# File 'app/helpers/foreman_ansible/ansible_roles_data_preparations.rb', line 49

def role_match_excluded_roles(role_name)
  match = role_name.match(excluded_roles)
  match.to_s.empty? ? nil : match
end

#variable_action_name(kind) ⇒ Object



8
9
10
# File 'app/helpers/foreman_ansible/ansible_roles_data_preparations.rb', line 8

def variable_action_name(kind)
  VARIABLE_ACTION_NAMES[kind]
end

#variables_to_s(variables) ⇒ Object



26
27
28
29
30
31
32
# File 'app/helpers/foreman_ansible/ansible_roles_data_preparations.rb', line 26

def variables_to_s(variables)
  str = ''
  variables.each do |action, temp_variables|
    str += "#{variable_action_name action}: #{temp_variables.size}, " unless temp_variables.empty?
  end
  str[0..-3]
end