Class: ForemanNetbox::SyncHost::SyncDevice::SyncInterfaces::SyncIpAddresses::Create

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

Instance Method Summary collapse

Methods included from Concerns::AssignTags

#assign_tags_to, #default_tag_ids

Instance Method Details

#callObject



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

def call
  netbox_params.fetch(:ip_addresses, []).map do |ip_address|
    interface_id = interfaces_map.fetch(ip_address.dig(:interface, :name), nil)

    next unless interface_id
    next unless ForemanNetbox::API.client
                                  .ipam
                                  .ip_addresses
                                  .filter(interface_id: interface_id, address: ip_address[:address])
                                  .total
                                  .zero?

    ForemanNetbox::API.client::IPAM::IpAddress.new(
      ip_address.slice(:address)
                .merge(
                  assigned_object_type: 'dcim.interface', assigned_object_id: interface_id,
                  tags: default_tag_ids
                )
    ).save
  end
rescue NetboxClientRuby::LocalError, NetboxClientRuby::ClientError, NetboxClientRuby::RemoteError => e
  ::Foreman::Logging.logger('foreman_netbox/import').error("#{self.class} error #{e}: #{e.backtrace}")
  context.fail!(error: "#{self.class}: #{e}")
end