|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +import json |
| 3 | +import logging |
| 4 | + |
| 5 | +import saltext.vmware.modules.storage_policies as policy_module |
| 6 | +import saltext.vmware.utils.common as utils_common |
| 7 | +import saltext.vmware.utils.connect as connect |
| 8 | +import saltext.vmware.utils.drift as drift |
| 9 | + |
| 10 | +log = logging.getLogger(__name__) |
| 11 | + |
| 12 | +try: |
| 13 | + from pyVmomi import vim |
| 14 | + |
| 15 | + HAS_PYVMOMI = True |
| 16 | +except ImportError: |
| 17 | + HAS_PYVMOMI = False |
| 18 | + |
| 19 | + |
| 20 | +__virtualname__ = "storage_policy" |
| 21 | +__proxyenabled__ = ["storage_policy"] |
| 22 | + |
| 23 | + |
| 24 | +def __virtual__(): |
| 25 | + if not HAS_PYVMOMI: |
| 26 | + return False, "Unable to import pyVmomi module." |
| 27 | + return __virtualname__ |
| 28 | + |
| 29 | + |
| 30 | +def config(name, config, service_instance=None, profile=None): |
| 31 | + """ |
| 32 | + Get/Set storage policies configuration based on drift report. |
| 33 | +
|
| 34 | + name |
| 35 | + Name of configuration. (required). |
| 36 | +
|
| 37 | + config |
| 38 | + List of objects with configuration values. (required). |
| 39 | +
|
| 40 | + .. code-block:: yaml |
| 41 | +
|
| 42 | + storage_policies_drift: |
| 43 | + storage_policy.config: |
| 44 | + - profile: vcenter |
| 45 | + - config: |
| 46 | + - policyName: Performance - Thick |
| 47 | + constraints: |
| 48 | + - name: VSAN |
| 49 | + replicaPreference: 1 failures - RAID-1 (Mirroring) |
| 50 | + hostFailuresToTolerate: 2 |
| 51 | + checksumDisabled: true |
| 52 | + stripeWidth: 2 |
| 53 | + proportionalCapacity: 50 |
| 54 | + cacheReservation: 0 |
| 55 | + - policyName: New VM Storage Policy |
| 56 | + constraints: |
| 57 | + - name: VSAN |
| 58 | + hostFailuresToTolerate: 2 |
| 59 | + replicaPreference: 2 failures - RAID-1 (Mirroring) |
| 60 | + proportionalCapacity: 1 |
| 61 | +
|
| 62 | + service_instance |
| 63 | + Use this vCenter service connection instance instead of creating a new one. (optional). |
| 64 | +
|
| 65 | + profile |
| 66 | + Profile to use (optional) |
| 67 | + """ |
| 68 | + |
| 69 | + service_instance = service_instance or connect.get_service_instance( |
| 70 | + config=__opts__, profile=profile |
| 71 | + ) |
| 72 | + ret = {"name": name, "changes": {}, "result": None, "comment": ""} |
| 73 | + |
| 74 | + # Clone config input to a Map. |
| 75 | + # Can be used to transform input to internal objects and do validation if needed |
| 76 | + new_config = {} |
| 77 | + for policy_config in config: |
| 78 | + # Create full representation of the object, default or empty values |
| 79 | + new_policy_config = {"constraints": {}} |
| 80 | + # Transform / Validate input vs object, e.g. constraints section |
| 81 | + if "constraints" in policy_config: |
| 82 | + for constraint in policy_config["constraints"]: |
| 83 | + subProfileName = constraint["name"] |
| 84 | + new_policy_config["constraints"][subProfileName] = {} |
| 85 | + for key in constraint.keys(): |
| 86 | + if key != "name": |
| 87 | + new_policy_config["constraints"][subProfileName][key] = constraint[key] |
| 88 | + new_config[policy_config["policyName"]] = new_policy_config |
| 89 | + |
| 90 | + log.debug("---------------NEW--------------") |
| 91 | + log.debug(json.dumps(new_config, indent=2)) |
| 92 | + log.debug("---------------NEW--------------") |
| 93 | + |
| 94 | + # Get all policies from vCenter, the objects are VMOMI objects |
| 95 | + old_configs = policy_module.find( |
| 96 | + policy_name=None, service_instance=service_instance, profile=profile |
| 97 | + ) |
| 98 | + |
| 99 | + # make JSON representation of current policies |
| 100 | + # old_configs holds only the rules that are in the scope of interest (provided in argument config_input) |
| 101 | + old_config = {} |
| 102 | + for policy in old_configs: |
| 103 | + if policy["policyName"] not in new_config.keys(): |
| 104 | + continue |
| 105 | + |
| 106 | + policy_json = {"constraints": {}} |
| 107 | + |
| 108 | + for constraint in policy["constraints"]: |
| 109 | + subProfileName = constraint["name"] |
| 110 | + policy_json["constraints"][subProfileName] = {} |
| 111 | + for key in constraint.keys(): |
| 112 | + if key != "name": |
| 113 | + policy_json["constraints"][subProfileName][key] = constraint[key] |
| 114 | + |
| 115 | + old_config[policy["policyName"]] = policy_json |
| 116 | + |
| 117 | + log.debug("--------------OLD---------------") |
| 118 | + log.debug(json.dumps(old_config, indent=2)) |
| 119 | + log.debug("--------------OLD---------------") |
| 120 | + |
| 121 | + # Find rules changes |
| 122 | + changes = [] |
| 123 | + rule_diff = drift.drift_report(old_config, new_config, diff_level=0) |
| 124 | + policies_diff = json.loads(json.dumps(rule_diff)) # clone object |
| 125 | + if policies_diff is not None: |
| 126 | + ret["changes"] = policies_diff |
| 127 | + |
| 128 | + log.debug("==============DRIFT===============") |
| 129 | + log.debug(json.dumps(rule_diff, indent=2)) |
| 130 | + log.debug("==============DRIFT===============") |
| 131 | + |
| 132 | + # add changes for process if not dry-run |
| 133 | + for policy_name in policies_diff: |
| 134 | + new_policy = policies_diff[policy_name]["new"] |
| 135 | + changes.append({**{"name": policy_name}, **new_policy}) |
| 136 | + |
| 137 | + # If it's not dry-run and has changes, then apply changes |
| 138 | + if not __opts__["test"] and changes: |
| 139 | + success = True |
| 140 | + |
| 141 | + log.debug("===============CHANGES==============") |
| 142 | + log.debug(json.dumps(changes, indent=2)) |
| 143 | + log.debug("===============CHANGES==============") |
| 144 | + |
| 145 | + comments = {} |
| 146 | + for change in changes: |
| 147 | + try: |
| 148 | + # convert change report to configuration object |
| 149 | + new_constraints = [] |
| 150 | + for constr_name in change["constraints"].keys(): |
| 151 | + constr_props = change["constraints"][constr_name].copy() |
| 152 | + constr_props["name"] = constr_name |
| 153 | + new_constraints.append(constr_props) |
| 154 | + update = {"policyName": change["name"], "constraints": new_constraints} |
| 155 | + # save policy |
| 156 | + policy_module.save(update, service_instance, profile) |
| 157 | + comments[change["name"]] = { |
| 158 | + "status": "SUCCESS", |
| 159 | + "message": f"Storage policy '{change['name']}' has been changed successfully.", |
| 160 | + } |
| 161 | + except Exception as err: |
| 162 | + success = False |
| 163 | + comments[change["name"]] = { |
| 164 | + "status": "FAILURE", |
| 165 | + "message": f"Error occured while save storage policy '{change['name']}': {err}", |
| 166 | + } |
| 167 | + |
| 168 | + ret["comment"] = comments # it's more readable if passed as object |
| 169 | + ret["result"] = success # at least one success |
| 170 | + |
| 171 | + return ret |
0 commit comments