Skip to content

Commit 574f6cc

Browse files
committed
add base_url to api endpoint to allow non-root context nextbox endpoints
1 parent cd0b654 commit 574f6cc

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

changelogs/fragments/nb_inventory.yml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
bug_fixes:
2+
- nb_inventory - Fixed empty inventory results when netbox server URL is a non-root path

plugins/inventory/nb_inventory.py

+16-8
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,8 @@
406406
from ansible.module_utils.urls import open_url
407407
from ansible.module_utils.six.moves.urllib import error as urllib_error
408408
from ansible.module_utils.six.moves.urllib.parse import urlencode
409+
from ansible.module_utils.six.moves.urllib.parse import urlparse
410+
409411
from ansible.module_utils.six import raise_from
410412

411413
try:
@@ -1629,26 +1631,32 @@ def fetch_api_docs(self):
16291631
self.api_version = version.parse(netbox_api_version)
16301632

16311633
if self.api_version >= version.parse("3.5.0"):
1634+
parsed_endpoint_url = urlparse(self.api_endpoint)
1635+
base_path = parsed_endpoint_url.path
16321636
self.allowed_device_query_parameters = [
16331637
p["name"]
1634-
for p in openapi["paths"]["/api/dcim/devices/"]["get"]["parameters"]
1638+
for p in openapi["paths"][base_path + "/api/dcim/devices/"]["get"][
1639+
"parameters"
1640+
]
16351641
]
16361642
self.allowed_vm_query_parameters = [
16371643
p["name"]
1638-
for p in openapi["paths"]["/api/virtualization/virtual-machines/"][
1639-
"get"
1640-
]["parameters"]
1644+
for p in openapi["paths"][
1645+
base_path + "/api/virtualization/virtual-machines/"
1646+
]["get"]["parameters"]
16411647
]
16421648
else:
16431649
self.allowed_device_query_parameters = [
16441650
p["name"]
1645-
for p in openapi["paths"]["/dcim/devices/"]["get"]["parameters"]
1651+
for p in openapi["paths"][base_path + "/dcim/devices/"]["get"][
1652+
"parameters"
1653+
]
16461654
]
16471655
self.allowed_vm_query_parameters = [
16481656
p["name"]
1649-
for p in openapi["paths"]["/virtualization/virtual-machines/"]["get"][
1650-
"parameters"
1651-
]
1657+
for p in openapi["paths"][
1658+
base_path + "/virtualization/virtual-machines/"
1659+
]["get"]["parameters"]
16521660
]
16531661

16541662
def validate_query_parameter(self, parameter, allowed_query_parameters):

0 commit comments

Comments
 (0)