Skip to content

Commit 01d3302

Browse files
Bugfix: Type for termination_a_type and circuits.circuittermination (#367)
1 parent 7bb393d commit 01d3302

File tree

5 files changed

+186
-36
lines changed

5 files changed

+186
-36
lines changed

plugins/module_utils/netbox_utils.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@
137137
"circuit": "circuits",
138138
"circuit_type": "circuit_types",
139139
"circuit_termination": "circuit_terminations",
140-
"circuit.circuittermination": "circuit_terminations",
140+
"circuits.circuittermination": "circuit_terminations",
141141
"cluster": "clusters",
142142
"cluster_group": "cluster_groups",
143143
"cluster_type": "cluster_types",
@@ -257,7 +257,7 @@
257257
"circuit": set(["cid"]),
258258
"circuit_type": set(["slug"]),
259259
"circuit_termination": set(["circuit", "term_side"]),
260-
"circuit.circuittermination": set(["circuit", "term_side"]),
260+
"circuits.circuittermination": set(["circuit", "term_side"]),
261261
"cluster": set(["name", "type"]),
262262
"cluster_group": set(["slug"]),
263263
"cluster_type": set(["slug"]),
@@ -622,6 +622,12 @@ def _build_query_params(
622622
:params child(dict): This is used within `_find_ids` and passes the inner dictionary
623623
to build the appropriate `query_dict` for the parent
624624
"""
625+
# This is to change the parent key to use the proper ALLOWED_QUERY_PARAMS below for termination searches.
626+
if parent == "termination_a" and module_data.get("termination_a_type"):
627+
parent = module_data["termination_a_type"]
628+
elif parent == "termination_b" and module_data.get("termination_b_type"):
629+
parent = module_data["termination_b_type"]
630+
625631
query_dict = dict()
626632
if user_query_params:
627633
query_params = set(user_query_params)

tests/integration/netbox-deploy.py

Lines changed: 86 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,40 @@
55
__metaclass__ = type
66

77
import os
8+
import sys
89
import pynetbox
910

1011
# NOTE: If anything depends on specific versions of NetBox, can check INTEGRATION_TESTS in env
1112
# os.environ["INTEGRATION_TESTS"]
1213

1314

1415
# Set nb variable to connect to Netbox and use the veriable in future calls
15-
nb = pynetbox.api("http://localhost:32768", "0123456789abcdef0123456789abcdef01234567")
16+
nb_host = os.getenv("NETBOX_HOST", "http://localhost:32768")
17+
nb_token = os.getenv("NETBOX_TOKEN", "0123456789abcdef0123456789abcdef01234567")
18+
nb = pynetbox.api(nb_host, nb_token)
1619
version = float(nb.version)
1720

18-
# Create tags used in future tests
21+
ERRORS = False
22+
23+
24+
def make_netbox_calls(endpoint, payload):
25+
"""Make the necessary calls to create endpoints, and pass any errors.
26+
27+
Args:
28+
endpoint (obj): pynetbox endpoint object.
29+
payload (list): List of endpoint objects.
30+
"""
31+
try:
32+
created = endpoint.create(payload)
33+
except pynetbox.RequestError as e:
34+
print(e.error)
35+
ERRORS = True
36+
return
37+
38+
return created
1939

40+
41+
# Create tags used in future tests
2042
if version >= 2.9:
2143
create_tags = nb.extras.tags.create(
2244
[
@@ -37,14 +59,14 @@
3759

3860
## Create TENANTS
3961
tenants = [{"name": "Test Tenant", "slug": "test-tenant"}]
40-
created_tenants = nb.tenancy.tenants.create(tenants)
62+
created_tenants = make_netbox_calls(nb.tenancy.tenants, tenants)
4163
### Test Tenant to be used later on
4264
test_tenant = nb.tenancy.tenants.get(slug="test-tenant")
4365

4466

4567
## Create TENANT GROUPS
4668
tenant_groups = [{"name": "Test Tenant Group", "slug": "test-tenant-group"}]
47-
created_tenant_groups = nb.tenancy.tenant_groups.create(tenant_groups)
69+
created_tenant_groups = make_netbox_calls(nb.tenancy.tenant_groups, tenant_groups)
4870

4971

5072
## Create Regions
@@ -53,7 +75,7 @@
5375
{"name": "Parent Region", "slug": "parent-region"},
5476
{"name": "Other Region", "slug": "other-region"},
5577
]
56-
created_regions = nb.dcim.regions.create(regions)
78+
created_regions = make_netbox_calls(nb.dcim.regions, regions)
5779
### Region variables to be used later on
5880
parent_region = nb.dcim.regions.get(slug="parent-region")
5981
test_region = nb.dcim.regions.get(slug="test-region")
@@ -73,23 +95,23 @@
7395
},
7496
{"name": "Test Site2", "slug": "test-site2"},
7597
]
76-
created_sites = nb.dcim.sites.create(sites)
98+
created_sites = make_netbox_calls(nb.dcim.sites, sites)
7799
### Site variables to be used later on
78100
test_site = nb.dcim.sites.get(slug="test-site")
79101
test_site2 = nb.dcim.sites.get(slug="test-site2")
80102

81103

82104
## Create VRFs
83105
vrfs = [{"name": "Test VRF", "rd": "1:1"}]
84-
created_vrfs = nb.ipam.vrfs.create(vrfs)
106+
created_vrfs = make_netbox_calls(nb.ipam.vrfs, vrfs)
85107

86108

87109
## Create PREFIXES
88110
prefixes = [
89111
{"prefix": "192.168.100.0/24", "site": test_site2.id},
90112
{"prefix": "10.10.0.0/16"},
91113
]
92-
created_prefixes = nb.ipam.prefixes.create(prefixes)
114+
created_prefixes = make_netbox_calls(nb.ipam.prefixes, prefixes)
93115

94116

95117
## Create VLAN GROUPS
@@ -101,7 +123,7 @@
101123
"tenant": test_tenant.id,
102124
}
103125
]
104-
created_vlan_groups = nb.ipam.vlan_groups.create(vlan_groups)
126+
created_vlan_groups = make_netbox_calls(nb.ipam.vlan_groups, vlan_groups)
105127
## VLAN Group variables to be used later on
106128
test_vlan_group = nb.ipam.vlan_groups.get(slug="test-vlan-group")
107129

@@ -119,12 +141,12 @@
119141
"group": test_vlan_group.id,
120142
},
121143
]
122-
created_vlans = nb.ipam.vlans.create(vlans)
144+
created_vlans = make_netbox_calls(nb.ipam.vlans, vlans)
123145

124146

125147
## Create IPAM Roles
126148
ipam_roles = [{"name": "Network of care", "slug": "network-of-care"}]
127-
create_ipam_roles = nb.ipam.roles.create(ipam_roles)
149+
create_ipam_roles = make_netbox_calls(nb.ipam.roles, ipam_roles)
128150

129151

130152
## Create Manufacturers
@@ -133,7 +155,7 @@
133155
{"name": "Arista", "slug": "arista"},
134156
{"name": "Test Manufactuer", "slug": "test-manufacturer"},
135157
]
136-
created_manufacturers = nb.dcim.manufacturers.create(manufacturers)
158+
created_manufacturers = make_netbox_calls(nb.dcim.manufacturers, manufacturers)
137159
### Manufacturer variables to be used later on
138160
cisco_manu = nb.dcim.manufacturers.get(slug="cisco")
139161
arista_manu = nb.dcim.manufacturers.get(slug="arista")
@@ -169,7 +191,7 @@
169191
temp_dt.append(dt_type)
170192
device_types = temp_dt
171193

172-
created_device_types = nb.dcim.device_types.create(device_types)
194+
created_device_types = make_netbox_calls(nb.dcim.device_types, device_types)
173195
### Device type variables to be used later on
174196
cisco_test = nb.dcim.device_types.get(slug="cisco-test")
175197
arista_test = nb.dcim.device_types.get(slug="arista-test")
@@ -192,7 +214,7 @@
192214
"vm_role": True,
193215
},
194216
]
195-
created_device_roles = nb.dcim.device_roles.create(device_roles)
217+
created_device_roles = make_netbox_calls(nb.dcim.device_roles, device_roles)
196218
### Device role variables to be used later on
197219
core_switch = nb.dcim.device_roles.get(slug="core-switch")
198220

@@ -202,15 +224,15 @@
202224
{"name": "Test Rack Group", "slug": "test-rack-group", "site": test_site.id},
203225
{"name": "Parent Rack Group", "slug": "parent-rack-group", "site": test_site.id},
204226
]
205-
created_rack_groups = nb.dcim.rack_groups.create(rack_groups)
227+
created_rack_groups = make_netbox_calls(nb.dcim.rack_groups, rack_groups)
206228

207229
### Create Rack Group Parent relationship
208230
created_rack_groups[0].parent = created_rack_groups[1]
209231
created_rack_groups[0].save()
210232

211233
## Create Rack Roles
212234
rack_roles = [{"name": "Test Rack Role", "slug": "test-rack-role", "color": "4287f5"}]
213-
created_rack_roles = nb.dcim.rack_roles.create(rack_roles)
235+
created_rack_roles = make_netbox_calls(nb.dcim.rack_roles, rack_roles)
214236

215237
## Create Racks
216238
racks = [
@@ -221,7 +243,7 @@
221243
},
222244
{"name": "Test Rack", "site": test_site.id, "group": created_rack_groups[0].id},
223245
]
224-
created_racks = nb.dcim.racks.create(racks)
246+
created_racks = make_netbox_calls(nb.dcim.racks, racks)
225247
test_rack = nb.dcim.racks.get(name="Test Rack") # racks don't have slugs
226248
test_rack_site2 = nb.dcim.racks.get(name="Test Rack Site 2")
227249

@@ -262,12 +284,12 @@
262284
"site": test_site.id,
263285
},
264286
]
265-
created_devices = nb.dcim.devices.create(devices)
287+
created_devices = make_netbox_calls(nb.dcim.devices, devices)
266288
### Device variables to be used later on
267289
test100 = nb.dcim.devices.get(name="test100")
268290

269291
# Create VC, assign member, create initial interface
270-
created_vcs = nb.dcim.virtual_chassis.create({"name": "VC1", "master": 4})
292+
created_vcs = make_netbox_calls(nb.dcim.virtual_chassis, {"name": "VC1", "master": 4})
271293
nexus_child = nb.dcim.devices.get(5)
272294
nexus_child.update({"virtual_chassis": 1, "vc_position": 2})
273295
nexus = nb.dcim.devices.get(4)
@@ -276,14 +298,14 @@
276298
{"device": nexus.id, "name": "Ethernet1/1", "type": "1000base-t"},
277299
{"device": nexus_child.id, "name": "Ethernet2/1", "type": "1000base-t"},
278300
]
279-
created_nexus_interfaces = nb.dcim.interfaces.create(nexus_interfaces)
301+
created_nexus_interfaces = make_netbox_calls(nb.dcim.interfaces, nexus_interfaces)
280302

281303
## Create Interfaces
282304
dev_interfaces = [
283305
{"name": "GigabitEthernet1", "device": test100.id, "type": "1000base-t"},
284306
{"name": "GigabitEthernet2", "device": test100.id, "type": "1000base-t"},
285307
]
286-
created_interfaces = nb.dcim.interfaces.create(dev_interfaces)
308+
created_interfaces = make_netbox_calls(nb.dcim.interfaces, dev_interfaces)
287309
## Interface variables to be used later on
288310
test100_gi1 = nb.dcim.interfaces.get(name="GigabitEthernet1", device_id=1)
289311
test100_gi2 = nb.dcim.interfaces.get(name="GigabitEthernet2", device_id=1)
@@ -305,21 +327,25 @@
305327
ip["assigned_object_type"] = "dcim.interface"
306328
temp_ips.append(ip)
307329

308-
created_ip_addresses = nb.ipam.ip_addresses.create(ip_addresses)
330+
created_ip_addresses = make_netbox_calls(nb.ipam.ip_addresses, ip_addresses)
309331

310332

311333
## Create RIRs
312334
rirs = [{"name": "Example RIR", "slug": "example-rir"}]
313-
created_rirs = nb.ipam.rirs.create(rirs)
335+
created_rirs = make_netbox_calls(nb.ipam.rirs, rirs)
314336

315337
## Create Cluster Group
316338
cluster_groups = [{"name": "Test Cluster Group", "slug": "test-cluster-group"}]
317-
created_cluster_groups = nb.virtualization.cluster_groups.create(cluster_groups)
339+
created_cluster_groups = make_netbox_calls(
340+
nb.virtualization.cluster_groups, cluster_groups
341+
)
318342
test_cluster_group = nb.virtualization.cluster_groups.get(slug="test-cluster-group")
319343

320344
## Create Cluster Type
321345
cluster_types = [{"name": "Test Cluster Type", "slug": "test-cluster-type"}]
322-
created_cluster_types = nb.virtualization.cluster_types.create(cluster_types)
346+
created_cluster_types = make_netbox_calls(
347+
nb.virtualization.cluster_types, cluster_types
348+
)
323349
test_cluster_type = nb.virtualization.cluster_types.get(slug="test-cluster-type")
324350

325351
## Create Cluster
@@ -332,7 +358,7 @@
332358
},
333359
{"name": "Test Cluster 2", "type": test_cluster_type.id,},
334360
]
335-
created_clusters = nb.virtualization.clusters.create(clusters)
361+
created_clusters = make_netbox_calls(nb.virtualization.clusters, clusters)
336362
test_cluster = nb.virtualization.clusters.get(name="Test Cluster")
337363
test_cluster2 = nb.virtualization.clusters.get(name="Test Cluster 2")
338364

@@ -345,7 +371,9 @@
345371
{"name": "test104-vm", "cluster": test_cluster2.id},
346372
{"name": "Test VM With Spaces", "cluster": test_cluster2.id},
347373
]
348-
created_virtual_machines = nb.virtualization.virtual_machines.create(virtual_machines)
374+
created_virtual_machines = make_netbox_calls(
375+
nb.virtualization.virtual_machines, virtual_machines
376+
)
349377
test100_vm = nb.virtualization.virtual_machines.get(name="test100-vm")
350378
test101_vm = nb.virtualization.virtual_machines.get(name="test101-vm")
351379
test_spaces_vm = nb.virtualization.virtual_machines.get(name="Test VM With Spaces")
@@ -368,8 +396,8 @@
368396
{"name": "Eth0", "virtual_machine": test_spaces_vm.id},
369397
{"name": "Eth1", "virtual_machine": test_spaces_vm.id},
370398
]
371-
created_virtual_machines_intfs = nb.virtualization.interfaces.create(
372-
virtual_machines_intfs
399+
created_virtual_machines_intfs = make_netbox_calls(
400+
nb.virtualization.interfaces, virtual_machines_intfs
373401
)
374402

375403

@@ -391,21 +419,45 @@
391419
"protocol": "tcp",
392420
},
393421
]
394-
created_services = nb.ipam.services.create(services)
422+
created_services = make_netbox_calls(nb.ipam.services, services)
395423

396424

397425
## Create Circuit Provider
398426
providers = [{"name": "Test Provider", "slug": "test-provider"}]
399-
created_providers = nb.circuits.providers.create(providers)
427+
created_providers = make_netbox_calls(nb.circuits.providers, providers)
400428
test_provider = nb.circuits.providers.get(slug="test-provider")
401429

402430
## Create Circuit Type
403431
circuit_types = [{"name": "Test Circuit Type", "slug": "test-circuit-type"}]
404-
created_circuit_types = nb.circuits.circuit_types.create(circuit_types)
432+
created_circuit_types = make_netbox_calls(nb.circuits.circuit_types, circuit_types)
405433
test_circuit_type = nb.circuits.circuit_types.get(slug="test-circuit-type")
406434

407435
## Create Circuit
408436
circuits = [
409-
{"cid": "Test Circuit", "provider": test_provider.id, "type": test_circuit_type.id}
437+
{"cid": "Test Circuit", "provider": test_provider.id, "type": test_circuit_type.id},
438+
{
439+
"cid": "Test Circuit Two",
440+
"provider": test_provider.id,
441+
"type": test_circuit_type.id,
442+
},
443+
]
444+
created_circuits = make_netbox_calls(nb.circuits.circuits, circuits)
445+
test_circuit_two = nb.circuits.circuits.get(cid="Test Circuit Two")
446+
447+
## Create Circuit Termination
448+
circuit_terms = [
449+
{
450+
"circuit": test_circuit_two.id,
451+
"term_side": "A",
452+
"port_speed": 10000,
453+
"site": test_site.id,
454+
}
410455
]
411-
created_circuits = nb.circuits.circuits.create(circuits)
456+
created_circuit_terms = make_netbox_calls(
457+
nb.circuits.circuit_terminations, circuit_terms
458+
)
459+
460+
if ERRORS:
461+
sys.exit(
462+
"Errors have occurred when creating objects, and should have been printed out. Check previous output."
463+
)

tests/integration/targets/latest/tasks/netbox_cable.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,3 +156,31 @@
156156
- test_five['cable']['termination_b_type'] == "dcim.consoleport"
157157
- test_five['cable']['termination_b_id'] == 1
158158
- test_five['msg'] == "cable dcim.consoleserverport Console Server Port <> dcim.consoleport Console Port created"
159+
160+
- name: "CABLE 6: Circuits Termination as side A"
161+
netbox.netbox.netbox_cable:
162+
netbox_url: http://localhost:32768
163+
netbox_token: 0123456789abcdef0123456789abcdef01234567
164+
data:
165+
termination_a_type: circuits.circuittermination
166+
termination_a:
167+
circuit: "Test Circuit Two"
168+
term_side: "A"
169+
termination_b_type: dcim.interface
170+
termination_b:
171+
device: "test100"
172+
name: "GigabitEthernet2"
173+
state: present
174+
register: test_six
175+
176+
- name: "CABLE 6: ASSERT - Circuits Termination as side A"
177+
assert:
178+
that:
179+
- test_six is changed
180+
- test_six['diff']['before']['state'] == "absent"
181+
- test_six['diff']['after']['state'] == "present"
182+
- test_six['cable']['termination_a_type'] == "circuits.circuittermination"
183+
- test_six['cable']['termination_a_id'] == 1
184+
- test_six['cable']['termination_b_type'] == "dcim.interface"
185+
- test_six['cable']['termination_b_id'] == 4
186+
- test_six['msg'] == "cable circuits.circuittermination 1 <> dcim.interface GigabitEthernet2 created"

0 commit comments

Comments
 (0)