foreman_netbox 1.0.2 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
@@ -12,7 +12,8 @@ class UpdateDeviceTest < ActiveSupport::TestCase
12
12
  device_type: device_type,
13
13
  site: site,
14
14
  tenant: tenant,
15
- ip_addresses: ip_addresses
15
+ ip_addresses: ip_addresses,
16
+ tags: default_tags
16
17
  )
17
18
  end
18
19
 
@@ -44,7 +45,7 @@ class UpdateDeviceTest < ActiveSupport::TestCase
44
45
  end
45
46
  end
46
47
  let(:device_name) { 'name.example.com' }
47
- let(:device_tags) { ['tag'] }
48
+ let(:device_tags) { [] }
48
49
  let(:device_data) { device.instance_variable_get(:@data).deep_symbolize_keys }
49
50
  let(:device_role) { OpenStruct.new(id: device_data.dig(:device_role, :id)) }
50
51
  let(:device_type) { OpenStruct.new(id: device_data.dig(:device_type, :id)) }
@@ -96,9 +97,22 @@ class UpdateDeviceTest < ActiveSupport::TestCase
96
97
  end
97
98
 
98
99
  context 'if the host has not been updated since the last synchronization' do
99
- let(:device_tags) { ForemanNetbox::NetboxParameters::DEFAULT_TAGS }
100
+ let(:device_tags) do
101
+ default_tags.map { |t| { 'id' => t.id, 'name' => t.name, 'slug' => t.slug } }
102
+ end
100
103
 
101
104
  it 'does not update device' do
105
+ device_tags.each do |t|
106
+ stub_request(:get, "#{Setting[:netbox_url]}/api/extras/tags/#{t['id']}.json")
107
+ .to_return(
108
+ status: 200, headers: { 'Content-Type': 'application/json' },
109
+ body: {
110
+ id: t['id'],
111
+ name: t['name'],
112
+ slug: t['slug']
113
+ }.to_json
114
+ )
115
+ end
102
116
  stub_patch = stub_request(:patch, "#{Setting[:netbox_url]}/api/dcim/devices/#{device.id}.json")
103
117
 
104
118
  assert_equal device, subject.device
@@ -149,7 +163,7 @@ class UpdateDeviceTest < ActiveSupport::TestCase
149
163
  site: site.id,
150
164
  tenant: tenant.id,
151
165
  serial: serialnumber,
152
- tags: device_tags | ForemanNetbox::NetboxParameters::DEFAULT_TAGS
166
+ tags: default_tags.map(&:id)
153
167
  }.to_json
154
168
  ).to_return(
155
169
  status: 200, headers: { 'Content-Type': 'application/json' },
@@ -173,7 +187,7 @@ class UpdateDeviceTest < ActiveSupport::TestCase
173
187
  primary_ip6: primary_ip6.id,
174
188
  site: site.id,
175
189
  tenant: tenant.id,
176
- tags: device_tags | ForemanNetbox::NetboxParameters::DEFAULT_TAGS
190
+ tags: default_tags.map(&:id)
177
191
  }.to_json
