foreman_netbox 1.0.2 → 1.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (78) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +8 -0
  3. data/app/contracts/foreman_netbox/device_contract.rb +3 -3
  4. data/app/contracts/foreman_netbox/params/interface_params.rb +6 -8
  5. data/app/contracts/foreman_netbox/params/ip_address_params.rb +5 -7
  6. data/app/contracts/foreman_netbox/params/tenant_params.rb +4 -6
  7. data/app/contracts/foreman_netbox/virtual_machine_contract.rb +3 -3
  8. data/app/interactors/foreman_netbox/concerns/assign_tags.rb +21 -0
  9. data/app/interactors/foreman_netbox/sync_host/sync_device/create.rb +3 -1
  10. data/app/interactors/foreman_netbox/sync_host/sync_device/organizer.rb +1 -0
  11. data/app/interactors/foreman_netbox/sync_host/sync_device/save_netbox_url.rb +1 -1
  12. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/create.rb +2 -1
  13. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/update.rb +2 -2
  14. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/create.rb +3 -1
  15. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/create.rb +6 -1
  16. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/delete.rb +1 -1
  17. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/update.rb +2 -14
  18. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/update.rb +2 -3
  19. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_site/create.rb +4 -1
  20. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_site/update.rb +3 -9
  21. data/app/interactors/foreman_netbox/sync_host/sync_device/update.rb +5 -7
  22. data/app/interactors/foreman_netbox/sync_host/sync_tags/create.rb +30 -0
  23. data/app/interactors/foreman_netbox/sync_host/sync_tags/find.rb +26 -0
  24. data/app/interactors/foreman_netbox/sync_host/sync_tags/organizer.rb +22 -0
  25. data/app/interactors/foreman_netbox/sync_host/sync_tenant/create.rb +4 -1
  26. data/app/interactors/foreman_netbox/sync_host/sync_tenant/update.rb +3 -9
  27. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/create.rb +3 -1
  28. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/organizer.rb +1 -0
  29. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/save_netbox_url.rb +1 -1
  30. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/create.rb +3 -1
  31. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/update.rb +3 -9
  32. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/create.rb +6 -1
  33. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/create.rb +7 -2
  34. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/delete.rb +1 -1
  35. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/update.rb +2 -14
  36. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/update.rb +2 -3
  37. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/update.rb +3 -7
  38. data/app/lib/foreman_netbox/api.rb +2 -2
  39. data/app/models/concerns/foreman_netbox/user_usergroup_common_extensions.rb +1 -1
  40. data/app/models/orchestration/netbox.rb +4 -1
  41. data/app/services/foreman_netbox/cached_netbox_parameters.rb +8 -8
  42. data/app/services/foreman_netbox/netbox_parameters.rb +8 -17
  43. data/app/services/foreman_netbox/netbox_parameters_comparator.rb +2 -2
  44. data/db/migrate/20230303095650_fix_netbox_settings_category_to_dsl.rb +9 -0
  45. data/lib/foreman_netbox/engine.rb +28 -11
  46. data/lib/foreman_netbox/version.rb +1 -1
  47. data/test/integration/foreman_netbox/sync_k8s_physical_host_test.rb +12 -8
  48. data/test/integration/foreman_netbox/sync_rhel_physical_host_test.rb +12 -8
  49. data/test/integration/foreman_netbox/sync_rhel_virtual_host_test.rb +12 -8
  50. data/test/interactors/foreman_netbox/sync_host/organizer_test.rb +22 -6
  51. data/test/interactors/foreman_netbox/sync_host/sync_device/create_test.rb +5 -3
  52. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/create_test.rb +4 -3
  53. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/update_test.rb +8 -3
  54. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/create_test.rb +8 -4
  55. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/create_test.rb +10 -5
  56. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/delete_test.rb +2 -2
  57. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/update_test.rb +5 -3
  58. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/update_test.rb +7 -5
  59. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_site/create_test.rb +2 -2
  60. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_site/update_test.rb +4 -3
  61. data/test/interactors/foreman_netbox/sync_host/sync_device/update_test.rb +49 -5
  62. data/test/interactors/foreman_netbox/sync_host/sync_tenant/create_test.rb +2 -2
  63. data/test/interactors/foreman_netbox/sync_host/sync_tenant/update_test.rb +5 -3
  64. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/create_test.rb +4 -3
  65. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/create_test.rb +7 -3
  66. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/update_test.rb +5 -3
  67. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/create_test.rb +5 -4
  68. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/create_test.rb +9 -7
  69. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/delete_test.rb +2 -2
  70. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/update_test.rb +13 -4
  71. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/update_test.rb +9 -4
  72. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/update_test.rb +7 -4
  73. data/test/models/foreman_netbox/usergroup_test.rb +4 -4
  74. data/test/services/foreman_netbox/netbox_attributes_test.rb +10 -19
  75. data/test/services/foreman_netbox/netbox_parameters_comparator_test.rb +6 -16
  76. data/test/test_plugin_helper.rb +10 -3
  77. metadata +122 -12
  78. data/app/models/setting/netbox.rb +0 -32
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e5c0bba2855f92b79b87545d69196dc2556311d59f7e2d90bfb1ea7acfe2a55f
4
- data.tar.gz: 8847d6add790eedfcb2703a3db61acf2645eb1a60c37fd66e4614ac4bbb85917
3
+ metadata.gz: 4b8ad442bf38c2bf5ac0ed5b94580b108a16d267a2ae2a37f456a73ebb1f8cd6
4
+ data.tar.gz: e31f39d22ea73a0bda27093a4f8474740836899c4d0379cc8763a1b9ec766e24
5
5
  SHA512:
