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,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
module SyncDevice
|
6
|
+
class Validate
|
7
|
+
include ::Interactor
|
8
|
+
|
9
|
+
def call
|
10
|
+
validator = ForemanNetbox::DeviceContract.new.call(netbox_params)
|
11
|
+
|
12
|
+
return if validator.success?
|
13
|
+
|
14
|
+
message = validator.errors
|
15
|
+
.messages
|
16
|
+
.map { |m| "#{m.path} #{m.text}" }
|
17
|
+
.to_sentence
|
18
|
+
context.fail!(error: _('Invalid Device parameters: %s') % message)
|
19
|
+
end
|
20
|
+
|
21
|
+
delegate :netbox_params, to: :context
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
module SyncTenant
|
6
|
+
class Create
|
7
|
+
include ::Interactor
|
8
|
+
|
9
|
+
around do |interactor|
|
10
|
+
interactor.call unless context.tenant
|
11
|
+
end
|
12
|
+
|
13
|
+
def call
|
14
|
+
context.tenant = ForemanNetbox::API.client::Tenancy::Tenant.new(params).save
|
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 params
|
25
|
+
netbox_params.fetch(:tenant).compact
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
module SyncTenant
|
6
|
+
class Find
|
7
|
+
include ::Interactor
|
8
|
+
|
9
|
+
def call
|
10
|
+
context.tenant = ForemanNetbox::API.client.tenancy.tenants.find_by(params)
|
11
|
+
rescue NetboxClientRuby::LocalError, NetboxClientRuby::ClientError, NetboxClientRuby::RemoteError => e
|
12
|
+
::Foreman::Logging.logger('foreman_netbox/import').error("#{self.class} error #{e}: #{e.backtrace}")
|
13
|
+
context.fail!(error: "#{self.class}: #{e}")
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
delegate :netbox_params, to: :context
|
19
|
+
|
20
|
+
def params
|
21
|
+
netbox_params.fetch(:tenant).slice(:slug)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
module SyncTenant
|
6
|
+
class Organizer
|
7
|
+
include ::Interactor::Organizer
|
8
|
+
|
9
|
+
after do
|
10
|
+
context.raw_data[:tenant] = context.tenant.raw_data!
|
11
|
+
end
|
12
|
+
|
13
|
+
organize SyncTenant::Find,
|
14
|
+
SyncTenant::Update,
|
15
|
+
SyncTenant::Create
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
module SyncTenant
|
6
|
+
class Update
|
7
|
+
include ::Interactor
|
8
|
+
|
9
|
+
around do |interactor|
|
10
|
+
interactor.call if context.tenant
|
11
|
+
end
|
12
|
+
|
13
|
+
def call
|
14
|
+
new_tags = new_tenant_params.fetch(:tags, []) - tenant.tags
|
15
|
+
tenant.tags = (tenant.tags | new_tags) if new_tags.any?
|
16
|
+
|
17
|
+
tenant.save
|
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, :tenant, to: :context
|
26
|
+
|
27
|
+
def new_tenant_params
|
28
|
+
netbox_params.fetch(:tenant, {})
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
module SyncVirtualMachine
|
6
|
+
class Create
|
7
|
+
include ::Interactor
|
8
|
+
|
9
|
+
around do |interactor|
|
10
|
+
interactor.call unless context.virtual_machine
|
11
|
+
end
|
12
|
+
|
13
|
+
def call
|
14
|
+
context.virtual_machine = ForemanNetbox::API.client::Virtualization::VirtualMachine.new(params).save
|
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, :cluster, to: :context
|
23
|
+
delegate :tenant, to: :context, allow_nil: true
|
24
|
+
|
25
|
+
def params
|
26
|
+
netbox_params.fetch(:virtual_machine).merge(
|
27
|
+
cluster: cluster.id,
|
28
|
+
tenant: tenant&.id
|
29
|
+
).compact
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
module SyncVirtualMachine
|
6
|
+
class Find
|
7
|
+
include ::Interactor
|
8
|
+
|
9
|
+
def call
|
10
|
+
# rubocop:disable Rails/DynamicFindBy
|
11
|
+
context.virtual_machine = find_by_mac || find_by_name
|
12
|
+
# rubocop:enable Rails/DynamicFindBy
|
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
|
+
private
|
19
|
+
|
20
|
+
delegate :netbox_params, :host, to: :context
|
21
|
+
delegate :mac, to: :host
|
22
|
+
|
23
|
+
def find_by_mac
|
24
|
+
ForemanNetbox::API.client.virtualization.virtual_machines.filter(mac_address: mac).first if mac
|
25
|
+
end
|
26
|
+
|
27
|
+
def find_by_name
|
28
|
+
params = netbox_params.fetch(:virtual_machine).slice(:name)
|
29
|
+
ForemanNetbox::API.client.virtualization.virtual_machines.find_by(params)
|
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 SyncVirtualMachine
|
6
|
+
class Organizer
|
7
|
+
include ::Interactor::Organizer
|
8
|
+
|
9
|
+
around do |interactor|
|
10
|
+
interactor.call if context.host.compute?
|
11
|
+
end
|
12
|
+
|
13
|
+
after do
|
14
|
+
context.raw_data[:virtual_machine] = context.virtual_machine.raw_data!
|
15
|
+
end
|
16
|
+
|
17
|
+
organize SyncVirtualMachine::Validate,
|
18
|
+
SyncHost::SyncTenant::Organizer,
|
19
|
+
SyncVirtualMachine::SyncCluster::Organizer,
|
20
|
+
SyncVirtualMachine::Find,
|
21
|
+
SyncVirtualMachine::Create,
|
22
|
+
SyncVirtualMachine::SyncInterfaces::Organizer,
|
23
|
+
SyncVirtualMachine::Update,
|
24
|
+
SyncVirtualMachine::SaveNetboxUrl
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
module SyncVirtualMachine
|
6
|
+
class SaveNetboxUrl
|
7
|
+
include ::Interactor
|
8
|
+
|
9
|
+
around do |interactor|
|
10
|
+
interactor.call if netbox_virtual_machine_url != netbox_facet_url
|
11
|
+
end
|
12
|
+
|
13
|
+
def call
|
14
|
+
netbox_facet.update(url: netbox_virtual_machine_url)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
delegate :host, to: :context
|
20
|
+
delegate :netbox_facet, to: :host
|
21
|
+
delegate :url, to: :netbox_facet, prefix: true, allow_nil: true
|
22
|
+
|
23
|
+
def netbox_virtual_machine_url
|
24
|
+
return unless context.virtual_machine&.id
|
25
|
+
|
26
|
+
"#{Setting::Netbox[:netbox_url]}/virtualization/virtual-machines/#{context.virtual_machine&.id}"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
module SyncVirtualMachine
|
6
|
+
module SyncCluster
|
7
|
+
class Create
|
8
|
+
include ::Interactor
|
9
|
+
|
10
|
+
around do |interactor|
|
11
|
+
interactor.call unless context.cluster
|
12
|
+
end
|
13
|
+
|
14
|
+
def call
|
15
|
+
context.cluster = ForemanNetbox::API.client::Virtualization::Cluster.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
|
+
delegate :cluster_type, to: :context, allow_nil: true
|
25
|
+
|
26
|
+
def params
|
27
|
+
netbox_params.fetch(:cluster).merge(
|
28
|
+
type: cluster_type&.id
|
29
|
+
).compact
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
module SyncVirtualMachine
|
6
|
+
module SyncCluster
|
7
|
+
class Find
|
8
|
+
include ::Interactor
|
9
|
+
|
10
|
+
def call
|
11
|
+
context.cluster = ForemanNetbox::API.client.virtualization.clusters.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(:cluster).slice(:name)
|
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 SyncVirtualMachine
|
6
|
+
module SyncCluster
|
7
|
+
class Organizer
|
8
|
+
include ::Interactor::Organizer
|
9
|
+
|
10
|
+
after do
|
11
|
+
context.raw_data[:cluster] = context.cluster.raw_data!
|
12
|
+
end
|
13
|
+
|
14
|
+
organize SyncCluster::SyncClusterType::Organizer,
|
15
|
+
SyncCluster::Find,
|
16
|
+
SyncCluster::Update,
|
17
|
+
SyncCluster::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 SyncVirtualMachine
|
6
|
+
module SyncCluster
|
7
|
+
module SyncClusterType
|
8
|
+
class Create
|
9
|
+
include ::Interactor
|
10
|
+
|
11
|
+
around do |interactor|
|
12
|
+
interactor.call unless context.cluster_type
|
13
|
+
end
|
14
|
+
|
15
|
+
def call
|
16
|
+
context.cluster_type = ForemanNetbox::API.client::Virtualization::ClusterType.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(:cluster_type).compact
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
module SyncVirtualMachine
|
6
|
+
module SyncCluster
|
7
|
+
module SyncClusterType
|
8
|
+
class Find
|
9
|
+
include ::Interactor
|
10
|
+
|
11
|
+
def call
|
12
|
+
return unless params
|
13
|
+
|
14
|
+
context.cluster_type = ForemanNetbox::API.client.virtualization.cluster_types.find_by(params)
|
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 params
|
25
|
+
netbox_params.fetch(:cluster_type).slice(:slug)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ForemanNetbox
|
4
|
+
module SyncHost
|
5
|
+
module SyncVirtualMachine
|
6
|
+
module SyncCluster
|
7
|
+
module SyncClusterType
|
8
|
+
class Organizer
|
9
|
+
include ::Interactor::Organizer
|
10
|
+
|
11
|
+
after do
|
12
|
+
context.raw_data[:cluster_type] = context.cluster_type.raw_data!
|
13
|
+
end
|
14
|
+
|
15
|
+
organize SyncClusterType::Find,
|
16
|
+
SyncClusterType::Create
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|