foreman_netbox 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +619 -0
- data/README.md +50 -0
- data/Rakefile +50 -0
- data/app/contracts/foreman_netbox/device_contract.rb +42 -0
- data/app/contracts/foreman_netbox/params/interface_params.rb +16 -0
- data/app/contracts/foreman_netbox/params/ip_address_params.rb +15 -0
- data/app/contracts/foreman_netbox/params/tenant_params.rb +13 -0
- data/app/contracts/foreman_netbox/virtual_machine_contract.rb +29 -0
- data/app/interactors/foreman_netbox/concerns/facts.rb +11 -0
- data/app/interactors/foreman_netbox/concerns/primary_ips.rb +25 -0
- data/app/interactors/foreman_netbox/delete_host/delete_device.rb +20 -0
- data/app/interactors/foreman_netbox/delete_host/delete_virtual_machine.rb +20 -0
- data/app/interactors/foreman_netbox/delete_host/organizer.rb +12 -0
- data/app/interactors/foreman_netbox/sync_host/organizer.rb +35 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/create.rb +36 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/find.rb +41 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/organizer.rb +30 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/save_netbox_url.rb +29 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_role/create.rb +32 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_role/find.rb +28 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_role/organizer.rb +20 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/create.rb +34 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/find.rb +28 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/organizer.rb +22 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/sync_manufacturer/create.rb +34 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/sync_manufacturer/find.rb +40 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/sync_manufacturer/organizer.rb +22 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/update.rb +33 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/create.rb +37 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/delete.rb +36 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/find.rb +30 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/organizer.rb +23 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/create.rb +46 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/delete.rb +40 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/find.rb +32 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/organizer.rb +24 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/update.rb +39 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/update.rb +38 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_site/create.rb +32 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_site/find.rb +28 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_site/organizer.rb +25 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_site/update.rb +35 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/update.rb +85 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/validate.rb +25 -0
- data/app/interactors/foreman_netbox/sync_host/sync_tenant/create.rb +30 -0
- data/app/interactors/foreman_netbox/sync_host/sync_tenant/find.rb +26 -0
- data/app/interactors/foreman_netbox/sync_host/sync_tenant/organizer.rb +19 -0
- data/app/interactors/foreman_netbox/sync_host/sync_tenant/update.rb +33 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/create.rb +34 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/find.rb +34 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/organizer.rb +28 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/save_netbox_url.rb +31 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/create.rb +35 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/find.rb +28 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/organizer.rb +22 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/sync_cluster_type/create.rb +34 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/sync_cluster_type/find.rb +32 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/sync_cluster_type/organizer.rb +22 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/update.rb +35 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/create.rb +33 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/delete.rb +36 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/find.rb +30 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/organizer.rb +23 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/create.rb +46 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/delete.rb +40 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/find.rb +28 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/organizer.rb +24 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/update.rb +39 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/update.rb +38 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/update.rb +82 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/validate.rb +25 -0
- data/app/jobs/foreman_netbox/sync_host_job.rb +19 -0
- data/app/lib/foreman_netbox/api.rb +12 -0
- data/app/models/concerns/foreman_netbox/host_extensions.rb +27 -0
- data/app/models/concerns/foreman_netbox/location_extensions.rb +13 -0
- data/app/models/concerns/foreman_netbox/nic/base_extensions.rb +23 -0
- data/app/models/concerns/foreman_netbox/user_usergroup_common_extensions.rb +18 -0
- data/app/models/foreman_netbox/netbox_facet.rb +34 -0
- data/app/models/orchestration/netbox.rb +64 -0
- data/app/models/setting/netbox.rb +32 -0
- data/app/services/foreman_netbox/cached_netbox_parameters.rb +103 -0
- data/app/services/foreman_netbox/netbox_parameters.rb +185 -0
- data/app/services/foreman_netbox/netbox_parameters_comparator.rb +96 -0
- data/app/views/hosts/_netbox_tab.html.erb +28 -0
- data/config/routes.rb +4 -0
- data/db/migrate/20200527135812_create_netbox_facet.rb +12 -0
- data/db/migrate/20200604141933_add_sync_info_to_netbox_facet.rb +8 -0
- data/db/migrate/20200708093607_add_raw_data_to_netbox_facet.rb +7 -0
- data/lib/foreman_netbox.rb +6 -0
- data/lib/foreman_netbox/engine.rb +64 -0
- data/lib/foreman_netbox/version.rb +5 -0
- data/lib/tasks/foreman_netbox_tasks.rake +67 -0
- data/test/factories/host.rb +17 -0
- data/test/factories/netbox_facet.rb +17 -0
- data/test/fixtures/facts/k8s_physical_host.json +328 -0
- data/test/fixtures/facts/rhel_physical_host.json +512 -0
- data/test/fixtures/facts/rhel_virtual_host.json +455 -0
- data/test/fixtures/netbox_device_raw_data.json +330 -0
- data/test/fixtures/netbox_virtual_machine_raw_data.json +241 -0
- data/test/integration/foreman_netbox/sync_k8s_physical_host_test.rb +81 -0
- data/test/integration/foreman_netbox/sync_rhel_physical_host_test.rb +81 -0
- data/test/integration/foreman_netbox/sync_rhel_virtual_host_test.rb +93 -0
- data/test/interactors/foreman_netbox/sync_host/organizer_test.rb +78 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/create_test.rb +65 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/find_test.rb +105 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/save_netbox_url_test.rb +39 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_role/create_test.rb +45 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_role/find_test.rb +58 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/create_test.rb +55 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/find_test.rb +63 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/sync_manufacturer/create_test.rb +48 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/sync_manufacturer/find_test.rb +105 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/update_test.rb +54 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/create_test.rb +51 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/delete_test.rb +41 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/find_test.rb +53 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/create_test.rb +80 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/delete_test.rb +72 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/find_test.rb +51 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/update_test.rb +64 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/update_test.rb +92 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_site/create_test.rb +51 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_site/find_test.rb +59 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_site/update_test.rb +54 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/update_test.rb +188 -0
- data/test/interactors/foreman_netbox/sync_host/sync_tenant/create_test.rb +50 -0
- data/test/interactors/foreman_netbox/sync_host/sync_tenant/find_test.rb +58 -0
- data/test/interactors/foreman_netbox/sync_host/sync_tenant/update_test.rb +59 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/create_test.rb +74 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/find_test.rb +77 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/save_netbox_url_test.rb +39 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/create_test.rb +54 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/find_test.rb +58 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/sync_cluster_type/create_test.rb +50 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/sync_cluster_type/find_test.rb +65 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/update_test.rb +54 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/create_test.rb +53 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/delete_test.rb +37 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/find_test.rb +53 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/create_test.rb +80 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/delete_test.rb +73 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/find_test.rb +51 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/update_test.rb +64 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/update_test.rb +88 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/update_test.rb +171 -0
- data/test/jobs/foreman_netbox/sync_host_job_test.rb +14 -0
- data/test/models/foreman_netbox/location_test.rb +42 -0
- data/test/models/foreman_netbox/netbox_facet_test.rb +219 -0
- data/test/models/foreman_netbox/nics/base_test.rb +81 -0
- data/test/models/foreman_netbox/usergroup_test.rb +23 -0
- data/test/models/host/managed_test.rb +63 -0
- data/test/services/foreman_netbox/netbox_attributes_test.rb +278 -0
- data/test/services/foreman_netbox/netbox_parameters_comparator_test.rb +136 -0
- data/test/test_plugin_helper.rb +25 -0
- metadata +343 -0
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
module SyncVirtualMachine
|
6
|
+
module SyncCluster
|
7
|
+
class Update
|
8
|
+
include ::Interactor
|
9
|
+
|
10
|
+
around do |interactor|
|
11
|
+
interactor.call if context.cluster
|
12
|
+
end
|
13
|
+
|
14
|
+
def call
|
15
|
+
new_tags = new_cluster_params.fetch(:tags, []) - cluster.tags
|
16
|
+
cluster.tags = (cluster.tags | new_tags) if new_tags.any?
|
17
|
+
|
18
|
+
cluster.save
|
19
|
+
rescue NetboxClientRuby::LocalError, NetboxClientRuby::ClientError, NetboxClientRuby::RemoteError => e
|
20
|
+
::Foreman::Logging.logger('foreman_netbox/import').error("#{self.class} error #{e}: #{e.backtrace}")
|
21
|
+
context.fail!(error: "#{self.class}: #{e}")
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
delegate :netbox_params, :cluster, to: :context
|
27
|
+
|
28
|
+
def new_cluster_params
|
29
|
+
netbox_params.fetch(:cluster, {})
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
module SyncVirtualMachine
|
6
|
+
module SyncInterfaces
|
7
|
+
class Create
|
8
|
+
include ::Interactor
|
9
|
+
|
10
|
+
after do
|
11
|
+
context.interfaces.reload
|
12
|
+
end
|
13
|
+
|
14
|
+
def call
|
15
|
+
netbox_params.fetch(:interfaces, [])
|
16
|
+
.select { |i| i[:name] }
|
17
|
+
.reject { |i| interfaces.map(&:name).include?(i[:name]) }
|
18
|
+
.map do |new_interface|
|
19
|
+
ForemanNetbox::API.client::Virtualization::Interface.new(
|
20
|
+
new_interface.except(:type).merge(virtual_machine: virtual_machine.id)
|
21
|
+
).save
|
22
|
+
end
|
23
|
+
rescue NetboxClientRuby::LocalError, NetboxClientRuby::ClientError, NetboxClientRuby::RemoteError => e
|
24
|
+
::Foreman::Logging.logger('foreman_netbox/import').error("#{self.class} error #{e}: #{e.backtrace}")
|
25
|
+
context.fail!(error: "#{self.class}: #{e}")
|
26
|
+
end
|
27
|
+
|
28
|
+
delegate :virtual_machine, :interfaces, :netbox_params, to: :context
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
module SyncVirtualMachine
|
6
|
+
module SyncInterfaces
|
7
|
+
class Delete
|
8
|
+
include ::Interactor
|
9
|
+
|
10
|
+
around do |interactor|
|
11
|
+
interactor.call unless context.interfaces.total.zero?
|
12
|
+
end
|
13
|
+
|
14
|
+
def call
|
15
|
+
context.interfaces
|
16
|
+
.reject { |netbox_interface| interfaces_names.include?(netbox_interface.name) }
|
17
|
+
.each(&:delete)
|
18
|
+
rescue NetboxClientRuby::LocalError, NetboxClientRuby::ClientError, NetboxClientRuby::RemoteError => e
|
19
|
+
::Foreman::Logging.logger('foreman_netbox/import').error("#{self.class} error #{e}: #{e.backtrace}")
|
20
|
+
context.fail!(error: "#{self.class}: #{e}")
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
delegate :netbox_params, to: :context
|
26
|
+
|
27
|
+
def interfaces_names
|
28
|
+
netbox_params.fetch(:interfaces, [])
|
29
|
+
.map { |i| i[:name] }
|
30
|
+
.compact
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
module SyncVirtualMachine
|
6
|
+
module SyncInterfaces
|
7
|
+
class Find
|
8
|
+
include ::Interactor
|
9
|
+
|
10
|
+
def call
|
11
|
+
context.interfaces = ForemanNetbox::API.client.virtualization.interfaces.filter(params)
|
12
|
+
rescue NetboxClientRuby::LocalError, NetboxClientRuby::ClientError, NetboxClientRuby::RemoteError => e
|
13
|
+
::Foreman::Logging.logger('foreman_netbox/import').error("#{self.class} error #{e}: #{e.backtrace}")
|
14
|
+
context.fail!(error: "#{self.class}: #{e}")
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
delegate :virtual_machine, to: :context
|
20
|
+
|
21
|
+
def params
|
22
|
+
{
|
23
|
+
virtual_machine_id: virtual_machine.id
|
24
|
+
}
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/organizer.rb
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
module SyncVirtualMachine
|
6
|
+
module SyncInterfaces
|
7
|
+
class Organizer
|
8
|
+
include ::Interactor::Organizer
|
9
|
+
|
10
|
+
after do
|
11
|
+
context.raw_data[:interfaces] = context.interfaces.reload.raw_data!
|
12
|
+
end
|
13
|
+
|
14
|
+
organize SyncInterfaces::Find,
|
15
|
+
SyncInterfaces::Delete,
|
16
|
+
SyncInterfaces::Create,
|
17
|
+
SyncInterfaces::SyncIpAddresses::Organizer,
|
18
|
+
SyncInterfaces::Update
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
module SyncVirtualMachine
|
6
|
+
module SyncInterfaces
|
7
|
+
module SyncIpAddresses
|
8
|
+
class Create
|
9
|
+
include ::Interactor
|
10
|
+
|
11
|
+
def call
|
12
|
+
netbox_params.fetch(:ip_addresses, []).map do |ip_address|
|
13
|
+
interface_id = interfaces_map.fetch(ip_address.dig(:interface, :name), nil)
|
14
|
+
|
15
|
+
next unless interface_id
|
16
|
+
next unless ForemanNetbox::API.client
|
17
|
+
.ipam
|
18
|
+
.ip_addresses
|
19
|
+
.filter(interface_id: interface_id, address: ip_address[:address])
|
20
|
+
.total
|
21
|
+
.zero?
|
22
|
+
|
23
|
+
ForemanNetbox::API.client::IPAM::IpAddress.new(
|
24
|
+
ip_address.slice(:address, :tags).merge(interface: interface_id)
|
25
|
+
).save
|
26
|
+
end
|
27
|
+
rescue NetboxClientRuby::LocalError, NetboxClientRuby::ClientError, NetboxClientRuby::RemoteError => e
|
28
|
+
::Foreman::Logging.logger('foreman_netbox/import').error("#{self.class} error #{e}: #{e.backtrace}")
|
29
|
+
context.fail!(error: "#{self.class}: #{e}")
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
delegate :interfaces, :netbox_params, to: :context
|
35
|
+
|
36
|
+
def interfaces_map
|
37
|
+
interfaces.each_with_object({}) do |int, memo|
|
38
|
+
memo[int.name] = int.id
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
module SyncVirtualMachine
|
6
|
+
module SyncInterfaces
|
7
|
+
module SyncIpAddresses
|
8
|
+
class Delete
|
9
|
+
include ::Interactor
|
10
|
+
|
11
|
+
before do
|
12
|
+
context.interfaces.reload
|
13
|
+
end
|
14
|
+
|
15
|
+
def call
|
16
|
+
if context.interfaces.total.zero?
|
17
|
+
context.ip_addresses.each(&:delete)
|
18
|
+
return
|
19
|
+
end
|
20
|
+
|
21
|
+
ip_addresses_netbox_params = netbox_params.fetch(:ip_addresses, [])
|
22
|
+
|
23
|
+
context.interfaces.each do |netbox_interface|
|
24
|
+
host_interface_ips = ip_addresses_netbox_params.select { |ip| ip.dig(:interface, :name) == netbox_interface.name }
|
25
|
+
.map { |ip| ip.fetch(:address) }
|
26
|
+
|
27
|
+
context.ip_addresses
|
28
|
+
.select { |ip| ip['interface']['id'] == netbox_interface.id }
|
29
|
+
.reject { |ip| host_interface_ips.include?(ip['address']) }
|
30
|
+
.each(&:delete)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
delegate :netbox_params, to: :context
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
module SyncVirtualMachine
|
6
|
+
module SyncInterfaces
|
7
|
+
module SyncIpAddresses
|
8
|
+
class Find
|
9
|
+
include ::Interactor
|
10
|
+
|
11
|
+
def call
|
12
|
+
context.ip_addresses = ForemanNetbox::API.client.ipam.ip_addresses.filter(params)
|
13
|
+
rescue NetboxClientRuby::LocalError, NetboxClientRuby::ClientError, NetboxClientRuby::RemoteError => e
|
14
|
+
::Foreman::Logging.logger('foreman_netbox/import').error("#{self.class} error #{e}: #{e.backtrace}")
|
15
|
+
context.fail!(error: "#{self.class}: #{e}")
|
16
|
+
end
|
17
|
+
|
18
|
+
def params
|
19
|
+
{
|
20
|
+
virtual_machine_id: context.virtual_machine.id
|
21
|
+
}
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
module SyncVirtualMachine
|
6
|
+
module SyncInterfaces
|
7
|
+
module SyncIpAddresses
|
8
|
+
class Organizer
|
9
|
+
include ::Interactor::Organizer
|
10
|
+
|
11
|
+
after do
|
12
|
+
context.raw_data[:ip_addresses] = context.ip_addresses.reload.raw_data!
|
13
|
+
end
|
14
|
+
|
15
|
+
organize SyncIpAddresses::Find,
|
16
|
+
SyncIpAddresses::Delete,
|
17
|
+
SyncIpAddresses::Update,
|
18
|
+
SyncIpAddresses::Create
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
module SyncVirtualMachine
|
6
|
+
module SyncInterfaces
|
7
|
+
module SyncIpAddresses
|
8
|
+
class Update
|
9
|
+
include ::Interactor
|
10
|
+
|
11
|
+
def call
|
12
|
+
context.ip_addresses.each do |ip_address|
|
13
|
+
new_tags = tags_map.fetch(ip_address[:address], []) - ip_address.tags
|
14
|
+
|
15
|
+
ip_address.tags = (ip_address.tags | new_tags) if new_tags.any?
|
16
|
+
|
17
|
+
ip_address.save
|
18
|
+
end
|
19
|
+
rescue NetboxClientRuby::LocalError, NetboxClientRuby::ClientError, NetboxClientRuby::RemoteError => e
|
20
|
+
::Foreman::Logging.logger('foreman_netbox/import').error("#{self.class} error #{e}: #{e.backtrace}")
|
21
|
+
context.fail!(error: "#{self.class}: #{e}")
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
delegate :netbox_params, to: :context
|
27
|
+
|
28
|
+
def tags_map
|
29
|
+
netbox_params.fetch(:ip_addresses, [])
|
30
|
+
.each_with_object({}) do |item, memo|
|
31
|
+
memo[item[:address]] = item.fetch(:tags, [])
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
module SyncVirtualMachine
|
6
|
+
module SyncInterfaces
|
7
|
+
class Update
|
8
|
+
include ::Interactor
|
9
|
+
|
10
|
+
def call
|
11
|
+
context.interfaces.each { |netbox_interface| update(netbox_interface) }
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
delegate :netbox_params, to: :context
|
17
|
+
|
18
|
+
def update(netbox_interface)
|
19
|
+
new_params = netbox_params.fetch(:interfaces, [])
|
20
|
+
.find { |i| i[:name] == netbox_interface.name }
|
21
|
+
|
22
|
+
return unless new_params
|
23
|
+
|
24
|
+
netbox_interface.mac_address = new_params[:mac_address] if netbox_interface.mac_address != new_params[:mac_address]
|
25
|
+
|
26
|
+
new_tags = new_params.fetch(:tags, []) - netbox_interface.tags
|
27
|
+
netbox_interface.tags = (netbox_interface.tags | new_tags) if new_tags.any?
|
28
|
+
|
29
|
+
netbox_interface.save
|
30
|
+
rescue NetboxClientRuby::LocalError, NetboxClientRuby::ClientError, NetboxClientRuby::RemoteError => e
|
31
|
+
::Foreman::Logging.logger('foreman_netbox/import').error("#{self.class} error #{e}: #{e.backtrace}")
|
32
|
+
context.fail!(error: "#{self.class}: #{e}")
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
module SyncVirtualMachine
|
6
|
+
class Update
|
7
|
+
include ::Interactor
|
8
|
+
include ForemanNetbox::Concerns::PrimaryIps
|
9
|
+
|
10
|
+
ATTRIBUTES = %i[name cluster disk memory primary_ip4 primary_ip6 tenant vcpus tags].freeze
|
11
|
+
|
12
|
+
around do |interactor|
|
13
|
+
interactor.call if context.virtual_machine
|
14
|
+
end
|
15
|
+
|
16
|
+
before do
|
17
|
+
context.ip_addresses.reload
|
18
|
+
end
|
19
|
+
|
20
|
+
def call
|
21
|
+
assign_new_attributes
|
22
|
+
|
23
|
+
virtual_machine.save
|
24
|
+
rescue NetboxClientRuby::LocalError, NetboxClientRuby::ClientError, NetboxClientRuby::RemoteError => e
|
25
|
+
::Foreman::Logging.logger('foreman_netbox/import').error("#{self.class} error #{e}: #{e.backtrace}")
|
26
|
+
context.fail!(error: "#{self.class}: #{e}")
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
delegate :netbox_params, :virtual_machine, :cluster, to: :context
|
32
|
+
delegate :tenant, to: :context, allow_nil: true
|
33
|
+
|
34
|
+
def assign_new_attributes
|
35
|
+
ATTRIBUTES.map { |attribute| send("assign_#{attribute}") }
|
36
|
+
end
|
37
|
+
|
38
|
+
def assign_name
|
39
|
+
name = netbox_params.dig(:virtual_machine, :name)
|
40
|
+
virtual_machine.name = name if virtual_machine.name != name
|
41
|
+
end
|
42
|
+
|
43
|
+
def assign_cluster
|
44
|
+
virtual_machine.cluster = cluster.id if virtual_machine.cluster.id != cluster.id
|
45
|
+
end
|
46
|
+
|
47
|
+
def assign_disk
|
48
|
+
disk = netbox_params.dig(:virtual_machine, :disk)
|
49
|
+
virtual_machine.disk = disk if virtual_machine.disk != disk
|
50
|
+
end
|
51
|
+
|
52
|
+
def assign_memory
|
53
|
+
memory = netbox_params.dig(:virtual_machine, :memory)
|
54
|
+
virtual_machine.memory = memory if virtual_machine.memory != memory
|
55
|
+
end
|
56
|
+
|
57
|
+
def assign_primary_ip4
|
58
|
+
virtual_machine.primary_ip4 = primary_ip4 if virtual_machine.primary_ip4&.id != primary_ip4
|
59
|
+
end
|
60
|
+
|
61
|
+
def assign_primary_ip6
|
62
|
+
virtual_machine.primary_ip6 = primary_ip6 if virtual_machine.primary_ip6&.id != primary_ip6
|
63
|
+
end
|
64
|
+
|
65
|
+
def assign_tenant
|
66
|
+
virtual_machine.tenant = tenant&.id if virtual_machine.tenant&.id != tenant&.id
|
67
|
+
end
|
68
|
+
|
69
|
+
def assign_vcpus
|
70
|
+
vcpus = netbox_params.dig(:virtual_machine, :vcpus)
|
71
|
+
virtual_machine.vcpus = vcpus if virtual_machine.vcpus != vcpus
|
72
|
+
end
|
73
|
+
|
74
|
+
def assign_tags
|
75
|
+
new_tags = (netbox_params.dig(:virtual_machine, :tags) || []) - virtual_machine.tags
|
76
|
+
|
77
|
+
virtual_machine.tags = (virtual_machine.tags | new_tags) if new_tags.any?
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|