6
- metadata.gz: cc82ec6557890e5457f9471e98560cbf9f12e4074752bbff7cc8e21f5cf8f34b0288150a1dedec75fd99c3d1b649ef943e74c6b633dd09548643d950dedcb75f
7
- data.tar.gz: c7907c045cb7cc50ac25d098276c4666301aecf492802714297a08dfe12bf488f72c7a6bb7e5d1b31531b9d0025a9d979f282f7083d0ae04b3c2c50bc755ef77
6
+ metadata.gz: ab087e119d4a0fd10f4847a4e6aaa829e55e2b1036393058ccc90363a098ea12fa156fd7f62c1eba98d0e2a15e0f3fd0aeec77d65f4a78a515ff6b16b6739f13
7
+ data.tar.gz: 47561411ae532f4229a45ce71443f89fadc94aae271d25be9e201148500cfa7f013ff7e24c94c1134738e95f7a200ed204378a4dee65cd72e7694e64f81c5911
data/README.md CHANGED
@@ -4,6 +4,14 @@
4
4
 
5
5
  This is a plugin for Foreman that introduces integration with [NetBox](https://netbox.readthedocs.io)
6
6
 
7
+ ## Compatibility
8
+
9
+ | Netbox Version | Plugin Version |
10
+ | -------------- | -------------- |
11
+ | 2.8 | 1.0 |
12
+ | 2.11 | 1.1 |
13
+ | 3.3 | 1.2 |
14
+
7
15
  ## Installation
8
16
 
9
17
  See [How_to_Install_a_Plugin](https://theforeman.org/manuals/2.3/index.html#6.1InstallaPlugin)
@@ -4,9 +4,9 @@ module ForemanNetbox
4
4
  class DeviceContract < Dry::Validation::Contract
5
5
  # rubocop:disable Metrics/BlockLength
6
6
  params do
7
- optional(:tenant).hash(ForemanNetbox::Params::TenantParams.new)
8
- optional(:ip_addresses).array(ForemanNetbox::Params::IpAddressParams.new)
9
- optional(:interfaces).array(ForemanNetbox::Params::InterfaceParams.new)
7
+ optional(:tenant).hash(ForemanNetbox::Params::TenantParams)
8
+ optional(:ip_addresses).array(ForemanNetbox::Params::IpAddressParams)
9
+ optional(:interfaces).array(ForemanNetbox::Params::InterfaceParams)
10
10
 
11
11
  required(:device).filled(:hash) do
12
12
  required(:name).filled(:string)
@@ -2,15 +2,13 @@
2
2
 
3
3
  module ForemanNetbox
4
4
  module Params
5
- class InterfaceParams < Dry::Schema::Params
6
- define do
7
- required(:name).filled(:string)
8
- required(:type).filled(:hash) do
9
- required(:value).filled(:string)
10
- end
11
- optional(:mac_address).maybe(:string)
12
- optional(:tags).array(:string)
5
+ InterfaceParams = Dry::Schema.Params do
6
+ required(:name).filled(:string)
7
+ required(:type).filled(:hash) do
8
+ required(:value).filled(:string)
13
9
  end
10
+ optional(:mac_address).maybe(:string)
11
+ optional(:tags).array(:string)
14
12
  end
15
13
  end
16
14
  end
@@ -2,14 +2,12 @@
2
2
 
3
3
  module ForemanNetbox
4
4
  module Params
5
- class IpAddressParams < Dry::Schema::Params
6
- define do
7
- required(:address).filled(:string)
8
- required(:interface).filled(:hash) do
9
- required(:name).filled(:string)
10
- end
11
- optional(:tags).array(:string)
5
+ IpAddressParams = Dry::Schema.Params do
6
+ required(:address).filled(:string)
7
+ required(:interface).filled(:hash) do
8
+ required(:name).filled(:string)
12
9
  end
10
+ optional(:tags).array(:string)
13
11
  end
14
12
  end
15
13
  end
@@ -2,12 +2,10 @@
2
2
 
3
3
  module ForemanNetbox
4
4
  module Params
5
- class TenantParams < Dry::Schema::Params
6
- define do
7
- required(:name).filled(:string)
8
- required(:slug).filled(:string)
9
- optional(:tags).array(:string)
10
- end
5
+ TenantParams = Dry::Schema.Params do
6
+ required(:name).filled(:string)
7
+ required(:slug).filled(:string)
8
+ optional(:tags).array(:string)
11
9
  end
12
10
  end
13
11
  end
@@ -3,9 +3,9 @@
3
3
  module ForemanNetbox
4
4
  class VirtualMachineContract < Dry::Validation::Contract
5
5
  params do
6
- optional(:tenant).hash(ForemanNetbox::Params::TenantParams.new)
7
- optional(:ip_addresses).array(ForemanNetbox::Params::IpAddressParams.new)
8
- optional(:interfaces).array(ForemanNetbox::Params::InterfaceParams.new)
6
+ optional(:tenant).hash(ForemanNetbox::Params::TenantParams)
7
+ optional(:ip_addresses).array(ForemanNetbox::Params::IpAddressParams)
8
+ optional(:interfaces).array(ForemanNetbox::Params::InterfaceParams)
9
9
 
10
10
  required(:virtual_machine).filled(:hash) do
11
11
  required(:name).filled(:string)
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ForemanNetbox
4
+ module Concerns
5
+ module AssignTags
6
+ delegate :tags, to: :context
7
+
8
+ def assign_tags_to(object)
9
+ current_tag_ids = object.tags.pluck('id')
10
+
11
+ return if (default_tag_ids - current_tag_ids).empty?
12
+
13
+ object.tags = current_tag_ids | default_tag_ids
14
+ end
15
+
16
+ def default_tag_ids
17
+ tags.map(&:id)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -5,6 +5,7 @@ module ForemanNetbox
5
5
  module SyncDevice
6
6
  class Create
7
7
  include ::Interactor
8
+ include ForemanNetbox::Concerns::AssignTags
8
9
 
9
10
  around do |interactor|
10
11
  interactor.call unless context.device
@@ -27,7 +28,8 @@ module ForemanNetbox
27
28
  device_type: device_type.id,
28
29
  device_role: device_role.id,
29
30
  site: site.id,
30
- tenant: tenant&.id
31
+ tenant: tenant&.id,
32
+ tags: default_tag_ids
31
33
  ).compact
32
34
  end
33
35
  end
@@ -15,6 +15,7 @@ module ForemanNetbox
15
15
  end
16
16
 
17
17
  organize SyncDevice::Validate,
18
+ SyncHost::SyncTags::Organizer,
18
19
  SyncHost::SyncTenant::Organizer,
19
20
  SyncDevice::SyncSite::Organizer,
20
21
  SyncDevice::SyncDeviceRole::Organizer,
@@ -21,7 +21,7 @@ module ForemanNetbox
21
21
  delegate :url, to: :netbox_facet, prefix: true, allow_nil: true
22
22
 
23
23
  def netbox_device_url
24
- "#{Setting::Netbox[:netbox_url]}/dcim/devices/#{device&.id}" if device&.id
24
+ "#{Setting[:netbox_url]}/dcim/devices/#{device&.id}" if device&.id
25
25
  end
26
26
  end
27
27
  end
@@ -6,6 +6,7 @@ module ForemanNetbox
6
6
  module SyncDeviceType
7
7
  class Create
8
8
  include ::Interactor
9
+ include ForemanNetbox::Concerns::AssignTags
9
10
 
10
11
  around do |interactor|
11
12
  interactor.call unless context.device_type
@@ -24,7 +25,7 @@ module ForemanNetbox
24
25
 
25
26
  def params
26
27
  netbox_params.fetch(:device_type)
27
- .merge(manufacturer: manufacturer.id)
28
+ .merge(manufacturer: manufacturer.id, tags: default_tag_ids)
28
29
  .compact
29
30
  end
30
31
  end
@@ -6,14 +6,14 @@ module ForemanNetbox
6
6
  module SyncDeviceType
7
7
  class Update
8
8
  include ::Interactor
9
+ include ForemanNetbox::Concerns::AssignTags
9
10
 
10
11
  around do |interactor|
11
12
  interactor.call if context.device_type
12
13
  end
13
14
 
14
15
  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?
16
+ assign_tags_to(device_type)
17
17
 
18
18
  device_type.save
19
19
  rescue NetboxClientRuby::LocalError, NetboxClientRuby::ClientError, NetboxClientRuby::RemoteError => e
@@ -6,6 +6,7 @@ module ForemanNetbox
6
6
  module SyncInterfaces
7
7
  class Create
8
8
  include ::Interactor
9
+ include ForemanNetbox::Concerns::AssignTags
9
10
 
10
11
  after do
11
12
  context.interfaces.reload
@@ -20,7 +21,8 @@ module ForemanNetbox
20
21
  new_interface.except(:type)
21
22
  .merge(
22
23
  type: new_interface.dig(:type, :value),
23
- device: device.id
24
+ device: device.id,
25
+ tags: default_tag_ids
24
26
  )
25
27
  ).save