178
192
  ).to_return(
179
193
  status: 200, headers: { 'Content-Type': 'application/json' },
@@ -5,7 +5,7 @@ require 'test_plugin_helper'
5
5
  class CreateTenantTest < ActiveSupport::TestCase
6
6
  subject do
7
7
  ForemanNetbox::SyncHost::SyncTenant::Create.call(
8
- host: host, tenant: tenant, netbox_params: host.netbox_facet.netbox_params
8
+ host: host, tenant: tenant, netbox_params: host.netbox_facet.netbox_params, tags: default_tags
9
9
  )
10
10
  end
11
11
 
@@ -28,7 +28,7 @@ class CreateTenantTest < ActiveSupport::TestCase
28
28
  body: {
29
29
  name: host.owner.name,
30
30
  slug: host.owner.name.parameterize,
31
- tags: ForemanNetbox::NetboxParameters::DEFAULT_TAGS
31
+ tags: default_tags.map(&:id)
32
32
  }.to_json
33
33
  ).to_return(
34
34
  status: 201, headers: { 'Content-Type': 'application/json' },
@@ -5,7 +5,7 @@ require 'test_plugin_helper'
5
5
  class UpdateTenantTest < ActiveSupport::TestCase
6
6
  subject do
7
7
  ForemanNetbox::SyncHost::SyncTenant::Update.call(
8
- host: host, tenant: tenant, netbox_params: host.netbox_facet.netbox_params
8
+ host: host, tenant: tenant, netbox_params: host.netbox_facet.netbox_params, tags: default_tags
9
9
  )
10
10
  end
11
11
 
@@ -34,7 +34,7 @@ class UpdateTenantTest < ActiveSupport::TestCase
34
34
  it 'updates tenant' do
35
35
  stub_patch = stub_request(:patch, "#{Setting[:netbox_url]}/api/tenancy/tenants/1.json").with(
36
36
  body: {
37
- tags: ForemanNetbox::NetboxParameters::DEFAULT_TAGS
37
+ tags: default_tags.map(&:id)
38
38
  }.to_json
39
39
  ).to_return(
40
40
  status: 200, headers: { 'Content-Type': 'application/json' },
@@ -47,7 +47,9 @@ class UpdateTenantTest < ActiveSupport::TestCase
47
47
  end
48
48
 
49
49
  context 'when unchanged' do
50
- let(:tenant_tags) { ForemanNetbox::NetboxParameters::DEFAULT_TAGS }
50
+ let(:tenant_tags) do
51
+ default_tags.map { |t| { 'id' => t.id, 'name' => t.name, 'slug' => t.slug } }
52
+ end
51
53
 
52
54
  it 'does not update tenant' do
53
55
  stub_patch = stub_request(:patch, "#{Setting[:netbox_url]}/api/tenancy/tenants/1.json")
@@ -9,7 +9,8 @@ class CreateVirtualMachineTest < ActiveSupport::TestCase
9
9
  netbox_params: host.netbox_facet.netbox_params,
10
10
  virtual_machine: virtual_machine,
11
11
  cluster: cluster,
12
- tenant: tenant
12
+ tenant: tenant,
13
+ tags: default_tags
13
14
  )
14
15
  end
15
16
 
@@ -50,9 +51,9 @@ class CreateVirtualMachineTest < ActiveSupport::TestCase
50
51
  memory: netbox_virtual_machine_params[:memory],
51
52
  disk: netbox_virtual_machine_params[:disk],
52
53
  name: netbox_virtual_machine_params[:name],
53
- tags: netbox_virtual_machine_params[:tags],
54
54
  cluster: cluster.id,
55
- tenant: tenant.id
55
+ tenant: tenant.id,
56
+ tags: default_tags.map(&:id)
56
57
  }.to_json
