| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2022, 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 HA Groups""" |
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
|
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
DOCUMENTATION = """ |
| 13 |
module: na_sg_grid_ha_group |
| 14 |
short_description: Manage high availability (HA) group configuration on StorageGRID. |
| 15 |
extends_documentation_fragment: |
| 16 |
- netapp.storagegrid.netapp.sg |
| 17 |
version_added: '21.10.0' |
| 18 |
author: NetApp Ansible Team (@joshedmonds) <[email protected]> |
| 19 |
description: |
| 20 |
- Create, Update, Delete HA Groups on NetApp StorageGRID. |
| 21 |
options: |
| 22 |
state: |
| 23 |
description: |
| 24 |
- Whether the specified HA Group should exist. |
| 25 |
type: str |
| 26 |
choices: ['present', 'absent'] |
| 27 |
default: present |
| 28 |
name: |
| 29 |
description: |
| 30 |
- Name of the HA Group. |
| 31 |
type: str |
| 32 |
ha_group_id: |
| 33 |
description: |
| 34 |
- HA Group ID. |
| 35 |
- May be used for modify or delete operation. |
| 36 |
type: str |
| 37 |
description: |
| 38 |
description: |
| 39 |
- Description of the HA Group. |
| 40 |
type: str |
| 41 |
gateway_cidr: |
| 42 |
description: |
| 43 |
- The C(gateway_cidr) field specifies the gateway IP address and prefix length of the VIP subnet in CIDR notation. i.e. C(IP_address/prefix_length). |
| 44 |
- This must match the subnet CIDR if any interface in the HA group already has an IP address configured. |
| 45 |
- The IP address may be specified as the network address of the subnet, or as the gateway IP address within the subnet if clients will access the VIP |
| 46 |
address from a different subnet. |
| 47 |
- For example C(10.193.150.0/25) or C(10.193.150.1/25). |
| 48 |
type: str |
| 49 |
virtual_ips: |
| 50 |
description: |
| 51 |
- A list of virtual IP addresses. |
| 52 |
type: list |
| 53 |
elements: str |
| 54 |
interfaces: |
| 55 |
description: |
| 56 |
- A set of StorageGRID node interface pairs. |
| 57 |
- The primary interface is specified first, followed by the other interface pairs in failover order. |
| 58 |
type: list |
| 59 |
elements: dict |
| 60 |
suboptions: |
| 61 |
node: |
| 62 |
description: |
| 63 |
- Name of the StorageGRID node. |
| 64 |
type: str |
| 65 |
interface: |
| 66 |
description: |
| 67 |
- The interface to bind to. eth0 corresponds to the Grid Network, eth1 to the Admin Network, and eth2 to the Client Network. |
| 68 |
type: str |
| 69 |
""" |
| 70 |
|
| 71 |
EXAMPLES = """ |
| 72 |
- name: create HA Group (layer 2) |
| 73 |
netapp.storagegrid.na_sg_grid_ha_group: |
| 74 |
api_url: "https://<storagegrid-endpoint-url>" |
| 75 |
auth_token: "storagegrid-auth-token" |
| 76 |
validate_certs: false |
| 77 |
state: present |
| 78 |
name: Site1-HA-Group |
| 79 |
description: "Site 1 HA Group" |
| 80 |
gateway_cidr: 192.168.50.0/24 |
| 81 |
virtual_ips: 192.168.50.5 |
| 82 |
interfaces: |
| 83 |
- node: SITE1-ADM1 |
| 84 |
interface: eth2 |
| 85 |
- node: SITE1-G1 |
| 86 |
interface: eth2 |
| 87 |
|
| 88 |
- name: create HA Group (routable) |
| 89 |
netapp.storagegrid.na_sg_grid_ha_group: |
| 90 |
api_url: "https://<storagegrid-endpoint-url>" |
| 91 |
auth_token: "storagegrid-auth-token" |
| 92 |
validate_certs: false |
| 93 |
state: present |
| 94 |
name: Site1-HA-Group |
| 95 |
description: "Site 1 HA Group" |
| 96 |
gateway_cidr: 192.168.50.1/24 |
| 97 |
virtual_ips: 192.168.50.5 |
| 98 |
interfaces: |
| 99 |
- node: SITE1-ADM1 |
| 100 |
interface: eth2 |
| 101 |
- node: SITE1-G1 |
| 102 |
interface: eth2 |
| 103 |
|
| 104 |
- name: add VIP to HA Group |
| 105 |
netapp.storagegrid.na_sg_grid_ha_group: |
| 106 |
api_url: "https://<storagegrid-endpoint-url>" |
| 107 |
auth_token: "storagegrid-auth-token" |
| 108 |
validate_certs: false |
| 109 |
state: present |
| 110 |
name: Site1-HA-Group |
| 111 |
description: "Site 1 HA Group" |
| 112 |
gateway_cidr: 192.168.50.1/24 |
| 113 |
virtual_ips: 192.168.50.5,192.168.50.6 |
| 114 |
interfaces: |
| 115 |
- node: SITE1-ADM1 |
| 116 |
interface: eth2 |
| 117 |
- node: SITE1-G1 |
| 118 |
interface: eth2 |
| 119 |
|
| 120 |
- name: rename HA Group |
| 121 |
netapp.storagegrid.na_sg_grid_ha_group: |
| 122 |
api_url: "https://<storagegrid-endpoint-url>" |
| 123 |
auth_token: "storagegrid-auth-token" |
| 124 |
validate_certs: false |
| 125 |
state: present |
| 126 |
ha_group_id: 00000000-0000-0000-0000-000000000000 |
| 127 |
name: Site1-HA-Group-New-Name |
| 128 |
description: "Site 1 HA Group" |
| 129 |
gateway_cidr: 192.168.50.1/24 |
| 130 |
virtual_ips: 192.168.50.5 |
| 131 |
interfaces: |
| 132 |
- node: SITE1-ADM1 |
| 133 |
interface: eth2 |
| 134 |
- node: SITE1-G1 |
| 135 |
interface: eth2 |
| 136 |
|
| 137 |
- name: delete HA Group |
| 138 |
netapp.storagegrid.na_sg_grid_ha_group: |
| 139 |
api_url: "https://<storagegrid-endpoint-url>" |
| 140 |
auth_token: "storagegrid-auth-token" |
| 141 |
validate_certs: false |
| 142 |
state: absent |
| 143 |
name: Site1-HA-Group |
| 144 |
""" |
| 145 |
|
| 146 |
RETURN = """ |
| 147 |
resp: |
| 148 |
description: Returns information about the StorageGRID HA Group. |
| 149 |
returned: success |
| 150 |
type: dict |
| 151 |
sample: { |
| 152 |
"description": "Site 1 HA Group", |
| 153 |
"gatewayCidr": "192.168.50.1/24", |
| 154 |
"id": "bb386f30-805d-4fec-a2c5-85790b460db0", |
| 155 |
"interfaces": [ |
| 156 |
{ |
| 157 |
"interface": "eth2", |
| 158 |
"nodeId": "0b1866ed-d6e7-41b4-815f-bf867348b76b" |
| 159 |
}, |
| 160 |
{ |
| 161 |
"interface": "eth2", |
| 162 |
"nodeId": "7bb5bf05-a04c-4344-8abd-08c5c4048666" |
| 163 |
} |
| 164 |
], |
| 165 |
"name": "Site1-HA-Group", |
| 166 |
"virtualIps": [ |
| 167 |
"192.168.50.5", |
| 168 |
"192.168.50.6" |
| 169 |
] |
| 170 |
} |
| 171 |
""" |
| 172 |
|
| 173 |
import ansible_collections.netapp.storagegrid.plugins.module_utils.netapp as netapp_utils |
| 174 |
from ansible.module_utils.basic import AnsibleModule |
| 175 |
from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp_module import NetAppModule |
| 176 |
from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp import SGRestAPI |
| 177 |
|
| 178 |
|
| 179 |
class SgGridHaGroup: |
| 180 |
""" |
| 181 |
Create, modify and delete HA Group configurations for StorageGRID |
| 182 |
""" |
| 183 |
|
| 184 |
def __init__(self): |
| 185 |
""" |
| 186 |
Parse arguments, setup state variables, |
| 187 |
check parameters and ensure request module is installed |
| 188 |
""" |
| 189 |
self.argument_spec = netapp_utils.na_storagegrid_host_argument_spec() |
| 190 |
self.argument_spec.update( |
| 191 |
dict( |
| 192 |
state=dict(required=False, type="str", choices=["present", "absent"], default="present"), |
| 193 |
name=dict(required=False, type="str"), |
| 194 |
ha_group_id=dict(required=False, type="str"), |
| 195 |
description=dict(required=False, type="str"), |
| 196 |
gateway_cidr=dict(required=False, type="str"), |
| 197 |
virtual_ips=dict(required=False, type="list", elements="str"), |
| 198 |
interfaces=dict( |
| 199 |
required=False, |
| 200 |
type="list", |
| 201 |
elements="dict", |
| 202 |
options=dict( |
| 203 |
node=dict(required=False, type="str"), |
| 204 |
interface=dict(required=False, type="str"), |
| 205 |
), |
| 206 |
), |
| 207 |
) |
| 208 |
) |
| 209 |
|
| 210 |
parameter_map = { |
| 211 |
"name": "name", |
| 212 |
"description": "description", |
| 213 |
"gateway_cidr": "gatewayCidr", |
| 214 |
"virtual_ips": "virtualIps", |
| 215 |
} |
| 216 |
|
| 217 |
self.module = AnsibleModule( |
| 218 |
argument_spec=self.argument_spec, |
| 219 |
required_if=[("state", "present", ["name", "gateway_cidr", "virtual_ips", "interfaces"])], |
| 220 |
required_one_of=[("name", "ha_group_id")], |
| 221 |
) |
| 222 |
|
| 223 |
self.na_helper = NetAppModule() |
| 224 |
|
| 225 |
# set up state variables |
| 226 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 227 |
# Calling generic SG rest_api class |
| 228 |
self.rest_api = SGRestAPI(self.module) |
| 229 |
# Get API version |
| 230 |
self.rest_api.get_sg_product_version() |
| 231 |
self.api_version = self.rest_api.get_api_version() |
| 232 |
|
| 233 |
# Checking for the parameters passed and create new parameters list |
| 234 |
self.data = {} |
| 235 |
|
| 236 |
if self.parameters["state"] == "present": |
| 237 |
for k in parameter_map.keys(): |
| 238 |
if self.parameters.get(k) is not None: |
| 239 |
self.data[parameter_map[k]] = self.parameters[k] |
| 240 |
|
| 241 |
if self.parameters.get("interfaces") is not None: |
| 242 |
self.data["interfaces"] = self.build_node_interface_list() |
| 243 |
|
| 244 |
def build_node_interface_list(self): |
| 245 |
node_interfaces = [] |
| 246 |
|
| 247 |
api = "api/%s/grid/node-health" % self.api_version |
| 248 |
nodes, error = self.rest_api.get(api) |
| 249 |
|
| 250 |
if error: |
| 251 |
self.module.fail_json(msg=error) |
| 252 |
|
| 253 |
for node_interface in self.parameters["interfaces"]: |
| 254 |
node_dict = {} |
| 255 |
node = next((item for item in nodes["data"] if item["name"] == node_interface["node"]), None) |
| 256 |
if node is not None: |
| 257 |
node_dict["nodeId"] = node["id"] |
| 258 |
node_dict["interface"] = node_interface["interface"] |
| 259 |
node_interfaces.append(node_dict) |
| 260 |
else: |
| 261 |
self.module.fail_json(msg="Node '%s' is invalid" % node_interface["node"]) |
| 262 |
|
| 263 |
return node_interfaces |
| 264 |
|
| 265 |
def get_ha_group_id(self): |
| 266 |
# Check if HA Group exists |
| 267 |
# Return HA Group info if found, or None |
| 268 |
api = "api/%s/private/ha-groups" % self.api_version |
| 269 |
response, error = self.rest_api.get(api) |
| 270 |
|
| 271 |
if error: |
| 272 |
self.module.fail_json(msg=error) |
| 273 |
|
| 274 |
return next((item["id"] for item in response.get("data") if item["name"] == self.parameters["name"]), None) |
| 275 |
|
| 276 |
def get_ha_group(self, ha_group_id): |
| 277 |
api = "api/%s/private/ha-groups/%s" % (self.api_version, ha_group_id) |
| 278 |
response, error = self.rest_api.get(api) |
| 279 |
|
| 280 |
if error: |
| 281 |
self.module.fail_json(msg=error) |
| 282 |
|
| 283 |
return response["data"] |
| 284 |
|
| 285 |
def create_ha_group(self): |
| 286 |
api = "api/%s/private/ha-groups" % self.api_version |
| 287 |
response, error = self.rest_api.post(api, self.data) |
| 288 |
|
| 289 |
if error: |
| 290 |
self.module.fail_json(msg=error) |
| 291 |
|
| 292 |
return response["data"] |
| 293 |
|
| 294 |
def delete_ha_group(self, ha_group_id): |
| 295 |
api = "api/%s/private/ha-groups/%s" % (self.api_version, ha_group_id) |
| 296 |
dummy, error = self.rest_api.delete(api, self.data) |
| 297 |
|
| 298 |
if error: |
| 299 |
self.module.fail_json(msg=error) |
| 300 |
|
| 301 |
def update_ha_group(self, ha_group_id): |
| 302 |
api = "api/%s/private/ha-groups/%s" % (self.api_version, ha_group_id) |
| 303 |
response, error = self.rest_api.put(api, self.data) |
| 304 |
|
| 305 |
if error: |
| 306 |
self.module.fail_json(msg=error) |
| 307 |
|
| 308 |
return response["data"] |
| 309 |
|
| 310 |
def apply(self): |
| 311 |
""" |
| 312 |
Perform pre-checks, call functions and exit |
| 313 |
""" |
| 314 |
|
| 315 |
ha_group = None |
| 316 |
|
| 317 |
if self.parameters.get("ha_group_id"): |
| 318 |
ha_group = self.get_ha_group(self.parameters["ha_group_id"]) |
| 319 |
else: |
| 320 |
ha_group_id = self.get_ha_group_id() |
| 321 |
if ha_group_id: |
| 322 |
ha_group = self.get_ha_group(ha_group_id) |
| 323 |
|
| 324 |
cd_action = self.na_helper.get_cd_action(ha_group, self.parameters) |
| 325 |
|
| 326 |
if cd_action is None and self.parameters["state"] == "present": |
| 327 |
# let's see if we need to update parameters |
| 328 |
modify = self.na_helper.get_modified_attributes(ha_group, self.data) |
| 329 |
|
| 330 |
result_message = "" |
| 331 |
resp_data = {} |
| 332 |
|
| 333 |
# check if we are in check mode |
| 334 |
if self.module.check_mode: |
| 335 |
if cd_action == "delete": |
| 336 |
self.module.exit_json(changed=True, msg="HA Group would be deleted.") |
| 337 |
elif cd_action == "create": |
| 338 |
self.module.exit_json(changed=True, msg="HA Group would be created.") |
| 339 |
elif modify: |
| 340 |
self.module.exit_json(changed=True, msg="HA Group would be updated.") |
| 341 |
else: |
| 342 |
self.module.exit_json(changed=False, msg="No changes would be made.") |
| 343 |
|
| 344 |
if self.na_helper.changed: |
| 345 |
if cd_action == "delete": |
| 346 |
self.delete_ha_group(ha_group["id"]) |
| 347 |
result_message = "HA Group deleted" |
| 348 |
elif cd_action == "create": |
| 349 |
resp_data = self.create_ha_group() |
| 350 |
result_message = "HA Group created" |
| 351 |
elif modify: |
| 352 |
resp_data = self.update_ha_group(ha_group["id"]) |
| 353 |
result_message = "HA Group updated" |
| 354 |
|
| 355 |
self.module.exit_json(changed=self.na_helper.changed, msg=result_message, resp=resp_data) |
| 356 |
|
| 357 |
|
| 358 |
def main(): |
| 359 |
""" |
| 360 |
Main function |
| 361 |
""" |
| 362 |
na_sg_grid_ha_group = SgGridHaGroup() |
| 363 |
na_sg_grid_ha_group.apply() |
| 364 |
|
| 365 |
|
| 366 |
if __name__ == "__main__": |
| 367 |
main() |