Skip to content

Commit bb892df

Browse files
committed
add base_url to api endpoint to allow non-root context nextbox endpoints
fix linting
1 parent cd0b654 commit bb892df

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

plugins/inventory/nb_inventory.py

+25-17
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:
@@ -1348,9 +1350,9 @@ def refresh_services(self):
13481350
service_id = service["id"]
13491351

13501352
if service.get("device"):
1351-
self.device_services_lookup[service["device"]["id"]][
1352-
service_id
1353-
] = service
1353+
self.device_services_lookup[service["device"]["id"]][service_id] = (
1354+
service
1355+
)
13541356

13551357
if service.get("virtual_machine"):
13561358
self.vm_services_lookup[service["virtual_machine"]["id"]][
@@ -1486,14 +1488,14 @@ def refresh_ipaddresses(self):
14861488

14871489
if ipaddress["assigned_object_type"] == "virtualization.vminterface":
14881490
self.vm_ipaddresses_lookup[ip_id] = ipaddress_copy
1489-
self.vm_ipaddresses_intf_lookup[interface_id][
1490-
ip_id
1491-
] = ipaddress_copy
1491+
self.vm_ipaddresses_intf_lookup[interface_id][ip_id] = (
1492+
ipaddress_copy
1493+
)
14921494
else:
14931495
self.device_ipaddresses_lookup[ip_id] = ipaddress_copy
1494-
self.device_ipaddresses_intf_lookup[interface_id][
1495-
ip_id
1496-
] = ipaddress_copy # Remove "assigned_object_X" attributes, as that's redundant when ipaddress is added to an interface
1496+
self.device_ipaddresses_intf_lookup[interface_id][ip_id] = (
1497+
ipaddress_copy # Remove "assigned_object_X" attributes, as that's redundant when ipaddress is added to an interface
1498+
)
14971499

14981500
del ipaddress_copy["assigned_object_id"]
14991501
del ipaddress_copy["assigned_object_type"]
@@ -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)