Skip to content

Commit

Permalink
Inventory: Clean up logic handling plurals and group_by
Browse files Browse the repository at this point in the history
  • Loading branch information
DouglasHeriot committed May 13, 2020
1 parent add5fe8 commit 743e8ff
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions plugins/inventory/nb_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,18 +347,14 @@ def group_extractors(self):
"cluster_group": self.extract_cluster_group,
"cluster_type": self.extract_cluster_type,
"is_virtual": self.extract_is_virtual,
("sites" if self.plurals else "site"): self.extract_site,
("tenants" if self.plurals else "tenant"): self.extract_tenant,
("racks" if self.plurals else "rack"): self.extract_rack,
("tags" if self.plurals else "tag"): self.extract_tags,
("device_roles" if self.plurals else "role"): self.extract_device_role,
("platforms" if self.plurals else "platform"): self.extract_platform,
(
"device_types" if self.plurals else "device_type"
): self.extract_device_type,
(
"manufacturers" if self.plurals else "manufacturer"
): self.extract_manufacturer,
self._pluralize_group_by("site"): self.extract_site,
self._pluralize_group_by("tenant"): self.extract_tenant,
self._pluralize_group_by("rack"): self.extract_rack,
self._pluralize_group_by("tag"): self.extract_tags,
self._pluralize_group_by("role"): self.extract_device_role,
self._pluralize_group_by("platform"): self.extract_platform,
self._pluralize_group_by("device_type"): self.extract_device_type,
self._pluralize_group_by("manufacturer"): self.extract_manufacturer,
}

if self.services:
Expand All @@ -373,6 +369,24 @@ def group_extractors(self):

return extractors

def _pluralize_group_by(self, group_by):
mapping = {
"site": "sites",
"tenant": "tenants",
"rack": "racks",
"tag": "tags",
"role": "device_roles",
"platform": "platforms",
"device_type": "device_types",
"manufacturer": "manufacturers",
}

if self.plurals:
mapped = mapping.get(group_by)
return mapped or group_by
else:
return group_by

def _pluralize(self, extracted_value):
# If plurals is enabled, wrap in a single-element list for backwards compatibility
if self.plurals:
Expand Down Expand Up @@ -1036,10 +1050,9 @@ def add_host_to_groups(self, host, hostname):

if "region" in self.group_by:
# Make sure "site" or "sites" grouping also exists, depending on plurals options
if self.plurals and "sites" not in self.group_by:
self.group_by.append("sites")
elif not self.plurals and "site" not in self.group_by:
self.group_by.append("site")
site_group_by = self._pluralize_group_by("site")
if site_group_by not in self.group_by:
self.group_by.append(site_group_by)

for grouping in self.group_by:

Expand Down Expand Up @@ -1108,7 +1121,7 @@ def _add_region_groups(self):

site_name = self.sites_lookup[site_id]
site_group_name = self.generate_group_name(
"sites" if self.plurals else "site", site_name
self._pluralize_group_by("site"), site_name
)
# Add the site group to get its transformed name
# Will already be created by add_host_to_groups - it's ok to call add_group again just to get its name
Expand Down

0 comments on commit 743e8ff

Please sign in to comment.