foreman_netbox 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/LICENSE +619 -0
- data/README.md +50 -0
- data/Rakefile +50 -0
- data/app/contracts/foreman_netbox/device_contract.rb +42 -0
- data/app/contracts/foreman_netbox/params/interface_params.rb +16 -0
- data/app/contracts/foreman_netbox/params/ip_address_params.rb +15 -0
- data/app/contracts/foreman_netbox/params/tenant_params.rb +13 -0
- data/app/contracts/foreman_netbox/virtual_machine_contract.rb +29 -0
- data/app/interactors/foreman_netbox/concerns/facts.rb +11 -0
- data/app/interactors/foreman_netbox/concerns/primary_ips.rb +25 -0
- data/app/interactors/foreman_netbox/delete_host/delete_device.rb +20 -0
- data/app/interactors/foreman_netbox/delete_host/delete_virtual_machine.rb +20 -0
- data/app/interactors/foreman_netbox/delete_host/organizer.rb +12 -0
- data/app/interactors/foreman_netbox/sync_host/organizer.rb +35 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/create.rb +36 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/find.rb +41 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/organizer.rb +30 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/save_netbox_url.rb +29 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_role/create.rb +32 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_role/find.rb +28 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_role/organizer.rb +20 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/create.rb +34 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/find.rb +28 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/organizer.rb +22 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/sync_manufacturer/create.rb +34 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/sync_manufacturer/find.rb +40 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/sync_manufacturer/organizer.rb +22 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/update.rb +33 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/create.rb +37 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/delete.rb +36 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/find.rb +30 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/organizer.rb +23 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/create.rb +46 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/delete.rb +40 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/find.rb +32 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/organizer.rb +24 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/update.rb +39 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/update.rb +38 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_site/create.rb +32 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_site/find.rb +28 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_site/organizer.rb +25 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_site/update.rb +35 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/update.rb +85 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/validate.rb +25 -0
- data/app/interactors/foreman_netbox/sync_host/sync_tenant/create.rb +30 -0
- data/app/interactors/foreman_netbox/sync_host/sync_tenant/find.rb +26 -0
- data/app/interactors/foreman_netbox/sync_host/sync_tenant/organizer.rb +19 -0
- data/app/interactors/foreman_netbox/sync_host/sync_tenant/update.rb +33 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/create.rb +34 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/find.rb +34 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/organizer.rb +28 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/save_netbox_url.rb +31 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/create.rb +35 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/find.rb +28 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/organizer.rb +22 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/sync_cluster_type/create.rb +34 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/sync_cluster_type/find.rb +32 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/sync_cluster_type/organizer.rb +22 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/update.rb +35 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/create.rb +33 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/delete.rb +36 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/find.rb +30 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/organizer.rb +23 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/create.rb +46 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/delete.rb +40 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/find.rb +28 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/organizer.rb +24 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/update.rb +39 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/update.rb +38 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/update.rb +82 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/validate.rb +25 -0
- data/app/jobs/foreman_netbox/sync_host_job.rb +19 -0
- data/app/lib/foreman_netbox/api.rb +12 -0
- data/app/models/concerns/foreman_netbox/host_extensions.rb +27 -0
- data/app/models/concerns/foreman_netbox/location_extensions.rb +13 -0
- data/app/models/concerns/foreman_netbox/nic/base_extensions.rb +23 -0
- data/app/models/concerns/foreman_netbox/user_usergroup_common_extensions.rb +18 -0
- data/app/models/foreman_netbox/netbox_facet.rb +34 -0
- data/app/models/orchestration/netbox.rb +64 -0
- data/app/models/setting/netbox.rb +32 -0
- data/app/services/foreman_netbox/cached_netbox_parameters.rb +103 -0
- data/app/services/foreman_netbox/netbox_parameters.rb +185 -0
- data/app/services/foreman_netbox/netbox_parameters_comparator.rb +96 -0
- data/app/views/hosts/_netbox_tab.html.erb +28 -0
- data/config/routes.rb +4 -0
- data/db/migrate/20200527135812_create_netbox_facet.rb +12 -0
- data/db/migrate/20200604141933_add_sync_info_to_netbox_facet.rb +8 -0
- data/db/migrate/20200708093607_add_raw_data_to_netbox_facet.rb +7 -0
- data/lib/foreman_netbox.rb +6 -0
- data/lib/foreman_netbox/engine.rb +64 -0
- data/lib/foreman_netbox/version.rb +5 -0
- data/lib/tasks/foreman_netbox_tasks.rake +67 -0
- data/test/factories/host.rb +17 -0
- data/test/factories/netbox_facet.rb +17 -0
- data/test/fixtures/facts/k8s_physical_host.json +328 -0
- data/test/fixtures/facts/rhel_physical_host.json +512 -0
- data/test/fixtures/facts/rhel_virtual_host.json +455 -0
- data/test/fixtures/netbox_device_raw_data.json +330 -0
- data/test/fixtures/netbox_virtual_machine_raw_data.json +241 -0
- data/test/integration/foreman_netbox/sync_k8s_physical_host_test.rb +81 -0
- data/test/integration/foreman_netbox/sync_rhel_physical_host_test.rb +81 -0
- data/test/integration/foreman_netbox/sync_rhel_virtual_host_test.rb +93 -0
- data/test/interactors/foreman_netbox/sync_host/organizer_test.rb +78 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/create_test.rb +65 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/find_test.rb +105 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/save_netbox_url_test.rb +39 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_role/create_test.rb +45 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_role/find_test.rb +58 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/create_test.rb +55 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/find_test.rb +63 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/sync_manufacturer/create_test.rb +48 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/sync_manufacturer/find_test.rb +105 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/update_test.rb +54 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/create_test.rb +51 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/delete_test.rb +41 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/find_test.rb +53 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/create_test.rb +80 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/delete_test.rb +72 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/find_test.rb +51 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/update_test.rb +64 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/update_test.rb +92 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_site/create_test.rb +51 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_site/find_test.rb +59 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_site/update_test.rb +54 -0
- data/test/interactors/foreman_netbox/sync_host/sync_device/update_test.rb +188 -0
- data/test/interactors/foreman_netbox/sync_host/sync_tenant/create_test.rb +50 -0
- data/test/interactors/foreman_netbox/sync_host/sync_tenant/find_test.rb +58 -0
- data/test/interactors/foreman_netbox/sync_host/sync_tenant/update_test.rb +59 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/create_test.rb +74 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/find_test.rb +77 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/save_netbox_url_test.rb +39 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/create_test.rb +54 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/find_test.rb +58 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/sync_cluster_type/create_test.rb +50 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/sync_cluster_type/find_test.rb +65 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/update_test.rb +54 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/create_test.rb +53 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/delete_test.rb +37 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/find_test.rb +53 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/create_test.rb +80 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/delete_test.rb +73 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/find_test.rb +51 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/update_test.rb +64 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/update_test.rb +88 -0
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/update_test.rb +171 -0
- data/test/jobs/foreman_netbox/sync_host_job_test.rb +14 -0
- data/test/models/foreman_netbox/location_test.rb +42 -0
- data/test/models/foreman_netbox/netbox_facet_test.rb +219 -0
- data/test/models/foreman_netbox/nics/base_test.rb +81 -0
- data/test/models/foreman_netbox/usergroup_test.rb +23 -0
- data/test/models/host/managed_test.rb +63 -0
- data/test/services/foreman_netbox/netbox_attributes_test.rb +278 -0
- data/test/services/foreman_netbox/netbox_parameters_comparator_test.rb +136 -0
- data/test/test_plugin_helper.rb +25 -0
- metadata +343 -0
@@ -0,0 +1,51 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'test_plugin_helper'
|
4
|
+
|
5
|
+
class CreateSiteTest < ActiveSupport::TestCase
|
6
|
+
subject do
|
7
|
+
ForemanNetbox::SyncHost::SyncDevice::SyncSite::Create.call(
|
8
|
+
host: host, site: site, netbox_params: host.netbox_facet.netbox_params
|
9
|
+
)
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:site_id) { 1 }
|
13
|
+
let(:host) do
|
14
|
+
FactoryBot.build_stubbed(
|
15
|
+
:host,
|
16
|
+
location: FactoryBot.build_stubbed(:location)
|
17
|
+
)
|
18
|
+
end
|
19
|
+
|
20
|
+
setup do
|
21
|
+
setup_default_netbox_settings
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'when site is not assigned to the context' do
|
25
|
+
let(:site) { nil }
|
26
|
+
|
27
|
+
it 'assigns site to context' do
|
28
|
+
stub_post = stub_request(:post, "#{Setting[:netbox_url]}/api/dcim/sites/").with(
|
29
|
+
body: {
|
30
|
+
name: host.location.netbox_site_name,
|
31
|
+
slug: host.location.netbox_site_slug,
|
32
|
+
tags: ForemanNetbox::NetboxParameters::DEFAULT_TAGS
|
33
|
+
}.to_json
|
34
|
+
).to_return(
|
35
|
+
status: 201, headers: { 'Content-Type': 'application/json' },
|
36
|
+
body: { id: site_id }.to_json
|
37
|
+
)
|
38
|
+
|
39
|
+
assert_equal site_id, subject.site.id
|
40
|
+
assert_requested(stub_post)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
context 'when site is already assigned to the context' do
|
45
|
+
let(:site) { OpenStruct.new(id: site_id) }
|
46
|
+
|
47
|
+
it 'does not change site' do
|
48
|
+
assert_equal site_id, subject.site.id
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'test_plugin_helper'
|
4
|
+
|
5
|
+
class FindSiteTest < ActiveSupport::TestCase
|
6
|
+
subject do
|
7
|
+
ForemanNetbox::SyncHost::SyncDevice::SyncSite::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(
|
15
|
+
:host,
|
16
|
+
location: FactoryBot.build_stubbed(:location)
|
17
|
+
)
|
18
|
+
end
|
19
|
+
|
20
|
+
setup do
|
21
|
+
setup_default_netbox_settings
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'when site exists in Netbox' do
|
25
|
+
it 'assigns site to context' do
|
26
|
+
stub_get = stub_request(:get, "#{Setting[:netbox_url]}/api/dcim/sites.json").with(
|
27
|
+
query: { limit: 50, slug: host.location.netbox_site_slug }
|
28
|
+
).to_return(
|
29
|
+
status: 200, headers: { 'Content-Type': 'application/json' },
|
30
|
+
body: {
|
31
|
+
count: 1,
|
32
|
+
results: [
|
33
|
+
{ id: 1, name: host.location.netbox_site_name, slug: host.location.netbox_site_slug }
|
34
|
+
]
|
35
|
+
}.to_json
|
36
|
+
)
|
37
|
+
|
38
|
+
assert_equal 1, subject.site.id
|
39
|
+
assert_requested(stub_get)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context 'when site does not exist in NetBox' do
|
44
|
+
it 'does not assign site to context' do
|
45
|
+
stub_get = stub_request(:get, "#{Setting[:netbox_url]}/api/dcim/sites.json").with(
|
46
|
+
query: { limit: 50, slug: host.location.netbox_site_slug }
|
47
|
+
).to_return(
|
48
|
+
status: 200, headers: { 'Content-Type': 'application/json' },
|
49
|
+
body: {
|
50
|
+
count: 0,
|
51
|
+
results: []
|
52
|
+
}.to_json
|
53
|
+
)
|
54
|
+
|
55
|
+
assert_nil subject.site
|
56
|
+
assert_requested(stub_get)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'test_plugin_helper'
|
4
|
+
|
5
|
+
class UpdateSiteTest < ActiveSupport::TestCase
|
6
|
+
subject do
|
7
|
+
ForemanNetbox::SyncHost::SyncDevice::SyncSite::Update.call(
|
8
|
+
host: host, site: site, netbox_params: host.netbox_facet.netbox_params
|
9
|
+
)
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:host) { FactoryBot.build_stubbed(:host) }
|
13
|
+
let(:site) do
|
14
|
+
ForemanNetbox::API.client::DCIM::Site.new(id: 1).tap do |site|
|
15
|
+
site.instance_variable_set(
|
16
|
+
:@data,
|
17
|
+
{ 'id' => 1, 'tags' => site_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(:site_tags) { [] }
|
28
|
+
|
29
|
+
it 'updates site' do
|
30
|
+
stub_patch = stub_request(:patch, "#{Setting[:netbox_url]}/api/dcim/sites/1.json").with(
|
31
|
+
body: {
|
32
|
+
tags: host.netbox_facet.netbox_params.dig(:site, :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(:site_tags) { host.netbox_facet.netbox_params.dig(:site, :tags) }
|
46
|
+
|
47
|
+
it 'does not update site' do
|
48
|
+
stub_patch = stub_request(:patch, "#{Setting[:netbox_url]}/api/dcim/sites/1.json")
|
49
|
+
|
50
|
+
assert subject.success?
|
51
|
+
assert_not_requested stub_patch
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,188 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'test_plugin_helper'
|
4
|
+
|
5
|
+
class UpdateDeviceTest < ActiveSupport::TestCase
|
6
|
+
subject do
|
7
|
+
ForemanNetbox::SyncHost::SyncDevice::Update.call(
|
8
|
+
device: device,
|
9
|
+
host: host,
|
10
|
+
netbox_params: host.netbox_facet.netbox_params,
|
11
|
+
device_role: device_role,
|
12
|
+
device_type: device_type,
|
13
|
+
site: site,
|
14
|
+
tenant: tenant,
|
15
|
+
ip_addresses: ip_addresses
|
16
|
+
)
|
17
|
+
end
|
18
|
+
|
19
|
+
let(:device) do
|
20
|
+
ForemanNetbox::API.client::DCIM::Device.new(id: 1).tap do |device|
|
21
|
+
device.instance_variable_set(
|
22
|
+
:@data,
|
23
|
+
{
|
24
|
+
'id' => 1,
|
25
|
+
'name' => device_name,
|
26
|
+
'device_role' => { 'id' => 1 },
|
27
|
+
'device_type' => { 'id' => 1 },
|
28
|
+
'site' => { 'id' => 1 },
|
29
|
+
'tenant' => { 'id' => 1 },
|
30
|
+
'serial' => 'old123',
|
31
|
+
'primary_ip4' => {
|
32
|
+
'id' => 1,
|
33
|
+
'family' => 4,
|
34
|
+
'address' => '10.0.0.8/24'
|
35
|
+
},
|
36
|
+
'primary_ip6' => {
|
37
|
+
'id' => 2,
|
38
|
+
'family' => 6,
|
39
|
+
'address' => '1600:0:2d0:202::18/64'
|
40
|
+
},
|
41
|
+
'tags' => device_tags
|
42
|
+
}
|
43
|
+
)
|
44
|
+
end
|
45
|
+
end
|
46
|
+
let(:device_name) { 'name.example.com' }
|
47
|
+
let(:device_tags) { ['tag'] }
|
48
|
+
let(:device_data) { device.instance_variable_get(:@data).deep_symbolize_keys }
|
49
|
+
let(:device_role) { OpenStruct.new(id: device_data.dig(:device_role, :id)) }
|
50
|
+
let(:device_type) { OpenStruct.new(id: device_data.dig(:device_type, :id)) }
|
51
|
+
let(:site) { OpenStruct.new(id: device_data.dig(:site, :id)) }
|
52
|
+
let(:tenant) { OpenStruct.new(id: device_data.dig(:tenant, :id)) }
|
53
|
+
let(:primary_ip4) do
|
54
|
+
OpenStruct.new(
|
55
|
+
id: device_data.dig(:primary_ip4, :id),
|
56
|
+
address: OpenStruct.new(
|
57
|
+
address: device_data.dig(:primary_ip4, :address).split('/')[0]
|
58
|
+
)
|
59
|
+
)
|
60
|
+
end
|
61
|
+
let(:primary_ip6) do
|
62
|
+
OpenStruct.new(
|
63
|
+
id: device_data.dig(:primary_ip6, :id),
|
64
|
+
address: OpenStruct.new(
|
65
|
+
address: device_data.dig(:primary_ip6, :address).split('/')[0]
|
66
|
+
)
|
67
|
+
)
|
68
|
+
end
|
69
|
+
let(:serialnumber) { device_data[:serial] }
|
70
|
+
let(:ip_addresses) { ForemanNetbox::API.client.ipam.ip_addresses.filter(device_id: device.id) }
|
71
|
+
let(:host) do
|
72
|
+
FactoryBot.build_stubbed(
|
73
|
+
:host,
|
74
|
+
hostname: device_name,
|
75
|
+
ip: primary_ip4.address.address,
|
76
|
+
ip6: primary_ip6.address.address
|
77
|
+
).tap do |host|
|
78
|
+
host.stubs(:facts).returns({ 'serialnumber' => serialnumber })
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
setup do
|
83
|
+
setup_default_netbox_settings
|
84
|
+
stub_request(:get, "#{Setting[:netbox_url]}/api/ipam/ip-addresses.json").with(
|
85
|
+
query: { limit: 50, device_id: device.id }
|
86
|
+
).to_return(
|
87
|
+
status: 200, headers: { 'Content-Type': 'application/json' },
|
88
|
+
body: {
|
89
|
+
count: 2,
|
90
|
+
results: [
|
91
|
+
{ id: primary_ip4.id, address: primary_ip4.address.address },
|
92
|
+
{ id: primary_ip6.id, address: primary_ip6.address.address }
|
93
|
+
]
|
94
|
+
}.to_json
|
95
|
+
)
|
96
|
+
end
|
97
|
+
|
98
|
+
context 'if the host has not been updated since the last synchronization' do
|
99
|
+
let(:device_tags) { ForemanNetbox::NetboxParameters::DEFAULT_TAGS }
|
100
|
+
|
101
|
+
it 'does not update device' do
|
102
|
+
stub_patch = stub_request(:patch, "#{Setting[:netbox_url]}/api/dcim/devices/#{device.id}.json")
|
103
|
+
|
104
|
+
assert_equal device, subject.device
|
105
|
+
assert_not_requested(stub_patch)
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
context 'if the host has been updated since the last synchronization' do
|
110
|
+
let(:device_role) { OpenStruct.new(id: 2) }
|
111
|
+
let(:device_type) { OpenStruct.new(id: 2) }
|
112
|
+
let(:site) { OpenStruct.new(id: 2) }
|
113
|
+
let(:tenant) { OpenStruct.new(id: 2) }
|
114
|
+
let(:serialnumber) { 'new123' }
|
115
|
+
let(:primary_ip4) do
|
116
|
+
OpenStruct.new(
|
117
|
+
id: 3,
|
118
|
+
address: OpenStruct.new(
|
119
|
+
address: '10.0.0.2'
|
120
|
+
)
|
121
|
+
)
|
122
|
+
end
|
123
|
+
let(:primary_ip6) do
|
124
|
+
OpenStruct.new(
|
125
|
+
id: 4,
|
126
|
+
address: OpenStruct.new(
|
127
|
+
address: '1500:0:2d0:201::2'
|
128
|
+
)
|
129
|
+
)
|
130
|
+
end
|
131
|
+
let(:host) do
|
132
|
+
FactoryBot.build_stubbed(
|
133
|
+
:host,
|
134
|
+
ip: primary_ip4.address.address,
|
135
|
+
ip6: primary_ip6.address.address
|
136
|
+
).tap do |host|
|
137
|
+
host.stubs(:facts).returns({ 'serialnumber' => serialnumber })
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
it 'updates device' do
|
142
|
+
stub_patch = stub_request(:patch, "#{Setting[:netbox_url]}/api/dcim/devices/#{device.id}.json").with(
|
143
|
+
body: {
|
144
|
+
name: host.name,
|
145
|
+
device_role: device_role.id,
|
146
|
+
device_type: device_type.id,
|
147
|
+
primary_ip4: primary_ip4.id,
|
148
|
+
primary_ip6: primary_ip6.id,
|
149
|
+
site: site.id,
|
150
|
+
tenant: tenant.id,
|
151
|
+
serial: serialnumber,
|
152
|
+
tags: device_tags | ForemanNetbox::NetboxParameters::DEFAULT_TAGS
|
153
|
+
}.to_json
|
154
|
+
).to_return(
|
155
|
+
status: 200, headers: { 'Content-Type': 'application/json' },
|
156
|
+
body: { id: 1 }.to_json
|
157
|
+
)
|
158
|
+
|
159
|
+
subject
|
160
|
+
assert_requested(stub_patch)
|
161
|
+
end
|
162
|
+
|
163
|
+
context 'when serialnumber is empty' do
|
164
|
+
let(:serialnumber) { nil }
|
165
|
+
|
166
|
+
it 'updates device' do
|
167
|
+
stub_patch = stub_request(:patch, "#{Setting[:netbox_url]}/api/dcim/devices/#{device.id}.json").with(
|
168
|
+
body: {
|
169
|
+
name: host.name,
|
170
|
+
device_role: device_role.id,
|
171
|
+
device_type: device_type.id,
|
172
|
+
primary_ip4: primary_ip4.id,
|
173
|
+
primary_ip6: primary_ip6.id,
|
174
|
+
site: site.id,
|
175
|
+
tenant: tenant.id,
|
176
|
+
tags: device_tags | ForemanNetbox::NetboxParameters::DEFAULT_TAGS
|
177
|
+
}.to_json
|
178
|
+
).to_return(
|
179
|
+
status: 200, headers: { 'Content-Type': 'application/json' },
|
180
|
+
body: { id: 1 }.to_json
|
181
|
+
)
|
182
|
+
|
183
|
+
subject
|
184
|
+
assert_requested(stub_patch)
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'test_plugin_helper'
|
4
|
+
|
5
|
+
class CreateTenantTest < ActiveSupport::TestCase
|
6
|
+
subject do
|
7
|
+
ForemanNetbox::SyncHost::SyncTenant::Create.call(
|
8
|
+
host: host, tenant: tenant, netbox_params: host.netbox_facet.netbox_params
|
9
|
+
)
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:host) do
|
13
|
+
FactoryBot.build_stubbed(
|
14
|
+
:host,
|
15
|
+
owner: FactoryBot.build_stubbed(:usergroup, name: 'Owner')
|
16
|
+
)
|
17
|
+
end
|
18
|
+
|
19
|
+
setup do
|
20
|
+
setup_default_netbox_settings
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'when tenant is not assigned to the context' do
|
24
|
+
let(:tenant) { nil }
|
25
|
+
|
26
|
+
it 'assigns tenant to context' do
|
27
|
+
stub_post = stub_request(:post, "#{Setting[:netbox_url]}/api/tenancy/tenants/").with(
|
28
|
+
body: {
|
29
|
+
name: host.owner.name,
|
30
|
+
slug: host.owner.name.parameterize,
|
31
|
+
tags: ForemanNetbox::NetboxParameters::DEFAULT_TAGS
|
32
|
+
}.to_json
|
33
|
+
).to_return(
|
34
|
+
status: 201, headers: { 'Content-Type': 'application/json' },
|
35
|
+
body: { id: 1 }.to_json
|
36
|
+
)
|
37
|
+
|
38
|
+
assert_equal 1, subject.tenant.id
|
39
|
+
assert_requested(stub_post)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
context 'when tenant is already assigned to the context' do
|
44
|
+
let(:tenant) { OpenStruct.new }
|
45
|
+
|
46
|
+
it 'does not change tenant' do
|
47
|
+
assert_equal tenant, subject.tenant
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'test_plugin_helper'
|
4
|
+
|
5
|
+
class FindTenantTest < ActiveSupport::TestCase
|
6
|
+
subject do
|
7
|
+
ForemanNetbox::SyncHost::SyncTenant::Find.call(
|
8
|
+
host: host, netbox_params: host.netbox_facet.netbox_params
|
9
|
+
)
|
10
|
+
end
|
11
|
+
|
12
|
+
let(:host) do
|
13
|
+
FactoryBot.build_stubbed(
|
14
|
+
:host,
|
15
|
+
owner: FactoryBot.build_stubbed(:usergroup, name: 'Owner')
|
16
|
+
)
|
17
|
+
end
|
18
|
+
|
19
|
+
setup do
|
20
|
+
setup_default_netbox_settings
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'when tenant exists in Netbox' do
|
24
|
+
it 'assigns tenant to context' do
|
25
|
+
stub_get = stub_request(:get, "#{Setting[:netbox_url]}/api/tenancy/tenants.json").with(
|
26
|
+
query: { limit: 50, slug: host.owner.name.parameterize }
|
27
|
+
).to_return(
|
28
|
+
status: 200, headers: { 'Content-Type': 'application/json' },
|
29
|
+
body: {
|
30
|
+
count: 1,
|
31
|
+
results: [
|
32
|
+
{ id: 1, name: host.owner.name, slug: host.owner.name.parameterize }
|
33
|
+
]
|
34
|
+
}.to_json
|
35
|
+
)
|
36
|
+
|
37
|
+
assert_equal 1, subject.tenant.id
|
38
|
+
assert_requested(stub_get)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context 'when tenant does not exist in NetBox' do
|
43
|
+
it 'does not assign tenant to context' do
|
44
|
+
stub_get = stub_request(:get, "#{Setting[:netbox_url]}/api/tenancy/tenants.json").with(
|
45
|
+
query: { limit: 50, slug: host.owner.name.parameterize }
|
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.tenant
|
55
|
+
assert_requested(stub_get)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|