diff --git a/changes/646.fixed b/changes/646.fixed new file mode 100644 index 00000000..ab2306f4 --- /dev/null +++ b/changes/646.fixed @@ -0,0 +1 @@ +Fixed IPAddress assigned wrong parent Prefix in Citrix ADM. \ No newline at end of file diff --git a/nautobot_ssot/integrations/citrix_adm/diffsync/adapters/citrix_adm.py b/nautobot_ssot/integrations/citrix_adm/diffsync/adapters/citrix_adm.py index ff51d96a..29187f9d 100644 --- a/nautobot_ssot/integrations/citrix_adm/diffsync/adapters/citrix_adm.py +++ b/nautobot_ssot/integrations/citrix_adm/diffsync/adapters/citrix_adm.py @@ -9,6 +9,7 @@ from nautobot.extras.choices import SecretsGroupAccessTypeChoices, SecretsGroupSecretTypeChoices from nautobot.extras.models import ExternalIntegration, Job from nautobot.tenancy.models import Tenant +from netutils.ip import is_ip_within from nautobot_ssot.integrations.citrix_adm.constants import DEVICETYPE_MAP from nautobot_ssot.integrations.citrix_adm.diffsync.models.citrix_adm import ( @@ -291,6 +292,25 @@ def load_address_to_interface(self, address: str, device: str, port: str, primar new_map = self.ip_on_intf(address=address, device=device, port=port, primary=primary, uuid=None) self.add(new_map) + def find_closer_parent_prefix(self) -> None: + """Find more accurate parent Prefix for loaded IPAddresses.""" + for ipaddr in self.get_all(obj="address"): + for prefix in self.get_all(obj="prefix"): + # check if prefixes are both IPv4 or IPv6 + if (":" in ipaddr.prefix and ":" not in prefix.prefix) or ( + ":" in prefix.prefix and ":" not in ipaddr.prefix + ): + continue + if not is_ip_within(ipaddr.prefix, prefix.prefix): + host_addr = ipaddr.address.split("/")[0] + if is_ip_within(host_addr, prefix.prefix): + if self.job.debug: + self.job.logger.debug( + "More specific Prefix %s found for IPAddress %s", prefix.prefix, ipaddr.address + ) + ipaddr.prefix = prefix.prefix + self.update(ipaddr) + def load(self): """Load data from Citrix ADM into DiffSync models.""" for instance in self.instances: @@ -321,6 +341,7 @@ def load(self): self.create_port_map() self.load_ports() self.load_addresses() + self.find_closer_parent_prefix() self.conn.logout() else: diff --git a/nautobot_ssot/integrations/citrix_adm/diffsync/models/nautobot.py b/nautobot_ssot/integrations/citrix_adm/diffsync/models/nautobot.py index 4adecf80..9558347a 100644 --- a/nautobot_ssot/integrations/citrix_adm/diffsync/models/nautobot.py +++ b/nautobot_ssot/integrations/citrix_adm/diffsync/models/nautobot.py @@ -255,7 +255,7 @@ def create(cls, adapter, ids, attrs): """Create IP Address in Nautobot from NautobotAddress object.""" new_ip = IPAddress( address=ids["address"], - parent=Prefix.objects.filter(network__net_contains=ids["address"].split("/")[0]).last(), + parent=Prefix.objects.get(prefix=ids["prefix"]), status=Status.objects.get(name="Active"), namespace=( Namespace.objects.get_or_create(name=attrs["tenant"])[0]