foreman_netbox 1.0.2 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +7 -0
- data/app/interactors/foreman_netbox/concerns/assign_tags.rb +21 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/create.rb +3 -1
- data/app/interactors/foreman_netbox/sync_host/sync_device/organizer.rb +1 -0
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/create.rb +2 -1
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/update.rb +2 -2
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/create.rb +3 -1
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/create.rb +6 -1
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/delete.rb +1 -1
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/update.rb +2 -14
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/update.rb +2 -3
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_site/create.rb +4 -1
- data/app/interactors/foreman_netbox/sync_host/sync_device/sync_site/update.rb +3 -9
- data/app/interactors/foreman_netbox/sync_host/sync_device/update.rb +3 -7
- data/app/interactors/foreman_netbox/sync_host/sync_tags/create.rb +30 -0
- data/app/interactors/foreman_netbox/sync_host/sync_tags/find.rb +26 -0
- data/app/interactors/foreman_netbox/sync_host/sync_tags/organizer.rb +22 -0
- data/app/interactors/foreman_netbox/sync_host/sync_tenant/create.rb +4 -1
- data/app/interactors/foreman_netbox/sync_host/sync_tenant/update.rb +3 -9
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/create.rb +3 -1
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/organizer.rb +1 -0
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/create.rb +3 -1
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/update.rb +3 -9
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/create.rb +6 -1
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/create.rb +7 -2
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/delete.rb +1 -1
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/update.rb +2 -14
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/update.rb +2 -3
- data/app/interactors/foreman_netbox/sync_host/sync_virtual_machine/update.rb +3 -7
- data/app/models/concerns/foreman_netbox/user_usergroup_common_extensions.rb +1 -1
- data/app/models/orchestration/netbox.rb +3 -0
- data/app/services/foreman_netbox/cached_netbox_parameters.rb +8 -8
- data/app/services/foreman_netbox/netbox_parameters.rb +8 -17
- data/app/services/foreman_netbox/netbox_parameters_comparator.rb +2 -2
- data/lib/foreman_netbox/version.rb +1 -1
- data/test/integration/foreman_netbox/sync_k8s_physical_host_test.rb +12 -8
- data/test/integration/foreman_netbox/sync_rhel_physical_host_test.rb +12 -8
- data/test/integration/foreman_netbox/sync_rhel_virtual_host_test.rb +12 -8
- data/test/interactors/foreman_netbox/sync_host/organizer_test.rb +18 -2
- data/test/interactors/foreman_netbox/sync_host/sync_device/create_test.rb +5 -3
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/create_test.rb +4 -3
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_device_type/update_test.rb +8 -3
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/create_test.rb +8 -4
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/create_test.rb +10 -5
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/delete_test.rb +2 -2
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/sync_ip_addresses/update_test.rb +5 -3
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_interfaces/update_test.rb +7 -5
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_site/create_test.rb +2 -2
- data/test/interactors/foreman_netbox/sync_host/sync_device/sync_site/update_test.rb +4 -3
- data/test/interactors/foreman_netbox/sync_host/sync_device/update_test.rb +19 -5
- data/test/interactors/foreman_netbox/sync_host/sync_tenant/create_test.rb +2 -2
- data/test/interactors/foreman_netbox/sync_host/sync_tenant/update_test.rb +5 -3
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/create_test.rb +4 -3
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/create_test.rb +7 -3
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_cluster/update_test.rb +5 -3
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/create_test.rb +5 -4
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/create_test.rb +9 -7
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/delete_test.rb +2 -2
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/sync_ip_addresses/update_test.rb +13 -4
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/sync_interfaces/update_test.rb +9 -4
- data/test/interactors/foreman_netbox/sync_host/sync_virtual_machine/update_test.rb +7 -4
- data/test/models/foreman_netbox/usergroup_test.rb +4 -4
- data/test/services/foreman_netbox/netbox_attributes_test.rb +10 -19
- data/test/services/foreman_netbox/netbox_parameters_comparator_test.rb +6 -16
- data/test/test_plugin_helper.rb +15 -3
- metadata +127 -53
@@ -30,6 +30,9 @@ module Orchestration
|
|
30
30
|
def queue_netbox_destroy
|
31
31
|
return unless errors.empty? && managed?
|
32
32
|
|
33
|
+
::Foreman::Logging.logger('foreman_netbox/import')
|
34
|
+
.info("Queued delete of #{name} from Netbox.")
|
35
|
+
|
33
36
|
queue.create(name: _('Delete host %s from Netbox') % self, priority: 60,
|
34
37
|
action: [self, :del_netbox])
|
35
38
|
end
|
@@ -28,7 +28,7 @@ module ForemanNetbox
|
|
28
28
|
return unless data.keys.include?(:interfaces)
|
29
29
|
|
30
30
|
result[:interfaces] = data.fetch(:interfaces, {}).fetch(:results, []).map do |interface|
|
31
|
-
interface.slice(:name, :mac_address
|
31
|
+
interface.slice(:name, :mac_address)
|
32
32
|
.merge(type: interface.fetch(:type, {}).slice(:value))
|
33
33
|
.compact
|
34
34
|
end
|
@@ -38,7 +38,7 @@ module ForemanNetbox
|
|
38
38
|
return unless data.keys.include?(:ip_addresses)
|
39
39
|
|
40
40
|
result[:ip_addresses] = data.fetch(:ip_addresses, {}).fetch(:results, []).map do |ip_address|
|
41
|
-
ip_address.slice(:address
|
41
|
+
ip_address.slice(:address)
|
42
42
|
.merge(interface: ip_address.fetch(:interface, {}).slice(:name))
|
43
43
|
end
|
44
44
|
end
|
@@ -46,19 +46,19 @@ module ForemanNetbox
|
|
46
46
|
def read_virtual_machine
|
47
47
|
return unless data.keys.include?(:virtual_machine)
|
48
48
|
|
49
|
-
result[:virtual_machine] = data.fetch(:virtual_machine, {}).slice(:name, :vcpus, :memory, :disk
|
49
|
+
result[:virtual_machine] = data.fetch(:virtual_machine, {}).slice(:name, :vcpus, :memory, :disk)
|
50
50
|
end
|
51
51
|
|
52
52
|
def read_tenant
|
53
53
|
return unless data.keys.include?(:tenant)
|
54
54
|
|
55
|
-
result[:tenant] = data.fetch(:tenant, {}).slice(:name, :slug
|
55
|
+
result[:tenant] = data.fetch(:tenant, {}).slice(:name, :slug)
|
56
56
|
end
|
57
57
|
|
58
58
|
def read_cluster
|
59
59
|
return unless data.keys.include?(:cluster)
|
60
60
|
|
61
|
-
result[:cluster] = data.fetch(:cluster, {}).slice(:name
|
61
|
+
result[:cluster] = data.fetch(:cluster, {}).slice(:name)
|
62
62
|
end
|
63
63
|
|
64
64
|
def read_cluster_type
|
@@ -71,7 +71,7 @@ module ForemanNetbox
|
|
71
71
|
return unless data.keys.include?(:device)
|
72
72
|
|
73
73
|
result[:device] = data.fetch(:device, {})
|
74
|
-
.slice(:name, :serial
|
74
|
+
.slice(:name, :serial)
|
75
75
|
.compact
|
76
76
|
.reject { |k, v| k == :serial && v.blank? }
|
77
77
|
end
|
@@ -85,7 +85,7 @@ module ForemanNetbox
|
|
85
85
|
def read_device_type
|
86
86
|
return unless data.keys.include?(:device_type)
|
87
87
|
|
88
|
-
result[:device_type] = data.fetch(:device_type, {}).slice(:model, :slug
|
88
|
+
result[:device_type] = data.fetch(:device_type, {}).slice(:model, :slug)
|
89
89
|
end
|
90
90
|
|
91
91
|
def read_manufacturer
|
@@ -97,7 +97,7 @@ module ForemanNetbox
|
|
97
97
|
def read_site
|
98
98
|
return unless data.keys.include?(:site)
|
99
99
|
|
100
|
-
result[:site] = data.fetch(:site, {}).slice(:name, :slug
|
100
|
+
result[:site] = data.fetch(:site, {}).slice(:name, :slug)
|
101
101
|
end
|
102
102
|
end
|
103
103
|
end
|
@@ -4,7 +4,6 @@ module ForemanNetbox
|
|
4
4
|
# rubocop:disable Metrics/ClassLength
|
5
5
|
class NetboxParameters
|
6
6
|
UNKNOWN = 'Unknown'
|
7
|
-
DEFAULT_TAGS = ['foreman'].freeze
|
8
7
|
DEFAULT_INTERFACE_TYPE = 'virtual'
|
9
8
|
CLUSTER_TYPES = {
|
10
9
|
:'Foreman::Model::Vmware' => {
|
@@ -56,8 +55,7 @@ module ForemanNetbox
|
|
56
55
|
{
|
57
56
|
tenant: {
|
58
57
|
name: host.owner&.netbox_tenant_name,
|
59
|
-
slug: host.owner&.netbox_tenant_slug
|
60
|
-
tags: DEFAULT_TAGS
|
58
|
+
slug: host.owner&.netbox_tenant_slug
|
61
59
|
}
|
62
60
|
}
|
63
61
|
end
|
@@ -66,8 +64,7 @@ module ForemanNetbox
|
|
66
64
|
{
|
67
65
|
device: {
|
68
66
|
name: host.name,
|
69
|
-
serial: host.facts.deep_symbolize_keys[:serialnumber]
|
70
|
-
tags: DEFAULT_TAGS
|
67
|
+
serial: host.facts.deep_symbolize_keys[:serialnumber]
|
71
68
|
}
|
72
69
|
}
|
73
70
|
end
|
@@ -84,8 +81,7 @@ module ForemanNetbox
|
|
84
81
|
{
|
85
82
|
device_type: {
|
86
83
|
model: model,
|
87
|
-
slug: model.parameterize
|
88
|
-
tags: DEFAULT_TAGS
|
84
|
+
slug: model.parameterize
|
89
85
|
}
|
90
86
|
}
|
91
87
|
end
|
@@ -105,8 +101,7 @@ module ForemanNetbox
|
|
105
101
|
{
|
106
102
|
site: {
|
107
103
|
name: host.location&.netbox_site_name,
|
108
|
-
slug: host.location&.netbox_site_slug
|
109
|
-
tags: DEFAULT_TAGS
|
104
|
+
slug: host.location&.netbox_site_slug
|
110
105
|
}
|
111
106
|
}
|
112
107
|
end
|
@@ -122,8 +117,7 @@ module ForemanNetbox
|
|
122
117
|
mac_address: mac_address,
|
123
118
|
type: {
|
124
119
|
value: DEFAULT_INTERFACE_TYPE
|
125
|
-
}
|
126
|
-
tags: DEFAULT_TAGS
|
120
|
+
}
|
127
121
|
}
|
128
122
|
end
|
129
123
|
}
|
@@ -144,8 +138,7 @@ module ForemanNetbox
|
|
144
138
|
address: ip_address,
|
145
139
|
interface: {
|
146
140
|
name: identifier || (mac_address && "Interface #{mac_address}")
|
147
|
-
}
|
148
|
-
tags: DEFAULT_TAGS
|
141
|
+
}
|
149
142
|
}
|
150
143
|
end
|
151
144
|
end.flatten
|
@@ -158,8 +151,7 @@ module ForemanNetbox
|
|
158
151
|
virtual_machine: netbox_compute_attributes.slice(
|
159
152
|
:vcpus, :memory, :disk
|
160
153
|
).merge(
|
161
|
-
name: host.name
|
162
|
-
tags: DEFAULT_TAGS
|
154
|
+
name: host.name
|
163
155
|
)
|
164
156
|
}
|
165
157
|
end
|
@@ -167,8 +159,7 @@ module ForemanNetbox
|
|
167
159
|
def cluster
|
168
160
|
{
|
169
161
|
cluster: {
|
170
|
-
name: netbox_compute_attributes[:cluster]
|
171
|
-
tags: DEFAULT_TAGS
|
162
|
+
name: netbox_compute_attributes[:cluster]
|
172
163
|
}.compact
|
173
164
|
}
|
174
165
|
end
|
@@ -48,7 +48,7 @@ module ForemanNetbox
|
|
48
48
|
|
49
49
|
memo[key] = diff
|
50
50
|
elsif old_value.is_a?(Array)
|
51
|
-
next
|
51
|
+
next if new_hash[key]
|
52
52
|
|
53
53
|
memo[key] = { added: [], removed: old_value }
|
54
54
|
else
|
@@ -77,7 +77,7 @@ module ForemanNetbox
|
|
77
77
|
elsif new_value.is_a?(Array)
|
78
78
|
old_value = old_hash.fetch(key, [])
|
79
79
|
added = new_value.reject { |item| old_value.find { |x| x == item } }
|
80
|
-
removed =
|
80
|
+
removed = old_value.reject { |item| new_value.find { |x| x == item } }
|
81
81
|
|
82
82
|
next unless added.any? || removed.any?
|
83
83
|
|
@@ -62,20 +62,24 @@ class SyncK8sPhysicalHostTest < ActiveSupport::TestCase
|
|
62
62
|
assert nx_interface.present?
|
63
63
|
|
64
64
|
h_interface.netbox_ips.each do |h_ip|
|
65
|
-
assert subject.ip_addresses.find
|
65
|
+
assert subject.ip_addresses.find do |nx_ip|
|
66
|
+
nx_ip.interface.assigned_object_type == 'dcim.interface' &&
|
67
|
+
nx_ip.interface.assigned_object_id == nx_interface.id &&
|
68
|
+
nx_ip.address == IPAddress.parse(h_ip)
|
69
|
+
end.present?
|
66
70
|
end
|
67
71
|
end
|
68
72
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
73
|
+
expected_tag_ids = subject.tags.pluck('id')
|
74
|
+
assert (expected_tag_ids - subject.device.tags.pluck('id')).empty?
|
75
|
+
assert (expected_tag_ids - subject.device_type.tags.pluck('id')).empty?
|
76
|
+
assert (expected_tag_ids - subject.site.tags.pluck('id')).empty?
|
77
|
+
assert (expected_tag_ids - subject.tenant.tags.pluck('id')).empty?
|
74
78
|
subject.interfaces.reload.each do |interface|
|
75
|
-
|
79
|
+
assert (expected_tag_ids - interface.tags.pluck('id')).empty?
|
76
80
|
end
|
77
81
|
subject.ip_addresses.reload.each do |interface|
|
78
|
-
|
82
|
+
assert (expected_tag_ids - interface.tags.pluck('id')).empty?
|
79
83
|
end
|
80
84
|
end
|
81
85
|
end
|
@@ -62,20 +62,24 @@ class SyncRhelPhysicalHostTest < ActiveSupport::TestCase
|
|
62
62
|
assert nx_interface.present?
|
63
63
|
|
64
64
|
h_interface.netbox_ips.each do |h_ip|
|
65
|
-
assert subject.ip_addresses.find
|
65
|
+
assert subject.ip_addresses.find do |nx_ip|
|
66
|
+
nx_ip.interface.assigned_object_type == 'dcim.interface' &&
|
67
|
+
nx_ip.interface.assigned_object_id == nx_interface.id &&
|
68
|
+
nx_ip.address == IPAddress.parse(h_ip)
|
69
|
+
end.present?
|
66
70
|
end
|
67
71
|
end
|
68
72
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
73
|
+
expected_tag_ids = subject.tags.pluck('id')
|
74
|
+
assert (expected_tag_ids - subject.device.tags.pluck('id')).empty?
|
75
|
+
assert (expected_tag_ids - subject.device_type.tags.pluck('id')).empty?
|
76
|
+
assert (expected_tag_ids - subject.site.tags.pluck('id')).empty?
|
77
|
+
assert (expected_tag_ids - subject.tenant.tags.pluck('id')).empty?
|
74
78
|
subject.interfaces.reload.each do |interface|
|
75
|
-
|
79
|
+
assert (expected_tag_ids - interface.tags.pluck('id')).empty?
|
76
80
|
end
|
77
81
|
subject.ip_addresses.reload.each do |interface|
|
78
|
-
|
82
|
+
assert (expected_tag_ids - interface.tags.pluck('id')).empty?
|
79
83
|
end
|
80
84
|
end
|
81
85
|
end
|
@@ -61,7 +61,7 @@ class SyncRhelVirtualHostTest < ActiveSupport::TestCase
|
|
61
61
|
|
62
62
|
assert_equal host.name, subject.virtual_machine.name
|
63
63
|
assert_equal host.owner.netbox_tenant_name, subject.virtual_machine.tenant.name
|
64
|
-
assert_equal host.compute_object.cpus,
|
64
|
+
assert_equal format('%.2f', host.compute_object.cpus), subject.virtual_machine.vcpus
|
65
65
|
assert_equal host.compute_object.memory_mb, subject.virtual_machine.memory
|
66
66
|
assert_equal host.compute_object.volumes.first.size_gb, subject.virtual_machine.disk
|
67
67
|
assert_equal host.compute_object.cluster, subject.virtual_machine.cluster.name
|
@@ -75,19 +75,23 @@ class SyncRhelVirtualHostTest < ActiveSupport::TestCase
|
|
75
75
|
assert nx_interface.present?
|
76
76
|
|
77
77
|
h_interface.netbox_ips.each do |h_ip|
|
78
|
-
assert subject.ip_addresses.find
|
78
|
+
assert subject.ip_addresses.find do |nx_ip|
|
79
|
+
nx_ip.interface.assigned_object_type == 'virtualization.vminterface' &&
|
80
|
+
nx_ip.interface.assigned_object_id == nx_interface.id &&
|
81
|
+
nx_ip.address == IPAddress.parse(h_ip)
|
82
|
+
end.present?
|
79
83
|
end
|
80
84
|
end
|
81
85
|
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
+
expected_tag_ids = subject.tags.pluck('id')
|
87
|
+
assert (expected_tag_ids - subject.virtual_machine.tags.pluck('id')).empty?
|
88
|
+
assert (expected_tag_ids - subject.tenant.tags.pluck('id')).empty?
|
89
|
+
assert (expected_tag_ids - subject.cluster.tags.pluck('id')).empty?
|
86
90
|
subject.interfaces.reload.each do |interface|
|
87
|
-
|
91
|
+
assert (expected_tag_ids - interface.tags.pluck('id')).empty?
|
88
92
|
end
|
89
93
|
subject.ip_addresses.reload.each do |interface|
|
90
|
-
|
94
|
+
assert (expected_tag_ids - interface.tags.pluck('id')).empty?
|
91
95
|
end
|
92
96
|
end
|
93
97
|
end
|
@@ -3,7 +3,7 @@
|
|
3
3
|
require 'test_plugin_helper'
|
4
4
|
|
5
5
|
class SyncHostOrganizerTest < ActiveSupport::TestCase
|
6
|
-
subject { ForemanNetbox::SyncHost::Organizer.call(host: host) }
|
6
|
+
subject { ForemanNetbox::SyncHost::Organizer.call(host: host, tags: default_tags) }
|
7
7
|
|
8
8
|
let(:host) do
|
9
9
|
FactoryBot.create(:host).tap do |host|
|
@@ -11,11 +11,27 @@ class SyncHostOrganizerTest < ActiveSupport::TestCase
|
|
11
11
|
host.stubs(:facts).returns({ serialnumber: 'abc123' })
|
12
12
|
end
|
13
13
|
end
|
14
|
-
let(:tags)
|
14
|
+
let(:tags) do
|
15
|
+
default_tags.map { |t| { 'id' => t.id, 'name' => t.name, 'slug' => t.slug } }
|
16
|
+
end
|
15
17
|
|
16
18
|
setup do
|
17
19
|
setup_default_netbox_settings
|
18
20
|
# rubocop:disable Layout/FirstArrayElementIndentation
|
21
|
+
default_tags.each do |tag|
|
22
|
+
stub_get_netbox_request("extras/tags.json?limit=50&slug=#{tag.slug}", results: [
|
23
|
+
{ id: tag.id, name: tag.name, slug: tag.slug }
|
24
|
+
])
|
25
|
+
stub_request(:get, "#{Setting[:netbox_url]}/api/extras/tags/#{tag.id}.json")
|
26
|
+
.to_return(
|
27
|
+
status: 200, headers: { 'Content-Type': 'application/json' },
|
28
|
+
body: {
|
29
|
+
id: tag.id,
|
30
|
+
name: tag.name,
|
31
|
+
slug: tag.slug
|
32
|
+
}.to_json
|
33
|
+
)
|
34
|
+
end
|
19
35
|
stub_get_netbox_request('tenancy/tenants.json?limit=50&slug=admin-user', results: [
|
20
36
|
{ id: 1, name: host.owner.name, slug: host.owner.name.parameterize, tags: tags }
|
21
37
|
])
|
@@ -11,7 +11,8 @@ class CreateDeviceTest < ActiveSupport::TestCase
|
|
11
11
|
device_role: device_role,
|
12
12
|
site: site,
|
13
13
|
tenant: tenant,
|
14
|
-
device: device
|
14
|
+
device: device,
|
15
|
+
tags: tags
|
15
16
|
)
|
16
17
|
end
|
17
18
|
|
@@ -26,6 +27,7 @@ class CreateDeviceTest < ActiveSupport::TestCase
|
|
26
27
|
let(:site) { OpenStruct.new(id: 1) }
|
27
28
|
let(:tenant) { OpenStruct.new(id: 1) }
|
28
29
|
let(:netbox_device_params) { host.netbox_facet.netbox_params.fetch(:device) }
|
30
|
+
let(:tags) { [ForemanNetbox::API.client::Extras::Tag.new(id: 1, name: 'foreman', slug: 'foreman')] }
|
29
31
|
|
30
32
|
setup do
|
31
33
|
setup_default_netbox_settings
|
@@ -39,11 +41,11 @@ class CreateDeviceTest < ActiveSupport::TestCase
|
|
39
41
|
body: {
|
40
42
|
name: netbox_device_params[:name],
|
41
43
|
serial: netbox_device_params[:serial],
|
42
|
-
tags: netbox_device_params[:tags],
|
43
44
|
device_type: device_type.id,
|
44
45
|
device_role: device_role.id,
|
45
46
|
site: site.id,
|
46
|
-
tenant: tenant.id
|
47
|
+
tenant: tenant.id,
|
48
|
+
tags: tags.map(&:id)
|
47
49
|
}.to_json
|
48
50
|
).to_return(
|
49
51
|
status: 201, headers: { 'Content-Type': 'application/json' },
|
@@ -8,7 +8,8 @@ class CreateDeviceTypeTest < ActiveSupport::TestCase
|
|
8
8
|
netbox_params: host.netbox_facet.netbox_params,
|
9
9
|
device_type: device_type,
|
10
10
|
manufacturer: manufacturer,
|
11
|
-
host: host
|
11
|
+
host: host,
|
12
|
+
tags: default_tags
|
12
13
|
)
|
13
14
|
end
|
14
15
|
|
@@ -32,8 +33,8 @@ class CreateDeviceTypeTest < ActiveSupport::TestCase
|
|
32
33
|
body: {
|
33
34
|
model: host.netbox_facet.netbox_params.dig(:device_type, :model),
|
34
35
|
slug: host.netbox_facet.netbox_params.dig(:device_type, :slug),
|
35
|
-
|
36
|
-
|
36
|
+
manufacturer: manufacturer.id,
|
37
|
+
tags: default_tags.map(&:id)
|
37
38
|
}.to_json
|
38
39
|
).to_return(
|
39
40
|
status: 201, headers: { 'Content-Type': 'application/json' },
|
@@ -5,7 +5,10 @@ require 'test_plugin_helper'
|
|
5
5
|
class UpdateDeviceTypeTest < ActiveSupport::TestCase
|
6
6
|
subject do
|
7
7
|
ForemanNetbox::SyncHost::SyncDevice::SyncDeviceType::Update.call(
|
8
|
-
host: host,
|
8
|
+
host: host,
|
9
|
+
device_type: device_type,
|
10
|
+
netbox_params: host.netbox_facet.netbox_params,
|
11
|
+
tags: default_tags
|
9
12
|
)
|
10
13
|
end
|
11
14
|
|
@@ -29,7 +32,7 @@ class UpdateDeviceTypeTest < ActiveSupport::TestCase
|
|
29
32
|
it 'updates device type' do
|
30
33
|
stub_patch = stub_request(:patch, "#{Setting[:netbox_url]}/api/dcim/device-types/1.json").with(
|
31
34
|
body: {
|
32
|
-
tags:
|
35
|
+
tags: default_tags.map(&:id)
|
33
36
|
}.to_json
|
34
37
|
).to_return(
|
35
38
|
status: 200, headers: { 'Content-Type': 'application/json' },
|
@@ -42,7 +45,9 @@ class UpdateDeviceTypeTest < ActiveSupport::TestCase
|
|
42
45
|
end
|
43
46
|
|
44
47
|
context 'when unchanged' do
|
45
|
-
let(:device_type_tags)
|
48
|
+
let(:device_type_tags) do
|
49
|
+
default_tags.map { |t| { 'id' => t.id, 'name' => t.name, 'slug' => t.slug } }
|
50
|
+
end
|
46
51
|
|
47
52
|
it 'does not update device type' do
|
48
53
|
stub_patch = stub_request(:patch, "#{Setting[:netbox_url]}/api/dcim/device-types/1.json")
|
@@ -5,7 +5,11 @@ require 'test_plugin_helper'
|
|
5
5
|
class CreateDeviceInterfacesTest < ActiveSupport::TestCase
|
6
6
|
subject do
|
7
7
|
ForemanNetbox::SyncHost::SyncDevice::SyncInterfaces::Create.call(
|
8
|
-
host: host,
|
8
|
+
host: host,
|
9
|
+
netbox_params: host.netbox_facet.netbox_params,
|
10
|
+
device: device,
|
11
|
+
interfaces: interfaces,
|
12
|
+
tags: default_tags
|
9
13
|
)
|
10
14
|
end
|
11
15
|
|
@@ -32,10 +36,10 @@ class CreateDeviceInterfacesTest < ActiveSupport::TestCase
|
|
32
36
|
stub_post = stub_request(:post, "#{Setting[:netbox_url]}/api/dcim/interfaces/").with(
|
33
37
|
body: {
|
34
38
|
name: host.interfaces.first.netbox_name,
|
35
|
-
mac_address: host.interfaces.first.mac,
|
36
|
-
tags: ForemanNetbox::NetboxParameters::DEFAULT_TAGS,
|
39
|
+
mac_address: host.interfaces.first.mac.upcase,
|
37
40
|
type: ForemanNetbox::NetboxParameters::DEFAULT_INTERFACE_TYPE,
|
38
|
-
device: device.id
|
41
|
+
device: device.id,
|
42
|
+
tags: default_tags.map(&:id)
|
39
43
|
}.to_json
|
40
44
|
).to_return(
|
41
45
|
status: 201, headers: { 'Content-Type': 'application/json' },
|
@@ -5,7 +5,10 @@ require 'test_plugin_helper'
|
|
5
5
|
class CreateDeviceIpAddressesTest < ActiveSupport::TestCase
|
6
6
|
subject do
|
7
7
|
ForemanNetbox::SyncHost::SyncDevice::SyncInterfaces::SyncIpAddresses::Create.call(
|
8
|
-
host: host,
|
8
|
+
host: host,
|
9
|
+
interfaces: interfaces,
|
10
|
+
netbox_params: host.netbox_facet.netbox_params,
|
11
|
+
tags: default_tags
|
9
12
|
)
|
10
13
|
end
|
11
14
|
|
@@ -54,8 +57,9 @@ class CreateDeviceIpAddressesTest < ActiveSupport::TestCase
|
|
54
57
|
stub_post_ip_address_v4 = stub_request(:post, "#{Setting[:netbox_url]}/api/ipam/ip-addresses/").with(
|
55
58
|
body: {
|
56
59
|
address: host.interfaces.first.netbox_ip,
|
57
|
-
|
58
|
-
|
60
|
+
assigned_object_type: 'dcim.interface',
|
61
|
+
assigned_object_id: interfaces.first.id,
|
62
|
+
tags: default_tags.map(&:id)
|
59
63
|
}.to_json
|
60
64
|
).to_return(
|
61
65
|
status: 201, headers: { 'Content-Type': 'application/json' },
|
@@ -65,8 +69,9 @@ class CreateDeviceIpAddressesTest < ActiveSupport::TestCase
|
|
65
69
|
stub_post_ip_address_v6 = stub_request(:post, "#{Setting[:netbox_url]}/api/ipam/ip-addresses/").with(
|
66
70
|
body: {
|
67
71
|
address: host.interfaces.first.netbox_ip6,
|
68
|
-
|
69
|
-
|
72
|
+
assigned_object_type: 'dcim.interface',
|
73
|
+
assigned_object_id: interfaces.first.id,
|
74
|
+
tags: default_tags.map(&:id)
|
70
75
|
}.to_json
|
71
76
|
).to_return(
|
72
77
|
status: 201, headers: { 'Content-Type': 'application/json' },
|
@@ -50,8 +50,8 @@ class DeleteDeviceIpAddressesTest < ActiveSupport::TestCase
|
|
50
50
|
body: {
|
51
51
|
count: 2,
|
52
52
|
results: [
|
53
|
-
{ id: ip_addresses_v4_id, address: ip_addresses_v4,
|
54
|
-
{ id: ip_addresses_v6_id, address: ip_addresses_v6,
|
53
|
+
{ id: ip_addresses_v4_id, address: ip_addresses_v4, assigned_object_type: 'dcim.interface', assigned_object_id: interface_id },
|
54
|
+
{ id: ip_addresses_v6_id, address: ip_addresses_v6, assigned_object_type: 'dcim.interface', assigned_object_id: interface_id }
|
55
55
|
]
|
56
56
|
}.to_json
|
57
57
|
)
|
@@ -6,14 +6,16 @@ class UpdateDeviceIpAddressesTest < ActiveSupport::TestCase
|
|
6
6
|
subject do
|
7
7
|
ForemanNetbox::SyncHost::SyncDevice::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(device_id: 1) }
|
15
|
+
let(:tags_data) { default_tags.map { |t| { id: t.id, name: t.name, slug: t.slug } } }
|
14
16
|
let(:ip_addresses_data) do
|
15
17
|
[
|
16
|
-
{ id: 1, address: host.netbox_facet.netbox_params[:ip_addresses].first[:address], tags:
|
18
|
+
{ id: 1, address: host.netbox_facet.netbox_params[:ip_addresses].first[:address], tags: tags_data },
|
17
19
|
{ id: 2, address: host.netbox_facet.netbox_params[:ip_addresses].second[:address], tags: [] }
|
18
20
|
]
|
19
21
|
end
|
@@ -50,7 +52,7 @@ class UpdateDeviceIpAddressesTest < ActiveSupport::TestCase
|
|
50
52
|
stub_unexpected_patch = stub_request(:patch, "#{Setting[:netbox_url]}/api/ipam/ip-addresses/#{ip_addresses_data.first[:id]}.json")
|
51
53
|
stub_expected_patch = stub_request(:patch, "#{Setting[:netbox_url]}/api/ipam/ip-addresses/#{ip_addresses_data.second[:id]}.json").with(
|
52
54
|
body: {
|
53
|
-
tags:
|
55
|
+
tags: default_tags.map(&:id)
|
54
56
|
}.to_json
|
55
57
|
).to_return(
|
56
58
|
status: 200, headers: { 'Content-Type': 'application/json' },
|
@@ -7,11 +7,13 @@ class UpdateDeviceInterfacesTest < ActiveSupport::TestCase
|
|
7
7
|
ForemanNetbox::SyncHost::SyncDevice::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: tags
|
11
12
|
)
|
12
13
|
end
|
13
14
|
|
14
15
|
let(:interfaces) { ForemanNetbox::API.client::DCIM::Interfaces.new }
|
16
|
+
let(:tags) { [ForemanNetbox::API.client::Extras::Tag.new(id: 1, name: 'foreman', slug: 'foreman')] }
|
15
17
|
let(:old_mac) { 'FE:13:C6:44:29:22' }
|
16
18
|
let(:host) do
|
17
19
|
FactoryBot.build_stubbed(
|
@@ -44,7 +46,7 @@ class UpdateDeviceInterfacesTest < ActiveSupport::TestCase
|
|
44
46
|
{
|
45
47
|
id: 1,
|
46
48
|
name: host.interfaces.first.netbox_name,
|
47
|
-
mac_address: new_mac,
|
49
|
+
mac_address: new_mac.upcase,
|
48
50
|
tags: []
|
49
51
|
}
|
50
52
|
]
|
@@ -52,8 +54,8 @@ class UpdateDeviceInterfacesTest < ActiveSupport::TestCase
|
|
52
54
|
)
|
53
55
|
stub_patch = stub_request(:patch, "#{Setting[:netbox_url]}/api/dcim/interfaces/1.json").with(
|
54
56
|
body: {
|
55
|
-
mac_address: host.interfaces.first.mac,
|
56
|
-
tags:
|
57
|
+
mac_address: host.interfaces.first.mac.upcase,
|
58
|
+
tags: tags.map(&:id)
|
57
59
|
}.to_json
|
58
60
|
).to_return(
|
59
61
|
status: 200, headers: { 'Content-Type': 'application/json' },
|
@@ -79,7 +81,7 @@ class UpdateDeviceInterfacesTest < ActiveSupport::TestCase
|
|
79
81
|
id: 1,
|
80
82
|
name: host.interfaces.first.netbox_name,
|
81
83
|
mac_address: old_mac,
|
82
|
-
tags:
|
84
|
+
tags: tags.map { |t| { id: t.id, name: t.name, slug: t.slug } }
|
83
85
|
}
|
84
86
|
]
|
85
87
|
}.to_json
|
@@ -5,7 +5,7 @@ require 'test_plugin_helper'
|
|
5
5
|
class CreateSiteTest < ActiveSupport::TestCase
|
6
6
|
subject do
|
7
7
|
ForemanNetbox::SyncHost::SyncDevice::SyncSite::Create.call(
|
8
|
-
host: host, site: site, netbox_params: host.netbox_facet.netbox_params
|
8
|
+
host: host, site: site, netbox_params: host.netbox_facet.netbox_params, tags: default_tags
|
9
9
|
)
|
10
10
|
end
|
11
11
|
|
@@ -29,7 +29,7 @@ class CreateSiteTest < ActiveSupport::TestCase
|
|
29
29
|
body: {
|
30
30
|
name: host.location.netbox_site_name,
|
31
31
|
slug: host.location.netbox_site_slug,
|
32
|
-
tags:
|
32
|
+
tags: default_tags.map(&:id)
|
33
33
|
}.to_json
|
34
34
|
).to_return(
|
35
35
|
status: 201, headers: { 'Content-Type': 'application/json' },
|
@@ -5,11 +5,12 @@ require 'test_plugin_helper'
|
|
5
5
|
class UpdateSiteTest < ActiveSupport::TestCase
|
6
6
|
subject do
|
7
7
|
ForemanNetbox::SyncHost::SyncDevice::SyncSite::Update.call(
|
8
|
-
host: host, site: site, netbox_params: host.netbox_facet.netbox_params
|
8
|
+
host: host, site: site, netbox_params: host.netbox_facet.netbox_params, tags: default_tags
|
9
9
|
)
|
10
10
|
end
|
11
11
|
|
12
12
|
let(:host) { FactoryBot.build_stubbed(:host) }
|
13
|
+
# let(:tags) { [ForemanNetbox::API.client::Extras::Tag.new(id: 1, name: 'foreman', slug: 'foreman')] }
|
13
14
|
let(:site) do
|
14
15
|
ForemanNetbox::API.client::DCIM::Site.new(id: 1).tap do |site|
|
15
16
|
site.instance_variable_set(
|
@@ -29,7 +30,7 @@ class UpdateSiteTest < ActiveSupport::TestCase
|
|
29
30
|
it 'updates site' do
|
30
31
|
stub_patch = stub_request(:patch, "#{Setting[:netbox_url]}/api/dcim/sites/1.json").with(
|
31
32
|
body: {
|
32
|
-
tags:
|
33
|
+
tags: default_tags.map(&:id)
|
33
34
|
}.to_json
|
34
35
|
).to_return(
|
35
36
|
status: 200, headers: { 'Content-Type': 'application/json' },
|
@@ -42,7 +43,7 @@ class UpdateSiteTest < ActiveSupport::TestCase
|
|
42
43
|
end
|
43
44
|
|
44
45
|
context 'when unchanged' do
|
45
|
-
let(:site_tags) {
|
46
|
+
let(:site_tags) { default_tags.map { |t| { 'id' => t.id, 'name' => t.name, 'slug' => t.slug } } }
|
46
47
|
|
47
48
|
it 'does not update site' do
|
48
49
|
stub_patch = stub_request(:patch, "#{Setting[:netbox_url]}/api/dcim/sites/1.json")
|