Skip to content

Commit eb30321

Browse files
Inventory: Added logic to not pull devices/vms if query_filters are not valid (#103)
1 parent ad10114 commit eb30321

File tree

1 file changed

+83
-32
lines changed

1 file changed

+83
-32
lines changed

plugins/inventory/netbox.py

Lines changed: 83 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@
122122

123123
import json
124124
import uuid
125+
from functools import partial
125126
from sys import version as python_version
126127
from threading import Thread
127128
from itertools import chain
@@ -167,6 +168,34 @@
167168
"virtual_chassis_id",
168169
)
169170

171+
ALLOWED_VM_QUERY_PARAMETERS = (
172+
"cluster",
173+
"cluster_id",
174+
"cluster_group",
175+
"cluster_group_id",
176+
"cluster_type",
177+
"cluster_type_id",
178+
"disk",
179+
"mac_address",
180+
"memory",
181+
"name",
182+
"platform",
183+
"platform_id",
184+
"region",
185+
"region_id",
186+
"role",
187+
"role_id",
188+
"site",
189+
"site_id",
190+
"status",
191+
"tag",
192+
"tenant",
193+
"tenant_id",
194+
"tenant_group",
195+
"tenant_group_id",
196+
"vcpus",
197+
)
198+
170199

171200
class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable):
172201
NAME = "netbox_community.ansible_modules.netbox"
@@ -479,7 +508,7 @@ def refresh_lookups(self):
479508
for thread in thread_list:
480509
thread.join()
481510

482-
def validate_query_parameters(self, x):
511+
def validate_query_parameters(self, x, allowed_query_parameters):
483512
if not (isinstance(x, dict) and len(x) == 1):
484513
self.display.warning(
485514
"Warning query parameters %s not a dict with a single key." % x
@@ -489,51 +518,74 @@ def validate_query_parameters(self, x):
489518
k = tuple(x.keys())[0]
490519
v = tuple(x.values())[0]
491520

492-
if not (k in ALLOWED_DEVICE_QUERY_PARAMETERS or k.startswith("cf_")):
521+
if not (k in allowed_query_parameters or k.startswith("cf_")):
493522
msg = "Warning: %s not in %s or starting with cf (Custom field)" % (
494523
k,
495-
ALLOWED_DEVICE_QUERY_PARAMETERS,
524+
allowed_query_parameters,
496525
)
497526
self.display.warning(msg=msg)
498527
return
499528
return k, v
500529

501530
def refresh_url(self):
502-
query_parameters = [("limit", 0)]
531+
dev_query_parameters = [("limit", 0)]
532+
vm_query_parameters = [("limit", 0)]
533+
device_url = self.api_endpoint + "/api/dcim/devices/?"
534+
vm_url = self.api_endpoint + "/api/virtualization/virtual-machines/?"
503535
if self.query_filters:
504-
query_parameters.extend(
536+
dev_query_parameters.extend(
505537
filter(
506-
lambda x: x, map(self.validate_query_parameters, self.query_filters)
538+
lambda x: x,
539+
map(
540+
partial(
541+
self.validate_query_parameters,
542+
allowed_query_parameters=ALLOWED_DEVICE_QUERY_PARAMETERS,
543+
),
544+
self.query_filters,
545+
),
507546
)
508547
)
509-
if self.config_context:
510-
self.device_url = (
511-
self.api_endpoint + "/api/dcim/devices/?" + urlencode(query_parameters)
512-
)
513-
self.virtual_machines_url = (
514-
self.api_endpoint
515-
+ "/api/virtualization/virtual-machines/?"
516-
+ urlencode(query_parameters)
517-
)
518-
else:
519-
self.device_url = (
520-
self.api_endpoint
521-
+ "/api/dcim/devices/?"
522-
+ urlencode(query_parameters)
523-
+ "&exclude=config_context"
524-
)
525-
self.virtual_machines_url = (
526-
self.api_endpoint
527-
+ "/api/virtualization/virtual-machines/?"
528-
+ urlencode(query_parameters)
529-
+ "&exclude=config_context"
548+
vm_query_parameters.extend(
549+
filter(
550+
lambda x: x,
551+
map(
552+
partial(
553+
self.validate_query_parameters,
554+
allowed_query_parameters=ALLOWED_VM_QUERY_PARAMETERS,
555+
),
556+
self.query_filters,
557+
),
558+
)
530559
)
560+
if len(dev_query_parameters) <= 1:
561+
device_url = None
562+
563+
if len(vm_query_parameters) <= 1:
564+
vm_url = None
565+
566+
if device_url:
567+
device_url = device_url + urlencode(dev_query_parameters)
568+
if vm_url:
569+
vm_url = vm_url + urlencode(vm_query_parameters)
570+
571+
if not self.config_context:
572+
if device_url:
573+
device_url = device_url + "&exclude=config_context"
574+
if vm_url:
575+
vm_url = vm_url + "&exclude=config_context"
576+
577+
return device_url, vm_url
531578

532579
def fetch_hosts(self):
533-
return chain(
534-
self.get_resource_list(self.device_url),
535-
self.get_resource_list(self.virtual_machines_url),
536-
)
580+
device_url, vm_url = self.refresh_url()
581+
if device_url and vm_url:
582+
return chain(
583+
self.get_resource_list(device_url), self.get_resource_list(vm_url),
584+
)
585+
elif device_url:
586+
return self.get_resource_list(device_url)
587+
elif vm_url:
588+
return self.get_resource_list(vm_url)
537589

538590
def extract_name(self, host):
539591
# An host in an Ansible inventory requires an hostname.
@@ -576,7 +628,6 @@ def _fill_host_variables(self, host, hostname):
576628

577629
def main(self):
578630
self.refresh_lookups()
579-
self.refresh_url()
580631
hosts_list = self.fetch_hosts()
581632

582633
for host in hosts_list:

0 commit comments

Comments
 (0)