Class: ForemanAnsibleDirector::AssignmentService

Inherits:
Object
  • Object
show all
Defined in:
app/services/foreman_ansible_director/assignment_service.rb

Class Method Summary collapse

Class Method Details

.create_assignment(consumable:, assignable:) ⇒ Object



12
13
14
15
16
17
18
19
20
# File 'app/services/foreman_ansible_director/assignment_service.rb', line 12

def create_assignment(consumable:,
                      assignable:)
  ActiveRecord::Base.transaction do
    ::ForemanAnsibleDirector::AnsibleContentAssignment.create!(
      consumable: consumable,
      assignable: assignable
    )
  end
end

.create_bulk_assignments(assignments:) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'app/services/foreman_ansible_director/assignment_service.rb', line 22

def create_bulk_assignments(assignments:)
  cleared_targets = []
  ActiveRecord::Base.transaction do
    assignments.each do |assignment|
      source_finder = ::ForemanAnsibleDirector::AssignmentService.finder(type: assignment[:source][:type])
      target_finder = ::ForemanAnsibleDirector::AssignmentService.finder(type: assignment[:target][:type])
      source = source_finder.find_by(id: assignment[:source][:id])
      target = target_finder.find_by(id: assignment[:target][:id])

      unless target.id.in?(cleared_targets)
        ::ForemanAnsibleDirector::AnsibleContentAssignment.where(assignable: target).destroy_all
        cleared_targets.push(target.id)
      end
      ::ForemanAnsibleDirector::AnsibleContentAssignment.create!(consumable: source, assignable: target)
    end
  end
end

.destroy_assignment(assignment) ⇒ Object



6
7
8
9
10
# File 'app/services/foreman_ansible_director/assignment_service.rb', line 6

def destroy_assignment(assignment)
  ActiveRecord::Base.transaction do
    assignment.destroy
  end
end

.find_target(target_type:, target_id:) ⇒ Object



57
58
59
60
61
# File 'app/services/foreman_ansible_director/assignment_service.rb', line 57

def find_target(target_type:, target_id:)
  # TODO: Null check target
  finder = finder(type: target_type)
  finder.find_by(id: target_id)
end

.finder(type:) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/services/foreman_ansible_director/assignment_service.rb', line 40

def finder(type:)
  case type

  when 'ACR'
    ::ForemanAnsibleDirector::AnsibleCollectionRole
  when 'CONTENT'
    ::ForemanAnsibleDirector::ContentUnitVersion
  when 'HOST'
    Host::Managed
  when 'HOSTGROUP'
    Hostgroup
  else
    # TODO: Actual error message
    raise "Invalid type: #{type}"
  end
end