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,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
module SyncDevice
|
6
|
+
class SaveNetboxUrl
|
7
|
+
include ::Interactor
|
8
|
+
|
9
|
+
around do |interactor|
|
10
|
+
interactor.call if netbox_device_url != netbox_facet_url
|
11
|
+
end
|
12
|
+
|
13
|
+
def call
|
14
|
+
netbox_facet.update(url: netbox_device_url)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
delegate :host, :device, to: :context
|
20
|
+
delegate :netbox_facet, to: :host
|
21
|
+
delegate :url, to: :netbox_facet, prefix: true, allow_nil: true
|
22
|
+
|
23
|
+
def netbox_device_url
|
24
|
+
"#{Setting::Netbox[:netbox_url]}/dcim/devices/#{device&.id}" if device&.id
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
module SyncDevice
|
6
|
+
module SyncDeviceRole
|
7
|
+
class Create
|
8
|
+
include ::Interactor
|
9
|
+
|
10
|
+
around do |interactor|
|
11
|
+
interactor.call unless context.device_role
|
12
|
+
end
|
13
|
+
|
14
|
+
def call
|
15
|
+
context.device_role = ForemanNetbox::API.client::DCIM::DeviceRole.new(params).save
|
16
|
+
rescue NetboxClientRuby::LocalError, NetboxClientRuby::ClientError, NetboxClientRuby::RemoteError => e
|
17
|
+
::Foreman::Logging.logger('foreman_netbox/import').error("#{self.class} error #{e}: #{e.backtrace}")
|
18
|
+
context.fail!(error: "#{self.class}: #{e}")
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
delegate :netbox_params, to: :context
|
24
|
+
|
25
|
+
def params
|
26
|
+
netbox_params.fetch(:device_role).compact
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
module SyncDevice
|
6
|
+
module SyncDeviceRole
|
7
|
+
class Find
|
8
|
+
include ::Interactor
|
9
|
+
|
10
|
+
def call
|
11
|
+
context.device_role = ForemanNetbox::API.client.dcim.device_roles.find_by(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 :netbox_params, to: :context
|
20
|
+
|
21
|
+
def params
|
22
|
+
netbox_params.fetch(:device_role).slice(:slug)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
module SyncDevice
|
6
|
+
module SyncDeviceRole
|
7
|
+
class Organizer
|
8
|
+
include ::Interactor::Organizer
|
9
|
+
|
10
|
+
after do
|
11
|
+
context.raw_data[:device_role] = context.device_role.raw_data!
|
12
|
+
end
|
13
|
+
|
14
|
+
organize SyncDeviceRole::Find,
|
15
|
+
SyncDeviceRole::Create
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
module SyncDevice
|
6
|
+
module SyncDeviceType
|
7
|
+
class Create
|
8
|
+
include ::Interactor
|
9
|
+
|
10
|
+
around do |interactor|
|
11
|
+
interactor.call unless context.device_type
|
12
|
+
end
|
13
|
+
|
14
|
+
def call
|
15
|
+
context.device_type = ForemanNetbox::API.client::DCIM::DeviceType.new(params).save
|
16
|
+
rescue NetboxClientRuby::LocalError, NetboxClientRuby::ClientError, NetboxClientRuby::RemoteError => e
|
17
|
+
::Foreman::Logging.logger('foreman_netbox/import').error("#{self.class} error #{e}: #{e.backtrace}")
|
18
|
+
context.fail!(error: "#{self.class}: #{e}")
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
delegate :netbox_params, :manufacturer, to: :context
|
24
|
+
|
25
|
+
def params
|
26
|
+
netbox_params.fetch(:device_type)
|
27
|
+
.merge(manufacturer: manufacturer.id)
|
28
|
+
.compact
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
module SyncDevice
|
6
|
+
module SyncDeviceType
|
7
|
+
class Find
|
8
|
+
include ::Interactor
|
9
|
+
|
10
|
+
def call
|
11
|
+
context.device_type = ForemanNetbox::API.client.dcim.device_types.find_by(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 :netbox_params, to: :context
|
20
|
+
|
21
|
+
def params
|
22
|
+
netbox_params.fetch(:device_type).slice(:slug)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
module SyncDevice
|
6
|
+
module SyncDeviceType
|
7
|
+
class Organizer
|
8
|
+
include ::Interactor::Organizer
|
9
|
+
|
10
|
+
after do
|
11
|
+
context.raw_data[:device_type] = context.device_type.raw_data!
|
12
|
+
end
|
13
|
+
|
14
|
+
organize SyncDeviceType::SyncManufacturer::Organizer,
|
15
|
+
SyncDeviceType::Find,
|
16
|
+
SyncDeviceType::Update,
|
17
|
+
SyncDeviceType::Create
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
module SyncDevice
|
6
|
+
module SyncDeviceType
|
7
|
+
module SyncManufacturer
|
8
|
+
class Create
|
9
|
+
include ::Interactor
|
10
|
+
|
11
|
+
around do |interactor|
|
12
|
+
interactor.call unless context.manufacturer
|
13
|
+
end
|
14
|
+
|
15
|
+
def call
|
16
|
+
context.manufacturer = ForemanNetbox::API.client::DCIM::Manufacturer.new(params).save
|
17
|
+
rescue NetboxClientRuby::LocalError, NetboxClientRuby::ClientError, NetboxClientRuby::RemoteError => e
|
18
|
+
::Foreman::Logging.logger('foreman_netbox/import').error("#{self.class} error #{e}: #{e.backtrace}")
|
19
|
+
context.fail!(error: "#{self.class}: #{e}")
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
delegate :netbox_params, to: :context
|
25
|
+
|
26
|
+
def params
|
27
|
+
netbox_params.fetch(:manufacturer).compact
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/sync_manufacturer/find.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
module SyncDevice
|
6
|
+
module SyncDeviceType
|
7
|
+
module SyncManufacturer
|
8
|
+
class Find
|
9
|
+
include ::Interactor
|
10
|
+
|
11
|
+
def call
|
12
|
+
# rubocop:disable Rails/DynamicFindBy
|
13
|
+
context.manufacturer = find_by_slug || find_by_name
|
14
|
+
# rubocop:enable Rails/DynamicFindBy
|
15
|
+
rescue NetboxClientRuby::LocalError, NetboxClientRuby::ClientError, NetboxClientRuby::RemoteError => e
|
16
|
+
::Foreman::Logging.logger('foreman_netbox/import').error("#{self.class} error #{e}: #{e.backtrace}")
|
17
|
+
context.fail!(error: "#{self.class}: #{e}")
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
delegate :netbox_params, to: :context
|
23
|
+
|
24
|
+
def find_by_slug
|
25
|
+
params = netbox_params.fetch(:manufacturer).slice(:slug)
|
26
|
+
|
27
|
+
ForemanNetbox::API.client.dcim.manufacturers.find_by(params)
|
28
|
+
end
|
29
|
+
|
30
|
+
def find_by_name
|
31
|
+
params = netbox_params.fetch(:manufacturer).slice(:name)
|
32
|
+
|
33
|
+
ForemanNetbox::API.client.dcim.manufacturers.find_by(params)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
module SyncDevice
|
6
|
+
module SyncDeviceType
|
7
|
+
module SyncManufacturer
|
8
|
+
class Organizer
|
9
|
+
include ::Interactor::Organizer
|
10
|
+
|
11
|
+
after do
|
12
|
+
context.raw_data[:manufacturer] = context.manufacturer.raw_data!
|
13
|
+
end
|
14
|
+
|
15
|
+
organize SyncManufacturer::Find,
|
16
|
+
SyncManufacturer::Create
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
module SyncDevice
|
6
|
+
module SyncDeviceType
|
7
|
+
class Update
|
8
|
+
include ::Interactor
|
9
|
+
|
10
|
+
around do |interactor|
|
11
|
+
interactor.call if context.device_type
|
12
|
+
end
|
13
|
+
|
14
|
+
def call
|
15
|
+
new_tags = new_device_type_params.fetch(:tags, []) - device_type.tags
|
16
|
+
device_type.tags = (device_type.tags | new_tags) if new_tags.any?
|
17
|
+
|
18
|
+
device_type.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
|
+
delegate :netbox_params, :device_type, to: :context
|
25
|
+
|
26
|
+
def new_device_type_params
|
27
|
+
netbox_params.fetch(:device_type, {})
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
module SyncDevice
|
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::DCIM::Interface.new(
|
20
|
+
new_interface.except(:type)
|
21
|
+
.merge(
|
22
|
+
type: new_interface.dig(:type, :value),
|
23
|
+
device: device.id
|
24
|
+
)
|
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
|
+
delegate :device, :interfaces, :netbox_params, to: :context
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
module SyncDevice
|
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 SyncDevice
|
6
|
+
module SyncInterfaces
|
7
|
+
class Find
|
8
|
+
include ::Interactor
|
9
|
+
|
10
|
+
def call
|
11
|
+
context.interfaces = ForemanNetbox::API.client.dcim.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 :device, to: :context
|
20
|
+
|
21
|
+
def params
|
22
|
+
{
|
23
|
+
device_id: device.id
|
24
|
+
}
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|