Skip to content

Commit

Permalink
fix: Report rendering for Nautobot 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
abates committed Mar 26, 2024
1 parent 17ba2c7 commit a20c618
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
9 changes: 9 additions & 0 deletions development/docker-compose.test-designs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
version: "3.8"
services:
nautobot:
volumes:
- "../nautobot_design_builder/tests/designs:/opt/nautobot/jobs"
worker:
volumes:
- "../nautobot_design_builder/tests/designs:/opt/nautobot/jobs"
10 changes: 8 additions & 2 deletions nautobot_design_builder/design_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def __init__(self, *args, **kwargs):
# TODO: Remove this when we no longer support Nautobot 1.x
self.rendered = None
self.failed = False
self.report = None

super().__init__(*args, **kwargs)

Expand All @@ -67,10 +68,13 @@ def post_implementation(self, context: Context, environment: Environment):

def post_run(self):
"""Method that will run after the main Nautobot job has executed."""
# TODO: This is not supported in Nautobot 2 and the entire method
# should be removed once we no longer support Nautobot 1.
if self.rendered:
self.job_result.data["output"] = self.rendered

self.job_result.data["designs"] = self.designs
self.job_result.data["report"] = self.report

def render(self, context: Context, filename: str) -> str:
"""High level function to render the Jinja design templates into YAML.
Expand Down Expand Up @@ -194,8 +198,10 @@ def run(self, **kwargs): # pylint: disable=arguments-differ,too-many-branches
if commit:
self.post_implementation(context, self.environment)
if hasattr(self.Meta, "report"):
self.job_result.data["report"] = self.render_report(context, self.environment.journal)
self.log_success(message=self.job_result.data["report"])
self.report = self.render_report(context, self.environment.journal)
self.log_success(message=self.report)
if nautobot_version >= "2.0":
self.save_design_file("report.md", self.report)
else:
transaction.savepoint_rollback(sid)
self.log_info(
Expand Down
21 changes: 18 additions & 3 deletions nautobot_design_builder/tests/designs/test_designs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from nautobot_design_builder.design_job import DesignJob
from nautobot_design_builder.ext import Extension
from nautobot_design_builder.util import nautobot_version


class SimpleDesign(DesignJob):
Expand All @@ -16,7 +17,7 @@ class SimpleDesignReport(DesignJob):
"""Simple design job that includes a post-implementation report."""

class Meta: # pylint: disable=too-few-public-methods
name = "Simple Design"
name = "Simple Design with Report"
design_file = "templates/simple_design.yaml.j2"
report = "templates/simple_report.md.j2"

Expand All @@ -25,7 +26,7 @@ class MultiDesignJob(DesignJob):
"""Design job that is implemented from multiple design files."""

class Meta: # pylint: disable=too-few-public-methods
name = "Simple Design"
name = "Multi File Design"
design_files = [
"templates/simple_design.yaml.j2",
"templates/simple_design_2.yaml.j2",
Expand All @@ -36,7 +37,7 @@ class MultiDesignJobWithError(DesignJob):
"""Design job that includes an error (for unit testing)."""

class Meta: # pylint: disable=too-few-public-methods
name = "Simple Design"
name = "Multi File Design with Error"
design_files = [
"templates/simple_design.yaml.j2",
"templates/simple_design.yaml.j2",
Expand Down Expand Up @@ -72,3 +73,17 @@ class DesignWithValidationError(DesignJob):
class Meta: # pylint: disable=too-few-public-methods
name = "Design with validation errors"
design_file = "templates/design_with_validation_error.yaml.j2"


if nautobot_version >= "2.0":
from nautobot.apps.jobs import register_jobs

register_jobs(
SimpleDesign,
SimpleDesignReport,
MultiDesignJob,
MultiDesignJobWithError,
DesignJobWithExtensions,
DesignWithRefError,
DesignWithValidationError,
)
5 changes: 4 additions & 1 deletion nautobot_design_builder/tests/test_design_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ def test_simple_design_report(self):
job = self.get_mocked_job(test_designs.SimpleDesignReport)
job.run(data={}, commit=True)
self.assertJobSuccess(job)
self.assertEqual("Report output", job.job_result.data["report"]) # pylint: disable=unsubscriptable-object
if nautobot_version >= "2.0":
self.assertEqual("Report output", job.report) # pylint: disable=unsubscriptable-object
else:
self.assertEqual("Report output", job.job_result.data["report"]) # pylint: disable=unsubscriptable-object

def test_multiple_design_files(self):
job = self.get_mocked_job(test_designs.MultiDesignJob)
Expand Down

0 comments on commit a20c618

Please sign in to comment.