Skip to content

Commit be2ed02

Browse files
committed
Inventory: Remove unnessary lists around singular host vars netbox-community#141
1 parent 9bdafac commit be2ed02

File tree

1 file changed

+79
-34
lines changed

1 file changed

+79
-34
lines changed

plugins/inventory/nb_inventory.py

Lines changed: 79 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@
4848
# in order of precedence
4949
- name: NETBOX_TOKEN
5050
- name: NETBOX_API_KEY
51+
plurals:
52+
description:
53+
- If True, all host vars are contained inside single-element arrays for legacy compatibility. Group names will be plural (ie. "sites_mysite" instead of "site_mysite")
54+
default: True
55+
type: boolean
56+
version_added: "0.1.11"
5157
interfaces:
5258
description:
5359
- If True, it adds the device or virtual machine interface information in host vars.
@@ -61,17 +67,25 @@
6167
type: boolean
6268
version_added: "2.0"
6369
group_by:
64-
description: Keys used to create groups.
70+
description: Keys used to create groups. The 'plurals' option controls which of these are valid.
6571
type: list
6672
choices:
6773
- sites
74+
- site
6875
- tenants
76+
- tenant
6977
- racks
78+
- rack
7079
- tags
80+
- tag
7181
- device_roles
82+
- device_role
7283
- device_types
84+
- device_type
7385
- manufacturers
86+
- manufacturer
7487
- platforms
88+
- platform
7589
default: []
7690
group_names_raw:
7791
description: Will not add the group_by choice name to the group names
@@ -285,23 +299,50 @@ def get_resource_list(self, api_url):
285299

286300
@property
287301
def group_extractors(self):
288-
return {
289-
"sites": self.extract_site,
290-
"tenants": self.extract_tenant,
291-
"racks": self.extract_rack,
292-
"tags": self.extract_tags,
293-
"disk": self.extract_disk,
294-
"memory": self.extract_memory,
295-
"vcpus": self.extract_vcpus,
296-
"device_roles": self.extract_device_role,
297-
"platforms": self.extract_platform,
298-
"device_types": self.extract_device_type,
299-
"services": self.extract_services,
300-
"config_context": self.extract_config_context,
301-
"manufacturers": self.extract_manufacturer,
302-
"interfaces": self.extract_interfaces,
303-
"custom_fields": self.extract_custom_fields,
304-
}
302+
303+
if self.plurals:
304+
return {
305+
"sites": self.extract_site,
306+
"tenants": self.extract_tenant,
307+
"racks": self.extract_rack,
308+
"tags": self.extract_tags,
309+
"disk": self.extract_disk,
310+
"memory": self.extract_memory,
311+
"vcpus": self.extract_vcpus,
312+
"device_roles": self.extract_device_role,
313+
"platforms": self.extract_platform,
314+
"device_types": self.extract_device_type,
315+
"services": self.extract_services,
316+
"config_context": self.extract_config_context,
317+
"manufacturers": self.extract_manufacturer,
318+
"interfaces": self.extract_interfaces,
319+
"custom_fields": self.extract_custom_fields,
320+
}
321+
else:
322+
return {
323+
"site": self.extract_site,
324+
"tenant": self.extract_tenant,
325+
"rack": self.extract_rack,
326+
"tag": self.extract_tags,
327+
"disk": self.extract_disk,
328+
"memory": self.extract_memory,
329+
"vcpus": self.extract_vcpus,
330+
"device_role": self.extract_device_role,
331+
"platform": self.extract_platform,
332+
"device_type": self.extract_device_type,
333+
"services": self.extract_services,
334+
"config_context": self.extract_config_context,
335+
"manufacturer": self.extract_manufacturer,
336+
"interfaces": self.extract_interfaces,
337+
"custom_fields": self.extract_custom_fields,
338+
}
339+
340+
def _pluralize(self, something):
341+
# If plurals is enabled, wrap in a single-element list for backwards compatibility
342+
if self.plurals:
343+
return [something]
344+
else:
345+
return something
305346

