nautobot-app-ssot icon indicating copy to clipboard operation
nautobot-app-ssot copied to clipboard

VLANs not properly associated with Prefixes

Open steven-douilliet opened this issue 2 months ago • 3 comments

Environment

  • Python version: 3.9.19
  • Nautobot version: 2.2.0
  • nautobot-ssot version: 2.5.0

Expected Behavior

I expected Prefixes synchronization from Infoblox to Nautobot, including the associated VLANs.

During synchronization, Prefixes should be automatically updated or created and linked to VLANs using the VLAN field available in the Location/VLAN Assignment section (which contains fields for Locations, VLAN group, and VLAN).

Observed Behavior

The VLAN field from the Location/VLAN Assignment section is not set.

The SSoT plugin creates a Relationship Prefix -> VLAN and uses this field to associate the Prefix to VLAN, while the Prefix Model has a VLAN field which is a foreign key to the VLAN model.

So, the SSoT will update the relationship field instead of updating the VLAN field.

Workaround

Update VLAN field instead the relation ship:

class NautobotNetwork(Network):
    """Nautobot implementation of the Network Model."""

    @classmethod
    def create(cls, diffsync, ids, attrs):
        """Create Prefix object in Nautobot."""
        _prefix = OrmPrefix(
            prefix=ids["network"],
            status_id=diffsync.status_map["Active"],
            type=attrs["network_type"],
            description=attrs.get("description", ""),
        )
        prefix_ranges = attrs.get("ranges")
        if prefix_ranges:
            _prefix.cf["dhcp_ranges"] = ",".join(prefix_ranges)
        if attrs.get("vlans"):
            for _, _vlan in attrs["vlans"].items():
                index = 0
                try:
                    found_vlan = diffsync.vlan_map[_vlan["group"]][_vlan["vid"]]
                    if found_vlan:
                        if index == 0:
                            _prefix.vlan_id = found_vlan
                    index += 1
                except KeyError as err:
                    diffsync.job.logger.warning(
                        f"Unable to find VLAN {_vlan['vid']} {_vlan['name']} in {_vlan['group']}. {err}"
                    )

        if attrs.get("ext_attrs"):
            process_ext_attrs(diffsync=diffsync, obj=_prefix, extattrs=attrs["ext_attrs"])
        _prefix.validated_save()
        diffsync.prefix_map[ids["network"]] = _prefix.id
        return super().create(ids=ids, diffsync=diffsync, attrs=attrs)

    def update(self, attrs):  # pylint: disable=too-many-branches
        """Update Prefix object in Nautobot."""
        _pf = OrmPrefix.objects.get(id=self.pk)
        if self.diffsync.job.debug:
            self.diffsync.job.logger.debug(f"Attempting to update Prefix {_pf.prefix} with {attrs}.")
        if "description" in attrs:
            _pf.description = attrs["description"]
        if "network_type" in attrs:
            _pf.type = attrs["network_type"]
        if "ext_attrs" in attrs:
            process_ext_attrs(diffsync=self.diffsync, obj=_pf, extattrs=attrs["ext_attrs"])
        prefix_ranges = attrs.get("ranges")
        if prefix_ranges:
            _pf.cf["dhcp_ranges"] = ",".join(prefix_ranges)
        if "vlans" in attrs:
            _vlans = attrs["vlans"]
            if len(_vlans) == 1:
                for _, item in attrs["vlans"].items():
                    try:
                        vlan = OrmVlan.objects.get(vid=item["vid"], name=item["name"], vlan_group__name=item["group"])
                        _pf.vlan = vlan
                        self.diffsync.job.logger.debug(f"{_pf.prefix} updated with {vlan}")

                    except OrmVlan.DoesNotExist:
                        if self.diffsync.job.debug:
                            self.diffsync.job.logger.debug(
                                f"Unable to find VLAN {item['vid']} {item['name']} in {item['group']} to assign to prefix {_pf.prefix}."
                            )
                        continue
            _pf.validated_save()
        return super().update(attrs)

steven-douilliet avatar Apr 16 '24 13:04 steven-douilliet