-
Notifications
You must be signed in to change notification settings - Fork 66
Dynamic Remediation Functions and Mapping to override default hier_config #770
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
d954e5b
3a0be06
f869e8e
97b3b7b
fa9b10e
9b6b30c
7ebd084
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,8 @@ | |
|
|
||
| import json | ||
| import logging | ||
| import pkgutil | ||
| import sys | ||
|
|
||
| from deepdiff import DeepDiff | ||
| from django.core.exceptions import ValidationError | ||
|
|
@@ -11,7 +13,7 @@ | |
| from nautobot.core.models.generics import PrimaryModel | ||
| from nautobot.core.models.utils import serialize_object, serialize_object_v2 | ||
| from nautobot.dcim.models import Device | ||
| from nautobot.extras.models import ObjectChange | ||
| from nautobot.extras.models import ObjectChange, GitRepository | ||
| from nautobot.extras.models.statuses import StatusField | ||
| from nautobot.extras.utils import extras_features | ||
| from netutils.config.compliance import feature_compliance | ||
|
|
@@ -198,7 +200,19 @@ def _get_hierconfig_remediation(obj): | |
|
|
||
| host.load_generated_config(obj.intended) | ||
| host.load_running_config(obj.actual) | ||
| host.remediation_config() | ||
| rem = host.remediation_config() | ||
|
|
||
| if remediation_setting_obj.remediation_type == RemediationTypeChoice.TYPE_DYNAMIC_HIERCONFIG: | ||
| repos = GitRepository.objects.filter(provided_contents__contains="nautobot_golden_config.hierconfigdynamicremediations") | ||
| for repo in repos: | ||
| for importer, discovered_module_name, _ in pkgutil.iter_modules( | ||
| [f"{repo.filesystem_path}/hier_config_dynamic_remediations"] | ||
| ): | ||
| if "__init__" in discovered_module_name: | ||
| continue | ||
| module = importer.find_module(discovered_module_name).load_module(discovered_module_name) | ||
| module.remediation(rem) | ||
|
|
||
| remediation_config = host.remediation_config_filtered_text(include_tags={}, exclude_tags={}) | ||
|
|
||
| return remediation_config | ||
|
|
@@ -210,6 +224,7 @@ def _get_hierconfig_remediation(obj): | |
| ComplianceRuleConfigTypeChoice.TYPE_JSON: _get_json_compliance, | ||
| ComplianceRuleConfigTypeChoice.TYPE_XML: _get_xml_compliance, | ||
| RemediationTypeChoice.TYPE_HIERCONFIG: _get_hierconfig_remediation, | ||
| RemediationTypeChoice.TYPE_DYNAMIC_HIERCONFIG: _get_hierconfig_remediation, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not tracking to me, we are pointing it to the same function of
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i added that as an option so that if your environment had dynamic functions loaded via git and for a particular remediation setting you didn't need or want to use any dynamic functions at all, it bypassed that loop over the dynamic functions entirely |
||
| } | ||
| # The below conditionally add the custom provided compliance type | ||
| for custom_function, custom_type in CUSTOM_FUNCTIONS.items(): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't looked at the code, but this part makes sense, I think that this is mostly what needs to be updated, more specifically there shouldn't be a reason to create a new model here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it should be possible to have a similar solution :
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
made some changes to remove the model and just load modules from the git repo when doing remediation; lmk what you think