57
58
  ).to_return(
58
59
  status: 201, headers: { 'Content-Type': 'application/json' },
@@ -5,7 +5,11 @@ require 'test_plugin_helper'
5
5
  class CreateClusterTest < ActiveSupport::TestCase
6
6
  subject do
7
7
  ForemanNetbox::SyncHost::SyncVirtualMachine::SyncCluster::Create.call(
8
- host: host, cluster_type: cluster_type, cluster: cluster, netbox_params: host.netbox_facet.netbox_params
8
+ host: host,
9
+ cluster_type: cluster_type,
10
+ cluster: cluster,
11
+ netbox_params: host.netbox_facet.netbox_params,
12
+ tags: default_tags
9
13
  )
10
14
  end
11
15
 
@@ -31,8 +35,8 @@ class CreateClusterTest < ActiveSupport::TestCase
31
35
  stub_post = stub_request(:post, "#{Setting[:netbox_url]}/api/virtualization/clusters/").with(
32
36
  body: {
33
37
  name: host.netbox_facet.netbox_params.dig(:cluster, :name),
34
- tags: host.netbox_facet.netbox_params.dig(:cluster, :tags),
35
- type: cluster_type.id
38
+ type: cluster_type.id,
39
+ tags: default_tags.map(&:id)
36
40
  }.to_json
37
41
  ).to_return(
38
42
  status: 201, headers: { 'Content-Type': 'application/json' },
@@ -5,7 +5,7 @@ require 'test_plugin_helper'
5
5
  class UpdateClusterTest < ActiveSupport::TestCase
6
6
  subject do
7
7
  ForemanNetbox::SyncHost::SyncVirtualMachine::SyncCluster::Update.call(
8
- host: host, cluster: cluster, netbox_params: host.netbox_facet.netbox_params
8
+ host: host, cluster: cluster, netbox_params: host.netbox_facet.netbox_params, tags: default_tags
9
9
  )
10
10
  end
11
11
 
@@ -29,7 +29,7 @@ class UpdateClusterTest < ActiveSupport::TestCase
29
29
  it 'updates cluster' do
30
30
  stub_patch = stub_request(:patch, "#{Setting[:netbox_url]}/api/virtualization/clusters/1.json").with(
31
31
  body: {
32
- tags: host.netbox_facet.netbox_params.dig(:cluster, :tags)
32
+ tags: default_tags.map(&:id)
33
33
  }.to_json
34
34
  ).to_return(
35
35
  status: 200, headers: { 'Content-Type': 'application/json' },
@@ -42,7 +42,9 @@ class UpdateClusterTest < ActiveSupport::TestCase
42
42
  end
43
43
 
44
44
  context 'when unchanged' do
45
- let(:cluster_tags) { host.netbox_facet.netbox_params.dig(:cluster, :tags) }
45
+ let(:cluster_tags) do
46
+ default_tags.map { |t| { 'id' => t.id, 'name' => t.name, 'slug' => t.slug } }
47
+ end
46
48
 
47
49
  it 'does not update cluster' do
48
50
  stub_patch = stub_request(:patch, "#{Setting[:netbox_url]}/api/virtualization/clusters/1.json")
@@ -8,7 +8,8 @@ class CreateVirtualMachineInterfacesTest < ActiveSupport::TestCase
8
8
  host: host,
9
9
  netbox_params: host.netbox_facet.netbox_params,
10
10
  virtual_machine: virtual_machine,
11
- interfaces: interfaces
11
+ interfaces: interfaces,
12
+ tags: default_tags
12
13
  )
13
14
  end
14
15
 
@@ -35,9 +36,9 @@ class CreateVirtualMachineInterfacesTest < ActiveSupport::TestCase
35
36
  stub_post = stub_request(:post, "#{Setting[:netbox_url]}/api/virtualization/interfaces/").with(
36
37
  body: {
37
38
  name: host.interfaces.first.netbox_name,
38
- mac_address: host.interfaces.first.mac,
39
- tags: ForemanNetbox::NetboxParameters::DEFAULT_TAGS,
40
- virtual_machine: virtual_machine.id
39
+ mac_address: host.interfaces.first.mac.upcase,
40
+ virtual_machine: virtual_machine.id,
41
+ tags: default_tags.map(&:id)
41
42
  }.to_json
42
43
  ).to_return(
43
44
  status: 201, headers: { 'Content-Type': 'application/json' },
@@ -5,7 +5,7 @@ require 'test_plugin_helper'
5
5
  class CreateVirtualMachineIpAddressesTest < ActiveSupport::TestCase
6
6
  subject do
7
7
  ForemanNetbox::SyncHost::SyncVirtualMachine::SyncInterfaces::SyncIpAddresses::Create.call(
8
- host: host, interfaces: interfaces, netbox_params: host.netbox_facet.netbox_params
8
+ host: host, interfaces: interfaces, netbox_params: host.netbox_facet.netbox_params, tags: default_tags
9
9
  )
10
10
  end
11
11
 
@@ -31,7 +31,7 @@ class CreateVirtualMachineIpAddressesTest < ActiveSupport::TestCase
31
31
  setup do
32
32
  setup_default_netbox_settings
33
33
  stub_request(:get, "#{Setting[:netbox_url]}/api/ipam/ip-addresses.json").with(
34
- query: { limit: 50, interface_id: interfaces.first.id, address: host.interfaces.first.netbox_ip }
34
+ query: { limit: 50, vminterface_id: interfaces.first.id, address: host.interfaces.first.netbox_ip }
35
35
  ).to_return(
36
36
  status: 200, headers: { 'Content-Type': 'application/json' },
37
37
  body: {
@@ -40,7 +40,7 @@ class CreateVirtualMachineIpAddressesTest < ActiveSupport::TestCase
40
40
  }.to_json
41
41
  )
42
42
  stub_request(:get, "#{Setting[:netbox_url]}/api/ipam/ip-addresses.json").with(
43
- query: { limit: 50, interface_id: interfaces.first.id, address: host.interfaces.first.netbox_ip6 }
43
+ query: { limit: 50, vminterface_id: interfaces.first.id, address: host.interfaces.first.netbox_ip6 }
44
44
  ).to_return(
45
45
  status: 200, headers: { 'Content-Type': 'application/json' },
46
46
  body: {
@@ -54,8 +54,9 @@ class CreateVirtualMachineIpAddressesTest < ActiveSupport::TestCase
54
54
  stub_post_ip_address_v4 = stub_request(:post, "#{Setting[:netbox_url]}/api/ipam/ip-addresses/").with(
55
55
  body: {
56
56
  address: host.interfaces.first.netbox_ip,
57
- tags: ForemanNetbox::NetboxParameters::DEFAULT_TAGS,
58
- interface: interfaces.first.id
57
+ assigned_object_type: 'virtualization.vminterface',
58
+ assigned_object_id: interfaces.first.id,
59
+ tags: default_tags.map(&:id)
59
60
  }.to_json
60
61
  ).to_return(
61
62
  status: 201, headers: { 'Content-Type': 'application/json' },
@@ -65,8 +66,9 @@ class CreateVirtualMachineIpAddressesTest < ActiveSupport::TestCase
65
66
  stub_post_ip_address_v6 = stub_request(:post, "#{Setting[:netbox_url]}/api/ipam/ip-addresses/").with(
66
67
  body: {
67
68
  address: host.interfaces.first.netbox_ip6,
68
- tags: ForemanNetbox::NetboxParameters::DEFAULT_TAGS,
69
- interface: interfaces.first.id
69
+ assigned_object_type: 'virtualization.vminterface',
70
+ assigned_object_id: interfaces.first.id,
71
+ tags: default_tags.map(&:id)
70
72
  }.to_json
71
73
  ).to_return(
72
74
  status: 201, headers: { 'Content-Type': 'application/json' },
@@ -51,8 +51,8 @@ class DeleteVirtualMachineIpAddressesTest < ActiveSupport::TestCase
51
51
  body: {
52
52
  count: 2,
53
53
  results: [
54
- { id: ip_addresses_v4_id, address: ip_addresses_v4, interface: { id: interface_id } },
55
- { id: ip_addresses_v6_id, address: ip_addresses_v6, interface: { id: interface_id } }
54
+ { id: ip_addresses_v4_id, address: ip_addresses_v4, assigned_object_type: 'virtualization.vminterface', assigned_object_id: interface_id },
55
+ { id: ip_addresses_v6_id, address: ip_addresses_v6, assigned_object_type: 'virtualization.vminterface', assigned_object_id: interface_id }
56
56
  ]
57
57
  }.to_json
58
58
  )
@@ -6,15 +6,24 @@ class UpdateVirtualMachineIpAddressesTest < ActiveSupport::TestCase
6
6
  subject do
7
7
  ForemanNetbox::SyncHost::SyncVirtualMachine::SyncInterfaces::SyncIpAddresses::Update.call(
8
8
  ip_addresses: ip_addresses,
9
- netbox_params: host.netbox_facet.netbox_params
9
+ netbox_params: host.netbox_facet.netbox_params,
10
+ tags: default_tags
10
11
  )
11
12
  end
12
13
 
13
14
  let(:ip_addresses) { ForemanNetbox::API.client.ipam.ip_addresses.filter(virtual_machine_id: 1) }
14
15
  let(:ip_addresses_data) do
15
16
  [
16
- { id: 1, address: host.netbox_facet.netbox_params[:ip_addresses].first[:address], tags: ForemanNetbox::NetboxParameters::DEFAULT_TAGS },
17
- { id: 2, address: host.netbox_facet.netbox_params[:ip_addresses].second[:address], tags: [] }
17
+ {
18
+ id: 1,
19
+ address: host.netbox_facet.netbox_params[:ip_addresses].first[:address],
20
+ tags: default_tags.map { |t| { id: t.id, name: t.name, slug: t.slug } }
21
+ },
22
+ {
23
+ id: 2,
24
+ address: host.netbox_facet.netbox_params[:ip_addresses].second[:address],
25
+ tags: []
26
+ }
18
27
  ]
19
28
  end
20
29
  let(:host) do
@@ -50,7 +59,7 @@ class UpdateVirtualMachineIpAddressesTest < ActiveSupport::TestCase
50
59
  stub_unexpected_patch = stub_request(:patch, "#{Setting[:netbox_url]}/api/ipam/ip-addresses/#{ip_addresses_data.first[:id]}.json")
51
60
  stub_expected_patch = stub_request(:patch, "#{Setting[:netbox_url]}/api/ipam/ip-addresses/#{ip_addresses_data.second[:id]}.json").with(
52
61
  body: {
53
- tags: ForemanNetbox::NetboxParameters::DEFAULT_TAGS
62
+ tags: default_tags.map(&:id)
54
63
  }.to_json
55
64
  ).to_return(
56
65
  status: 200, headers: { 'Content-Type': 'application/json' },
@@ -7,7 +7,8 @@ class UpdateVirtualMachineInterfacesTest < ActiveSupport::TestCase
7
7
  ForemanNetbox::SyncHost::SyncVirtualMachine::SyncInterfaces::Update.call(
8
8
  interfaces: interfaces,
9
9
  host: host,
10
- netbox_params: host.netbox_facet.netbox_params
10
+ netbox_params: host.netbox_facet.netbox_params,
11
+ tags: default_tags
11
12
  )
12
13
  end
13
14
 
@@ -48,8 +49,8 @@ class UpdateVirtualMachineInterfacesTest < ActiveSupport::TestCase
48
49
  )
49
50
  stub_patch = stub_request(:patch, "#{Setting[:netbox_url]}/api/virtualization/interfaces/1.json").with(
50
51
  body: {
51
- mac_address: host.interfaces.first.mac,
52
- tags: ForemanNetbox::NetboxParameters::DEFAULT_TAGS
52
+ mac_address: host.interfaces.first.mac.upcase,
53
+ tags: default_tags.map(&:id)
53
54
  }.to_json
54
55
  ).to_return(
55
56
  status: 200, headers: { 'Content-Type': 'application/json' },
@@ -75,14 +76,18 @@ class UpdateVirtualMachineInterfacesTest < ActiveSupport::TestCase
75
76
  id: 1,
76
77
  name: host.interfaces.first.netbox_name,
77
78
  mac_address: old_mac,
78
- tags: ForemanNetbox::NetboxParameters::DEFAULT_TAGS
79
+ tags: default_tags.map do |tag|
80
+ { id: tag.id }
81
+ end
79
82
  }
80
83
  ]
81
84
  }.to_json
82
85
  )
86
+ stub_patch = stub_request(:patch, "#{Setting[:netbox_url]}/api/virtualization/interfaces/1.json")
83
87
 
84
88
  subject
85
89
  assert_requested(get_stub)
90
+ assert_not_requested(stub_patch)
86
91
  end
87
92
  end
88
93
  end
@@ -10,7 +10,8 @@ class UpdateVirtualMachineTest < ActiveSupport::TestCase
10
10
  netbox_params: host.netbox_facet.netbox_params,
11
11
  cluster: cluster,
12
12
  tenant: tenant,
13
- ip_addresses: ip_addresses
13
+ ip_addresses: ip_addresses,
14
+ tags: default_tags
14
15
  )
15
16
  end
16
17
 
@@ -43,7 +44,7 @@ class UpdateVirtualMachineTest < ActiveSupport::TestCase
43
44
  end
44
45
 
45
46
  let(:virtual_machine_name) { 'name.example.com' }
46
- let(:virtual_machine_tags) { ['tag'] }
47
+ let(:virtual_machine_tags) { [{ 'id' => 987_654_321, 'name' => 'tag', 'slug' => 'tag' }] }
47
48
  let(:virtual_machine_data) { virtual_machine.instance_variable_get(:@data).deep_symbolize_keys }
48
49
  let(:cluster) { OpenStruct.new(id: virtual_machine_data.dig(:cluster, :id)) }
49
50
  let(:tenant) { OpenStruct.new(id: virtual_machine_data.dig(:tenant, :id)) }
@@ -101,7 +102,9 @@ class UpdateVirtualMachineTest < ActiveSupport::TestCase
101
102
  end
102
103
 
103
104
  context 'if the host has not been updated since the last synchronization' do
104
- let(:virtual_machine_tags) { ForemanNetbox::NetboxParameters::DEFAULT_TAGS }
105
+ let(:virtual_machine_tags) do
106
+ default_tags.map { |t| { 'id' => t.id, 'name' => t.name, 'slug' => t.slug } }
107
+ end
105
108
 
106
109
  it 'does not update virtual_machine' do
107
110
  assert_equal virtual_machine, subject.virtual_machine
@@ -157,7 +160,7 @@ class UpdateVirtualMachineTest < ActiveSupport::TestCase
157
160
  primary_ip6: primary_ip6.id,
158
161
  tenant: tenant.id,
159
162
  vcpus: host.compute_object.cpus,
160
- tags: virtual_machine_tags | ForemanNetbox::NetboxParameters::DEFAULT_TAGS
163
+ tags: virtual_machine_tags.map { |t| t['id'] } | default_tags.map(&:id)
161
164
  }.to_json
162
165
  ).to_return(
163
166
  status: 200, headers: { 'Content-Type': 'application/json' },
@@ -7,13 +7,13 @@ module ForemanNetbox
7
7
  describe '#netbox_tenant_name' do
8
8
  let(:usergroup) { FactoryBot.build_stubbed(:usergroup, id: 1, name: usergroup_name) }
9
9
 
10
- context 'when name is longer than 30' do
11
- let(:usergroup_name) { SecureRandom.hex(16) }
10
+ context 'when name is longer than 100' do
11
+ let(:usergroup_name) { SecureRandom.hex(64) }
12
12
 
13
- it { assert_equal "#{usergroup.name.truncate(28, omission: '')}-#{usergroup.id}", usergroup.netbox_tenant_name }
13
+ it { assert_equal "#{usergroup.name.truncate(98, omission: '')}-#{usergroup.id}", usergroup.netbox_tenant_name }
14
14
  end
15
15
 
16
- context 'when name is shorter than 30' do
16
+ context 'when name is shorter than 100' do
17
17
  let(:usergroup_name) { 'Name' }
18
18
 
19
19
  it { assert_equal usergroup.name, usergroup.netbox_tenant_name }
@@ -16,8 +16,7 @@ module ForemanNetbox
16
16
  let(:expected) do
17
17
  {
18
18
  name: 'Tenant Name',
19
- slug: 'tenant-name',
20
- tags: ForemanNetbox::NetboxParameters::DEFAULT_TAGS
19
+ slug: 'tenant-name'
21
20
  }
22
21
  end
23
22
 
@@ -34,8 +33,7 @@ module ForemanNetbox
34
33
  let(:expected) do
35
34
  {
36
35
  name: 'device_name',
37
- serial: 'abc123',
38
- tags: ForemanNetbox::NetboxParameters::DEFAULT_TAGS
36
+ serial: 'abc123'
39
37
  }
40
38
  end
41
39
 
@@ -64,8 +62,7 @@ module ForemanNetbox
64
62
  let(:expected) do
65
63
  {
66
64
  model: 'Product Name',
67
- slug: 'product-name',
68
- tags: ForemanNetbox::NetboxParameters::DEFAULT_TAGS
65
+ slug: 'product-name'
69
66
  }
70
67
  end
71
68
 
@@ -77,8 +74,7 @@ module ForemanNetbox
77
74
  let(:expected) do
78
75
  {
79
76
  model: 'Unknown',
80
- slug: 'unknown',
81
- tags: ForemanNetbox::NetboxParameters::DEFAULT_TAGS
77
+ slug: 'unknown'
82
78
  }
83
79
  end
84
80
 
@@ -124,8 +120,7 @@ module ForemanNetbox
124
120
  let(:expected) do
125
121
  {
126
122
  name: 'Site Name',
127
- slug: 'site-name',
128
- tags: ForemanNetbox::NetboxParameters::DEFAULT_TAGS
123
+ slug: 'site-name'
129
124
  }
130
125
  end
131
126
 
@@ -153,8 +148,7 @@ module ForemanNetbox
153
148
  mac_address: 'FE:13:C6:44:29:24',
154
149
  type: {
155
150
  value: 'virtual'
156
- },
157
- tags: ForemanNetbox::NetboxParameters::DEFAULT_TAGS
151
+ }
158
152
  ]
