| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2025, NetApp Inc |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
"""NetApp StorageGRID - Manage EC profiles""" |
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
|
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
|
| 13 |
ANSIBLE_METADATA = { |
| 14 |
"metadata_version": "1.0", |
| 15 |
"status": ["preview"], |
| 16 |
"supported_by": "community", |
| 17 |
} |
| 18 |
|
| 19 |
DOCUMENTATION = """ |
| 20 |
module: na_sg_grid_ec_profile |
| 21 |
short_description: Manage EC profiles on StorageGRID. |
| 22 |
extends_documentation_fragment: |
| 23 |
- netapp.storagegrid.netapp.sg |
| 24 |
version_added: '21.14.0' |
| 25 |
author: Denis Magel (@dmagel-netapp) <[email protected]> |
| 26 |
description: |
| 27 |
- Interact with EC profiles on NetApp StorageGRID. |
| 28 |
options: |
| 29 |
name: |
| 30 |
description: |
| 31 |
- The EC Profile's name |
| 32 |
required: true |
| 33 |
type: str |
| 34 |
pool_id: |
| 35 |
description: |
| 36 |
- The Storage Pool ID of the selected scheme |
| 37 |
type: str |
| 38 |
scheme_id: |
| 39 |
description: |
| 40 |
- The selected scheme for the EC profile |
| 41 |
type: str |
| 42 |
state: |
| 43 |
description: |
| 44 |
- Whether the specified policy should be created or deactivated. |
| 45 |
- State "absent" only deactivates the EC profile. |
| 46 |
- Deactivated EC profiles cannot be activated |
| 47 |
required: false |
| 48 |
type: str |
| 49 |
choices: ['present', 'absent'] |
| 50 |
default: present |
| 51 |
validate_certs: |
| 52 |
description: |
| 53 |
- Should https certificates be validated? |
| 54 |
required: false |
| 55 |
type: bool |
| 56 |
default: true |
| 57 |
""" |
| 58 |
|
| 59 |
EXAMPLES = """ |
| 60 |
- name: Create EC profile |
| 61 |
na_sg_grid_ec_profile: |
| 62 |
api_url: "https://<storagegrid-endpoint-url>" |
| 63 |
auth_token: "storagegrid-auth-token" |
| 64 |
name: "profile1" |
| 65 |
pool_id: p10771105546308032398 |
| 66 |
scheme_id: "1" |
| 67 |
|
| 68 |
- name: Deactivate existing EC profile |
| 69 |
na_sg_grid_ec_profile: |
| 70 |
api_url: "https://<storagegrid-endpoint-url>" |
| 71 |
auth_token: "storagegrid-auth-token" |
| 72 |
name: "profile1" |
| 73 |
state: absent |
| 74 |
""" |
| 75 |
|
| 76 |
RETURN = """ |
| 77 |
resp: |
| 78 |
description: Returns information about the StorageGRID EC profile. |
| 79 |
returned: If state is 'present'. |
| 80 |
type: dict |
| 81 |
sample: { |
| 82 |
"id": "5", |
| 83 |
"name": "EC profile 123", |
| 84 |
"poolId": "p10771105546308032398", |
| 85 |
"schemeId": "4", |
| 86 |
"active": true |
| 87 |
} |
| 88 |
""" |
| 89 |
|
| 90 |
from datetime import datetime |
| 91 |
|
| 92 |
import ansible_collections.netapp.storagegrid.plugins.module_utils.netapp as netapp_utils |
| 93 |
from ansible.module_utils.basic import AnsibleModule |
| 94 |
from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp_module import NetAppModule |
| 95 |
from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp import SGRestAPI |
| 96 |
|
| 97 |
__LOGGING__ = [] |
| 98 |
|
| 99 |
|
| 100 |
class EC_profile(object): |
| 101 |
""" |
| 102 |
Create, modify and delete StorageGRID EC profile |
| 103 |
""" |
| 104 |
|
| 105 |
def __init__(self): |
| 106 |
""" |
| 107 |
Parse arguments, setup state variables, |
| 108 |
check parameters and ensure request module is installed |
| 109 |
""" |
| 110 |
self.argument_spec = netapp_utils.na_storagegrid_host_argument_spec() |
| 111 |
self.argument_spec.update( |
| 112 |
dict( |
| 113 |
state=dict(required=False, type="str", choices=["present", "absent"], default="present"), |
| 114 |
name=dict(required=True, type="str"), |
| 115 |
pool_id=dict(required=False, type="str"), |
| 116 |
scheme_id=dict(required=False, type="str"), |
| 117 |
) |
| 118 |
) |
| 119 |
self.module = AnsibleModule( |
| 120 |
argument_spec=self.argument_spec, |
| 121 |
required_if=[("state", "present", ["pool_id", "scheme_id"])], |
| 122 |
supports_check_mode=True, |
| 123 |
) |
| 124 |
self.na_helper = NetAppModule() |
| 125 |
|
| 126 |
# set up state variables |
| 127 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 128 |
__LOGGING__.append("params: %s" % self.module.params) |
| 129 |
# Calling generic SG rest_api class |
| 130 |
self.rest_api = SGRestAPI(self.module) |
| 131 |
# Get API version |
| 132 |
self.rest_api.get_sg_product_version() |
| 133 |
self.api_version = self.rest_api.get_api_version() |
| 134 |
|
| 135 |
# Create body for creation request (POST with state present) |
| 136 |
self.data = {} |
| 137 |
self.data["name"] = self.parameters["name"] |
| 138 |
if self.parameters.get("pool_id"): |
| 139 |
self.data["poolId"] = self.parameters["pool_id"] |
| 140 |
if self.parameters.get("scheme_id"): |
| 141 |
self.data["schemeId"] = self.parameters["scheme_id"] |
| 142 |
__LOGGING__.append("data: %s" % self.data) |
| 143 |
|
| 144 |
def module_logging_handler(self, log_msg): |
| 145 |
"""Module logging handler""" |
| 146 |
# Create timestamp for logs |
| 147 |
date_now = datetime.now() |
| 148 |
timestamp = date_now.strftime("%Y/%m/%d-%H:%M:%S") |
| 149 |
# Log events |
| 150 |
self.module.log(log_msg) |
| 151 |
__LOGGING__.append("%s: %s" % (timestamp, log_msg)) |
| 152 |
|
| 153 |
def get_ec_profile(self): |
| 154 |
# Check if profile exists |
| 155 |
# Return info if found, or None |
| 156 |
api = "api/%s/grid/ec-profiles?showDeactivated=true" % self.api_version |
| 157 |
response, error = self.rest_api.get(api) |
| 158 |
if error: |
| 159 |
self.module.fail_json(msg=error, log=__LOGGING__) |
| 160 |
self.module_logging_handler("all EC profiles: %s" % response['data']) |
| 161 |
# if EC profile with 'name' exists, return it, else none |
| 162 |
for profile in response["data"]: |
| 163 |
if profile["name"] == self.parameters["name"]: |
| 164 |
self.id = profile["id"] |
| 165 |
return profile |
| 166 |
return None |
| 167 |
|
| 168 |
def create_ec_profile(self): |
| 169 |
__LOGGING__.append("creating EC profile with payload: %s" % self.data) |
| 170 |
api = "api/%s/private/ec-profiles" % self.api_version |
| 171 |
response, error = self.rest_api.post(api, self.data) |
| 172 |
if error: |
| 173 |
self.module.fail_json(msg=error, log=__LOGGING__) |
| 174 |
return response["data"] |
| 175 |
|
| 176 |
def deactivate_ec_profile(self): |
| 177 |
__LOGGING__.append("deactivating EC profile") |
| 178 |
api = "api/%s/private/ec-profiles/%s/deactivate" % (self.rest_api, self.id) |
| 179 |
response, error = self.rest_api.post(api, None) |
| 180 |
if error: |
| 181 |
self.module.fail_json(msg=error, log=__LOGGING__) |
| 182 |
|
| 183 |
def update_ec_profile(self): |
| 184 |
__LOGGING__.append("updating EC profile with payload: %s" % self.data) |
| 185 |
api = "api/%s/private/ec-profiles/%s" % (self.rest_api, self.id) |
| 186 |
response, error = self.rest_api.put(api, self.data) |
| 187 |
if error: |
| 188 |
self.module.fail_json(msg=error, log=__LOGGING__) |
| 189 |
return response["data"] |
| 190 |
|
| 191 |
def apply(self): |
| 192 |
""" |
| 193 |
Perform pre-checks, call functions and exit |
| 194 |
""" |
| 195 |
|
| 196 |
ec_profile = self.get_ec_profile() |
| 197 |
self.module_logging_handler("got matching EC profile: %s" % ec_profile) |
| 198 |
|
| 199 |
cd_action = self.na_helper.get_cd_action(ec_profile, self.parameters) |
| 200 |
|
| 201 |
if self.na_helper.changed and self.parameters["state"] == "absent": |
| 202 |
# only change something if the EC profile is not already inactive |
| 203 |
if not ec_profile.get("active"): |
| 204 |
self.na_helper.changed = False |
| 205 |
|
| 206 |
if cd_action is None and self.parameters["state"] == "present": |
| 207 |
# let's see if we need to update parameters |
| 208 |
if self.data.get("poolId") and self.data.get("poolId") != ec_profile.get("poolId"): |
| 209 |
self.na_helper.changed = True |
| 210 |
if self.data.get("schemeId") and self.data.get("schemeId") != ec_profile.get("schemeId"): |
| 211 |
self.na_helper.changed = True |
| 212 |
|
| 213 |
result_message = "" |
| 214 |
resp_data = ec_profile |
| 215 |
if self.na_helper.changed: |
| 216 |
if self.module.check_mode: |
| 217 |
pass |
| 218 |
else: |
| 219 |
if cd_action == "delete": |
| 220 |
self.deactivate_ec_profile() |
| 221 |
resp_data = None |
| 222 |
result_message = "EC profile deactivated" |
| 223 |
__LOGGING__.append("EC profile deactivated") |
| 224 |
|
| 225 |
elif cd_action == "create": |
| 226 |
resp_data = self.create_ec_profile() |
| 227 |
result_message = "EC profile created" |
| 228 |
__LOGGING__.append("EC profile created") |
| 229 |
|
| 230 |
else: |
| 231 |
resp_data = self.update_ec_profile() |
| 232 |
result_message = "EC profile updated" |
| 233 |
__LOGGING__.append("EC profile updated") |
| 234 |
|
| 235 |
self.module.exit_json(changed=self.na_helper.changed, msg=result_message, resp=resp_data, log=__LOGGING__) |
| 236 |
|
| 237 |
|
| 238 |
def main(): |
| 239 |
""" |
| 240 |
Main function |
| 241 |
""" |
| 242 |
na_sg_ec_profile = EC_profile() |
| 243 |
na_sg_ec_profile.apply() |
| 244 |
|
| 245 |
|
| 246 |
if __name__ == "__main__": |
| 247 |
main() |