| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2018, Hewlett Packard Enterprise Development LP |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
|
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
short_description: Manage HPE StoreServ 3PAR CPG |
| 11 |
author: |
| 12 |
- Farhan Nomani (@farhan7500) |
| 13 |
- Gautham P Hegde (@gautamphegde) |
| 14 |
description: |
| 15 |
- Create and delete CPG on HPE 3PAR. |
| 16 |
module: ss_3par_cpg |
| 17 |
attributes: |
| 18 |
check_mode: |
| 19 |
support: none |
| 20 |
diff_mode: |
| 21 |
support: none |
| 22 |
options: |
| 23 |
cpg_name: |
| 24 |
description: |
| 25 |
- Name of the CPG. |
| 26 |
type: str |
| 27 |
required: true |
| 28 |
disk_type: |
| 29 |
choices: |
| 30 |
- FC |
| 31 |
- NL |
| 32 |
- SSD |
| 33 |
description: |
| 34 |
- Specifies that physical disks must have the specified device type. |
| 35 |
type: str |
| 36 |
domain: |
| 37 |
description: |
| 38 |
- Specifies the name of the domain in which the object resides. |
| 39 |
type: str |
| 40 |
growth_increment: |
| 41 |
description: |
| 42 |
- Specifies the growth increment(in MiB, GiB or TiB) the amount of logical disk storage created on each auto-grow operation. |
| 43 |
type: str |
| 44 |
growth_limit: |
| 45 |
description: |
| 46 |
- Specifies that the autogrow operation is limited to the specified storage amount that sets the growth limit (in MiB, |
| 47 |
GiB or TiB). |
| 48 |
type: str |
| 49 |
growth_warning: |
| 50 |
description: |
| 51 |
- Specifies that the threshold (in MiB, GiB or TiB) of used logical disk space when exceeded results in a warning alert. |
| 52 |
type: str |
| 53 |
high_availability: |
| 54 |
choices: |
| 55 |
- PORT |
| 56 |
- CAGE |
| 57 |
- MAG |
| 58 |
description: |
| 59 |
- Specifies that the layout must support the failure of one port pair, one cage, or one magazine. |
| 60 |
type: str |
| 61 |
raid_type: |
| 62 |
choices: |
| 63 |
- R0 |
| 64 |
- R1 |
| 65 |
- R5 |
| 66 |
- R6 |
| 67 |
description: |
| 68 |
- Specifies the RAID type for the logical disk. |
| 69 |
type: str |
| 70 |
set_size: |
| 71 |
description: |
| 72 |
- Specifies the set size in the number of chunklets. |
| 73 |
type: int |
| 74 |
state: |
| 75 |
choices: |
| 76 |
- present |
| 77 |
- absent |
| 78 |
description: |
| 79 |
- Whether the specified CPG should exist or not. |
| 80 |
required: true |
| 81 |
type: str |
| 82 |
secure: |
| 83 |
description: |
| 84 |
- Specifies whether the certificate needs to be validated while communicating. |
| 85 |
type: bool |
| 86 |
default: false |
| 87 |
extends_documentation_fragment: |
| 88 |
- community.general._hpe3par |
| 89 |
- community.general._attributes |
| 90 |
""" |
| 91 |
|
| 92 |
|
| 93 |
EXAMPLES = r""" |
| 94 |
- name: Create CPG sample_cpg |
| 95 |
community.general.ss_3par_cpg: |
| 96 |
storage_system_ip: 10.10.10.1 |
| 97 |
storage_system_username: username |
| 98 |
storage_system_password: password |
| 99 |
state: present |
| 100 |
cpg_name: sample_cpg |
| 101 |
domain: sample_domain |
| 102 |
growth_increment: 32000 MiB |
| 103 |
growth_limit: 64000 MiB |
| 104 |
growth_warning: 48000 MiB |
| 105 |
raid_type: R6 |
| 106 |
set_size: 8 |
| 107 |
high_availability: MAG |
| 108 |
disk_type: FC |
| 109 |
secure: false |
| 110 |
|
| 111 |
- name: Delete CPG sample_cpg |
| 112 |
community.general.ss_3par_cpg: |
| 113 |
storage_system_ip: 10.10.10.1 |
| 114 |
storage_system_username: username |
| 115 |
storage_system_password: password |
| 116 |
state: absent |
| 117 |
cpg_name: sample_cpg |
| 118 |
secure: false |
| 119 |
""" |
| 120 |
|
| 121 |
RETURN = r""" |
| 122 |
""" |
| 123 |
|
| 124 |
from ansible.module_utils.basic import AnsibleModule |
| 125 |
|
| 126 |
from ansible_collections.community.general.plugins.module_utils import _hpe3par as hpe3par |
| 127 |
|
| 128 |
try: |
| 129 |
from hpe3par_sdk import client |
| 130 |
from hpe3parclient import exceptions |
| 131 |
|
| 132 |
HAS_3PARCLIENT = True |
| 133 |
except ImportError: |
| 134 |
HAS_3PARCLIENT = False |
| 135 |
|
| 136 |
|
| 137 |
def validate_set_size(raid_type, set_size): |
| 138 |
if raid_type: |
| 139 |
set_size_array = client.HPE3ParClient.RAID_MAP[raid_type]["set_sizes"] |
| 140 |
if set_size in set_size_array: |
| 141 |
return True |
| 142 |
return False |
| 143 |
|
| 144 |
|
| 145 |
def cpg_ldlayout_map(ldlayout_dict): |
| 146 |
if ldlayout_dict["RAIDType"] is not None and ldlayout_dict["RAIDType"]: |
| 147 |
ldlayout_dict["RAIDType"] = client.HPE3ParClient.RAID_MAP[ldlayout_dict["RAIDType"]]["raid_value"] |
| 148 |
if ldlayout_dict["HA"] is not None and ldlayout_dict["HA"]: |
| 149 |
ldlayout_dict["HA"] = getattr(client.HPE3ParClient, ldlayout_dict["HA"]) |
| 150 |
return ldlayout_dict |
| 151 |
|
| 152 |
|
| 153 |
def create_cpg( |
| 154 |
client_obj, |
| 155 |
cpg_name, |
| 156 |
domain, |
| 157 |
growth_increment, |
| 158 |
growth_limit, |
| 159 |
growth_warning, |
| 160 |
raid_type, |
| 161 |
set_size, |
| 162 |
high_availability, |
| 163 |
disk_type, |
| 164 |
): |
| 165 |
try: |
| 166 |
if not validate_set_size(raid_type, set_size): |
| 167 |
return (False, False, f"Set size {set_size} not part of RAID set {raid_type}") |
| 168 |
if not client_obj.cpgExists(cpg_name): |
| 169 |
disk_patterns = [] |
| 170 |
if disk_type: |
| 171 |
disk_type = getattr(client.HPE3ParClient, disk_type) |
| 172 |
disk_patterns = [{"diskType": disk_type}] |
| 173 |
ld_layout = { |
| 174 |
"RAIDType": raid_type, |
| 175 |
"setSize": set_size, |
| 176 |
"HA": high_availability, |
| 177 |
"diskPatterns": disk_patterns, |
| 178 |
} |
| 179 |
ld_layout = cpg_ldlayout_map(ld_layout) |
| 180 |
if growth_increment is not None: |
| 181 |
growth_increment = hpe3par.convert_to_binary_multiple(growth_increment) |
| 182 |
if growth_limit is not None: |
| 183 |
growth_limit = hpe3par.convert_to_binary_multiple(growth_limit) |
| 184 |
if growth_warning is not None: |
| 185 |
growth_warning = hpe3par.convert_to_binary_multiple(growth_warning) |
| 186 |
optional = { |
| 187 |
"domain": domain, |
| 188 |
"growthIncrementMiB": growth_increment, |
| 189 |
"growthLimitMiB": growth_limit, |
| 190 |
"usedLDWarningAlertMiB": growth_warning, |
| 191 |
"LDLayout": ld_layout, |
| 192 |
} |
| 193 |
client_obj.createCPG(cpg_name, optional) |
| 194 |
else: |
| 195 |
return (True, False, "CPG already present") |
| 196 |
except exceptions.ClientException as e: |
| 197 |
return (False, False, f"CPG creation failed | {e}") |
| 198 |
return (True, True, f"Created CPG {cpg_name} successfully.") |
| 199 |
|
| 200 |
|
| 201 |
def delete_cpg(client_obj, cpg_name): |
| 202 |
try: |
| 203 |
if client_obj.cpgExists(cpg_name): |
| 204 |
client_obj.deleteCPG(cpg_name) |
| 205 |
else: |
| 206 |
return (True, False, "CPG does not exist") |
| 207 |
except exceptions.ClientException as e: |
| 208 |
return (False, False, f"CPG delete failed | {e}") |
| 209 |
return (True, True, f"Deleted CPG {cpg_name} successfully.") |
| 210 |
|
| 211 |
|
| 212 |
def main(): |
| 213 |
module = AnsibleModule(argument_spec=hpe3par.cpg_argument_spec(), required_together=[["raid_type", "set_size"]]) |
| 214 |
if not HAS_3PARCLIENT: |
| 215 |
module.fail_json(msg="the python hpe3par_sdk library is required (https://pypi.org/project/hpe3par_sdk)") |
| 216 |
|
| 217 |
if len(module.params["cpg_name"]) < 1 or len(module.params["cpg_name"]) > 31: |
| 218 |
module.fail_json(msg="CPG name must be at least 1 character and not more than 31 characters") |
| 219 |
|
| 220 |
storage_system_ip = module.params["storage_system_ip"] |
| 221 |
storage_system_username = module.params["storage_system_username"] |
| 222 |
storage_system_password = module.params["storage_system_password"] |
| 223 |
cpg_name = module.params["cpg_name"] |
| 224 |
domain = module.params["domain"] |
| 225 |
growth_increment = module.params["growth_increment"] |
| 226 |
growth_limit = module.params["growth_limit"] |
| 227 |
growth_warning = module.params["growth_warning"] |
| 228 |
raid_type = module.params["raid_type"] |
| 229 |
set_size = module.params["set_size"] |
| 230 |
high_availability = module.params["high_availability"] |
| 231 |
disk_type = module.params["disk_type"] |
| 232 |
secure = module.params["secure"] |
| 233 |
|
| 234 |
wsapi_url = f"https://{storage_system_ip}:8080/api/v1" |
| 235 |
try: |
| 236 |
client_obj = client.HPE3ParClient(wsapi_url, secure) |
| 237 |
except exceptions.SSLCertFailed: |
| 238 |
module.fail_json(msg="SSL Certificate Failed") |
| 239 |
except exceptions.ConnectionError: |
| 240 |
module.fail_json(msg="Connection Error") |
| 241 |
except exceptions.UnsupportedVersion: |
| 242 |
module.fail_json(msg="Unsupported WSAPI version") |
| 243 |
except Exception as e: |
| 244 |
module.fail_json(msg=f"Initializing client failed. {e}") |
| 245 |
|
| 246 |
if storage_system_username is None or storage_system_password is None: |
| 247 |
module.fail_json(msg="Storage system username or password is None") |
| 248 |
if cpg_name is None: |
| 249 |
module.fail_json(msg="CPG Name is None") |
| 250 |
|
| 251 |
# States |
| 252 |
if module.params["state"] == "present": |
| 253 |
try: |
| 254 |
client_obj.login(storage_system_username, storage_system_password) |
| 255 |
return_status, changed, msg = create_cpg( |
| 256 |
client_obj, |
| 257 |
cpg_name, |
| 258 |
domain, |
| 259 |
growth_increment, |
| 260 |
growth_limit, |
| 261 |
growth_warning, |
| 262 |
raid_type, |
| 263 |
set_size, |
| 264 |
high_availability, |
| 265 |
disk_type, |
| 266 |
) |
| 267 |
except Exception as e: |
| 268 |
module.fail_json(msg=f"CPG create failed | {e}") |
| 269 |
finally: |
| 270 |
client_obj.logout() |
| 271 |
|
| 272 |
elif module.params["state"] == "absent": |
| 273 |
try: |
| 274 |
client_obj.login(storage_system_username, storage_system_password) |
| 275 |
return_status, changed, msg = delete_cpg(client_obj, cpg_name) |
| 276 |
except Exception as e: |
| 277 |
module.fail_json(msg=f"CPG create failed | {e}") |
| 278 |
finally: |
| 279 |
client_obj.logout() |
| 280 |
|
| 281 |
if return_status: |
| 282 |
module.exit_json(changed=changed, msg=msg) |
| 283 |
else: |
| 284 |
module.fail_json(msg=msg) |
| 285 |
|
| 286 |
|
| 287 |
if __name__ == "__main__": |
| 288 |
main() |