foreman_netbox 1.0.2 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +7 -0
  3. data/app/interactors/foreman_netbox/concerns/assign_tags.rb +21 -0
  4. data/app/interactors/foreman_netbox/sync_host/sync_device/create.rb +3 -1
  5. data/app/interactors/foreman_netbox/sync_host/sync_device/organizer.rb +1 -0
  6. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/create.rb +2 -1
  7. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/update.rb +2 -2
  8. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/create.rb +3 -1
  9. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/create.rb +6 -1
  10. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/delete.rb +1 -1
  11. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/update.rb +2 -14
  12. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/update.rb +2 -3
  13. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_site/create.rb +4 -1
  14. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_site/update.rb +3 -9
  15. data/app/interactors/foreman_netbox/sync_host/sync_device/update.rb +3 -7
  16. data/app/interactors/foreman_netbox/sync_host/sync_tags/create.rb +30 -0
  17. data/app/interactors/foreman_netbox/sync_host/sync_tags/find.rb +26 -0
  18. data/app/interactors/foreman_netbox/sync_host/sync_tags/organizer.rb +22 -0
  19. data/app/interactors/foreman_netbox/sync_host/sync_tenant/create.rb +4 -1
  20. data/app/interactors/foreman_netbox/sync_host/sync_tenant/update.rb +3 -9
  21. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/create.rb +3 -1
  22. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/organizer.rb +1 -0
  23. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/create.rb +3 -1
  24. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/update.rb +3 -9
  25. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/create.rb +6 -1
  26. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/create.rb +7 -2
  27. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/delete.rb +1 -1
  28. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/update.rb +2 -14
  29. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/update.rb +2 -3
  30. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/update.rb +3 -7
  31. data/app/models/concerns/foreman_netbox/user_usergroup_common_extensions.rb +1 -1
  32. data/app/models/orchestration/netbox.rb +3 -0
  33. data/app/services/foreman_netbox/cached_netbox_parameters.rb +8 -8
  34. data/app/services/foreman_netbox/netbox_parameters.rb +8 -17
  35. data/app/services/foreman_netbox/netbox_parameters_comparator.rb +2 -2
  36. data/lib/foreman_netbox/version.rb +1 -1
  37. data/test/integration/foreman_netbox/sync_k8s_physical_host_test.rb +12 -8
  38. data/test/integration/foreman_netbox/sync_rhel_physical_host_test.rb +12 -8
  39. data/test/integration/foreman_netbox/sync_rhel_virtual_host_test.rb +12 -8
  40. data/test/interactors/foreman_netbox/sync_host/organizer_test.rb +18 -2
  41. data/test/interactors/foreman_netbox/sync_host/sync_device/create_test.rb +5 -3
  42. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/create_test.rb +4 -3
  43. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/update_test.rb +8 -3
  44. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/create_test.rb +8 -4
  45. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/create_test.rb +10 -5
  46. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/delete_test.rb +2 -2
  47. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/update_test.rb +5 -3
  48. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/update_test.rb +7 -5
  49. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_site/create_test.rb +2 -2
  50. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_site/update_test.rb +4 -3
  51. data/test/interactors/foreman_netbox/sync_host/sync_device/update_test.rb +19 -5
  52. data/test/interactors/foreman_netbox/sync_host/sync_tenant/create_test.rb +2 -2
  53. data/test/interactors/foreman_netbox/sync_host/sync_tenant/update_test.rb +5 -3
  54. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/create_test.rb +4 -3
  55. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/create_test.rb +7 -3
  56. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/update_test.rb +5 -3
  57. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/create_test.rb +5 -4
  58. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/create_test.rb +9 -7
  59. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/delete_test.rb +2 -2
  60. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/update_test.rb +13 -4
  61. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/update_test.rb +9 -4
  62. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/update_test.rb +7 -4
  63. data/test/models/foreman_netbox/usergroup_test.rb +4 -4
  64. data/test/services/foreman_netbox/netbox_attributes_test.rb +10 -19
  65. data/test/services/foreman_netbox/netbox_parameters_comparator_test.rb +6 -16
  66. data/test/test_plugin_helper.rb +15 -3
  67. metadata +127 -53
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e5c0bba2855f92b79b87545d69196dc2556311d59f7e2d90bfb1ea7acfe2a55f
4
- data.tar.gz: 8847d6add790eedfcb2703a3db61acf2645eb1a60c37fd66e4614ac4bbb85917
3
+ metadata.gz: 9df549aa83fb9c91e18bba3c2690cb6f9c14977ca2364198b12b20cc01140069
4
+ data.tar.gz: 0adf0c06724a964efa56d7ca8b294ca73882c351a50c8063a1906f4ad3a429ac
5
5
  SHA512:
6
- metadata.gz: cc82ec6557890e5457f9471e98560cbf9f12e4074752bbff7cc8e21f5cf8f34b0288150a1dedec75fd99c3d1b649ef943e74c6b633dd09548643d950dedcb75f
7
- data.tar.gz: c7907c045cb7cc50ac25d098276c4666301aecf492802714297a08dfe12bf488f72c7a6bb7e5d1b31531b9d0025a9d979f282f7083d0ae04b3c2c50bc755ef77
6
+ metadata.gz: dcd1893a719badc47fe8d083a46fa5ee403bc9f1c31a0963d05624c34f00dad74a8320cdaaf0917e6fc484c899f62fe56db277642e62ba240ef471d1214bd67c
7
+ data.tar.gz: 1e3e688b369849e00c54440e4de52507bdefd30109d4688fc478b22a1ceaa751593859be6fbc8480ce60a6ee0050f0ab783dbfc721df428d93aa3bfd44feb7de
data/README.md CHANGED
@@ -4,6 +4,13 @@
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
+
7
14
  ## Installation
8
15
 
9
16
  See [How_to_Install_a_Plugin](https://theforeman.org/manuals/2.3/index.html#6.1InstallaPlugin)
@@ -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,
@@ -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
@@ -73,12 +75,6 @@ module ForemanNetbox
73
75
 
74
76
  device.serial = new_serial
75
77
  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
78
  end
83
79
  end
84
80
  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,
@@ -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
@@ -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|
@@ -16,12 +17,16 @@ module ForemanNetbox
16
17
  next unless ForemanNetbox::API.client
17
18
  .ipam
18
19
  .ip_addresses
19
- .filter(interface_id: interface_id, address: ip_address[:address])
20
+ .filter(vminterface_id: interface_id, address: ip_address[:address])
20
21
  .total
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: 'virtualization.vminterface', 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 { |ip| ip.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'] == 'virtualization.vminterface' && 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,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 cluster disk memory primary_ip4 primary_ip6 tenant vcpus tags].freeze
11
+ ATTRIBUTES = %i[name cluster disk memory primary_ip4 primary_ip6 tenant vcpus].freeze
11
12
 
12
13
  around do |interactor|
13
14
  interactor.call if context.virtual_machine
@@ -19,6 +20,7 @@ module ForemanNetbox
19
20
 
20
21
  def call
21
22
  assign_new_attributes
23
+ assign_tags_to(virtual_machine)
22
24
 
23
25
  virtual_machine.save
24
26
  rescue NetboxClientRuby::LocalError, NetboxClientRuby::ClientError, NetboxClientRuby::RemoteError => e
@@ -70,12 +72,6 @@ module ForemanNetbox
70
72
  vcpus = netbox_params.dig(:virtual_machine, :vcpus)
71
73
  virtual_machine.vcpus = vcpus if virtual_machine.vcpus != vcpus
72
74
  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
75
  end
80
76
  end
81
77
  end