Skip to content

Commit

Permalink
improves error handling for git repositories providing command mappers
Browse files Browse the repository at this point in the history
  • Loading branch information
Kircheneer committed Dec 19, 2024
1 parent 7720cee commit faf5353
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 7 deletions.
1 change: 1 addition & 0 deletions changes/289.added
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Added additional error handling/logging to the git repository sync method
2 changes: 2 additions & 0 deletions changes/289.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Fixed typos in the 3.0 changelog
- Fixed a logging typo in an adapter
6 changes: 6 additions & 0 deletions nautobot_device_onboarding/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,9 @@
"FastEthernet": "100base-fx",
"ethernet": "1000base-t",
}

# The git repository data source content identifier for custom command mappers.
ONBOARDING_COMMAND_MAPPERS_CONTENT_IDENTIFIER = "nautobot_device_onboarding.onboarding_command_mappers"

# The git repository data source folder name for custom command mappers.
ONBOARDING_COMMAND_MAPPERS_REPOSITORY_FOLDER = "onboarding_command_mappers"
36 changes: 33 additions & 3 deletions nautobot_device_onboarding/datasources.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,53 @@
"""Datasources to override command_mapper yaml files."""

from pathlib import Path

from nautobot.apps.datasources import DatasourceContent
from nautobot.extras.choices import LogLevelChoices

from nautobot_device_onboarding.constants import (
ONBOARDING_COMMAND_MAPPERS_CONTENT_IDENTIFIER,
ONBOARDING_COMMAND_MAPPERS_REPOSITORY_FOLDER,
)


def refresh_git_command_mappers(repository_record, job_result, delete=False): # pylint: disable=unused-argument
"""Callback for gitrepository updates on Command Mapper Repo."""
# Since we don't create any DB records we can just ignore deletions.
if delete:
return
if ONBOARDING_COMMAND_MAPPERS_CONTENT_IDENTIFIER not in repository_record.provided_contents:
return
job_result.log(
"Successfully Pulled Command Mapper Repo",
level_choice=LogLevelChoices.LOG_DEBUG,
"Refreshing network sync job command mappers...",
level_choice=LogLevelChoices.LOG_INFO,
)
repo_data_dir = Path(repository_record.filesystem_path) / ONBOARDING_COMMAND_MAPPERS_REPOSITORY_FOLDER
if not repo_data_dir.exists():
# Shouldn't use an f string here as it is a log message.
job_result.log(
"Command mapper repo folder does not exist. Create a sub folder in the repository at '%'".format( # pylint: disable=consider-using-fstring
ONBOARDING_COMMAND_MAPPERS_REPOSITORY_FOLDER
),
level_choice=LogLevelChoices.LOG_WARNING,
)
return
try:
next(repo_data_dir.glob("*.yml"))
except StopIteration:
job_result.log(
"Command mapper repo folder found, but it doesn't contain any command mapper files."
"They need to have the '.yml' extension.",
level_choice=LogLevelChoices.LOG_WARNING,
)


datasource_contents = [
(
"extras.gitrepository",
DatasourceContent(
name="Network Sync Job Command Mappers",
content_identifier="nautobot_device_onboarding.onboarding_command_mappers",
content_identifier=ONBOARDING_COMMAND_MAPPERS_CONTENT_IDENTIFIER,
icon="mdi-paw",
callback=refresh_git_command_mappers,
),
Expand Down
9 changes: 7 additions & 2 deletions nautobot_device_onboarding/nornir_plays/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
import yaml
from nautobot.extras.models import GitRepository

from nautobot_device_onboarding.constants import (
ONBOARDING_COMMAND_MAPPERS_CONTENT_IDENTIFIER,
ONBOARDING_COMMAND_MAPPERS_REPOSITORY_FOLDER,
)

DATA_DIR = os.path.abspath(os.path.join(os.path.dirname(os.path.dirname(__file__)), "command_mappers"))


Expand All @@ -17,7 +22,7 @@ def get_git_repo():
== 1
):
repository_record = GitRepository.objects.filter(
provided_contents=["nautobot_device_onboarding.onboarding_command_mappers"]
provided_contents=[ONBOARDING_COMMAND_MAPPERS_CONTENT_IDENTIFIER]
).first()
return repository_record
return None
Expand All @@ -40,7 +45,7 @@ def add_platform_parsing_info():
"""Merges platform command mapper from repo or defaults."""
repository_record = get_git_repo()
if repository_record:
repo_data_dir = os.path.join(repository_record.filesystem_path, "onboarding_command_mappers")
repo_data_dir = os.path.join(repository_record.filesystem_path, ONBOARDING_COMMAND_MAPPERS_REPOSITORY_FOLDER)
command_mappers_repo_path = load_command_mappers_from_dir(repo_data_dir)
else:
command_mappers_repo_path = {}
Expand Down
5 changes: 3 additions & 2 deletions nautobot_device_onboarding/tests/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from nautobot.extras.choices import JobResultStatusChoices
from nautobot.extras.models import GitRepository, JobResult

from nautobot_device_onboarding.constants import ONBOARDING_COMMAND_MAPPERS_CONTENT_IDENTIFIER
from nautobot_device_onboarding.nornir_plays.transform import add_platform_parsing_info, load_command_mappers_from_dir

MOCK_DIR = os.path.join("nautobot_device_onboarding", "tests", "mock")
Expand Down Expand Up @@ -56,7 +57,7 @@ def setUp(self):
name="Test Git Repository",
slug=self.repo_slug,
remote_url="http://localhost/git.git",
provided_contents=["nautobot_device_onboarding.onboarding_command_mappers"],
provided_contents=[ONBOARDING_COMMAND_MAPPERS_CONTENT_IDENTIFIER],
)
self.repo.save()
self.job_result = JobResult.objects.create(name=self.repo.name)
Expand All @@ -81,7 +82,7 @@ def populate_repo(self, path, url, *args, **kwargs):

def test_git_repo_was_created(self, MockGitRepo): # pylint:disable=invalid-name
repo_count = GitRepository.objects.filter(
provided_contents=["nautobot_device_onboarding.onboarding_command_mappers"]
provided_contents=[ONBOARDING_COMMAND_MAPPERS_CONTENT_IDENTIFIER]
).count()
self.assertEqual(1, repo_count)

Expand Down

0 comments on commit faf5353

Please sign in to comment.