Class: ForemanAnsible::RolesImporter

Inherits:
Object
  • Object
show all
Includes:
ProxyAPI
Defined in:
app/services/foreman_ansible/roles_importer.rb

Overview

Imports roles from smart proxy

Direct Known Subclasses

ApiRolesImporter, UiRolesImporter

Instance Method Summary collapse

Constructor Details

#initialize(proxy = nil) ⇒ RolesImporter

Returns a new instance of RolesImporter.



8
9
10
# File 'app/services/foreman_ansible/roles_importer.rb', line 8

def initialize(proxy = nil)
  @ansible_proxy = proxy
end

Instance Method Details

#detect_changes(imported) ⇒ Object



27
28
29
30
31
32
# File 'app/services/foreman_ansible/roles_importer.rb', line 27

def detect_changes(imported)
  changes = {}.with_indifferent_access
  old, changes[:new] = imported.partition { |role| role.id.present? }
  changes[:obsolete] = ::AnsibleRole.where.not(:id => old.map(&:id))
  changes
end

#fetch_role_namesObject



16
17
18
# File 'app/services/foreman_ansible/roles_importer.rb', line 16

def fetch_role_names
  remote_roles if ansible_proxy
end

#import_role_namesObject



12
13
14
# File 'app/services/foreman_ansible/roles_importer.rb', line 12

def import_role_names
  import_roles remote_roles if @ansible_proxy.present?
end

#import_roles(roles) ⇒ Object



20
21
22
23
24
25
# File 'app/services/foreman_ansible/roles_importer.rb', line 20

def import_roles(roles)
  imported = roles.map do |role_name|
    ::AnsibleRole.find_or_initialize_by(:name => role_name)
  end
  detect_changes imported
end