Skip to content

Commit

Permalink
Resovles #257 (#258)
Browse files Browse the repository at this point in the history
* Resovles #257
  • Loading branch information
whitej6 authored Jun 15, 2022
1 parent f4cea3c commit 5060615
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 36 deletions.
7 changes: 6 additions & 1 deletion nautobot_golden_config/template_content.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
"""Added content to the device model view for config compliance."""
from django.db.models import Count, Q
from nautobot.dcim.models import Device
from nautobot.extras.plugins import PluginTemplateExtension

from nautobot_golden_config.models import ConfigCompliance, GoldenConfig
from nautobot_golden_config.utilities.constant import ENABLE_COMPLIANCE, CONFIG_FEATURES
from nautobot_golden_config.utilities.helper import get_device_to_settings_map


class ConfigComplianceDeviceCheck(PluginTemplateExtension): # pylint: disable=abstract-method
Expand Down Expand Up @@ -69,12 +71,15 @@ def get_device(self):

def right_page(self):
"""Content to add to the configuration compliance."""
golden_config = GoldenConfig.objects.filter(device=self.get_device()).first()
device = self.get_device()
golden_config = GoldenConfig.objects.filter(device=device).first()
settings = get_device_to_settings_map(queryset=Device.objects.filter(id=device.id))
extra_context = {
"device": self.get_device(), # device,
"golden_config": golden_config,
"template_type": "device-configs",
"config_features": CONFIG_FEATURES,
"matched_config_setting": settings.get(device.id, False),
}
return self.render(
"nautobot_golden_config/content_template.html",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,40 +64,42 @@
<th>Config</th>
</tr>
</thead>
<tbody>
{% if config_features.compliance and golden_config.compliance_config %}
<tr>
<td>Compliance</td>
<td>
<a href="{% url 'plugins:nautobot_golden_config:configcompliance_details' pk=device.pk config_type='compliance' %}"><i class="mdi mdi-file-compare"></i></a>
</td>
</tr>
{% if matched_config_setting %}
<tbody>
{% if config_features.compliance and golden_config.compliance_config %}
<tr>
<td>Compliance</td>
<td>
<a href="{% url 'plugins:nautobot_golden_config:configcompliance_details' pk=device.pk config_type='compliance' %}"><i class="mdi mdi-file-compare"></i></a>
</td>
</tr>
{% endif %}
{% if config_features.intended and golden_config.intended_config %}
<tr>
<td>Intended</td>
<td>
<a href="{% url 'plugins:nautobot_golden_config:configcompliance_details' pk=device.pk config_type='intended' %}"><i class="mdi mdi-text-box-check-outline"></i></a>
</td>
</tr>
{% endif %}
{% if config_features.backup and golden_config.backup_config %}
<tr>
<td>Actual</td>
<td>
<a href="{% url 'plugins:nautobot_golden_config:configcompliance_details' pk=device.pk config_type='backup' %}"><i class="mdi mdi-file-document-outline"></i></a>
</td>
</tr>
{% endif %}
{% if config_features.sotagg %}
<tr>
<td>SoT Aggregation Data</td>
<td>
<a href="{% url 'plugins:nautobot_golden_config:configcompliance_details' pk=device.pk config_type='sotagg' %}"><i class="mdi mdi-code-json"></i></a>
</td>
</tr>
{% endif %}
</tbody>
{% endif %}
{% if config_features.intended and golden_config.intended_config %}
<tr>
<td>Intended</td>
<td>
<a href="{% url 'plugins:nautobot_golden_config:configcompliance_details' pk=device.pk config_type='intended' %}"><i class="mdi mdi-text-box-check-outline"></i></a>
</td>
</tr>
{% endif %}
{% if config_features.backup and golden_config.backup_config %}
<tr>
<td>Actual</td>
<td>
<a href="{% url 'plugins:nautobot_golden_config:configcompliance_details' pk=device.pk config_type='backup' %}"><i class="mdi mdi-file-document-outline"></i></a>
</td>
</tr>
{% endif %}
{% if config_features.sotagg %}
<tr>
<td>SoT Aggregation Data</td>
<td>
<a href="{% url 'plugins:nautobot_golden_config:configcompliance_details' pk=device.pk config_type='sotagg' %}"><i class="mdi mdi-code-json"></i></a>
</td>
</tr>
{% endif %}
</tbody>
</table>
</div>
{% endif %}
Expand Down
8 changes: 6 additions & 2 deletions nautobot_golden_config/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import numpy as np
import yaml
from django.contrib import messages
from django.core.exceptions import ObjectDoesNotExist
from django.db.models import Count, ExpressionWrapper, F, FloatField, Max, ProtectedError, Q
from django.forms import ModelMultipleChoiceField, MultipleHiddenInput
from django.shortcuts import redirect, render
Expand Down Expand Up @@ -393,8 +394,11 @@ def diff_structured_data(backup_data, intended_data):
if request.GET.get("format") in ["json", "yaml"]:
structure_format = request.GET.get("format")

settings = get_device_to_settings_map(queryset=Device.objects.filter(pk=device.pk))[device.id]
_, output = graph_ql_query(request, device, settings.sot_agg_query.query)
settings = get_device_to_settings_map(queryset=Device.objects.filter(pk=device.pk))
if device.id in settings:
_, output = graph_ql_query(request, device, settings[device.id].sot_agg_query.query)
else:
raise ObjectDoesNotExist(f"{device.name} does not map to a Golden Config Setting.")

if structure_format == "yaml":
output = yaml.dump(json.loads(json.dumps(output)), default_flow_style=False)
Expand Down

0 comments on commit 5060615

Please sign in to comment.