Skip to content

Commit

Permalink
Add facility option to group_by in Inventory Plugin (#1059)
Browse files Browse the repository at this point in the history
* Add facility option to group_by in Inventory Plugin

* Fix Linting Issues
  • Loading branch information
stuntguy3000 authored Dec 10, 2023
1 parent ef2df5d commit 59ad4d8
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions plugins/inventory/nb_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@
- status
- time_zone
- utc_offset
- facility
default: []
group_names_raw:
description: Will not add the group_by choice name to the group names
Expand Down Expand Up @@ -543,6 +544,7 @@ def group_extractors(self):
"asset_tag": self.extract_asset_tag,
"time_zone": self.extract_site_time_zone,
"utc_offset": self.extract_site_utc_offset,
"facility": self.extract_site_facility,
self._pluralize_group_by("site"): self.extract_site,
self._pluralize_group_by("tenant"): self.extract_tenant,
self._pluralize_group_by("tag"): self.extract_tags,
Expand Down Expand Up @@ -763,6 +765,12 @@ def extract_site_utc_offset(self, host):
except Exception:
return

def extract_site_facility(self, host):
try:
return self.sites_facility_lookup[host["site"]["id"]]
except Exception:
return

def extract_config_context(self, host):
try:
if self.flatten_config_context:
Expand Down Expand Up @@ -1062,6 +1070,17 @@ def get_utc_offset_for_site(site):
if "utc_offset" in self.group_by:
self.sites_utc_offset_lookup = dict(map(get_utc_offset_for_site, sites))

def get_facility_for_site(site):
# Will fail if site does not have a facility defined in NetBox
try:
return (site["id"], site["facility"])
except Exception:
return (site["id"], None)

# Dictionary of site id to facility (if group by facility is used)
if "facility" in self.group_by:
self.sites_facility_lookup = dict(map(get_facility_for_site, sites))

# Note: depends on the result of refresh_sites_lookup for self.sites_with_prefixes
def refresh_prefixes(self):
# Pull all prefixes defined in NetBox
Expand Down

0 comments on commit 59ad4d8

Please sign in to comment.