-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathtables.py
More file actions
566 lines (460 loc) · 20.7 KB
/
Copy pathtables.py
File metadata and controls
566 lines (460 loc) · 20.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
"""Django Tables2 classes for golden_config app."""
from django.utils.html import format_html
from django_tables2 import Column, LinkColumn, TemplateColumn
from django_tables2.utils import A
from nautobot.apps.tables import BaseTable, BooleanColumn, TagColumn, ToggleColumn
from nautobot.extras.tables import StatusTableMixin
from nautobot_golden_config import models
from nautobot_golden_config.utilities.constant import CONFIG_FEATURES, ENABLE_BACKUP, ENABLE_COMPLIANCE, ENABLE_INTENDED
ALL_ACTIONS = """
{% if backup == True %}
{% if record.config_type == 'json' %}
<i class="mdi mdi-circle-small"></i>
{% else %}
{% if record.backup_config %}
<a value="{% url 'plugins:nautobot_golden_config:goldenconfig_backup' pk=record.device.pk %}" class="openBtn" data-href="{% url 'plugins:nautobot_golden_config:goldenconfig_backup' pk=record.device.pk %}?modal=true">
<i class="mdi mdi-file-document-outline" title="Backup Configuration"></i>
</a>
{% else %}
<i class="mdi mdi-circle-small"></i>
{% endif %}
{% endif %}
{% endif %}
{% if intended == True %}
{% if record.config_type == 'json' %}
<i class="mdi mdi-circle-small"></i>
{% else %}
{% if record.intended_config %}
<a value="{% url 'plugins:nautobot_golden_config:goldenconfig_intended' pk=record.device.pk %}" class="openBtn" data-href="{% url 'plugins:nautobot_golden_config:goldenconfig_intended' pk=record.device.pk %}?modal=true">
<i class="mdi mdi-text-box-check-outline" title="Intended Configuration"></i>
</a>
{% else %}
<i class="mdi mdi-circle-small"></i>
{% endif %}
{% endif %}
{% endif %}
{% if postprocessing == True %}
{% if record.intended_config %}
<a value="{% url 'plugins:nautobot_golden_config:goldenconfig_postprocessing' pk=record.device.pk %}" class="openBtn" data-href="{% url 'plugins:nautobot_golden_config:goldenconfig_postprocessing' pk=record.device.pk %}?modal=true">
<i class="mdi mdi-text-box-check" title="Configuration after Postprocessing"></i>
</a>
{% else %}
<i class="mdi mdi-circle-small"></i>
{% endif %}
{% endif %}
{% if compliance == True %}
{% if record.intended_config and record.backup_config %}
<a value="{% url 'plugins:nautobot_golden_config:goldenconfig_compliance' pk=record.device.pk %}" class="openBtn" data-href="{% url 'plugins:nautobot_golden_config:goldenconfig_compliance' pk=record.device.pk %}?modal=true">
<i class="mdi mdi-file-compare" title="Compliance Details"></i>
</a>
{% else %}
<i class="mdi mdi-circle-small"></i>
{% endif %}
{% endif %}
{% if sotagg == True %}
<a value="{% url 'plugins:nautobot_golden_config:goldenconfig_sotagg' pk=record.device.pk %}" class="openBtn" data-href="{% url 'plugins:nautobot_golden_config:goldenconfig_sotagg' pk=record.device.pk %}?modal=true">
<i class="mdi mdi-code-json" title="SOT Aggregate Data"></i>
</a>
{% if record.config_type == 'json' %}
<i class="mdi mdi-circle-small"></i>
{% else %}
<a href="{% url 'extras:job_run_by_class_path' class_path='nautobot_golden_config.jobs.AllGoldenConfig' %}?device={{ record.device.pk }}">
<span class="text-primary">
<i class="mdi mdi-play-circle" title="Execute All Golden Config Jobs"></i>
</span>
</a>
{% endif %}
{% endif %}
"""
CONFIG_SET_BUTTON = """
<a href="#" class="openBtn" data-toggle="modal" data-target="#codeModal-{{ record.pk }}">
<i class="mdi mdi-file-document-outline"></i>
</a>
<div class="modal" id="codeModal-{{ record.pk }}">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<!-- Modal Header -->
<div class="modal-header">
<h3 class="modal-title">Config Set - {{ record.device }}</h3>
<button type="button" class="close" data-dismiss="modal">×</button>
</div>
<!-- Modal body -->
<div class="modal-body">
<table class="table table-hover panel-body attr-table table-responsive table-wrapper">
<tr>
<td>Config Set</td>
<td>
<span id="config_set_{{ record.pk }}"><pre>{{ record.config_set }}</pre></span>
<span class="config_hover_button">
<button type="button" class="btn btn-inline btn-default hover_copy_button" data-clipboard-action='copy' data-clipboard-target="#config_set_{{ record.pk }}">
<span class="mdi mdi-content-copy"></span>
</button>
</span>
</td>
</tr>
<tr>
<td>Postprocessed Config Set</td>
<td>
<a href="{% url 'plugins:nautobot_golden_config:goldenconfig_postprocessing' pk=record.device.id %}?config_plan_id={{ record.id }}" target="_blank">
<i class="mdi mdi-text-box-check" title="Config Plan after Postprocessing"></i>
</a>
</td>
</tr>
</table>
</div>
<!-- Modal footer -->
<div class="modal-footer">
<button id="close" type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>"""
MATCH_CONFIG = """{{ record.match_config|linebreaksbr }}"""
def actual_fields():
"""Convienance function to conditionally toggle columns."""
active_fields = ["pk", "name"]
if ENABLE_BACKUP:
active_fields.append("backup_last_success_date")
if ENABLE_INTENDED:
active_fields.append("intended_last_success_date")
if ENABLE_COMPLIANCE:
active_fields.append("compliance_last_success_date")
active_fields.append("actions")
return tuple(active_fields)
#
# Columns
#
class PercentageColumn(Column):
"""Column used to display percentage."""
def render(self, value):
"""Render percentage value."""
return f"{value} %"
class ComplianceColumn(Column):
"""Column used to display config compliance status (True/False/None)."""
def render(self, value):
"""Render an entry in this column."""
if value == 1: # pylint: disable=no-else-return
return format_html('<span class="text-success"><i class="mdi mdi-check-bold"></i></span>')
elif value == 0:
return format_html('<span class="text-danger"><i class="mdi mdi-close-thick"></i></span>')
else: # value is None
return format_html('<span class="mdi mdi-minus"></span>')
#
# Tables
#
# ConfigCompliance
class ConfigComplianceTable(BaseTable):
"""Table for rendering a listing of Device entries and their associated ConfigCompliance record status."""
pk = ToggleColumn(accessor=A("device"))
device = TemplateColumn(
template_code="""<a href="{% url 'plugins:nautobot_golden_config:configcompliance_devicetab' pk=record.device %}?tab=nautobot_golden_config:1"><strong>{{ record.device__name }}</strong></a> """
)
def __init__(self, *args, **kwargs):
"""Override default values to dynamically add columns."""
# Used ConfigCompliance.objects on purpose, vs queryset (set in args[0]), as there were issues with that as
# well as not as expected from user standpoint (e.g. not always the same values on columns depending on
# filtering)
features = list(
models.ConfigCompliance.objects.order_by("rule__feature__slug")
.values_list("rule__feature__slug", flat=True)
.distinct()
)
# Nautobot's BaseTable.configurable_columns() only recognizes columns in self.base_columns,
# so override the class's base_columns to include our additional columns as configurable.
# Note: The correct way to modify django_tables2 columns at init is to use the extra_columns kwarg but Nautobot doesn't support that.
for feature in features:
self.base_columns[feature] = ComplianceColumn(verbose_name=feature) # pylint: disable=no-member
compliance_columns = [
column_name
for column_name, column in self.base_columns.items() # pylint: disable=no-member
if isinstance(column, ComplianceColumn)
]
removed_features = set(compliance_columns) - set(features)
for column_name in removed_features:
del self.base_columns[column_name] # pylint: disable=no-member
super().__init__(*args, **kwargs)
class Meta(BaseTable.Meta):
"""Metaclass attributes of ConfigComplianceTable."""
model = models.ConfigCompliance
fields = (
"pk",
"device",
)
# All other fields (ConfigCompliance names) are constructed dynamically at instantiation time - see views.py
class ConfigComplianceGlobalFeatureTable(BaseTable): # pylint: disable=nb-sub-class-name
"""Table for feature compliance report."""
name = Column(accessor="rule__feature__slug", verbose_name="Feature")
count = Column(accessor="count", verbose_name="Total")
compliant = Column(accessor="compliant", verbose_name="Compliant")
non_compliant = Column(accessor="non_compliant", verbose_name="Non-Compliant")
comp_percent = PercentageColumn(accessor="comp_percent", verbose_name="Compliance (%)")
class Meta(BaseTable.Meta):
"""Metaclass attributes of ConfigComplianceGlobalFeatureTable."""
model = models.ConfigCompliance
fields = ["name", "count", "compliant", "non_compliant", "comp_percent"]
default_columns = [
"name",
"count",
"compliant",
"non_compliant",
"comp_percent",
]
class ConfigComplianceDeleteTable(BaseTable): # pylint: disable=nb-sub-class-name
"""Table for device compliance report."""
feature = Column(accessor="rule__feature__name", verbose_name="Feature")
class Meta(BaseTable.Meta):
"""Metaclass attributes of ConfigComplianceDeleteTable."""
device = Column(accessor="device__name", verbose_name="Device Name")
model = models.ConfigCompliance
fields = ("device", "feature")
class DeleteGoldenConfigTable(BaseTable): # pylint: disable=nb-sub-class-name
"""
Table used in bulk delete confirmation.
This is required since there model is different when deleting the record compared to when viewing the records initially via Device.
"""
pk = ToggleColumn()
def __init__(self, *args, **kwargs):
"""Remove all fields from showing except device ."""
super().__init__(*args, **kwargs)
for feature in list(self.base_columns.keys()): # pylint: disable=no-member
if feature not in ["pk", "device"]:
self.base_columns.pop(feature) # pylint: disable=no-member
self.sequence.remove(feature)
class Meta(BaseTable.Meta):
"""Meta for class DeleteGoldenConfigTable."""
model = models.GoldenConfig
# GoldenConfig
class GoldenConfigTable(BaseTable):
"""Table to display Config Management Status."""
pk = ToggleColumn()
name = LinkColumn(
"plugins:nautobot_golden_config:goldenconfig",
args=[A("pk")],
text=lambda record: record.device.name,
verbose_name="Device",
)
if ENABLE_BACKUP:
backup_last_success_date = Column(
verbose_name="Backup Status", empty_values=(), order_by="backup_last_success_date"
)
if ENABLE_INTENDED:
intended_last_success_date = Column(
verbose_name="Intended Status",
empty_values=(),
order_by="intended_last_success_date",
)
if ENABLE_COMPLIANCE:
compliance_last_success_date = Column(
verbose_name="Compliance Status",
empty_values=(),
order_by="compliance_last_success_date",
)
actions = TemplateColumn(
template_code=ALL_ACTIONS, verbose_name="Actions", extra_context=CONFIG_FEATURES, orderable=False
)
def _render_last_success_date(self, record, column, value):
"""Abstract method to get last success per row record."""
last_success_date = getattr(record, f"{value}_last_success_date", None)
last_attempt_date = getattr(record, f"{value}_last_attempt_date", None)
if not last_success_date or not last_attempt_date:
column.attrs = {"td": {"style": "color:black"}}
return "--"
if not last_success_date and not last_attempt_date:
column.attrs = {"td": {"style": "color:black"}}
return "--"
if last_success_date and last_attempt_date == last_success_date:
column.attrs = {"td": {"style": "color:green"}}
return last_success_date
column.attrs = {"td": {"style": "color:red"}}
return last_success_date
def render_backup_last_success_date(self, record, column):
"""Pull back backup last success per row record."""
return self._render_last_success_date(record, column, "backup")
def render_intended_last_success_date(self, record, column):
"""Pull back intended last success per row record."""
return self._render_last_success_date(record, column, "intended")
def render_compliance_last_success_date(self, record, column):
"""Pull back compliance last success per row record."""
return self._render_last_success_date(record, column, "compliance")
class Meta(BaseTable.Meta):
"""Meta for class GoldenConfigTable."""
model = models.GoldenConfig
fields = actual_fields()
# ComplianceFeature
class ComplianceFeatureTable(BaseTable):
"""Table to display Compliance Features."""
pk = ToggleColumn()
name = LinkColumn("plugins:nautobot_golden_config:compliancefeature", args=[A("pk")])
class Meta(BaseTable.Meta):
"""Table to display Compliance Features Meta Data."""
model = models.ComplianceFeature
fields = ("pk", "name", "slug", "description")
default_columns = ("pk", "name", "slug", "description")
# ComplianceRule
class ComplianceRuleTable(BaseTable):
"""Table to display Compliance Rules."""
pk = ToggleColumn()
feature = LinkColumn("plugins:nautobot_golden_config:compliancerule", args=[A("pk")])
match_config = TemplateColumn(template_code=MATCH_CONFIG)
config_ordered = BooleanColumn()
custom_compliance = BooleanColumn()
config_remediation = BooleanColumn()
tags = TagColumn(url_name="plugins:nautobot_golden_config:compliancerule_list")
class Meta(BaseTable.Meta):
"""Table to display Compliance Rules Meta Data."""
model = models.ComplianceRule
fields = (
"pk",
"feature",
"platform",
"description",
"config_ordered",
"match_config",
"config_type",
"custom_compliance",
"config_remediation",
"tags",
)
default_columns = (
"pk",
"feature",
"platform",
"description",
"config_ordered",
"match_config",
"config_type",
"custom_compliance",
"config_remediation",
)
# ConfigRemove
class ConfigRemoveTable(BaseTable):
"""Table to display Compliance Rules."""
pk = ToggleColumn()
name = LinkColumn("plugins:nautobot_golden_config:configremove", args=[A("pk")])
class Meta(BaseTable.Meta):
"""Table to display Compliance Rules Meta Data."""
model = models.ConfigRemove
fields = ("pk", "name", "platform", "description", "regex")
default_columns = ("pk", "name", "platform", "description", "regex")
# ConfigReplace
class ConfigReplaceTable(BaseTable):
"""Table to display Compliance Rules."""
pk = ToggleColumn()
name = LinkColumn("plugins:nautobot_golden_config:configreplace", args=[A("pk")])
class Meta(BaseTable.Meta):
"""Table to display Compliance Rules Meta Data."""
model = models.ConfigReplace
fields = ("pk", "name", "platform", "description", "regex", "replace")
default_columns = ("pk", "name", "platform", "description", "regex", "replace")
class GoldenConfigSettingTable(BaseTable):
# pylint: disable=R0903
"""Table for list view."""
pk = ToggleColumn()
name = Column(order_by=("_name",), linkify=True)
jinja_repository = Column(
verbose_name="Jinja Repository",
empty_values=(),
)
intended_repository = Column(
verbose_name="Intended Repository",
empty_values=(),
)
backup_repository = Column(
verbose_name="Backup Repository",
empty_values=(),
)
def _render_capability(self, record, column, record_attribute): # pylint: disable=unused-argument
if getattr(record, record_attribute, None):
return format_html('<span class="text-success"><i class="mdi mdi-check-bold"></i></span>')
return format_html('<span class="text-danger"><i class="mdi mdi-close-thick"></i></span>')
def render_backup_repository(self, record, column):
"""Render backup repository boolean value."""
return self._render_capability(record=record, column=column, record_attribute="backup_repository")
def render_intended_repository(self, record, column):
"""Render intended repository boolean value."""
return self._render_capability(record=record, column=column, record_attribute="intended_repository")
def render_jinja_repository(self, record, column):
"""Render jinja repository boolean value."""
return self._render_capability(record=record, column=column, record_attribute="jinja_repository")
class Meta(BaseTable.Meta):
"""Meta attributes."""
model = models.GoldenConfigSetting
fields = (
"pk",
"name",
"weight",
"description",
"backup_repository",
"intended_repository",
"jinja_repository",
)
class RemediationSettingTable(BaseTable):
"""Table to display RemediationSetting Rules."""
pk = ToggleColumn()
platform = LinkColumn("plugins:nautobot_golden_config:remediationsetting", args=[A("pk")])
class Meta(BaseTable.Meta):
"""Table to display RemediationSetting Meta Data."""
model = models.RemediationSetting
fields = ("pk", "platform", "remediation_type")
default_columns = ("pk", "platform", "remediation_type")
# ConfigPlan
class ConfigPlanTable(StatusTableMixin, BaseTable):
"""Table to display Config Plans."""
pk = ToggleColumn()
device = LinkColumn("plugins:nautobot_golden_config:configplan", args=[A("pk")])
plan_result = TemplateColumn(
template_code="""<a href="{% url 'extras:jobresult' pk=record.plan_result.pk %}"><i class="mdi mdi-clipboard-text-play-outline"></i></a>"""
)
deploy_result = TemplateColumn(
template_code="""
{% if record.deploy_result %}
<a href="{% url 'extras:jobresult' pk=record.deploy_result.pk %}"><i class="mdi mdi-clipboard-text-play-outline"></i></a>
{% else %}
—
{% endif %}
"""
)
change_control_url = TemplateColumn(
template_code=(
"{% if record.change_control_url %}"
'<a href="{{ record.change_control_url }}" target="_blank" title="{{ record.change_control_url }}">'
"{{ record.change_control_url|truncatechars:80 }}"
"</a>"
"{% else %}—{% endif %}"
),
verbose_name="Change Control URL",
orderable=True,
order_by="change_control_url",
)
config_set = TemplateColumn(template_code=CONFIG_SET_BUTTON, verbose_name="Config Set", orderable=False)
tags = TagColumn(url_name="plugins:nautobot_golden_config:configplan_list")
class Meta(BaseTable.Meta):
"""Table to display Config Plans Meta Data."""
model = models.ConfigPlan
fields = (
"pk",
"device",
"created",
"plan_type",
"feature",
"change_control_id",
"change_control_url",
"plan_result",
"deploy_result",
"config_set",
"status",
"tags",
)
default_columns = (
"pk",
"device",
"created",
"plan_type",
"feature",
"change_control_id",
"change_control_url",
"plan_result",
"deploy_result",
"config_set",
"status",
)