159
153
  end
160
154
 
@@ -195,15 +189,13 @@ module ForemanNetbox
195
189
  address: '10.0.0.1/24',
196
190
  interface: {
197
191
  name: 'eth1'
198
- },
199
- tags: ForemanNetbox::NetboxParameters::DEFAULT_TAGS
192
+ }
200
193
  },
201
194
  {
202
195
  address: '1500:0:2d0:201::1/64',
203
196
  interface: {
204
197
  name: 'eth2'
205
- },
206
- tags: ForemanNetbox::NetboxParameters::DEFAULT_TAGS
198
+ }
207
199
  }
208
200
  ]
209
201
  end
@@ -233,8 +225,7 @@ module ForemanNetbox
233
225
  name: 'virtual_machine_name',
234
226
  vcpus: 2,
235
227
  memory: 128,
236
- disk: 20,
237
- tags: ForemanNetbox::NetboxParameters::DEFAULT_TAGS
228
+ disk: 20
238
229
  }
239
230
  end
240
231
 
@@ -252,7 +243,7 @@ module ForemanNetbox
252
243
  end
253
244
 
254
245
  let(:expected) do
255
- { name: 'Cluster Name', tags: ForemanNetbox::NetboxParameters::DEFAULT_TAGS }
246
+ { name: 'Cluster Name' }
256
247
  end