26
28
  end
@@ -7,6 +7,7 @@ module ForemanNetbox
7
7
  module SyncIpAddresses
8
8
  class Create
9
9
  include ::Interactor
10
+ include ForemanNetbox::Concerns::AssignTags
10
11
 
11
12
  def call
12
13
  netbox_params.fetch(:ip_addresses, []).map do |ip_address|
@@ -21,7 +22,11 @@ module ForemanNetbox
21
22
  .zero?
22
23
 
23
24
  ForemanNetbox::API.client::IPAM::IpAddress.new(
24
- ip_address.slice(:address, :tags).merge(interface: interface_id)
25
+ ip_address.slice(:address)
26
+ .merge(
27
+ assigned_object_type: 'dcim.interface', assigned_object_id: interface_id,
28
+ tags: default_tag_ids
29
+ )
25
30
  ).save
26
31
  end
27
32
  rescue NetboxClientRuby::LocalError, NetboxClientRuby::ClientError, NetboxClientRuby::RemoteError => e
@@ -25,7 +25,7 @@ module ForemanNetbox
25
25
  .map { |i| i.fetch(:address) }
26
26
 
27
27
  context.ip_addresses
28
- .select { |ip| ip['interface']['id'] == netbox_interface.id }
28
+ .select { |ip| ip['assigned_object_type'] == 'dcim.interface' && ip['assigned_object_id'] == netbox_interface.id }
29
29
  .reject { |ip| host_interface_ips.include?(ip['address']) }
