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

Fix Wrong Parent Prefix in Citrix ADM #647

Merged
merged 5 commits into from
Jan 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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