Class: Moose::Inventory::Operations::RemoveAssociations

Inherits:
Object
  • Object
show all
Includes:
OperationEventSupport
Defined in:
lib/moose_inventory/operations/remove_associations.rb

Overview

Removes host/group associations for existing primary entities.

Constant Summary collapse

AUTOMATIC_GROUP =
'ungrouped'

Instance Method Summary collapse

Constructor Details

#initialize(context:) ⇒ RemoveAssociations

Returns a new instance of RemoveAssociations.



13
14
15
# File 'lib/moose_inventory/operations/remove_associations.rb', line 13

def initialize(context:)
  @context = context
end

Instance Method Details

#group_from_hosts(group:, group_name:, host_names:, dry_run: false) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/moose_inventory/operations/remove_associations.rb', line 35

def group_from_hosts(group:, group_name:, host_names:, dry_run: false)
  events = []
  warning_count = 0
  @dry_run = dry_run
  hosts_dataset = group.hosts_dataset

  host_names.each do |host_name|
    next if host_name.nil? || host_name.empty?

    warning_count += remove_host_from_group(group, group_name, host_name, hosts_dataset, events)
  end

  emit(events, :dry_run_summary) if dry_run

  operation_result(events: events, warning_count: warning_count)
end

#host_from_groups(host:, host_name:, group_names:, dry_run: false) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/moose_inventory/operations/remove_associations.rb', line 17

def host_from_groups(host:, host_name:, group_names:, dry_run: false)
  events = []
  warning_count = 0
  @dry_run = dry_run

  group_names.each do |group_name|
    next if group_name.nil? || group_name.empty?

    warning_count += remove_group_from_host(host, host_name, group_name, events)
  end

  add_automatic_group_if_needed(host, host_name, events,
                                planned_empty: planned_host_groups_empty?(host, group_names))
  emit(events, :dry_run_summary) if dry_run

  operation_result(events: events, warning_count: warning_count)
end