foreman_netbox 1.0.2

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 (156) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE +619 -0
  3. data/README.md +50 -0
  4. data/Rakefile +50 -0
  5. data/app/contracts/foreman_netbox/device_contract.rb +42 -0
  6. data/app/contracts/foreman_netbox/params/interface_params.rb +16 -0
  7. data/app/contracts/foreman_netbox/params/ip_address_params.rb +15 -0
  8. data/app/contracts/foreman_netbox/params/tenant_params.rb +13 -0
  9. data/app/contracts/foreman_netbox/virtual_machine_contract.rb +29 -0
  10. data/app/interactors/foreman_netbox/concerns/facts.rb +11 -0
  11. data/app/interactors/foreman_netbox/concerns/primary_ips.rb +25 -0
  12. data/app/interactors/foreman_netbox/delete_host/delete_device.rb +20 -0
  13. data/app/interactors/foreman_netbox/delete_host/delete_virtual_machine.rb +20 -0
  14. data/app/interactors/foreman_netbox/delete_host/organizer.rb +12 -0
  15. data/app/interactors/foreman_netbox/sync_host/organizer.rb +35 -0
  16. data/app/interactors/foreman_netbox/sync_host/sync_device/create.rb +36 -0
  17. data/app/interactors/foreman_netbox/sync_host/sync_device/find.rb +41 -0
  18. data/app/interactors/foreman_netbox/sync_host/sync_device/organizer.rb +30 -0
  19. data/app/interactors/foreman_netbox/sync_host/sync_device/save_netbox_url.rb +29 -0
  20. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_role/create.rb +32 -0
  21. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_role/find.rb +28 -0
  22. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_role/organizer.rb +20 -0
  23. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/create.rb +34 -0
  24. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/find.rb +28 -0
  25. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/organizer.rb +22 -0
  26. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/sync_manufacturer/create.rb +34 -0
  27. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/sync_manufacturer/find.rb +40 -0
  28. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/sync_manufacturer/organizer.rb +22 -0
  29. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/update.rb +33 -0
  30. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/create.rb +37 -0
  31. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/delete.rb +36 -0
  32. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/find.rb +30 -0
  33. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/organizer.rb +23 -0
  34. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/create.rb +46 -0
  35. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/delete.rb +40 -0
  36. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/find.rb +32 -0
  37. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/organizer.rb +24 -0
  38. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/update.rb +39 -0
  39. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/update.rb +38 -0
  40. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_site/create.rb +32 -0
  41. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_site/find.rb +28 -0
  42. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_site/organizer.rb +25 -0
  43. data/app/interactors/foreman_netbox/sync_host/sync_device/sync_site/update.rb +35 -0
  44. data/app/interactors/foreman_netbox/sync_host/sync_device/update.rb +85 -0
  45. data/app/interactors/foreman_netbox/sync_host/sync_device/validate.rb +25 -0
  46. data/app/interactors/foreman_netbox/sync_host/sync_tenant/create.rb +30 -0
  47. data/app/interactors/foreman_netbox/sync_host/sync_tenant/find.rb +26 -0
  48. data/app/interactors/foreman_netbox/sync_host/sync_tenant/organizer.rb +19 -0
  49. data/app/interactors/foreman_netbox/sync_host/sync_tenant/update.rb +33 -0
  50. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/create.rb +34 -0
  51. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/find.rb +34 -0
  52. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/organizer.rb +28 -0
  53. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/save_netbox_url.rb +31 -0
  54. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/create.rb +35 -0
  55. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/find.rb +28 -0
  56. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/organizer.rb +22 -0
  57. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/sync_cluster_type/create.rb +34 -0
  58. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/sync_cluster_type/find.rb +32 -0
  59. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/sync_cluster_type/organizer.rb +22 -0
  60. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/update.rb +35 -0
  61. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/create.rb +33 -0
  62. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/delete.rb +36 -0
  63. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/find.rb +30 -0
  64. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/organizer.rb +23 -0
  65. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/create.rb +46 -0
  66. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/delete.rb +40 -0
  67. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/find.rb +28 -0
  68. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/organizer.rb +24 -0
  69. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/update.rb +39 -0
  70. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/update.rb +38 -0
  71. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/update.rb +82 -0
  72. data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/validate.rb +25 -0
  73. data/app/jobs/foreman_netbox/sync_host_job.rb +19 -0
  74. data/app/lib/foreman_netbox/api.rb +12 -0
  75. data/app/models/concerns/foreman_netbox/host_extensions.rb +27 -0
  76. data/app/models/concerns/foreman_netbox/location_extensions.rb +13 -0
  77. data/app/models/concerns/foreman_netbox/nic/base_extensions.rb +23 -0
  78. data/app/models/concerns/foreman_netbox/user_usergroup_common_extensions.rb +18 -0
  79. data/app/models/foreman_netbox/netbox_facet.rb +34 -0
  80. data/app/models/orchestration/netbox.rb +64 -0
  81. data/app/models/setting/netbox.rb +32 -0
  82. data/app/services/foreman_netbox/cached_netbox_parameters.rb +103 -0
  83. data/app/services/foreman_netbox/netbox_parameters.rb +185 -0
  84. data/app/services/foreman_netbox/netbox_parameters_comparator.rb +96 -0
  85. data/app/views/hosts/_netbox_tab.html.erb +28 -0
  86. data/config/routes.rb +4 -0
  87. data/db/migrate/20200527135812_create_netbox_facet.rb +12 -0
  88. data/db/migrate/20200604141933_add_sync_info_to_netbox_facet.rb +8 -0
  89. data/db/migrate/20200708093607_add_raw_data_to_netbox_facet.rb +7 -0
  90. data/lib/foreman_netbox.rb +6 -0
  91. data/lib/foreman_netbox/engine.rb +64 -0
  92. data/lib/foreman_netbox/version.rb +5 -0
  93. data/lib/tasks/foreman_netbox_tasks.rake +67 -0
  94. data/test/factories/host.rb +17 -0
  95. data/test/factories/netbox_facet.rb +17 -0
  96. data/test/fixtures/facts/k8s_physical_host.json +328 -0
  97. data/test/fixtures/facts/rhel_physical_host.json +512 -0
  98. data/test/fixtures/facts/rhel_virtual_host.json +455 -0
  99. data/test/fixtures/netbox_device_raw_data.json +330 -0
  100. data/test/fixtures/netbox_virtual_machine_raw_data.json +241 -0
  101. data/test/integration/foreman_netbox/sync_k8s_physical_host_test.rb +81 -0
  102. data/test/integration/foreman_netbox/sync_rhel_physical_host_test.rb +81 -0
  103. data/test/integration/foreman_netbox/sync_rhel_virtual_host_test.rb +93 -0
  104. data/test/interactors/foreman_netbox/sync_host/organizer_test.rb +78 -0
  105. data/test/interactors/foreman_netbox/sync_host/sync_device/create_test.rb +65 -0
  106. data/test/interactors/foreman_netbox/sync_host/sync_device/find_test.rb +105 -0
  107. data/test/interactors/foreman_netbox/sync_host/sync_device/save_netbox_url_test.rb +39 -0
  108. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_role/create_test.rb +45 -0
  109. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_role/find_test.rb +58 -0
  110. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/create_test.rb +55 -0
  111. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/find_test.rb +63 -0
  112. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/sync_manufacturer/create_test.rb +48 -0
  113. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/sync_manufacturer/find_test.rb +105 -0
  114. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/update_test.rb +54 -0
  115. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/create_test.rb +51 -0
  116. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/delete_test.rb +41 -0
  117. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/find_test.rb +53 -0
  118. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/create_test.rb +80 -0
  119. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/delete_test.rb +72 -0
  120. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/find_test.rb +51 -0
  121. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/update_test.rb +64 -0
  122. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/update_test.rb +92 -0
  123. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_site/create_test.rb +51 -0
  124. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_site/find_test.rb +59 -0
  125. data/test/interactors/foreman_netbox/sync_host/sync_device/sync_site/update_test.rb +54 -0
  126. data/test/interactors/foreman_netbox/sync_host/sync_device/update_test.rb +188 -0
  127. data/test/interactors/foreman_netbox/sync_host/sync_tenant/create_test.rb +50 -0
  128. data/test/interactors/foreman_netbox/sync_host/sync_tenant/find_test.rb +58 -0
  129. data/test/interactors/foreman_netbox/sync_host/sync_tenant/update_test.rb +59 -0
  130. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/create_test.rb +74 -0
  131. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/find_test.rb +77 -0
  132. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/save_netbox_url_test.rb +39 -0
  133. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/create_test.rb +54 -0
  134. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/find_test.rb +58 -0
  135. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/sync_cluster_type/create_test.rb +50 -0
  136. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/sync_cluster_type/find_test.rb +65 -0
  137. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/update_test.rb +54 -0
  138. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/create_test.rb +53 -0
  139. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/delete_test.rb +37 -0
  140. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/find_test.rb +53 -0
  141. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/create_test.rb +80 -0
  142. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/delete_test.rb +73 -0
  143. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/find_test.rb +51 -0
  144. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/update_test.rb +64 -0
  145. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/update_test.rb +88 -0
  146. data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/update_test.rb +171 -0
  147. data/test/jobs/foreman_netbox/sync_host_job_test.rb +14 -0
  148. data/test/models/foreman_netbox/location_test.rb +42 -0
  149. data/test/models/foreman_netbox/netbox_facet_test.rb +219 -0
  150. data/test/models/foreman_netbox/nics/base_test.rb +81 -0
  151. data/test/models/foreman_netbox/usergroup_test.rb +23 -0
  152. data/test/models/host/managed_test.rb +63 -0
  153. data/test/services/foreman_netbox/netbox_attributes_test.rb +278 -0
  154. data/test/services/foreman_netbox/netbox_parameters_comparator_test.rb +136 -0
  155. data/test/test_plugin_helper.rb +25 -0
  156. metadata +343 -0
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_plugin_helper'
4
+
5
+ class SaveNetboxUrlDeviceTest < ActiveSupport::TestCase
6
+ subject do
7
+ ForemanNetbox::SyncHost::SyncDevice::SaveNetboxUrl.call(host: host, device: device)
8
+ end
9
+
10
+ let(:device) { OpenStruct.new(id: 1) }
11
+
12
+ setup do
13
+ setup_default_netbox_settings
14
+ end
15
+
16
+ context 'when a host has Netbox facet' do
17
+ let(:host) { FactoryBot.create(:host, :with_netbox_facet, hostname: 'host.dev.example.com') }
18
+
19
+ it 'updates Netbox facet' do
20
+ assert_not host.netbox_facet.new_record?
21
+ assert_difference('ForemanNetbox::NetboxFacet.count', 0) do
22
+ subject
23
+ end
24
+ assert_equal 'https://netbox.example.com/dcim/devices/1', subject.host.reload.netbox_facet.url
25
+ end
26
+ end
27
+
28
+ context 'when a host has no Netbox facet' do
29
+ let(:host) { FactoryBot.create(:host, hostname: 'host.dev.example.com') }
30
+
31
+ it 'creates Netbox facet' do
32
+ assert host.netbox_facet.new_record?
33
+ assert_difference('ForemanNetbox::NetboxFacet.count', 1) do
34
+ subject
35
+ end
36
+ assert_equal 'https://netbox.example.com/dcim/devices/1', subject.host.reload.netbox_facet.url
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_plugin_helper'
4
+
5
+ class CreateDeviceRoleTest < ActiveSupport::TestCase
6
+ subject do
7
+ ForemanNetbox::SyncHost::SyncDevice::SyncDeviceRole::Create.call(
8
+ host: host, netbox_params: host.netbox_facet.netbox_params, device_role: device_role
9
+ )
10
+ end
11
+ let(:device_role_params) { host.netbox_facet.netbox_params.fetch(:device_role) }
12
+ let(:host) { FactoryBot.build_stubbed(:host) }
13
+
14
+ setup do
15
+ setup_default_netbox_settings
16
+ end
17
+
18
+ context 'when device_role is not assigned to the context' do
19
+ let(:device_role) { nil }
20
+
21
+ it 'assigns device_role to context' do
22
+ stub_post = stub_request(:post, "#{Setting[:netbox_url]}/api/dcim/device-roles/").with(
23
+ body: {
24
+ name: device_role_params[:name],
25
+ color: device_role_params[:color],
26
+ slug: device_role_params[:slug]
27
+ }.to_json
28
+ ).to_return(
29
+ status: 201, headers: { 'Content-Type': 'application/json' },
30
+ body: { id: 1 }.to_json
31
+ )
32
+
33
+ assert_equal 1, subject.device_role.id
34
+ assert_requested(stub_post)
35
+ end
36
+ end
37
+
38
+ context 'when device_role is already assigned to the context' do
39
+ let(:device_role) { OpenStruct.new }
40
+
41
+ it 'does not change device_role' do
42
+ assert_equal device_role, subject.device_role
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_plugin_helper'
4
+
5
+ class FindDeviceRoleTest < ActiveSupport::TestCase
6
+ subject do
7
+ ForemanNetbox::SyncHost::SyncDevice::SyncDeviceRole::Find.call(
8
+ host: host, netbox_params: host.netbox_facet.netbox_params
9
+ )
10
+ end
11
+
12
+ let(:host) { FactoryBot.build_stubbed(:host) }
13
+ let(:device_role_params) { host.netbox_facet.netbox_params.fetch(:device_role) }
14
+
15
+ setup do
16
+ setup_default_netbox_settings
17
+ end
18
+
19
+ context 'when device_role exists in Netbox' do
20
+ it 'assigns device_role to context' do
21
+ stub_get = stub_request(:get, "#{Setting[:netbox_url]}/api/dcim/device-roles.json").with(
22
+ query: { limit: 50, slug: device_role_params[:slug] }
23
+ ).to_return(
24
+ status: 200, headers: { 'Content-Type': 'application/json' },
25
+ body: {
26
+ count: 1,
27
+ results: [
28
+ {
29
+ id: 1,
30
+ name: device_role_params[:name],
31
+ slug: device_role_params[:slug]
32
+ }
33
+ ]
34
+ }.to_json
35
+ )
36
+
37
+ assert_equal 1, subject.device_role.id
38
+ assert_requested(stub_get)
39
+ end
40
+ end
41
+
42
+ context 'when device_role does not exist in NetBox' do
43
+ it 'does not assign device_role to context' do
44
+ stub_get = stub_request(:get, "#{Setting[:netbox_url]}/api/dcim/device-roles.json").with(
45
+ query: { limit: 50, slug: device_role_params[:slug] }
46
+ ).to_return(
47
+ status: 200, headers: { 'Content-Type': 'application/json' },
48
+ body: {
49
+ count: 0,
50
+ results: []
51
+ }.to_json
52
+ )
53
+
54
+ assert_nil subject.device_role
55
+ assert_requested(stub_get)
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_plugin_helper'
4
+
5
+ class CreateDeviceTypeTest < ActiveSupport::TestCase
6
+ subject do
7
+ ForemanNetbox::SyncHost::SyncDevice::SyncDeviceType::Create.call(
8
+ netbox_params: host.netbox_facet.netbox_params,
9
+ device_type: device_type,
10
+ manufacturer: manufacturer,
11
+ host: host
12
+ )
13
+ end
14
+
15
+ let(:device_type_id) { 1 }
16
+ let(:manufacturer) { OpenStruct.new(id: 1) }
17
+ let(:host) do
18
+ FactoryBot.build_stubbed(:host).tap do |host|
19
+ host.stubs(:facts).returns({ 'dmi::product::name': 'Device Type Model' })
20
+ end
21
+ end
22
+
23
+ setup do
24
+ setup_default_netbox_settings
25
+ end
26
+
27
+ context 'when device_type is not assigned to the context' do
28
+ let(:device_type) { nil }
29
+
30
+ it 'assigns device_type to context' do
31
+ stub_post = stub_request(:post, "#{Setting[:netbox_url]}/api/dcim/device-types/").with(
32
+ body: {
33
+ model: host.netbox_facet.netbox_params.dig(:device_type, :model),
34
+ slug: host.netbox_facet.netbox_params.dig(:device_type, :slug),
35
+ tags: host.netbox_facet.netbox_params.dig(:device_type, :tags),
36
+ manufacturer: manufacturer.id
37
+ }.to_json
38
+ ).to_return(
39
+ status: 201, headers: { 'Content-Type': 'application/json' },
40
+ body: { id: device_type_id }.to_json
41
+ )
42
+
43
+ assert_equal device_type_id, subject.device_type.id
44
+ assert_requested(stub_post)
45
+ end
46
+ end
47
+
48
+ context 'when device_type is already assigned to the context' do
49
+ let(:device_type) { OpenStruct.new(id: device_type_id) }
50
+
51
+ it 'does not change device_type' do
52
+ assert_equal device_type_id, subject.device_type.id
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_plugin_helper'
4
+
5
+ class FindDeviceTypeTest < ActiveSupport::TestCase
6
+ subject do
7
+ ForemanNetbox::SyncHost::SyncDevice::SyncDeviceType::Find.call(
8
+ host: host,
9
+ netbox_params: host.netbox_facet.netbox_params
10
+ )
11
+ end
12
+
13
+ let(:host) do
14
+ FactoryBot.build_stubbed(:host).tap do |host|
15
+ host.stubs(:facts).returns({ 'dmi::product::name': 'Device type' })
16
+ end
17
+ end
18
+ let(:slug) { host.netbox_facet.netbox_params.dig(:device_type, :slug) }
19
+
20
+ setup do
21
+ setup_default_netbox_settings
22
+ end
23
+
24
+ context 'when device_type exists in Netbox' do
25
+ it 'assigns device_type to context' do
26
+ stub_get = stub_request(:get, "#{Setting[:netbox_url]}/api/dcim/device-types.json").with(
27
+ query: { limit: 50, slug: slug }
28
+ ).to_return(
29
+ status: 200, headers: { 'Content-Type': 'application/json' },
30
+ body: {
31
+ count: 1,
32
+ results: [
33
+ {
34
+ id: 1,
35
+ name: host.facts.symbolize_keys.fetch(:'dmi::product::name'),
36
+ slug: slug
37
+ }
38
+ ]
39
+ }.to_json
40
+ )
41
+
42
+ assert_equal 1, subject.device_type.id
43
+ assert_requested(stub_get)
44
+ end
45
+ end
46
+
47
+ context 'when device_type does not exist in NetBox' do
48
+ it 'does not assign device_type to context' do
49
+ stub_get = stub_request(:get, "#{Setting[:netbox_url]}/api/dcim/device-types.json").with(
50
+ query: { limit: 50, slug: slug }
51
+ ).to_return(
52
+ status: 200, headers: { 'Content-Type': 'application/json' },
53
+ body: {
54
+ count: 0,
55
+ results: []
56
+ }.to_json
57
+ )
58
+
59
+ assert_nil subject.device_type
60
+ assert_requested(stub_get)
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_plugin_helper'
4
+
5
+ class CreateManufacturerTest < ActiveSupport::TestCase
6
+ subject do
7
+ ForemanNetbox::SyncHost::SyncDevice::SyncDeviceType::SyncManufacturer::Create.call(
8
+ manufacturer: manufacturer, host: host, netbox_params: host.netbox_facet.netbox_params
9
+ )
10
+ end
11
+
12
+ let(:host) do
13
+ FactoryBot.build_stubbed(:host).tap do |host|
14
+ host.stubs(:facts).returns({ 'dmi::manufacturer': 'Manufacturer' })
15
+ end
16
+ end
17
+
18
+ setup do
19
+ setup_default_netbox_settings
20
+ end
21
+
22
+ context 'when manufacturer is not assigned to the context' do
23
+ let(:manufacturer) { nil }
24
+
25
+ it 'assigns manufacturer to context' do
26
+ stub_post = stub_request(:post, "#{Setting[:netbox_url]}/api/dcim/manufacturers/").with(
27
+ body: {
28
+ name: host.netbox_facet.netbox_params.dig(:manufacturer, :name),
29
+ slug: host.netbox_facet.netbox_params.dig(:manufacturer, :slug)
30
+ }.to_json
31
+ ).to_return(
32
+ status: 201, headers: { 'Content-Type': 'application/json' },
33
+ body: { id: 1 }.to_json
34
+ )
35
+
36
+ assert_equal 1, subject.manufacturer.id
37
+ assert_requested(stub_post)
38
+ end
39
+ end
40
+
41
+ context 'when manufacturer is already assigned to the context' do
42
+ let(:manufacturer) { OpenStruct.new }
43
+
44
+ it 'does not change manufacturer' do
45
+ assert_equal manufacturer, subject.manufacturer
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,105 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_plugin_helper'
4
+
5
+ class FindManufacturerTest < ActiveSupport::TestCase
6
+ subject do
7
+ ForemanNetbox::SyncHost::SyncDevice::SyncDeviceType::SyncManufacturer::Find.call(
8
+ host: host, netbox_params: netbox_params
9
+ )
10
+ end
11
+
12
+ let(:host) do
13
+ FactoryBot.build_stubbed(:host).tap do |host|
14
+ host.stubs(:facts).returns(
15
+ {
16
+ 'dmi::manufacturer' => 'Manufacturer',
17
+ 'dmi::product::name' => 'device type 2'
18
+ }
19
+ )
20
+ end
21
+ end
22
+ let(:netbox_params) { host.netbox_facet.netbox_params }
23
+
24
+ setup do
25
+ setup_default_netbox_settings
26
+ end
27
+
28
+ context 'when manufacturer exists in Netbox' do
29
+ context 'by slug' do
30
+ it 'assigns manufacturer to context' do
31
+ stub_get_with_slug = stub_request(:get, "#{Setting[:netbox_url]}/api/dcim/manufacturers.json").with(
32
+ query: { limit: 50, slug: netbox_params.dig(:manufacturer, :slug) }
33
+ ).to_return(
34
+ status: 200, headers: { 'Content-Type': 'application/json' },
35
+ body: {
36
+ count: 1,
37
+ results: [netbox_params.fetch(:manufacturer).merge(id: 1)]
38
+ }.to_json
39
+ )
40
+ stub_get_with_name = stub_request(:get, "#{Setting[:netbox_url]}/api/dcim/manufacturers.json").with(
41
+ query: { limit: 50, name: netbox_params.dig(:manufacturer, :name) }
42
+ )
43
+
44
+ assert_equal 1, subject.manufacturer.id
45
+ assert_requested(stub_get_with_slug)
46
+ assert_not_requested(stub_get_with_name)
47
+ end
48
+ end
49
+
50
+ context 'by name' do
51
+ it 'assigns manufacturer to context' do
52
+ stub_get_with_slug = stub_request(:get, "#{Setting[:netbox_url]}/api/dcim/manufacturers.json").with(
53
+ query: { limit: 50, slug: netbox_params.dig(:manufacturer, :slug) }
54
+ ).to_return(
55
+ status: 200, headers: { 'Content-Type': 'application/json' },
56
+ body: {
57
+ count: 0,
58
+ results: []
59
+ }.to_json
60
+ )
61
+ stub_get_with_name = stub_request(:get, "#{Setting[:netbox_url]}/api/dcim/manufacturers.json").with(
62
+ query: { limit: 50, name: netbox_params.dig(:manufacturer, :name) }
63
+ ).to_return(
64
+ status: 200, headers: { 'Content-Type': 'application/json' },
65
+ body: {
66
+ count: 1,
67
+ results: [netbox_params.fetch(:manufacturer).merge(id: 1)]
68
+ }.to_json
69
+ )
70
+
71
+ assert_equal 1, subject.manufacturer.id
72
+ assert_requested(stub_get_with_slug)
73
+ assert_requested(stub_get_with_name)
74
+ end
75
+ end
76
+ end
77
+
78
+ context 'when manufacturer does not exist in NetBox' do
79
+ it 'does not assign manufacturer to context' do
80
+ stub_get_with_slug = stub_request(:get, "#{Setting[:netbox_url]}/api/dcim/manufacturers.json").with(
81
+ query: { limit: 50, slug: netbox_params.dig(:manufacturer, :slug) }
82
+ ).to_return(
83
+ status: 200, headers: { 'Content-Type': 'application/json' },
84
+ body: {
85
+ count: 0,
86
+ results: []
87
+ }.to_json
88
+ )
89
+
90
+ stub_get_with_name = stub_request(:get, "#{Setting[:netbox_url]}/api/dcim/manufacturers.json").with(
91
+ query: { limit: 50, name: netbox_params.dig(:manufacturer, :name) }
92
+ ).to_return(
93
+ status: 200, headers: { 'Content-Type': 'application/json' },
94
+ body: {
95
+ count: 0,
96
+ results: []
97
+ }.to_json
98
+ )
99
+
100
+ assert_nil subject.manufacturer
101
+ assert_requested(stub_get_with_slug)
102
+ assert_requested(stub_get_with_name)
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_plugin_helper'
4
+
5
+ class UpdateDeviceTypeTest < ActiveSupport::TestCase
6
+ subject do
7
+ ForemanNetbox::SyncHost::SyncDevice::SyncDeviceType::Update.call(
8
+ host: host, device_type: device_type, netbox_params: host.netbox_facet.netbox_params
9
+ )
10
+ end
11
+
12
+ let(:host) { FactoryBot.build_stubbed(:host) }
13
+ let(:device_type) do
14
+ ForemanNetbox::API.client::DCIM::DeviceType.new(id: 1).tap do |device|
15
+ device.instance_variable_set(
16
+ :@data,
17
+ { 'id' => 1, 'tags' => device_type_tags }
18
+ )
19
+ end
20
+ end
21
+
22
+ setup do
23
+ setup_default_netbox_settings
24
+ end
25
+
26
+ context 'when changed' do
27
+ let(:device_type_tags) { [] }
28
+
29
+ it 'updates device type' do
30
+ stub_patch = stub_request(:patch, "#{Setting[:netbox_url]}/api/dcim/device-types/1.json").with(
31
+ body: {
32
+ tags: host.netbox_facet.netbox_params.dig(:device_type, :tags)
33
+ }.to_json
34
+ ).to_return(
35
+ status: 200, headers: { 'Content-Type': 'application/json' },
36
+ body: { id: 1 }.to_json
37
+ )
38
+
39
+ assert subject.success?
40
+ assert_requested stub_patch
41
+ end
42
+ end
43
+
44
+ context 'when unchanged' do
45
+ let(:device_type_tags) { host.netbox_facet.netbox_params.dig(:device_type, :tags) }
46
+
47
+ it 'does not update device type' do
48
+ stub_patch = stub_request(:patch, "#{Setting[:netbox_url]}/api/dcim/device-types/1.json")
49
+
50
+ assert subject.success?
51
+ assert_not_requested stub_patch
52
+ end
53
+ end
54
+ end