| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2020-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 Grid Regions""" |
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
|
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
|
| 13 |
ANSIBLE_METADATA = { |
| 14 |
"metadata_version": "1.1", |
| 15 |
"status": ["preview"], |
| 16 |
"supported_by": "community", |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
DOCUMENTATION = """ |
| 21 |
module: na_sg_grid_regions |
| 22 |
short_description: NetApp StorageGRID manage Regions for the grid. |
| 23 |
extends_documentation_fragment: |
| 24 |
- netapp.storagegrid.netapp.sg |
| 25 |
version_added: '20.6.0' |
| 26 |
author: NetApp Ansible Team (@joshedmonds) <[email protected]> |
| 27 |
description: |
| 28 |
- Create and Update Regions configured for the NetApp StorageGRID. |
| 29 |
options: |
| 30 |
state: |
| 31 |
description: |
| 32 |
- Whether the specified regions should be present. |
| 33 |
type: str |
| 34 |
choices: ['present'] |
| 35 |
default: present |
| 36 |
regions: |
| 37 |
description: |
| 38 |
- List of the regions. |
| 39 |
required: true |
| 40 |
type: list |
| 41 |
elements: str |
| 42 |
""" |
| 43 |
|
| 44 |
EXAMPLES = """ |
| 45 |
- name: update Regions |
| 46 |
netapp.storagegrid.na_sg_grid_regions: |
| 47 |
api_url: "https://<storagegrid-endpoint-url>" |
| 48 |
auth_token: "storagegrid-auth-token" |
| 49 |
validate_certs: false |
| 50 |
state: present |
| 51 |
regions: "us-east-1" |
| 52 |
""" |
| 53 |
|
| 54 |
RETURN = """ |
| 55 |
resp: |
| 56 |
description: Returns information about the configured regions. |
| 57 |
returned: success |
| 58 |
type: list |
| 59 |
elements: str |
| 60 |
sample: ["us-east-1", "us-central-1"] |
| 61 |
""" |
| 62 |
|
| 63 |
import ansible_collections.netapp.storagegrid.plugins.module_utils.netapp as netapp_utils |
| 64 |
from ansible.module_utils.basic import AnsibleModule |
| 65 |
from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp_module import NetAppModule |
| 66 |
from ansible_collections.netapp.storagegrid.plugins.module_utils.netapp import SGRestAPI |
| 67 |
|
| 68 |
|
| 69 |
class SgGridRegions(object): |
| 70 |
""" |
| 71 |
Create, Update Regions for StorageGRID |
| 72 |
""" |
| 73 |
|
| 74 |
def __init__(self): |
| 75 |
""" |
| 76 |
Parse arguments, setup state variables, |
| 77 |
check parameters and ensure request module is installed |
| 78 |
""" |
| 79 |
self.argument_spec = netapp_utils.na_storagegrid_host_argument_spec() |
| 80 |
self.argument_spec.update( |
| 81 |
dict( |
| 82 |
state=dict(required=False, type="str", choices=["present"], default="present"), |
| 83 |
regions=dict(required=True, type="list", elements="str"), |
| 84 |
) |
| 85 |
) |
| 86 |
|
| 87 |
self.module = AnsibleModule( |
| 88 |
argument_spec=self.argument_spec, |
| 89 |
supports_check_mode=True, |
| 90 |
) |
| 91 |
|
| 92 |
self.na_helper = NetAppModule() |
| 93 |
|
| 94 |
# set up state variables |
| 95 |
self.parameters = self.na_helper.set_parameters(self.module.params) |
| 96 |
# Calling generic SG rest_api class |
| 97 |
self.rest_api = SGRestAPI(self.module) |
| 98 |
# Get API version |
| 99 |
self.rest_api.get_sg_product_version() |
| 100 |
self.api_version = self.rest_api.get_api_version() |
| 101 |
|
| 102 |
# Checking for the parameters passed and create new parameters list |
| 103 |
self.data = self.parameters["regions"] |
| 104 |
|
| 105 |
def get_grid_regions(self): |
| 106 |
""" Get the current grid regions """ |
| 107 |
api = "api/%s/grid/regions" % self.api_version |
| 108 |
|
| 109 |
response, error = self.rest_api.get(api) |
| 110 |
|
| 111 |
if error: |
| 112 |
self.module.fail_json(msg=error) |
| 113 |
|
| 114 |
return response["data"] |
| 115 |
|
| 116 |
def update_grid_regions(self): |
| 117 |
""" Update the grid regions """ |
| 118 |
api = "api/%s/grid/regions" % self.api_version |
| 119 |
|
| 120 |
response, error = self.rest_api.put(api, self.data) |
| 121 |
if error: |
| 122 |
self.module.fail_json(msg=error) |
| 123 |
|
| 124 |
return response["data"] |
| 125 |
|
| 126 |
def apply(self): |
| 127 |
""" |
| 128 |
Perform pre-checks, call functions and exit |
| 129 |
""" |
| 130 |
grid_regions = self.get_grid_regions() |
| 131 |
|
| 132 |
cd_action = self.na_helper.get_cd_action(grid_regions, self.parameters["regions"]) |
| 133 |
|
| 134 |
if cd_action is None and self.parameters["state"] == "present": |
| 135 |
# let's see if we need to update parameters |
| 136 |
update = False |
| 137 |
|
| 138 |
regions_diff = [i for i in self.data + grid_regions if i not in self.data or i not in grid_regions] |
| 139 |
if regions_diff: |
| 140 |
update = True |
| 141 |
|
| 142 |
if update: |
| 143 |
self.na_helper.changed = True |
| 144 |
|
| 145 |
result_message = "" |
| 146 |
resp_data = grid_regions |
| 147 |
if self.na_helper.changed: |
| 148 |
if self.module.check_mode: |
| 149 |
pass |
| 150 |
else: |
| 151 |
resp_data = self.update_grid_regions() |
| 152 |
result_message = "Grid Regions updated" |
| 153 |
|
| 154 |
self.module.exit_json(changed=self.na_helper.changed, msg=result_message, resp=resp_data) |
| 155 |
|
| 156 |
|
| 157 |
def main(): |
| 158 |
""" |
| 159 |
Main function |
| 160 |
""" |
| 161 |
na_sg_grid_regions = SgGridRegions() |
| 162 |
na_sg_grid_regions.apply() |
| 163 |
|
| 164 |
|
| 165 |
if __name__ == "__main__": |
| 166 |
main() |