122
122
123
123
import json
124
124
import uuid
125
+ from functools import partial
125
126
from sys import version as python_version
126
127
from threading import Thread
127
128
from itertools import chain
167
168
"virtual_chassis_id" ,
168
169
)
169
170
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
+
170
199
171
200
class InventoryModule (BaseInventoryPlugin , Constructable , Cacheable ):
172
201
NAME = "netbox_community.ansible_modules.netbox"
@@ -479,7 +508,7 @@ def refresh_lookups(self):
479
508
for thread in thread_list :
480
509
thread .join ()
481
510
482
- def validate_query_parameters (self , x ):
511
+ def validate_query_parameters (self , x , allowed_query_parameters ):
483
512
if not (isinstance (x , dict ) and len (x ) == 1 ):
484
513
self .display .warning (
485
514
"Warning query parameters %s not a dict with a single key." % x
@@ -489,51 +518,74 @@ def validate_query_parameters(self, x):
489
518
k = tuple (x .keys ())[0 ]
490
519
v = tuple (x .values ())[0 ]
491
520
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_" )):
493
522
msg = "Warning: %s not in %s or starting with cf (Custom field)" % (
494
523
k ,
495
- ALLOWED_DEVICE_QUERY_PARAMETERS ,
524
+ allowed_query_parameters ,
496
525
)
497
526
self .display .warning (msg = msg )
498
527
return
499
528
return k , v
500
529
501
530
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/?"
503
535
if self .query_filters :
504
- query_parameters .extend (
536
+ dev_query_parameters .extend (
505
537
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
+ ),
507
546
)
508
547
)
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
+ )
530
559
)
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
531
578
532
579
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 )
537
589
538
590
def extract_name (self , host ):
539
591
# An host in an Ansible inventory requires an hostname.
@@ -576,7 +628,6 @@ def _fill_host_variables(self, host, hostname):
576
628
577
629
def main (self ):
578
630
self .refresh_lookups ()
579
- self .refresh_url ()
580
631
hosts_list = self .fetch_hosts ()
581
632
582
633
for host in hosts_list :
0 commit comments