Skip to content

Commit

Permalink
changelog, pylint
Browse files Browse the repository at this point in the history
  • Loading branch information
gsnider2195 committed Oct 24, 2024
1 parent e29bafb commit 37bf820
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 19 deletions.
1 change: 1 addition & 0 deletions changes/824.added
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added a REST API endpoint for Jinja template developers to render intended configurations from templates in an arbitrary git repository.
1 change: 1 addition & 0 deletions changes/824.housekeeping
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Updated multiple tests to use the faster `setUpTestData` instead of `setUp`. Fixed incorrect base class on `ConfigPlanTest`.
2 changes: 1 addition & 1 deletion nautobot_golden_config/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ class Meta:
read_only_fields = ["device", "plan_type", "feature", "config_set"]


class GenerateIntendedConfigSerializer(serializers.Serializer):
class GenerateIntendedConfigSerializer(serializers.Serializer): # pylint: disable=abstract-method
"""Serializer for GenerateIntendedConfigView."""

intended_config = serializers.CharField()
Expand Down
10 changes: 5 additions & 5 deletions nautobot_golden_config/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,15 @@ def _get_object(self, request, model, query_param):
raise GenerateIntendedConfigException(f"Parameter {query_param} is required.")
try:
return model.objects.restrict(request.user, "view").get(pk=pk)
except model.DoesNotExist:
raise GenerateIntendedConfigException(f"{model.__name__} with id '{pk}' not found.")
except model.DoesNotExist as exc:
raise GenerateIntendedConfigException(f"{model.__name__} with id '{pk}' not found.") from exc

def _get_jinja_template_path(self, settings, device, git_repository):
"""Get the Jinja template path for the device in the provided git repository."""
try:
rendered_path = render_jinja2(template_code=settings.jinja_path_template, context={"obj": device})
except (TemplateSyntaxError, TemplateError) as exc:
raise GenerateIntendedConfigException(f"Error rendering Jinja path template: {exc}")
raise GenerateIntendedConfigException("Error rendering Jinja path template") from exc
filesystem_path = Path(git_repository.filesystem_path) / rendered_path
if not filesystem_path.is_file():
msg = f"Jinja template {filesystem_path} not found in git repository {git_repository}."
Expand Down Expand Up @@ -238,7 +238,7 @@ def get(self, request, *args, **kwargs):
try:
ensure_git_repository(git_repository)
except Exception as exc:
raise GenerateIntendedConfigException(f"Error trying to sync git repository: {exc}")
raise GenerateIntendedConfigException("Error trying to sync git repository") from exc

filesystem_path = self._get_jinja_template_path(settings, device, git_repository)

Expand All @@ -248,7 +248,7 @@ def get(self, request, *args, **kwargs):
try:
intended_config = render_jinja2(template_code=template_contents, context=context)
except (TemplateSyntaxError, TemplateError) as exc:
raise GenerateIntendedConfigException(f"Error rendering Jinja template: {exc}")
raise GenerateIntendedConfigException("Error rendering Jinja template") from exc
return Response(
data={
"intended_config": intended_config,
Expand Down
1 change: 1 addition & 0 deletions nautobot_golden_config/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,7 @@ def get_jinja_template_path_for_device(self, device):
if self.jinja_repository is not None:
rendered_path = render_jinja2(template_code=self.jinja_path_template, context={"obj": device})
return f"{self.jinja_repository.filesystem_path}{os.path.sep}{rendered_path}"
return None


@extras_features(
Expand Down
26 changes: 13 additions & 13 deletions nautobot_golden_config/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,17 +435,17 @@ def setUpTestData(cls):

cls.git_repository = GitRepository.objects.get(name="test-jinja-repo-1")

def _setup_mock_path(self, MockPath):
MockPathInstance = MockPath.return_value
MockPathInstance.__str__.return_value = "test.j2"
MockPathInstance.read_text.return_value = r"Jinja test for device {{ name }}."
MockPathInstance.is_file.return_value = True
MockPathInstance.__truediv__.return_value = MockPathInstance # to handle Path('path') / 'file'
return MockPathInstance
def _setup_mock_path(self, MockPath): # pylint: disable=invalid-name
mock_path_instance = MockPath.return_value
mock_path_instance.__str__.return_value = "test.j2"
mock_path_instance.read_text.return_value = r"Jinja test for device {{ name }}."
mock_path_instance.is_file.return_value = True
mock_path_instance.__truediv__.return_value = mock_path_instance # to handle Path('path') / 'file'
return mock_path_instance

@patch("nautobot_golden_config.api.views.ensure_git_repository")
@patch("nautobot_golden_config.api.views.Path")
def test_generate_intended_config(self, MockPath, mock_ensure_git_repository):
def test_generate_intended_config(self, MockPath, mock_ensure_git_repository): # pylint: disable=invalid-name
"""Verify that the intended config is generated as expected."""

self.add_permissions("dcim.view_device")
Expand All @@ -468,13 +468,13 @@ def test_generate_intended_config(self, MockPath, mock_ensure_git_repository):

@patch("nautobot_golden_config.api.views.ensure_git_repository")
@patch("nautobot_golden_config.api.views.Path")
def test_generate_intended_config_failures(self, MockPath, mock_ensure_git_repository):
def test_generate_intended_config_failures(self, MockPath, mock_ensure_git_repository): # pylint: disable=invalid-name
"""Verify that errors are handled as expected."""

self.add_permissions("dcim.view_device")
self.add_permissions("extras.view_gitrepository")

MockPathInstance = self._setup_mock_path(MockPath)
mock_path_instance = self._setup_mock_path(MockPath)

# test missing query parameters
response = self.client.get(
Expand Down Expand Up @@ -502,7 +502,7 @@ def test_generate_intended_config_failures(self, MockPath, mock_ensure_git_repos
)

# test git repo not present on filesystem
MockPathInstance.is_file.return_value = False
mock_path_instance.is_file.return_value = False

response = self.client.get(
reverse("plugins-api:nautobot_golden_config-api:generate_intended_config"),
Expand All @@ -519,8 +519,8 @@ def test_generate_intended_config_failures(self, MockPath, mock_ensure_git_repos
)

# test invalid jinja template
MockPathInstance.is_file.return_value = True
MockPathInstance.read_text.return_value = r"Jinja test for device {{ name }."
mock_path_instance.is_file.return_value = True
mock_path_instance.read_text.return_value = r"Jinja test for device {{ name }."

response = self.client.get(
reverse("plugins-api:nautobot_golden_config-api:generate_intended_config"),
Expand Down

0 comments on commit 37bf820

Please sign in to comment.