Skip to content

Fix netbox_inventory service query #1438

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 12 commits into
base: devel
Choose a base branch
from
Open
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- nb_inventory - Fix service collection for version greater than 4.3
36 changes: 28 additions & 8 deletions plugins/inventory/nb_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -1328,6 +1328,15 @@ def refresh_services(self):

if self.fetch_all:
services = self.get_resource_list(url)
elif self.api_version >= version.parse("4.3.0"):
services = self.get_resource_list_chunked(
api_url=url,
query_key="parent_object_id",
# Query only affected devices and vms and sanitize the list to only contain every ID once
query_values=set(
chain(self.vms_lookup.keys(), self.devices_lookup.keys())
),
)
else:
device_services = self.get_resource_list_chunked(
api_url=url,
Expand All @@ -1349,15 +1358,26 @@ def refresh_services(self):
for service in services:
service_id = service["id"]

if service.get("device"):
self.device_services_lookup[service["device"]["id"]][
service_id
] = service
if self.api_version >= version.parse("4.3.0"):
if service.get("parent_object_type") == "dcim.device":
self.device_services_lookup[service["parent_object_id"]][
service_id
] = service

if service.get("virtual_machine"):
self.vm_services_lookup[service["virtual_machine"]["id"]][
service_id
] = service
if service.get("parent_object_type") == "virtualization.virtualmachine":
self.vm_services_lookup[service["parent_object_id"]][
service_id
] = service
else:
if service.get("device"):
self.device_services_lookup[service["device"]["id"]][
service_id
] = service

if service.get("virtual_machine"):
self.vm_services_lookup[service["virtual_machine"]["id"]][
service_id
] = service

def refresh_virtual_disks(self):
url_vm_virtual_disks = (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def main():

if result:
# Dictionary is not empty - print differences
print(json.dumps(result, sort_keys=True, indent=4))
print(result.to_json(indent=4))
sys.exit(1)
else:
# Success, no differences
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def main():

if result:
# Dictionary is not empty - print differences
print(json.dumps(result, sort_keys=True, indent=4))
print(result.to_json(indent=4))
sys.exit(1)
else:
# Success, no differences
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def main():

if result:
# Dictionary is not empty - print differences
print(json.dumps(result, sort_keys=True, indent=4))
print(result.to_json(indent=4))
sys.exit(1)
else:
# Success, no differences
Expand Down
Loading