|
5 | 5 | __metaclass__ = type
|
6 | 6 |
|
7 | 7 | import os
|
| 8 | +import sys |
8 | 9 | import pynetbox
|
9 | 10 |
|
10 | 11 | # NOTE: If anything depends on specific versions of NetBox, can check INTEGRATION_TESTS in env
|
11 | 12 | # os.environ["INTEGRATION_TESTS"]
|
12 | 13 |
|
13 | 14 |
|
14 | 15 | # 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) |
16 | 19 | version = float(nb.version)
|
17 | 20 |
|
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 |
19 | 39 |
|
| 40 | + |
| 41 | +# Create tags used in future tests |
20 | 42 | if version >= 2.9:
|
21 | 43 | create_tags = nb.extras.tags.create(
|
22 | 44 | [
|
|
37 | 59 |
|
38 | 60 | ## Create TENANTS
|
39 | 61 | 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) |
41 | 63 | ### Test Tenant to be used later on
|
42 | 64 | test_tenant = nb.tenancy.tenants.get(slug="test-tenant")
|
43 | 65 |
|
44 | 66 |
|
45 | 67 | ## Create TENANT GROUPS
|
46 | 68 | 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) |
48 | 70 |
|
49 | 71 |
|
50 | 72 | ## Create Regions
|
|
53 | 75 | {"name": "Parent Region", "slug": "parent-region"},
|
54 | 76 | {"name": "Other Region", "slug": "other-region"},
|
55 | 77 | ]
|
56 |
| -created_regions = nb.dcim.regions.create(regions) |
| 78 | +created_regions = make_netbox_calls(nb.dcim.regions, regions) |
57 | 79 | ### Region variables to be used later on
|
58 | 80 | parent_region = nb.dcim.regions.get(slug="parent-region")
|
59 | 81 | test_region = nb.dcim.regions.get(slug="test-region")
|
|
73 | 95 | },
|
74 | 96 | {"name": "Test Site2", "slug": "test-site2"},
|
75 | 97 | ]
|
76 |
| -created_sites = nb.dcim.sites.create(sites) |
| 98 | +created_sites = make_netbox_calls(nb.dcim.sites, sites) |
77 | 99 | ### Site variables to be used later on
|
78 | 100 | test_site = nb.dcim.sites.get(slug="test-site")
|
79 | 101 | test_site2 = nb.dcim.sites.get(slug="test-site2")
|
80 | 102 |
|
81 | 103 |
|
82 | 104 | ## Create VRFs
|
83 | 105 | 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) |
85 | 107 |
|
86 | 108 |
|
87 | 109 | ## Create PREFIXES
|
88 | 110 | prefixes = [
|
89 | 111 | {"prefix": "192.168.100.0/24", "site": test_site2.id},
|
90 | 112 | {"prefix": "10.10.0.0/16"},
|
91 | 113 | ]
|
92 |
| -created_prefixes = nb.ipam.prefixes.create(prefixes) |
| 114 | +created_prefixes = make_netbox_calls(nb.ipam.prefixes, prefixes) |
93 | 115 |
|
94 | 116 |
|
95 | 117 | ## Create VLAN GROUPS
|
|
101 | 123 | "tenant": test_tenant.id,
|
102 | 124 | }
|
103 | 125 | ]
|
104 |
| -created_vlan_groups = nb.ipam.vlan_groups.create(vlan_groups) |
| 126 | +created_vlan_groups = make_netbox_calls(nb.ipam.vlan_groups, vlan_groups) |
105 | 127 | ## VLAN Group variables to be used later on
|
106 | 128 | test_vlan_group = nb.ipam.vlan_groups.get(slug="test-vlan-group")
|
107 | 129 |
|
|
119 | 141 | "group": test_vlan_group.id,
|
120 | 142 | },
|
121 | 143 | ]
|
122 |
| -created_vlans = nb.ipam.vlans.create(vlans) |
| 144 | +created_vlans = make_netbox_calls(nb.ipam.vlans, vlans) |
123 | 145 |
|
124 | 146 |
|
125 | 147 | ## Create IPAM Roles
|
126 | 148 | 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) |
128 | 150 |
|
129 | 151 |
|
130 | 152 | ## Create Manufacturers
|
|
133 | 155 | {"name": "Arista", "slug": "arista"},
|
134 | 156 | {"name": "Test Manufactuer", "slug": "test-manufacturer"},
|
135 | 157 | ]
|
136 |
| -created_manufacturers = nb.dcim.manufacturers.create(manufacturers) |
| 158 | +created_manufacturers = make_netbox_calls(nb.dcim.manufacturers, manufacturers) |
137 | 159 | ### Manufacturer variables to be used later on
|
138 | 160 | cisco_manu = nb.dcim.manufacturers.get(slug="cisco")
|
139 | 161 | arista_manu = nb.dcim.manufacturers.get(slug="arista")
|
|
169 | 191 | temp_dt.append(dt_type)
|
170 | 192 | device_types = temp_dt
|
171 | 193 |
|
172 |
| -created_device_types = nb.dcim.device_types.create(device_types) |
| 194 | +created_device_types = make_netbox_calls(nb.dcim.device_types, device_types) |
173 | 195 | ### Device type variables to be used later on
|
174 | 196 | cisco_test = nb.dcim.device_types.get(slug="cisco-test")
|
175 | 197 | arista_test = nb.dcim.device_types.get(slug="arista-test")
|
|
192 | 214 | "vm_role": True,
|
193 | 215 | },
|
194 | 216 | ]
|
195 |
| -created_device_roles = nb.dcim.device_roles.create(device_roles) |
| 217 | +created_device_roles = make_netbox_calls(nb.dcim.device_roles, device_roles) |
196 | 218 | ### Device role variables to be used later on
|
197 | 219 | core_switch = nb.dcim.device_roles.get(slug="core-switch")
|
198 | 220 |
|
|
202 | 224 | {"name": "Test Rack Group", "slug": "test-rack-group", "site": test_site.id},
|
203 | 225 | {"name": "Parent Rack Group", "slug": "parent-rack-group", "site": test_site.id},
|
204 | 226 | ]
|
205 |
| -created_rack_groups = nb.dcim.rack_groups.create(rack_groups) |
| 227 | +created_rack_groups = make_netbox_calls(nb.dcim.rack_groups, rack_groups) |
206 | 228 |
|
207 | 229 | ### Create Rack Group Parent relationship
|
208 | 230 | created_rack_groups[0].parent = created_rack_groups[1]
|
209 | 231 | created_rack_groups[0].save()
|
210 | 232 |
|
211 | 233 | ## Create Rack Roles
|
212 | 234 | 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) |
214 | 236 |
|
215 | 237 | ## Create Racks
|
216 | 238 | racks = [
|
|
221 | 243 | },
|
222 | 244 | {"name": "Test Rack", "site": test_site.id, "group": created_rack_groups[0].id},
|
223 | 245 | ]
|
224 |
| -created_racks = nb.dcim.racks.create(racks) |
| 246 | +created_racks = make_netbox_calls(nb.dcim.racks, racks) |
225 | 247 | test_rack = nb.dcim.racks.get(name="Test Rack") # racks don't have slugs
|
226 | 248 | test_rack_site2 = nb.dcim.racks.get(name="Test Rack Site 2")
|
227 | 249 |
|
|
262 | 284 | "site": test_site.id,
|
263 | 285 | },
|
264 | 286 | ]
|
265 |
| -created_devices = nb.dcim.devices.create(devices) |
| 287 | +created_devices = make_netbox_calls(nb.dcim.devices, devices) |
266 | 288 | ### Device variables to be used later on
|
267 | 289 | test100 = nb.dcim.devices.get(name="test100")
|
268 | 290 |
|
269 | 291 | # 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}) |
271 | 293 | nexus_child = nb.dcim.devices.get(5)
|
272 | 294 | nexus_child.update({"virtual_chassis": 1, "vc_position": 2})
|
273 | 295 | nexus = nb.dcim.devices.get(4)
|
|
276 | 298 | {"device": nexus.id, "name": "Ethernet1/1", "type": "1000base-t"},
|
277 | 299 | {"device": nexus_child.id, "name": "Ethernet2/1", "type": "1000base-t"},
|
278 | 300 | ]
|
279 |
| -created_nexus_interfaces = nb.dcim.interfaces.create(nexus_interfaces) |
| 301 | +created_nexus_interfaces = make_netbox_calls(nb.dcim.interfaces, nexus_interfaces) |
280 | 302 |
|
281 | 303 | ## Create Interfaces
|
282 | 304 | dev_interfaces = [
|
283 | 305 | {"name": "GigabitEthernet1", "device": test100.id, "type": "1000base-t"},
|
284 | 306 | {"name": "GigabitEthernet2", "device": test100.id, "type": "1000base-t"},
|
285 | 307 | ]
|
286 |
| -created_interfaces = nb.dcim.interfaces.create(dev_interfaces) |
| 308 | +created_interfaces = make_netbox_calls(nb.dcim.interfaces, dev_interfaces) |
287 | 309 | ## Interface variables to be used later on
|
288 | 310 | test100_gi1 = nb.dcim.interfaces.get(name="GigabitEthernet1", device_id=1)
|
289 | 311 | test100_gi2 = nb.dcim.interfaces.get(name="GigabitEthernet2", device_id=1)
|
|
305 | 327 | ip["assigned_object_type"] = "dcim.interface"
|
306 | 328 | temp_ips.append(ip)
|
307 | 329 |
|
308 |
| -created_ip_addresses = nb.ipam.ip_addresses.create(ip_addresses) |
| 330 | +created_ip_addresses = make_netbox_calls(nb.ipam.ip_addresses, ip_addresses) |
309 | 331 |
|
310 | 332 |
|
311 | 333 | ## Create RIRs
|
312 | 334 | 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) |
314 | 336 |
|
315 | 337 | ## Create Cluster Group
|
316 | 338 | 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 | +) |
318 | 342 | test_cluster_group = nb.virtualization.cluster_groups.get(slug="test-cluster-group")
|
319 | 343 |
|
320 | 344 | ## Create Cluster Type
|
321 | 345 | 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 | +) |
323 | 349 | test_cluster_type = nb.virtualization.cluster_types.get(slug="test-cluster-type")
|
324 | 350 |
|
325 | 351 | ## Create Cluster
|
|
332 | 358 | },
|
333 | 359 | {"name": "Test Cluster 2", "type": test_cluster_type.id,},
|
334 | 360 | ]
|
335 |
| -created_clusters = nb.virtualization.clusters.create(clusters) |
| 361 | +created_clusters = make_netbox_calls(nb.virtualization.clusters, clusters) |
336 | 362 | test_cluster = nb.virtualization.clusters.get(name="Test Cluster")
|
337 | 363 | test_cluster2 = nb.virtualization.clusters.get(name="Test Cluster 2")
|
338 | 364 |
|
|
345 | 371 | {"name": "test104-vm", "cluster": test_cluster2.id},
|
346 | 372 | {"name": "Test VM With Spaces", "cluster": test_cluster2.id},
|
347 | 373 | ]
|
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 | +) |
349 | 377 | test100_vm = nb.virtualization.virtual_machines.get(name="test100-vm")
|
350 | 378 | test101_vm = nb.virtualization.virtual_machines.get(name="test101-vm")
|
351 | 379 | test_spaces_vm = nb.virtualization.virtual_machines.get(name="Test VM With Spaces")
|
|
368 | 396 | {"name": "Eth0", "virtual_machine": test_spaces_vm.id},
|
369 | 397 | {"name": "Eth1", "virtual_machine": test_spaces_vm.id},
|
370 | 398 | ]
|
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 |
373 | 401 | )
|
374 | 402 |
|
375 | 403 |
|
|
391 | 419 | "protocol": "tcp",
|
392 | 420 | },
|
393 | 421 | ]
|
394 |
| -created_services = nb.ipam.services.create(services) |
| 422 | +created_services = make_netbox_calls(nb.ipam.services, services) |
395 | 423 |
|
396 | 424 |
|
397 | 425 | ## Create Circuit Provider
|
398 | 426 | 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) |
400 | 428 | test_provider = nb.circuits.providers.get(slug="test-provider")
|
401 | 429 |
|
402 | 430 | ## Create Circuit Type
|
403 | 431 | 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) |
405 | 433 | test_circuit_type = nb.circuits.circuit_types.get(slug="test-circuit-type")
|
406 | 434 |
|
407 | 435 | ## Create Circuit
|
408 | 436 | 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 | + } |
410 | 455 | ]
|
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 | + ) |
0 commit comments