Skip to content

Commit 1fa4bc8

Browse files
committed
Merge branch 'devel' into datasources
2 parents 17963cc + 8ca870e commit 1fa4bc8

File tree

15 files changed

+117
-41
lines changed

15 files changed

+117
-41
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bugfixes:
2+
- nb_inventory - Fix service collection for version greater than 4.3
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
minor_changes:
2+
- Fix ansible-bad-import-from pylint errors
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bugfixes:
2+
- "nb_device_interface: Fix specifying primary_mac_address objects by id for disambiguation"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
minor_changes:
2+
- Fix broken code path when using old api path on old netbox systems

docs/getting_started/how-to-use/advanced.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ To make this possible, every module accepts the ``query_params`` argument. This
224224
you want to use to search for the object.
225225

226226
Let's start with another example. We will continue to use :ref:`netbox.netbox.netbox_ip_address<ansible_collections.netbox.netbox.netbox_ip_address_module>`. We created **192.168.100.1/24** as a duplicate IP address within
227-
the global IP address space within NetBox. This task should fail saying there was more than result returned.
227+
the global IP address space within NetBox. This task should fail saying there was more than one result returned.
228228

229229
.. code-block:: yaml
230230
@@ -304,7 +304,9 @@ Hopefully this shines some light on this useful feature to allow you, as the use
304304
Using Module defaults groups
305305
--------------------------------------------
306306

