Skip to content

Commit 59ad4d8

Browse files
authored
Add facility option to group_by in Inventory Plugin (#1059)
* Add facility option to group_by in Inventory Plugin * Fix Linting Issues
1 parent ef2df5d commit 59ad4d8

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

plugins/inventory/nb_inventory.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@
171171
- status
172172
- time_zone
173173
- utc_offset
174+
- facility
174175
default: []
175176
group_names_raw:
176177
description: Will not add the group_by choice name to the group names
@@ -543,6 +544,7 @@ def group_extractors(self):
543544
"asset_tag": self.extract_asset_tag,
544545
"time_zone": self.extract_site_time_zone,
545546
"utc_offset": self.extract_site_utc_offset,
547+
"facility": self.extract_site_facility,
546548
self._pluralize_group_by("site"): self.extract_site,
547549
self._pluralize_group_by("tenant"): self.extract_tenant,
548550
self._pluralize_group_by("tag"): self.extract_tags,
@@ -763,6 +765,12 @@ def extract_site_utc_offset(self, host):
763765
except Exception:
764766
return
765767

768+
def extract_site_facility(self, host):
769+
try:
770+
return self.sites_facility_lookup[host["site"]["id"]]
771+
except Exception:
772+
return
773+
766774
def extract_config_context(self, host):
767775
try:
768776
if self.flatten_config_context:
@@ -1062,6 +1070,17 @@ def get_utc_offset_for_site(site):
10621070
if "utc_offset" in self.group_by:
10631071
self.sites_utc_offset_lookup = dict(map(get_utc_offset_for_site, sites))
10641072

1073+
def get_facility_for_site(site):
1074+
# Will fail if site does not have a facility defined in NetBox
1075+
try:
1076+
return (site["id"], site["facility"])
1077+
except Exception:
1078+
return (site["id"], None)
1079+
1080+
# Dictionary of site id to facility (if group by facility is used)
1081+
if "facility" in self.group_by:
1082+
self.sites_facility_lookup = dict(map(get_facility_for_site, sites))
1083+
10651084
# Note: depends on the result of refresh_sites_lookup for self.sites_with_prefixes
10661085
def refresh_prefixes(self):
10671086
# Pull all prefixes defined in NetBox

0 commit comments

Comments
 (0)