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

Preserve ACI Tag color and device types settings #660

Merged
merged 9 commits into from
Jan 16, 2025
2 changes: 2 additions & 0 deletions changes/654.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed ACI signal initializing Tags without ContentType or Color being populated
Fixed ACI signal initializing Tag throwing duplicate key exception.
36 changes: 19 additions & 17 deletions nautobot_ssot/integrations/aci/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,34 @@ def register_signals(sender):
nautobot_database_ready.connect(interface_custom_fields, sender=sender)


def _ensure_tag(apps, name, color):
"""Ensure tag exists and properly configured."""
ContentType = apps.get_model("contenttypes", "ContentType")
tag = apps.get_model("extras", "Tag")
_tag = tag.objects.get_or_create(name=name)[0]
if _tag.color != color:
_tag.color == color # pylint: disable=pointless-statement
kingfetty marked this conversation as resolved.
Show resolved Hide resolved
_tag.validated_save()
for content_type in ContentType.objects.all():
if content_type not in _tag.content_types.all():
_tag.content_types.add(content_type)


def aci_create_tag(apps, **kwargs):
"""Add a tag."""
tag = apps.get_model("extras", "Tag")
logger.info("Creating tags for ACI, interface status and Sites")
_ensure_tag(apps=apps, name=PLUGIN_CFG.get("tag"), color=PLUGIN_CFG.get("tag_color"))
_ensure_tag(apps=apps, name=PLUGIN_CFG.get("tag_up"), color=PLUGIN_CFG.get("tag_up_color"))
_ensure_tag(apps=apps, name=PLUGIN_CFG.get("tag_down"), color=PLUGIN_CFG.get("tag_down_color"))
_ensure_tag(apps=apps, name="ACI_MULTISITE", color="03a9f4")

tag.objects.update_or_create(
name=PLUGIN_CFG.get("tag"),
color=PLUGIN_CFG.get("tag_color"),
)
tag.objects.update_or_create(
name=PLUGIN_CFG.get("tag_up"),
color=PLUGIN_CFG.get("tag_up_color"),
)
tag.objects.update_or_create(
name=PLUGIN_CFG.get("tag_down"),
color=PLUGIN_CFG.get("tag_down_color"),
)
tag.objects.update_or_create(
name="ACI_MULTISITE",
color="03a9f4",
)
apics = PLUGIN_CFG.get("apics")
if apics:
for key in apics:
if ("SITE" in key or "STAGE" in key) and not tag.objects.filter(name=apics[key]).exists():
tag.objects.update_or_create(
_ensure_tag(
apps=apps,
name=apics[key],
color="".join([random.choice("ABCDEF0123456789") for i in range(6)]), # noqa: S311
)
Expand Down