306347
def extract_disk(self, host):
307348
return host.get("disk")
@@ -314,7 +355,7 @@ def extract_memory(self, host):
314355

315356
def extract_platform(self, host):
316357
try:
317-
return [self.platforms_lookup[host["platform"]["id"]]]
358+
return self._pluralize(self.platforms_lookup[host["platform"]["id"]])
318359
except Exception:
319360
return
320361

@@ -333,48 +374,50 @@ def extract_services(self, host):
333374

334375
def extract_device_type(self, host):
335376
try:
336-
return [self.device_types_lookup[host["device_type"]["id"]]]
377+
return self._pluralize(self.device_types_lookup[host["device_type"]["id"]])
337378
except Exception:
338379
return
339380

340381
def extract_rack(self, host):
341382
try:
342-
return [self.racks_lookup[host["rack"]["id"]]]
383+
return self._pluralize(self.racks_lookup[host["rack"]["id"]])
343384
except Exception:
344385
return
345386

346387
def extract_site(self, host):
347388
try:
348-
return [self.sites_lookup[host["site"]["id"]]]
389+
return self._pluralize(self.sites_lookup[host["site"]["id"]])
349390
except Exception:
350391
return
351392

352393
def extract_tenant(self, host):
353394
try:
354-
return [self.tenants_lookup[host["tenant"]["id"]]]
395+
return self._pluralize(self.tenants_lookup[host["tenant"]["id"]])
355396
except Exception:
356397
return
357398

358399
def extract_device_role(self, host):
359400
try:
360401
if "device_role" in host:
361-
return [self.device_roles_lookup[host["device_role"]["id"]]]
402+
return self._pluralize(
403+
self.device_roles_lookup[host["device_role"]["id"]]
404+
)
362405
elif "role" in host:
363-
return [self.device_roles_lookup[host["role"]["id"]]]
406+
return self._pluralize(self.device_roles_lookup[host["role"]["id"]])
364407
except Exception:
365408
return
366409

367410
def extract_config_context(self, host):
368411
try:
369-
return [host["config_context"]]
412+
return self._pluralize(host["config_context"])
370413
except Exception:
371414
return
372415

373416
def extract_manufacturer(self, host):
374417
try:
375-
return [
418+
return self._pluralize(
376419
self.manufacturers_lookup[host["device_type"]["manufacturer"]["id"]]
377-
]
420+
)
378421
except Exception:
379422
return
380423

@@ -619,17 +662,18 @@ def extract_name(self, host):
619662
return host["name"] or str(uuid.uuid4())
620663

621664
def add_host_to_groups(self, host, hostname):
622-
for group in self.group_by:
623-
sub_groups = self.group_extractors[group](host)
665+
for grouping in self.group_by:
666+
groups_for_host = self.group_extractors[grouping](host)
624667

625-
if not sub_groups:
668+
if not groups_for_host:
626669
continue
627670

628-
for sub_group in sub_groups:
671+
for group_for_host in groups_for_host:
629672
if self.group_names_raw:
630-
group_name = sub_group
673+
group_name = group_for_host
631674
else:
632-
group_name = "_".join([group, sub_group])
675+
group_name = "_".join([grouping, group_for_host])
676+
633677
self.inventory.add_group(group=group_name)
634678
self.inventory.add_host(group=group_name, host=hostname)
635679

@@ -693,6 +737,7 @@ def parse(self, inventory, loader, path, cache=True):
693737
self.timeout = self.get_option("timeout")
694738
self.validate_certs = self.get_option("validate_certs")
695739
self.config_context = self.get_option("config_context")
740+
self.plurals = self.get_option("plurals")
696741
self.interfaces = self.get_option("interfaces")
697742
self.services = self.get_option("services")
698743
self.headers = {

0 commit comments

Comments
 (0)