Skip to content

Commit

Permalink
Merge pull request #623 from nautobot/develop
Browse files Browse the repository at this point in the history
Sync 1.6.2 with main
  • Loading branch information
itdependsnetworks authored Sep 28, 2023
2 parents 680baa8 + 12a7885 commit 60a92dc
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 11 deletions.
6 changes: 6 additions & 0 deletions docs/admin/release_notes/version_1.6.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
- Add functionality to compliance result to provide a Remediation plan.
- Supports Nautobot >=1.6.1,<2.0.0.

## v1.6.2 - 2023-09

### Fixed

- [#621](https://github.com/nautobot/nautobot-plugin-golden-config/pull/621) - Moved jinja to be locally scoped, this was causing issues with Jinja filters based on import order.

## v1.6.1 - 2023-09

### Changed
Expand Down
16 changes: 6 additions & 10 deletions nautobot_golden_config/nornir_plays/config_intended.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import os
from datetime import datetime

from django.template import engines
from jinja2.sandbox import SandboxedEnvironment
from nautobot_plugin_nornir.constants import NORNIR_SETTINGS
from nautobot_plugin_nornir.plugins.inventory.nautobot_orm import NautobotORMInventory
from nautobot_plugin_nornir.utils import get_dispatcher
Expand All @@ -18,10 +16,10 @@

from nautobot_golden_config.models import GoldenConfig
from nautobot_golden_config.nornir_plays.processor import ProcessGoldenConfig
from nautobot_golden_config.utilities.constant import JINJA_ENV
from nautobot_golden_config.utilities.db_management import close_threaded_db_connections
from nautobot_golden_config.utilities.graphql import graph_ql_query
from nautobot_golden_config.utilities.helper import (
get_django_env,
get_device_to_settings_map,
get_job_filter,
render_jinja_template,
Expand All @@ -31,16 +29,10 @@
InventoryPluginRegister.register("nautobot-inventory", NautobotORMInventory)
LOGGER = logging.getLogger(__name__)

# Use a custom Jinja2 environment instead of Django's to avoid HTML escaping
jinja_env = SandboxedEnvironment(**JINJA_ENV)

# Retrieve filters from the Django jinja template engine
jinja_env.filters = engines["jinja"].env.filters


@close_threaded_db_connections
def run_template( # pylint: disable=too-many-arguments
task: Task, logger, device_to_settings_map, nautobot_job
task: Task, logger, device_to_settings_map, nautobot_job, jinja_env
) -> Result:
"""Render Jinja Template.
Expand Down Expand Up @@ -120,6 +112,9 @@ def config_intended(nautobot_job, data):
for settings in set(device_to_settings_map.values()):
verify_settings(logger, settings, ["jinja_path_template", "intended_path_template", "sot_agg_query"])

# Retrieve filters from the Django jinja template engine
jinja_env = get_django_env()

try:
with InitNornir(
runner=NORNIR_SETTINGS.get("runner"),
Expand All @@ -144,6 +139,7 @@ def config_intended(nautobot_job, data):
logger=logger,
device_to_settings_map=device_to_settings_map,
nautobot_job=nautobot_job,
jinja_env=jinja_env,
)

except Exception as err:
Expand Down
15 changes: 15 additions & 0 deletions nautobot_golden_config/utilities/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,21 @@
# pylint: disable=raise-missing-from
import json

from django.template import engines
from django.contrib import messages
from django.db.models import Q
from django.utils.html import format_html
from django.urls import reverse

from jinja2 import exceptions as jinja_errors
from jinja2.sandbox import SandboxedEnvironment
from nautobot.dcim.filters import DeviceFilterSet
from nautobot.dcim.models import Device
from nautobot.utilities.utils import render_jinja2
from nornir_nautobot.exceptions import NornirNautobotException

from nautobot_golden_config import models
from nautobot_golden_config.utilities.constant import JINJA_ENV

FIELDS_PK = {
"platform",
Expand Down Expand Up @@ -96,6 +99,18 @@ def verify_settings(logger, global_settings, attrs):
raise NornirNautobotException()


def get_django_env():
"""Load Django Jinja filters from the Django jinja template engine, and add them to the jinja_env.
Returns:
SandboxedEnvironment
"""
# Use a custom Jinja2 environment instead of Django's to avoid HTML escaping
jinja_env = SandboxedEnvironment(**JINJA_ENV)
jinja_env.filters = engines["jinja"].env.filters
return jinja_env


def render_jinja_template(obj, logger, template):
"""
Helper function to render Jinja templates.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "nautobot-golden-config"
version = "1.6.1"
version = "1.6.2"
description = "A plugin for configuration on nautobot"
authors = ["Network to Code, LLC", "<[email protected]>"]

Expand Down

0 comments on commit 60a92dc

Please sign in to comment.