Skip to content
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

add support to fetch cluster device information from netbox. #1169

Open
wants to merge 15 commits into
base: devel
Choose a base branch
from
Open
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions plugins/inventory/nb_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,7 @@ def group_extractors(self):
"custom_fields": self.extract_custom_fields,
"region": self.extract_regions,
"cluster": self.extract_cluster,
"cluster_device": self.extract_cluster_device,
"cluster_group": self.extract_cluster_group,
"cluster_type": self.extract_cluster_type,
"is_virtual": self.extract_is_virtual,
Expand Down Expand Up @@ -943,6 +944,13 @@ def extract_cluster(self, host):
except Exception:
return

def extract_cluster_device(self, host):
try:
# cluster device does not have a slug
return host["device"]["name"]
except Exception:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a more specific exception we could use like KeyError?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this also use a more common pattern of checking if the key exists before accessing, instead of a try/except block?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if i have understand it right, but have developed a solution without try/catch and with returning a suitable value in every case.
New version see below

return

def extract_cluster_group(self, host):
try:
return self.clusters_group_lookup[host["cluster"]["id"]]
Expand Down
Loading