30
30
  .each(&:delete)
31
31
  end
@@ -7,12 +7,11 @@ module ForemanNetbox
7
7
  module SyncIpAddresses
8
8
  class Update
9
9
  include ::Interactor
10
+ include ForemanNetbox::Concerns::AssignTags
10
11
 
11
12
  def call
12
13
  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?
14
+ assign_tags_to(ip_address)
16
15
 
17
16
  ip_address.save
18
17
  end
@@ -20,17 +19,6 @@ module ForemanNetbox
20
19
  ::Foreman::Logging.logger('foreman_netbox/import').error("#{self.class} error #{e}: #{e.backtrace}")
21
20
  context.fail!(error: "#{self.class}: #{e}")
22
21
  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
22
  end
35
23
  end
36
24
  end
@@ -6,6 +6,7 @@ module ForemanNetbox
6
6
  module SyncInterfaces
7
7
  class Update
8
8
  include ::Interactor
9
+ include ForemanNetbox::Concerns::AssignTags
9
10
 
10
11
  def call
11
12
  context.interfaces.each { |netbox_interface| update(netbox_interface) }
@@ -22,9 +23,7 @@ module ForemanNetbox
22
23
  return unless new_params
23
24
 
24
25
  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?
26
+ assign_tags_to(netbox_interface)
28
27
 
