Skip to content

Commit

Permalink
Merge pull request #647 from nautobot/patch-adm_ip_parent_bug
Browse files Browse the repository at this point in the history
Fix Wrong Parent Prefix in Citrix ADM
  • Loading branch information
jdrew82 authored Jan 8, 2025
2 parents b55043c + 60cba0a commit c3a9272
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions changes/646.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed IPAddress assigned wrong parent Prefix in Citrix ADM.
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit c3a9272

Please sign in to comment.