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 hostname_mapping in Citrix ADM #607

Merged
merged 4 commits into from
Nov 21, 2024
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/607.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix hostname_mapping functionailty in Citrix ADM integration.
1 change: 1 addition & 0 deletions changes/607.housekeeping
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove redundant parse_hostname_for_role() function in Meraki integration that was missed in 599.
12 changes: 7 additions & 5 deletions nautobot_ssot/integrations/citrix_adm/jobs.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
"""Jobs for Citrix ADM SSoT integration."""

from ast import literal_eval

from diffsync.enum import DiffSyncFlags
from django.templatetags.static import static
from django.urls import reverse
from nautobot.core.celery import register_jobs
from nautobot.dcim.models import Location, LocationType
from nautobot.extras.jobs import BooleanVar, Job, JSONVar, MultiObjectVar, ObjectVar
from nautobot.extras.jobs import BooleanVar, Job, JSONVar, MultiObjectVar, ObjectVar, StringVar
from nautobot.extras.models import ExternalIntegration
from nautobot.tenancy.models import Tenant

Expand Down Expand Up @@ -50,10 +52,10 @@ class CitrixAdmDataSource(DataSource, Job): # pylint: disable=too-many-instance
default={},
required=False,
)
hostname_mapping = JSONVar(
hostname_mapping = StringVar(
label="Hostname Mapping",
description="Mapping of Device hostname to Role. Ex: {'router01': 'router'}.",
default={},
description="List of tuples containing Device hostname regex patterns to assign to specified Role. ex: [('.*ilb.*', 'Internal Load-Balancer')]",
default=[],
required=False,
)
tenant = ObjectVar(model=Tenant, queryset=Tenant.objects.all(), display_field="display_name", required=False)
Expand Down Expand Up @@ -135,7 +137,7 @@ def run( # pylint: disable=arguments-differ, too-many-arguments
self.dc_loctype = kwargs["dc_loctype"]
self.parent_location = kwargs["parent_location"]
self.location_map = kwargs["location_map"]
self.hostname_mapping = kwargs["hostname_mapping"]
self.hostname_mapping = literal_eval(kwargs["hostname_mapping"])
self.validate_job_settings()
self.memory_profiling = memory_profiling
super().run(dryrun=self.dryrun, memory_profiling=self.memory_profiling, *args, **kwargs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
MerakiPrefix,
MerakiPrefixLocation,
)
from nautobot_ssot.integrations.meraki.utils.meraki import get_role_from_devicetype, parse_hostname_for_role
from nautobot_ssot.integrations.meraki.utils.meraki import get_role_from_devicetype
from nautobot_ssot.utils import parse_hostname_for_role


class MerakiAdapter(Adapter):
Expand Down Expand Up @@ -97,7 +98,9 @@ def load_devices(self): # pylint: disable=too-many-branches
if self.job.hostname_mapping and len(self.job.hostname_mapping) > 0:
if self.job.debug:
self.job.logger.debug(f"Parsing hostname for device {dev['name']} to determine role.")
role = parse_hostname_for_role(dev_hostname=dev["name"], hostname_map=self.job.hostname_mapping)
role = parse_hostname_for_role(
device_hostname=dev["name"], hostname_map=self.job.hostname_mapping, default_role="Unknown"
)
elif self.job.devicetype_mapping and len(self.job.devicetype_mapping) > 0:
if self.job.debug:
self.job.logger.debug(f"Parsing device model for device {dev['name']} to determine role.")
Expand Down
20 changes: 0 additions & 20 deletions nautobot_ssot/integrations/meraki/utils/meraki.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
"""Utility functions for working with Meraki."""

import re

import meraki


Expand Down Expand Up @@ -205,24 +203,6 @@ def get_appliance_switchports(self, network_id: str) -> list:
return ports


def parse_hostname_for_role(dev_hostname: str, hostname_map: dict) -> str:
"""Parse device hostname to get Device Role.

Args:
dev_hostname (str): Hostname of Device to determine role of.
hostname_map (dict): Dictionary of hostname's mapped to their Role.

Returns:
str: Name of DeviceRole. Defaults to Unknown.
"""
dev_role = "Unknown"
for entry in hostname_map:
match = re.match(pattern=entry[0], string=dev_hostname)
if match:
dev_role = entry[1]
return dev_role


def get_role_from_devicetype(dev_model: str, devicetype_map: dict) -> str:
"""Get Device Role using DeviceType from devicetype_mapping Setting.

Expand Down