Class: ForemanNetbox::SyncHost::SyncDevice::SyncInterfaces::SyncIpAddresses::Delete

Inherits:
Object
  • Object
show all
Includes:
Interactor
Defined in:
app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/delete.rb

Instance Method Summary collapse

Instance Method Details

#callObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/delete.rb', line 15

def call
  if context.interfaces.total.zero?
    context.ip_addresses.each(&:delete)
    return
  end

  ip_addresses_netbox_params = netbox_params.fetch(:ip_addresses, [])

  context.interfaces.each do |netbox_interface|
    host_interface_ips = ip_addresses_netbox_params.select do |i|
                           i.dig(:interface, :name) == netbox_interface.name
                         end
                                                   .map { |i| i.fetch(:address) }

    context.ip_addresses
           .select { |ip| ip['assigned_object_type'] == 'dcim.interface' && ip['assigned_object_id'] == netbox_interface.id }
           .reject { |ip| host_interface_ips.include?(ip['address']) }
           .each(&:delete)
  end
end