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

Nautobot changed vrf_id -> vrfs and tag -> tags in 2.x #68

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

dethmetaljeff
Copy link

@dethmetaljeff dethmetaljeff commented Jul 13, 2024

The API between nautobot and netbox slightly diverge with the release of 2.x. This fixes that.

Summary by Sourcery

This pull request updates the code to handle API differences between NetBox and Nautobot versions 1.x and 2.x by adding version detection and adjusting the field names for VRF and tags accordingly.

  • Enhancements:
    • Added version detection for NetBox and Nautobot to handle API differences between versions 1.x and 2.x.

Copy link

sourcery-ai bot commented Jul 13, 2024

Reviewer's Guide by Sourcery

This pull request addresses the divergence in API between NetBox and Nautobot with the release of version 2.x. The changes include detecting the version of the API and updating the filtering logic to use the new field names (vrfs and tags) when the version is 2.x.

File-Level Changes

Files Changes
octodns_netbox/__init__.py Introduced version detection for NetBox and Nautobot, and updated filtering logic to accommodate changes in field names (vrf_id to vrfs and tag to tags) in version 2.x.

Tips
  • Trigger a new Sourcery review by commenting @sourcery-ai review on the pull request.
  • Continue your discussion with Sourcery by replying directly to review comments.
  • You can change your review settings at any time by accessing your dashboard:
    • Enable or disable the Sourcery-generated pull request summary or reviewer's guide;
    • Change the review language;
  • You can always contact us if you have any questions or feedback.

@dethmetaljeff dethmetaljeff marked this pull request as draft July 13, 2024 18:05
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @dethmetaljeff - I've reviewed your changes and they look great!

Here's what I looked at during the review
  • 🟡 General issues: 3 issues found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.

Comment on lines 109 to 116
self._nb_version = "1.x"
status = self._nb_client.status()
if "netbox-version" in status:
self._nb_version = status["netbox-version"]
elif "nautobot-version" in status:
self._nb_version = status["nautobot-version"]
else:
self.log.warning("Failed to detect NetBox version, assuming 1.x")
Copy link

Choose a reason for hiding this comment

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

suggestion (bug_risk): Consider making version detection more robust

The current implementation assumes '1.x' if neither 'netbox-version' nor 'nautobot-version' is found. This could lead to issues if the actual version is different. Consider adding more robust error handling or a fallback mechanism.

Suggested change
self._nb_version = "1.x"
status = self._nb_client.status()
if "netbox-version" in status:
self._nb_version = status["netbox-version"]
elif "nautobot-version" in status:
self._nb_version = status["nautobot-version"]
else:
self.log.warning("Failed to detect NetBox version, assuming 1.x")
self._nb_version = None
status = self._nb_client.status()
if "netbox-version" in status:
self._nb_version = status["netbox-version"]
elif "nautobot-version" in status:
self._nb_version = status["nautobot-version"]
else:
self.log.error("Failed to detect NetBox version, cannot proceed")
raise ValueError("Unsupported NetBox version")

Comment on lines 170 to 175
vrf_filter_field = "vrfs" if self._nb_version.startswith("2.") else "vrf_id"
tag_filter_field = "tags" if self._nb_version.startswith("2.") else "tag"
kw = {
f"{self.field_name}__empty": "false",
f"{vrf_filter_field}": self.populate_vrf_id,
f"{tag_filter_field}": self.populate_tags,
Copy link

Choose a reason for hiding this comment

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

suggestion (performance): Potential performance impact due to repeated version checks

The repeated use of 'self._nb_version.startswith("2.")' in multiple places could have a performance impact. Consider storing the result of this check in a variable to avoid redundant computations.

Suggested change
vrf_filter_field = "vrfs" if self._nb_version.startswith("2.") else "vrf_id"
tag_filter_field = "tags" if self._nb_version.startswith("2.") else "tag"
kw = {
f"{self.field_name}__empty": "false",
f"{vrf_filter_field}": self.populate_vrf_id,
f"{tag_filter_field}": self.populate_tags,
is_version_2 = self._nb_version.startswith("2.")
vrf_filter_field = "vrfs" if is_version_2 else "vrf_id"
tag_filter_field = "tags" if is_version_2 else "tag"
kw = {
f"{self.field_name}__empty": "false",
f"{vrf_filter_field}": self.populate_vrf_id,
f"{tag_filter_field}": self.populate_tags,

Comment on lines 171 to 175
tag_filter_field = "tags" if self._nb_version.startswith("2.") else "tag"
kw = {
f"{self.field_name}__empty": "false",
f"{vrf_filter_field}": self.populate_vrf_id,
f"{tag_filter_field}": self.populate_tags,
Copy link

Choose a reason for hiding this comment

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

nitpick: Inconsistent naming conventions for filter fields

The naming conventions for 'vrf_filter_field' and 'tag_filter_field' are inconsistent. Consider standardizing the naming to improve code readability and maintainability.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant