|
| 1 | +# SPDX-License: Apache-2.0 |
| 2 | +import logging |
| 3 | + |
| 4 | +import salt.exceptions |
| 5 | +import saltext.vmware.utils.compliance_control as compliance_control_util |
| 6 | +from config_modules_vmware.interfaces.controller_interface import ControllerInterface |
| 7 | + |
| 8 | +log = logging.getLogger(__name__) |
| 9 | + |
| 10 | +__virtualname__ = "vmware_compliance_control" |
| 11 | + |
| 12 | + |
| 13 | +def __virtual__(): |
| 14 | + return __virtualname__ |
| 15 | + |
| 16 | + |
| 17 | +def control_config_compliance_check(control_config, product, auth_context=None): |
| 18 | + """ |
| 19 | + Checks compliance of control config. Control config can be ntp, dns, syslog, etc. |
| 20 | + Returns control compliance response object. |
| 21 | +
|
| 22 | + control_config |
| 23 | + control config dict object. |
| 24 | + product |
| 25 | + appliance name - vcenter, sddc-manager, etc. |
| 26 | + auth_context |
| 27 | + optional auth context to access product. |
| 28 | + """ |
| 29 | + |
| 30 | + log.info("Checking compliance %s", control_config) |
| 31 | + if not auth_context: |
| 32 | + config = __opts__ |
| 33 | + auth_context = compliance_control_util.create_auth_context(config=config, product=product) |
| 34 | + |
| 35 | + try: |
| 36 | + controller_interface_obj = ControllerInterface(auth_context) |
| 37 | + response_check_compliance = controller_interface_obj.check_compliance( |
| 38 | + desired_state_spec=control_config |
| 39 | + ) |
| 40 | + log.debug("Response for compliance check %s", response_check_compliance) |
| 41 | + return response_check_compliance |
| 42 | + except Exception as exc: |
| 43 | + log.error("Compliance check encountered an error: %s", str(exc)) |
| 44 | + raise salt.exceptions.VMwareRuntimeError(str(exc)) |
| 45 | + |
| 46 | + |
| 47 | +def control_config_remediate(control_config, product, auth_context=None): |
| 48 | + """ |
| 49 | + Remediate given compliance control config. Control config can be ntp, dns, syslog, etc. |
| 50 | + Returns remediation response object. |
| 51 | +
|
| 52 | + control_config |
| 53 | + control config dict object. |
| 54 | + product |
| 55 | + appliance name. vcenter, sddc-manager, etc. |
| 56 | + auth_context |
| 57 | + Optional auth context to access product. |
| 58 | + """ |
| 59 | + |
| 60 | + log.info("Remediation : %s", control_config) |
| 61 | + |
| 62 | + if not auth_context: |
| 63 | + config = __opts__ |
| 64 | + auth_context = compliance_control_util.create_auth_context(config=config, product=product) |
| 65 | + |
| 66 | + try: |
| 67 | + controller_interface_obj = ControllerInterface(auth_context) |
| 68 | + response_remediate = controller_interface_obj.remediate_with_desired_state( |
| 69 | + desired_state_spec=control_config |
| 70 | + ) |
| 71 | + log.debug("Remediation response %s", response_remediate) |
| 72 | + return response_remediate |
| 73 | + |
| 74 | + except Exception as exc: |
| 75 | + # Handle exceptions by setting status as false and including exception details |
| 76 | + log.error("Remediation encountered an error: %s", str(exc)) |
| 77 | + raise salt.exceptions.VMwareRuntimeError(str(exc)) |
0 commit comments