29
28
  netbox_interface.save
30
29
  rescue NetboxClientRuby::LocalError, NetboxClientRuby::ClientError, NetboxClientRuby::RemoteError => e
@@ -6,6 +6,7 @@ module ForemanNetbox
6
6
  module SyncSite
7
7
  class Create
8
8
  include ::Interactor
9
+ include ForemanNetbox::Concerns::AssignTags
9
10
 
10
11
  around do |interactor|
11
12
  interactor.call unless context.site
@@ -23,7 +24,9 @@ module ForemanNetbox
23
24
  delegate :netbox_params, to: :context
24
25
 
25
26
  def params
26
- netbox_params.fetch(:site).compact
27
+ netbox_params.fetch(:site)
28
+ .merge(tags: default_tag_ids)
29
+ .compact
27
30
  end
28
31
  end
29
32
  end
@@ -6,14 +6,14 @@ module ForemanNetbox
6
6
  module SyncSite
7
7
  class Update
8
8
  include ::Interactor
9
+ include ForemanNetbox::Concerns::AssignTags
9
10
 
10
11
  around do |interactor|
11
12
  interactor.call if context.site
12
13
  end
13
14
 
14
15
  def call
15
- new_tags = new_site_params.fetch(:tags, []) - site.tags
16
- site.tags = (site.tags | new_tags) if new_tags.any?
16
+ assign_tags_to(site)
17
17
 
18
18
  site.save
19
19
  rescue NetboxClientRuby::LocalError, NetboxClientRuby::ClientError, NetboxClientRuby::RemoteError => e
@@ -21,13 +21,7 @@ module ForemanNetbox
21
21
  context.fail!(error: "#{self.class}: #{e}")
22
22
  end
23
23
 
24
- private
25
-
26
- delegate :netbox_params, :site, to: :context
27
-
28
- def new_site_params
29
- netbox_params.fetch(:site, {})
30
- end
24
+ delegate :site, to: :context
31
25
  end
32
26
  end
33
27
  end
@@ -6,8 +6,9 @@ module ForemanNetbox
6
6
  class Update
7
7
  include ::Interactor
8
8
  include ForemanNetbox::Concerns::PrimaryIps
9
+ include ForemanNetbox::Concerns::AssignTags
9
10
 
10
- ATTRIBUTES = %i[name device_role device_type primary_ip4 primary_ip6 site tenant serial tags].freeze
11
+ ATTRIBUTES = %i[name device_role device_type primary_ip4 primary_ip6 site tenant serial].freeze
11
12
 
12
13
  around do |interactor|
13
14
  interactor.call if context.device
@@ -19,6 +20,7 @@ module ForemanNetbox
19
20
 
20
21
  def call
21
22
  assign_new_attributes
23
+ assign_tags_to(device)
22
24
 
23
25
  device.save
24
26
  rescue NetboxClientRuby::LocalError, NetboxClientRuby::ClientError, NetboxClientRuby::RemoteError => e
@@ -60,6 +62,8 @@ module ForemanNetbox
60
62
  end
61
63
 
62
64
  def assign_site
65
+ return if device.site&.id && Setting[:netbox_skip_site_update]
66
+
63
67
  device.site = site&.id if device.site&.id != site&.id
