foreman_netbox 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
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,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_plugin_helper'
4
+
5
+ class UpdateTenantTest < ActiveSupport::TestCase
6
+ subject do
7
+ ForemanNetbox::SyncHost::SyncTenant::Update.call(
8
+ host: host, tenant: tenant, netbox_params: host.netbox_facet.netbox_params
9
+ )
10
+ end
11
+
12
+ let(:tenant) do
13
+ ForemanNetbox::API.client::Tenancy::Tenant.new(id: 1).tap do |tenant|
14
+ tenant.instance_variable_set(
15
+ :@data,
16
+ { 'id' => 1, 'tags' => tenant_tags }
17
+ )
18
+ end
19
+ end
20
+ let(:host) do
21
+ FactoryBot.build_stubbed(
22
+ :host,
23
+ owner: FactoryBot.build_stubbed(:usergroup, name: 'Owner')
24
+ )
25
+ end
26
+
27
+ setup do
28
+ setup_default_netbox_settings
29
+ end
30
+
31
+ context 'when changed' do
32
+ let(:tenant_tags) { [] }
33
+
34
+ it 'updates tenant' do
35
+ stub_patch = stub_request(:patch, "#{Setting[:netbox_url]}/api/tenancy/tenants/1.json").with(
36
+ body: {
37
+ tags: ForemanNetbox::NetboxParameters::DEFAULT_TAGS
38
+ }.to_json
39
+ ).to_return(
40
+ status: 200, headers: { 'Content-Type': 'application/json' },
41
+ body: { id: 1 }.to_json
42
+ )
43
+
44
+ assert subject.success?
45
+ assert_requested stub_patch
46
+ end
47
+ end
48
+
49
+ context 'when unchanged' do
50
+ let(:tenant_tags) { ForemanNetbox::NetboxParameters::DEFAULT_TAGS }
51
+
52
+ it 'does not update tenant' do
53
+ stub_patch = stub_request(:patch, "#{Setting[:netbox_url]}/api/tenancy/tenants/1.json")
54
+
55
+ assert subject.success?
56
+ assert_not_requested stub_patch
57
+ end
58
+ end
59
+ end
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_plugin_helper'
4
+
5
+ class CreateVirtualMachineTest < ActiveSupport::TestCase
6
+ subject do
7
+ ForemanNetbox::SyncHost::SyncVirtualMachine::Create.call(
8
+ host: host,
9
+ netbox_params: host.netbox_facet.netbox_params,
10
+ virtual_machine: virtual_machine,
11
+ cluster: cluster,
12
+ tenant: tenant
13
+ )
14
+ end
15
+
16
+ let(:host) do
17
+ FactoryBot.build_stubbed(
18
+ :host,
19
+ hostname: 'host.dev.example.com',
20
+ location: FactoryBot.build_stubbed(:location)
21
+ ).tap do |host|
22
+ host.stubs(:compute?).returns(true)
23
+ host.stubs(:compute_object).returns(
24
+ OpenStruct.new(
25
+ cpus: 2,
26
+ memory_mb: 512,
27
+ volumes: [
28
+ OpenStruct.new(size_gb: 128)
29
+ ]
30
+ )
31
+ )
32
+ end
33
+ end
34
+
35
+ let(:cluster) { OpenStruct.new(id: 1) }
36
+ let(:tenant) { OpenStruct.new(id: 1) }
37
+ let(:netbox_virtual_machine_params) { host.netbox_facet.netbox_params.fetch(:virtual_machine) }
38
+
39
+ setup do
40
+ setup_default_netbox_settings
41
+ end
42
+
43
+ context 'when virtual_machine is not assigned to the context' do
44
+ let(:virtual_machine) { nil }
45
+
46
+ it 'creates a virtual_machine' do
47
+ stub_post = stub_request(:post, "#{Setting[:netbox_url]}/api/virtualization/virtual-machines/").with(
48
+ body: {
49
+ vcpus: netbox_virtual_machine_params[:vcpus],
50
+ memory: netbox_virtual_machine_params[:memory],
51
+ disk: netbox_virtual_machine_params[:disk],
52
+ name: netbox_virtual_machine_params[:name],
53
+ tags: netbox_virtual_machine_params[:tags],
54
+ cluster: cluster.id,
55
+ tenant: tenant.id
56
+ }.to_json
57
+ ).to_return(
58
+ status: 201, headers: { 'Content-Type': 'application/json' },
59
+ body: { id: 1, name: host.name }.to_json
60
+ )
61
+
62
+ assert_equal 1, subject.virtual_machine.id
63
+ assert_requested(stub_post)
64
+ end
65
+ end
66
+
67
+ context 'when virtual_machine is already assigned to the context' do
68
+ let(:virtual_machine) { OpenStruct.new }
69
+
70
+ it 'does not create a virtual_machine' do
71
+ assert_equal virtual_machine, subject.virtual_machine
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_plugin_helper'
4
+
5
+ class FindVirtualMachineTest < ActiveSupport::TestCase
6
+ subject do
7
+ ForemanNetbox::SyncHost::SyncVirtualMachine::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(:compute?).returns(true)
16
+ host.stubs(:mac).returns('C3:CD:63:54:21:62')
17
+ end
18
+ end
19
+
20
+ setup do
21
+ setup_default_netbox_settings
22
+ end
23
+
24
+ context 'when virtual machine already exists in Netbox' do
25
+ test 'find virtual_machine by host name' do
26
+ stub_get_by_mac_addres = stub_request(:get, "#{Setting[:netbox_url]}/api/virtualization/virtual-machines.json").with(
27
+ query: { limit: 50, mac_address: host.mac }
28
+ ).to_return(
29
+ status: 200, headers: { 'Content-Type': 'application/json' },
30
+ body: { count: 0, results: [] }.to_json
31
+ )
32
+ stub_get_by_name = stub_request(:get, "#{Setting[:netbox_url]}/api/virtualization/virtual-machines.json").with(
33
+ query: { limit: 50, name: host.name }
34
+ ).to_return(
35
+ status: 200, headers: { 'Content-Type': 'application/json' },
36
+ body: { count: 1, results: [{ id: 1, name: host.name }] }.to_json
37
+ )
38
+
39
+ assert_equal 1, subject.virtual_machine.id
40
+ assert_requested(stub_get_by_mac_addres)
41
+ assert_requested(stub_get_by_name)
42
+ end
43
+
44
+ test 'find virtual_machine by mac address' do
45
+ stub_get_by_mac_addres = stub_request(:get, "#{Setting[:netbox_url]}/api/virtualization/virtual-machines.json").with(
46
+ query: { limit: 50, mac_address: host.mac }
47
+ ).to_return(
48
+ status: 200, headers: { 'Content-Type': 'application/json' },
49
+ body: { count: 1, results: [{ id: 1, name: host.name }] }.to_json
50
+ )
51
+
52
+ assert_equal 1, subject.virtual_machine.id
53
+ assert_requested(stub_get_by_mac_addres)
54
+ end
55
+ end
56
+
57
+ context 'when virtual machine does not exist in Netbox' do
58
+ it 'does not assign virtual_machine to context' do
59
+ stub_get_by_mac_addres = stub_request(:get, "#{Setting[:netbox_url]}/api/virtualization/virtual-machines.json").with(
60
+ query: { limit: 50, mac_address: host.mac }
61
+ ).to_return(
62
+ status: 200, headers: { 'Content-Type': 'application/json' },
63
+ body: { count: 0, results: [] }.to_json
64
+ )
65
+ stub_get_by_name = stub_request(:get, "#{Setting[:netbox_url]}/api/virtualization/virtual-machines.json").with(
66
+ query: { limit: 50, name: host.name }
67
+ ).to_return(
68
+ status: 200, headers: { 'Content-Type': 'application/json' },
69
+ body: { count: 0, results: [] }.to_json
70
+ )
71
+
72
+ assert_nil subject.virtual_machine
73
+ assert_requested(stub_get_by_mac_addres)
74
+ assert_requested(stub_get_by_name)
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_plugin_helper'
4
+
5
+ class SaveNetboxUrlVirtualMachineTest < ActiveSupport::TestCase
6
+ subject do
7
+ ForemanNetbox::SyncHost::SyncVirtualMachine::SaveNetboxUrl.call(host: host, virtual_machine: virtual_machine)
8
+ end
9
+
10
+ let(:virtual_machine) { 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/virtualization/virtual-machines/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/virtualization/virtual-machines/1', subject.host.reload.netbox_facet.url
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_plugin_helper'
4
+
5
+ class CreateClusterTest < ActiveSupport::TestCase
6
+ subject do
7
+ ForemanNetbox::SyncHost::SyncVirtualMachine::SyncCluster::Create.call(
8
+ host: host, cluster_type: cluster_type, cluster: cluster, netbox_params: host.netbox_facet.netbox_params
9
+ )
10
+ end
11
+
12
+ let(:cluster_id) { 1 }
13
+ let(:cluster_type) { OpenStruct.new(id: 1) }
14
+ let(:host) do
15
+ FactoryBot.build_stubbed(:host).tap do |host|
16
+ host.stubs(:compute?).returns(true)
17
+ host.stubs(:compute_object).returns(
18
+ OpenStruct.new(cluster: 'CLUSTER')
19
+ )
20
+ end
21
+ end
22
+
23
+ setup do
24
+ setup_default_netbox_settings
25
+ end
26
+
27
+ context 'when cluster is not assigned to the context' do
28
+ let(:cluster) { nil }
29
+
30
+ it 'creates a cluster' do
31
+ stub_post = stub_request(:post, "#{Setting[:netbox_url]}/api/virtualization/clusters/").with(
32
+ body: {
33
+ name: host.netbox_facet.netbox_params.dig(:cluster, :name),
34
+ tags: host.netbox_facet.netbox_params.dig(:cluster, :tags),
35
+ type: cluster_type.id
36
+ }.to_json
37
+ ).to_return(
38
+ status: 201, headers: { 'Content-Type': 'application/json' },
39
+ body: { id: cluster_id }.to_json
40
+ )
41
+
42
+ assert_equal cluster_id, subject.cluster.id
43
+ assert_requested(stub_post)
44
+ end
45
+ end
46
+
47
+ context 'when cluster is already assigned to the context' do
48
+ let(:cluster) { OpenStruct.new(id: cluster_id) }
49
+
50
+ it 'does not create a cluster' do
51
+ assert_equal cluster_id, subject.cluster.id
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_plugin_helper'
4
+
5
+ class FindClusterTest < ActiveSupport::TestCase
6
+ subject do
7
+ ForemanNetbox::SyncHost::SyncVirtualMachine::SyncCluster::Find.call(
8
+ host: host, netbox_params: host.netbox_facet.netbox_params
9
+ )
10
+ end
11
+
12
+ let(:host) do
13
+ FactoryBot.build_stubbed(:host, hostname: 'host.dev.example.com').tap do |host|
14
+ host.stubs(:compute?).returns(true)
15
+ host.stubs(:compute_object).returns(
16
+ OpenStruct.new(cluster: 'CLUSTER')
17
+ )
18
+ end
19
+ end
20
+
21
+ setup do
22
+ setup_default_netbox_settings
23
+ end
24
+
25
+ context 'when cluster exists in Netbox' do
26
+ it 'assigns cluster to context' do
27
+ stub_get = stub_request(:get, "#{Setting[:netbox_url]}/api/virtualization/clusters.json").with(
28
+ query: { limit: 50, name: host.compute_object.cluster }
29
+ ).to_return(
30
+ status: 200, headers: { 'Content-Type': 'application/json' },
31
+ body: {
32
+ count: 1,
33
+ results: [{ id: 1, name: host.compute_object.cluster }]
34
+ }.to_json
35
+ )
36
+
37
+ assert_equal 1, subject.cluster.id
38
+ assert_requested(stub_get)
39
+ end
40
+ end
41
+
42
+ context 'when cluster does not exist in NetBox' do
43
+ it 'does not assign cluster to context' do
44
+ stub_get = stub_request(:get, "#{Setting[:netbox_url]}/api/virtualization/clusters.json").with(
45
+ query: { limit: 50, name: host.compute_object.cluster }
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.cluster
55
+ assert_requested(stub_get)
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_plugin_helper'
4
+
5
+ class CreateClusterTypeTest < ActiveSupport::TestCase
6
+ subject do
7
+ ForemanNetbox::SyncHost::SyncVirtualMachine::SyncCluster::SyncClusterType::Create.call(
8
+ host: host, cluster_type: cluster_type, netbox_params: host.netbox_facet.netbox_params
9
+ )
10
+ end
11
+
12
+ let(:cluster_type_id) { 1 }
13
+ let(:cluster_type_params) { host.netbox_facet.netbox_params.fetch(:cluster_type) }
14
+ let(:host) do
15
+ FactoryBot.build_stubbed(:host).tap do |host|
16
+ host.stubs(:compute?).returns(true)
17
+ host.stubs(:compute_resource).returns(
18
+ OpenStruct.new(type: 'Foreman::Model::Vmware')
19
+ )
20
+ end
21
+ end
22
+
23
+ setup do
24
+ setup_default_netbox_settings
25
+ end
26
+
27
+ context 'when cluster_type is not assigned to the context' do
28
+ let(:cluster_type) { nil }
29
+
30
+ it 'creates a cluster_type' do
31
+ stub_post = stub_request(:post, "#{Setting[:netbox_url]}/api/virtualization/cluster-types/").with(
32
+ body: cluster_type_params.to_json
33
+ ).to_return(
34
+ status: 201, headers: { 'Content-Type': 'application/json' },
35
+ body: { id: cluster_type_id }.to_json
36
+ )
37
+
38
+ assert_equal cluster_type_id, subject.cluster_type.id
39
+ assert_requested(stub_post)
40
+ end
41
+ end
42
+
43
+ context 'when cluster_type is already assigned to the context' do
44
+ let(:cluster_type) { OpenStruct.new(id: cluster_type_id) }
45
+
46
+ it 'does not create a cluster_type' do
47
+ assert_equal cluster_type_id, subject.cluster_type.id
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'test_plugin_helper'
4
+
5
+ class FindClusterTypeTest < ActiveSupport::TestCase
6
+ subject do
7
+ ForemanNetbox::SyncHost::SyncVirtualMachine::SyncCluster::SyncClusterType::Find.call(
8
+ host: host, netbox_params: host.netbox_facet.netbox_params
9
+ )
10
+ end
11
+
12
+ let(:host) do
13
+ FactoryBot.build_stubbed(:host, hostname: 'host.dev.example.com').tap do |host|
14
+ host.stubs(:compute?).returns(true)
15
+ host.stubs(:compute_resource).returns(
16
+ OpenStruct.new(type: 'Foreman::Model::Vmware')
17
+ )
18
+ end
19
+ end
20
+ let(:cluster_type_params) { host.netbox_facet.netbox_params.fetch(:cluster_type) }
21
+
22
+ setup do
23
+ setup_default_netbox_settings
24
+ end
25
+
26
+ context 'when cluster type exists in Netbox' do
27
+ it 'assigns cluster_type to context' do
28
+ stub_get = stub_request(:get, "#{Setting[:netbox_url]}/api/virtualization/cluster-types.json").with(
29
+ query: { limit: 50, slug: cluster_type_params[:slug] }
30
+ ).to_return(
31
+ status: 200, headers: { 'Content-Type': 'application/json' },
32
+ body: {
33
+ count: 1,
34
+ results: [
35
+ {
36
+ id: 1,
37
+ name: cluster_type_params[:name],
38
+ slug: cluster_type_params[:slug]
39
+ }
40
+ ]
41
+ }.to_json
42
+ )
43
+
44
+ assert_equal 1, subject.cluster_type.id
45
+ assert_requested(stub_get)
46
+ end
47
+ end
48
+
49
+ context 'when cluster type does not exist in NetBox' do
50
+ it 'does not assign cluster_type to context' do
51
+ stub_get = stub_request(:get, "#{Setting[:netbox_url]}/api/virtualization/cluster-types.json").with(
52
+ query: { limit: 50, slug: cluster_type_params[:slug] }
53
+ ).to_return(
54
+ status: 200, headers: { 'Content-Type': 'application/json' },
55
+ body: {
56
+ count: 0,
57
+ results: []
58
+ }.to_json
59
+ )
60
+
61
+ assert_nil subject.cluster_type
62
+ assert_requested(stub_get)
63
+ end
64
+ end
65
+ end