257
248
 
258
249
  it { assert_equal expected, host.netbox_facet.netbox_params.fetch(:cluster) }
@@ -10,8 +10,7 @@ module ForemanNetbox
10
10
  let(:my_hash) do
11
11
  {
12
12
  device: {
13
- name: 'My device',
14
- tags: ['foreman']
13
+ name: 'My device'
15
14
  }
16
15
  }
17
16
  end
@@ -26,15 +25,13 @@ module ForemanNetbox
26
25
  let(:old_hash) do
27
26
  {
28
27
  device: {
29
- name: 'Old name',
30
- tags: ['custom']
28
+ name: 'Old name'
31
29
  },
32
30
  device_role: {
33
31
  name: 'SERVER'
34
32
  },
35
33
  manufacturer: {
36
- name: 'Manufacturer',
37
- tags: %w[Synced foreman]
34
+ name: 'Manufacturer'
38
35
  },
39
36
  interfaces: [
40
37
  {
@@ -58,7 +55,6 @@ module ForemanNetbox
58
55
  {
59
56
  name: 'eth4',
60
57
  mac_address: '00:50:56:84:6D:84',
61
- tags: ['foreman'],
62
58
  type: {
63
59
  value: 'virtual'
64
60
  }
@@ -70,15 +66,13 @@ module ForemanNetbox
70
66
  let(:new_hash) do
71
67
  {
72
68
  device: {
73
- name: 'New name',
74
- tags: ['foreman']
69
+ name: 'New name'
75
70
  },
76
71
  device_type: {
77
72
  model: 'Model name'
78
73
  },
79
74
  manufacturer: {
80
- name: 'Manufacturer',
81
- tags: %w[foreman]
75
+ name: 'Manufacturer'
82
76
  },
83
77
  interfaces: [
84
78
  {
@@ -104,8 +98,7 @@ module ForemanNetbox
104
98
  mac_address: '00:50:56:84:6D:84',
105
99
  type: {
106
100
  value: 'virtual'
107
- },
108
- tags: ['foreman']
101
+ }
109
102
  }
110
103
  ]
111
104
  }
@@ -118,9 +111,6 @@ module ForemanNetbox
118
111
  device: {
119
112
  name: {
120
113
  old: old_hash.dig(:device, :name), new: new_hash.dig(:device, :name)
121
- },
122
- tags: {
123
- added: ['foreman'], removed: []
124
114
  }
125
115
  },
126
116
  interfaces: {
@@ -10,9 +10,15 @@ FactoryBot.definition_file_paths << File.join(File.dirname(__FILE__), 'factories
10
10
  FactoryBot.reload
11
11
 
12
12
  def setup_default_netbox_settings(netbox_url: 'https://netbox.example.com', netbox_api_token: 'api_key')
13
- FactoryBot.create(:setting, name: 'netbox_url', value: netbox_url, category: 'Setting::Netbox')
14
- FactoryBot.create(:setting, name: 'netbox_api_token', value: netbox_api_token, category: 'Setting::Netbox')
15
- FactoryBot.create(:setting, name: 'netbox_orchestration_enabled', value: true, category: 'Setting::Netbox')
13
+ if Gem::Version.new(SETTINGS[:version].notag) < Gem::Version.new('2.6')
14
+ FactoryBot.create(:setting, name: 'netbox_url', value: netbox_url, category: 'Setting::Netbox')
15
+ FactoryBot.create(:setting, name: 'netbox_api_token', value: netbox_api_token, category: 'Setting::Netbox')
16
+ FactoryBot.create(:setting, name: 'netbox_orchestration_enabled', value: true, category: 'Setting::Netbox')
17
+ else
18
+ Setting[:netbox_url] = netbox_url
19
+ Setting[:netbox_api_token] = netbox_api_token
20
+ Setting[:netbox_orchestration_enabled] = true
21
+ end
16
22
  end
17
23
 
18
24
  def setup_netbox_integration_test
@@ -23,3 +29,9 @@ def setup_netbox_integration_test
23
29
  netbox_api_token: ENV['FOREMAN_NETBOX_TOKEN']
24
30
  )
25
31
  end
32
+
33
+ def default_tags
34
+ ForemanNetbox::SyncHost::SyncTags::Organizer::DEFAULT_TAGS.map.with_index(1) do |tag, id|
35
+ ForemanNetbox::API.client::Extras::Tag.new(id: id, name: tag[:name], slug: tag[:slug])
36
+ end
37
+ end