64
68
  end
65
69
 
@@ -73,12 +77,6 @@ module ForemanNetbox
73
77
 
74
78
  device.serial = new_serial
75
79
  end
76
-
77
- def assign_tags
78
- new_tags = new_device_params.fetch(:tags, []) - device.tags
79
-
80
- device.tags = (device.tags | new_tags) if new_tags.any?
81
- end
82
80
  end
83
81
  end
84
82
  end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ForemanNetbox
4
+ module SyncHost
5
+ module SyncTags
6
+ class Create
7
+ include ::Interactor
8
+
9
+ def call
10
+ context.tags.push(*new_tags)
11
+ end
12
+
13
+ private
14
+
15
+ def new_tags
16
+ SyncTags::Organizer::DEFAULT_TAGS
17
+ .reject { |params| existing_slugs.include?(params[:slug]) }
18
+ .map { |params| ForemanNetbox::API.client::Extras::Tag.new(params).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
+ def existing_slugs
25
+ @existing_slugs ||= context.tags.pluck(:slug)
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 SyncTags
6
+ class Find
7
+ include ::Interactor
8
+
9
+ def call
10
+ context.tags = slugs.map do |slug|
11
+ ForemanNetbox::API.client.extras.tags.find_by(slug: slug)
12
+ end.compact
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
+ def slugs
21
+ SyncTags::Organizer::DEFAULT_TAGS.pluck(:slug)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ForemanNetbox
4
+ module SyncHost
5
+ module SyncTags
6
+ class Organizer
7
+ include ::Interactor::Organizer
8
+
9
+ DEFAULT_TAGS = [
10
+ { name: 'foreman', slug: 'foreman' }
11
+ ].freeze
12
+
13
+ after do
14
+ context.raw_data[:tags] = context.tags.map(&:raw_data!)
15
+ end
16
+
17
+ organize SyncTags::Find,
18
+ SyncTags::Create
19
+ end
20
+ end
21
+ end
22
+ end
@@ -5,6 +5,7 @@ module ForemanNetbox
5
5
  module SyncTenant
6
6
  class Create
7
7
  include ::Interactor
8
+ include ForemanNetbox::Concerns::AssignTags
8
9
 
9
10
  around do |interactor|
10
11
  interactor.call unless context.tenant
@@ -22,7 +23,9 @@ module ForemanNetbox
22
23
  delegate :netbox_params, to: :context
23
24
 
24
25
  def params
25
- netbox_params.fetch(:tenant).compact
26
+ netbox_params.fetch(:tenant)
27
+ .merge(tags: default_tag_ids)
28
+ .compact
26
29
  end
27
30
  end
28
31
  end
@@ -5,14 +5,14 @@ module ForemanNetbox
5
5
  module SyncTenant
6
6
  class Update
7
7
  include ::Interactor
8
+ include ForemanNetbox::Concerns::AssignTags
8
9
 
9
10
  around do |interactor|
10
11
  interactor.call if context.tenant
11
12
  end
12
13
 
13
14
  def call
14
- new_tags = new_tenant_params.fetch(:tags, []) - tenant.tags
15
- tenant.tags = (tenant.tags | new_tags) if new_tags.any?
15
+ assign_tags_to(tenant)
16
16
 
17
17
  tenant.save
18
18
  rescue NetboxClientRuby::LocalError, NetboxClientRuby::ClientError, NetboxClientRuby::RemoteError => e
@@ -20,13 +20,7 @@ module ForemanNetbox
20
20
  context.fail!(error: "#{self.class}: #{e}")
21
21
  end
22
22
 
23
- private
24
-
25
- delegate :netbox_params, :tenant, to: :context
26
-
27
- def new_tenant_params
28
- netbox_params.fetch(:tenant, {})
29
- end
23
+ delegate :tenant, to: :context
30
24
  end
31
25
  end
32
26
  end
@@ -5,6 +5,7 @@ module ForemanNetbox
5
5
  module SyncVirtualMachine
6
6
  class Create
7
7
  include ::Interactor
8
+ include ForemanNetbox::Concerns::AssignTags
8
9
 
9
10
  around do |interactor|
10
11
  interactor.call unless context.virtual_machine
@@ -25,7 +26,8 @@ module ForemanNetbox
25
26
  def params
26
27
  netbox_params.fetch(:virtual_machine).merge(
27
28
  cluster: cluster.id,
28
- tenant: tenant&.id
29
+ tenant: tenant&.id,
30
+ tags: default_tag_ids
29
31
  ).compact
30
32
  end
31
33
  end
@@ -15,6 +15,7 @@ module ForemanNetbox
15
15
  end
16
16
 
17
17
  organize SyncVirtualMachine::Validate,
18
+ SyncHost::SyncTags::Organizer,
18
19
  SyncHost::SyncTenant::Organizer,
19
20
  SyncVirtualMachine::SyncCluster::Organizer,
20
21
  SyncVirtualMachine::Find,
@@ -23,7 +23,7 @@ module ForemanNetbox
23
23
  def netbox_virtual_machine_url
24
24
  return unless context.virtual_machine&.id
25
25
 
26
- "#{Setting::Netbox[:netbox_url]}/virtualization/virtual-machines/#{context.virtual_machine&.id}"
26
+ "#{Setting[:netbox_url]}/virtualization/virtual-machines/#{context.virtual_machine&.id}"
27
27
  end
28
28
  end
29
29
  end
@@ -6,6 +6,7 @@ module ForemanNetbox
6
6
  module SyncCluster
7
7
  class Create
8
8
  include ::Interactor
9
+ include ForemanNetbox::Concerns::AssignTags
9
10
 
10
11
  around do |interactor|
11
12
  interactor.call unless context.cluster
@@ -25,7 +26,8 @@ module ForemanNetbox
25
26
 
26
27
  def params
27
28
  netbox_params.fetch(:cluster).merge(
28
- type: cluster_type&.id
29
+ type: cluster_type&.id,
30
+ tags: default_tag_ids
29
31
  ).compact
30
32
  end
31
33
  end
@@ -6,14 +6,14 @@ module ForemanNetbox
6
6
  module SyncCluster
7
7
  class Update
8
8
  include ::Interactor
9
+ include ForemanNetbox::Concerns::AssignTags
9
10
 
10
11
  around do |interactor|
11
12
  interactor.call if context.cluster
12
13
  end
13
14
 
14
15
  def call
15
- new_tags = new_cluster_params.fetch(:tags, []) - cluster.tags
16
- cluster.tags = (cluster.tags | new_tags) if new_tags.any?
16
+ assign_tags_to(cluster)
17
17
 
18
18
  cluster.save
19
19
  rescue NetboxClientRuby::LocalError, NetboxClientRuby::ClientError, NetboxClientRuby::RemoteError => e
@@ -21,13 +21,7 @@ module ForemanNetbox
21
21
  context.fail!(error: "#{self.class}: #{e}")
22
22
  end
23
23
 
24
- private
25
-
26
- delegate :netbox_params, :cluster, to: :context
27
-
28
- def new_cluster_params
29
- netbox_params.fetch(:cluster, {})
30
- end
24
+ delegate :cluster, to: :context
31
25
  end
32
26
  end
33
27
  end
@@ -6,6 +6,7 @@ module ForemanNetbox
6
6
  module SyncInterfaces
7
7
  class Create
8
8
  include ::Interactor
9
+ include ForemanNetbox::Concerns::AssignTags
9
10
 
10
11
  after do
11
12
  context.interfaces.reload
@@ -17,7 +18,11 @@ module ForemanNetbox
17
18
  .reject { |i| interfaces.map(&:name).include?(i[:name]) }
18
19
  .map do |new_interface|
19
20
  ForemanNetbox::API.client::Virtualization::Interface.new(
20
- new_interface.except(:type).merge(virtual_machine: virtual_machine.id)
21
+ new_interface.except(:type)
22
+ .merge(
23
+ virtual_machine: virtual_machine.id,
24
+ tags: default_tag_ids
25
+ )
21
26
  ).save
22
27
  end
23
28
  rescue NetboxClientRuby::LocalError, NetboxClientRuby::ClientError, NetboxClientRuby::RemoteError => e