Skip to content

Add facility option to group_by in Inventory Plugin #1059

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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