307-
Ansible-core >= 2.12 provide a useful feature called [Module defaults groups](https://docs.ansible.com/ansible/latest/user_guide/playbooks_module_defaults.html#module-defaults-groups) that lets us specify default parameters for a group of modules in a single place. We can use the action_group ``netbox`` that contains all modules from this collection to avoid setting e.g. ``token`` and ``url`` on each task and thus reduce boilerplate code.
307+
Ansible-core >= 2.12 provide a useful feature called `Module defaults groups`_ that lets us specify default parameters for a group of modules in a single place. We can use the action_group ``netbox`` that contains all modules from this collection to avoid setting e.g. ``token`` and ``url`` on each task and thus reduce boilerplate code.
308+
309+
.. _Module defaults groups: https://docs.ansible.com/ansible/latest/user_guide/playbooks_module_defaults.html#module-defaults-groups
308310

309311
.. code-block:: yaml
310312

plugins/inventory/nb_inventory.py

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,6 @@
408408
from ansible.module_utils.six.moves.urllib.parse import urlencode
409409
from ansible.module_utils.six.moves.urllib.parse import urlparse
410410

411-
from ansible.module_utils.six import raise_from
412411

413412
try:
414413
from packaging import specifiers, version
@@ -1328,6 +1327,15 @@ def refresh_services(self):
13281327

13291328
if self.fetch_all:
13301329
services = self.get_resource_list(url)
1330+
elif self.api_version >= version.parse("4.3.0"):
1331+
services = self.get_resource_list_chunked(
1332+
api_url=url,
1333+
query_key="parent_object_id",
1334+
# Query only affected devices and vms and sanitize the list to only contain every ID once
1335+
query_values=set(
1336+
chain(self.vms_lookup.keys(), self.devices_lookup.keys())
1337+
),
1338+
)
13311339
else:
13321340
device_services = self.get_resource_list_chunked(
13331341
api_url=url,
@@ -1349,15 +1357,26 @@ def refresh_services(self):
13491357
for service in services:
13501358
service_id = service["id"]
13511359

1352-
if service.get("device"):
1353-
self.device_services_lookup[service["device"]["id"]][
1354-
service_id
1355-
] = service
1360+
if self.api_version >= version.parse("4.3.0"):
1361+
if service.get("parent_object_type") == "dcim.device":
1362+
self.device_services_lookup[service["parent_object_id"]][
1363+
service_id
1364+
] = service
13561365

1357-
if service.get("virtual_machine"):
1358-
self.vm_services_lookup[service["virtual_machine"]["id"]][
1359-
service_id
1360-
] = service
1366+
if service.get("parent_object_type") == "virtualization.virtualmachine":
1367+
self.vm_services_lookup[service["parent_object_id"]][
1368+
service_id
1369+
] = service
1370+
else:
1371+
if service.get("device"):
1372+
self.device_services_lookup[service["device"]["id"]][
1373+
service_id
1374+
] = service
1375+
1376+
if service.get("virtual_machine"):
1377+
self.vm_services_lookup[service["virtual_machine"]["id"]][
1378+
service_id
1379+
] = service
13611380

13621381
def refresh_virtual_disks(self):
13631382
url_vm_virtual_disks = (
@@ -1629,10 +1648,10 @@ def fetch_api_docs(self):
16291648
pass
16301649

16311650
self.api_version = version.parse(netbox_api_version)
1651+
parsed_endpoint_url = urlparse(self.api_endpoint)
1652+
base_path = parsed_endpoint_url.path
16321653

16331654
if self.api_version >= version.parse("3.5.0"):
1634-
parsed_endpoint_url = urlparse(self.api_endpoint)
1635-
base_path = parsed_endpoint_url.path
16361655
self.allowed_device_query_parameters = [
16371656
p["name"]
16381657
for p in openapi["paths"][base_path + "/api/dcim/devices/"]["get"][
@@ -2024,10 +2043,9 @@ def _get_host_virtual_chassis_master(self, host):
20242043
def main(self):
20252044
# Check if pytz lib is install, and give error if not
20262045
if PYTZ_IMPORT_ERROR:
2027-
raise_from(
2028-
AnsibleError("pytz must be installed to use this plugin"),
2029-
PYTZ_IMPORT_ERROR,
2030-
)
2046+
raise AnsibleError(
2047+
"pytz must be installed to use this plugin"
2048+
) from PYTZ_IMPORT_ERROR
20312049

20322050
# Get info about the API - version, allowed query parameters
20332051
self.fetch_api_docs()

plugins/lookup/nb_lookup.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@
134134
from ansible.plugins.lookup import LookupBase
135135
from ansible.parsing.splitter import parse_kv, split_args
136136
from ansible.utils.display import Display
137-
from ansible.module_utils.six import raise_from
138137
from importlib.metadata import version
139138

140139
try:
@@ -413,16 +412,14 @@ class LookupModule(LookupBase):
413412

414413
def run(self, terms, variables=None, **kwargs):
415414
if PYNETBOX_LIBRARY_IMPORT_ERROR:
416-
raise_from(
417-
AnsibleError("pynetbox must be installed to use this plugin"),
418-
PYNETBOX_LIBRARY_IMPORT_ERROR,
419-
)
415+
raise AnsibleError(
416+
"pynetbox must be installed to use this plugin"
417+
) from PYNETBOX_LIBRARY_IMPORT_ERROR
420418

421419
if REQUESTS_LIBRARY_IMPORT_ERROR:
422-
raise_from(
423-
AnsibleError("requests must be installed to use this plugin"),
424-
REQUESTS_LIBRARY_IMPORT_ERROR,
425-
)
420+
raise AnsibleError(
421+
"requests must be installed to use this plugin"
422+
) from REQUESTS_LIBRARY_IMPORT_ERROR
426423

427424
netbox_api_token = (
428425
kwargs.get("token")

plugins/modules/netbox_device_interface.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@
9090
description:
9191
- The primary MAC address of the interface (NetBox 4.2 and later)
9292
required: false
93-
type: str
93+
type: raw
9494
wwn:
9595
description:
9696
- The WWN of the interface
@@ -340,7 +340,7 @@ def main():
340340
bridge=dict(required=False, type="raw"),
341341
mtu=dict(required=False, type="int"),
342342
mac_address=dict(required=False, type="str"),
343-
primary_mac_address=dict(required=False, type="str"),
343+
primary_mac_address=dict(required=False, type="raw"),
344344
wwn=dict(required=False, type="str"),
345345
mgmt_only=dict(required=False, type="bool"),
346346
poe_type=dict(required=False, type="raw"),

poetry.lock

Lines changed: 22 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/integration/netbox-deploy.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,11 @@ def make_netbox_calls(endpoint, payload):
387387
"assigned_object_id": test100_gi2.id,
388388
"assigned_object_type": "dcim.interface",
389389
},
390+
{
391+
"mac_address": "AA:BB:CC:DD:EE:FF", # Identical to id 1
392+
"assigned_object_id": test100_gi2.id,
393+
"assigned_object_type": "dcim.interface",
394+
},
390395
]
391396
created_mac_addresses = make_netbox_calls(nb.dcim.mac_addresses, mac_addresses)
392397

0 commit comments

Comments
 (0)