| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2024, NetApp, Inc |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
__metaclass__ = type |
| 8 |
|
| 9 |
|
| 10 |
ANSIBLE_METADATA = {'metadata_version': '1.1', |
| 11 |
'status': ['deprecated'], |
| 12 |
'supported_by': 'community'} |
| 13 |
|
| 14 |
|
| 15 |
DOCUMENTATION = """ |
| 16 |
--- |
| 17 |
module: netapp_e_amg_sync |
| 18 |
short_description: NetApp E-Series conduct synchronization actions on asynchronous mirror groups. |
| 19 |
description: |
| 20 |
- Allows for the initialization, suspension and resumption of an asynchronous mirror group's synchronization for NetApp E-series storage arrays. |
| 21 |
version_added: '2.2.0' |
| 22 |
author: |
| 23 |
- Kevin Hulquest (@hulquest) |
| 24 |
- Vu Tran (@VuTran007) |
| 25 |
options: |
| 26 |
api_username: |
| 27 |
required: true |
| 28 |
type: str |
| 29 |
description: |
| 30 |
- The username to authenticate with the SANtricity WebServices Proxy or embedded REST API. |
| 31 |
api_password: |
| 32 |
required: true |
| 33 |
type: str |
| 34 |
description: |
| 35 |
- The password to authenticate with the SANtricity WebServices Proxy or embedded REST API. |
| 36 |
api_url: |
| 37 |
required: true |
| 38 |
type: str |
| 39 |
description: |
| 40 |
- The url to the SANtricity WebServices Proxy or embedded REST API. |
| 41 |
validate_certs: |
| 42 |
required: false |
| 43 |
default: true |
| 44 |
description: |
| 45 |
- Should https certificates be validated? |
| 46 |
type: bool |
| 47 |
ssid: |
| 48 |
description: |
| 49 |
- The ID of the storage array containing the AMG you wish to target |
| 50 |
required: true |
| 51 |
type: str |
| 52 |
name: |
| 53 |
description: |
| 54 |
- The name of the async mirror group you wish to target |
| 55 |
type: str |
| 56 |
required: yes |
| 57 |
state: |
| 58 |
description: |
| 59 |
- The synchronization action you'd like to take. |
| 60 |
- If C(running) then it will begin syncing if there is no active sync or will resume a suspended sync. If there is already a sync in |
| 61 |
progress, it will return with an OK status. |
| 62 |
- If C(suspended) it will suspend any ongoing sync action, but return OK if there is no active sync or if the sync is already suspended |
| 63 |
type: str |
| 64 |
choices: |
| 65 |
- running |
| 66 |
- suspended |
| 67 |
required: yes |
| 68 |
delete_recovery_point: |
| 69 |
description: |
| 70 |
- Indicates whether the failures point can be deleted on the secondary if necessary to achieve the synchronization. |
| 71 |
- If true, and if the amount of unsynchronized data exceeds the CoW repository capacity on the secondary for any member volume, the last |
| 72 |
failures point will be deleted and synchronization will continue. |
| 73 |
- If false, the synchronization will be suspended if the amount of unsynchronized data exceeds the CoW Repository capacity on the secondary |
| 74 |
and the failures point will be preserved. |
| 75 |
- "NOTE: This only has impact for newly launched syncs." |
| 76 |
type: bool |
| 77 |
default: no |
| 78 |
""" |
| 79 |
EXAMPLES = """ |
| 80 |
- name: start AMG async |
| 81 |
netapp_e_amg_sync: |
| 82 |
name: "{{ amg_sync_name }}" |
| 83 |
state: running |
| 84 |
ssid: "{{ ssid }}" |
| 85 |
api_url: "{{ netapp_api_url }}" |
| 86 |
api_username: "{{ netapp_api_username }}" |
| 87 |
api_password: "{{ netapp_api_password }}" |
| 88 |
""" |
| 89 |
RETURN = """ |
| 90 |
json: |
| 91 |
description: The object attributes of the AMG. |
| 92 |
returned: success |
| 93 |
type: str |
| 94 |
example: |
| 95 |
{ |
| 96 |
"changed": false, |
| 97 |
"connectionType": "fc", |
| 98 |
"groupRef": "3700000060080E5000299C24000006EF57ACAC70", |
| 99 |
"groupState": "optimal", |
| 100 |
"id": "3700000060080E5000299C24000006EF57ACAC70", |
| 101 |
"label": "made_with_ansible", |
| 102 |
"localRole": "primary", |
| 103 |
"mirrorChannelRemoteTarget": "9000000060080E5000299C24005B06E557AC7EEC", |
| 104 |
"orphanGroup": false, |
| 105 |
"recoveryPointAgeAlertThresholdMinutes": 20, |
| 106 |
"remoteRole": "secondary", |
| 107 |
"remoteTarget": { |
| 108 |
"nodeName": { |
| 109 |
"ioInterfaceType": "fc", |
| 110 |
"iscsiNodeName": null, |
| 111 |
"remoteNodeWWN": "20040080E5299F1C" |
| 112 |
}, |
| 113 |
"remoteRef": "9000000060080E5000299C24005B06E557AC7EEC", |
| 114 |
"scsiinitiatorTargetBaseProperties": { |
| 115 |
"ioInterfaceType": "fc", |
| 116 |
"iscsiinitiatorTargetBaseParameters": null |
| 117 |
} |
| 118 |
}, |
| 119 |
"remoteTargetId": "ansible2", |
| 120 |
"remoteTargetName": "Ansible2", |
| 121 |
"remoteTargetWwn": "60080E5000299F880000000056A25D56", |
| 122 |
"repositoryUtilizationWarnThreshold": 80, |
| 123 |
"roleChangeProgress": "none", |
| 124 |
"syncActivity": "idle", |
| 125 |
"syncCompletionTimeAlertThresholdMinutes": 10, |
| 126 |
"syncIntervalMinutes": 10, |
| 127 |
"worldWideName": "60080E5000299C24000006EF57ACAC70" |
| 128 |
} |
| 129 |
""" |
| 130 |
import json |
| 131 |
|
| 132 |
from ansible.module_utils.api import basic_auth_argument_spec |
| 133 |
from ansible.module_utils.basic import AnsibleModule |
| 134 |
from ansible.module_utils.six.moves.urllib.error import HTTPError |
| 135 |
from ansible.module_utils.urls import open_url |
| 136 |
|
| 137 |
|
| 138 |
def request(url, data=None, headers=None, method='GET', use_proxy=True, |
| 139 |
force=False, last_mod_time=None, timeout=10, validate_certs=True, |
| 140 |
url_username=None, url_password=None, http_agent=None, force_basic_auth=True, ignore_errors=False): |
| 141 |
try: |
| 142 |
r = open_url(url=url, data=data, headers=headers, method=method, use_proxy=use_proxy, |
| 143 |
force=force, last_mod_time=last_mod_time, timeout=timeout, validate_certs=validate_certs, |
| 144 |
url_username=url_username, url_password=url_password, http_agent=http_agent, |
| 145 |
force_basic_auth=force_basic_auth) |
| 146 |
except HTTPError as e: |
| 147 |
r = e.fp |
| 148 |
|
| 149 |
try: |
| 150 |
raw_data = r.read() |
| 151 |
if raw_data: |
| 152 |
data = json.loads(raw_data) |
| 153 |
else: |
| 154 |
raw_data = None |
| 155 |
except Exception: |
| 156 |
if ignore_errors: |
| 157 |
pass |
| 158 |
else: |
| 159 |
raise Exception(raw_data) |
| 160 |
|
| 161 |
resp_code = r.getcode() |
| 162 |
|
| 163 |
if resp_code >= 400 and not ignore_errors: |
| 164 |
raise Exception(resp_code, data) |
| 165 |
else: |
| 166 |
return resp_code, data |
| 167 |
|
| 168 |
|
| 169 |
class AMGsync(object): |
| 170 |
def __init__(self): |
| 171 |
argument_spec = basic_auth_argument_spec() |
| 172 |
argument_spec.update(dict( |
| 173 |
api_username=dict(type='str', required=True), |
| 174 |
api_password=dict(type='str', required=True, no_log=True), |
| 175 |
api_url=dict(type='str', required=True), |
| 176 |
name=dict(required=True, type='str'), |
| 177 |
ssid=dict(required=True, type='str'), |
| 178 |
state=dict(required=True, type='str', choices=['running', 'suspended']), |
| 179 |
delete_recovery_point=dict(required=False, type='bool', default=False) |
| 180 |
)) |
| 181 |
self.module = AnsibleModule(argument_spec=argument_spec) |
| 182 |
args = self.module.params |
| 183 |
self.name = args['name'] |
| 184 |
self.ssid = args['ssid'] |
| 185 |
self.state = args['state'] |
| 186 |
self.delete_recovery_point = args['delete_recovery_point'] |
| 187 |
try: |
| 188 |
self.user = args['api_username'] |
| 189 |
self.pwd = args['api_password'] |
| 190 |
self.url = args['api_url'] |
| 191 |
except KeyError: |
| 192 |
self.module.fail_json(msg="You must pass in api_username" |
| 193 |
"and api_password and api_url to the module.") |
| 194 |
self.certs = args['validate_certs'] |
| 195 |
|
| 196 |
self.post_headers = { |
| 197 |
"Accept": "application/json", |
| 198 |
"Content-Type": "application/json" |
| 199 |
} |
| 200 |
self.amg_id, self.amg_obj = self.get_amg() |
| 201 |
|
| 202 |
def get_amg(self): |
| 203 |
endpoint = self.url + '/storage-systems/%s/async-mirrors' % self.ssid |
| 204 |
(rc, amg_objs) = request(endpoint, url_username=self.user, url_password=self.pwd, validate_certs=self.certs, |
| 205 |
headers=self.post_headers) |
| 206 |
try: |
| 207 |
amg_id = filter(lambda d: d['label'] == self.name, amg_objs)[0]['id'] |
| 208 |
amg_obj = filter(lambda d: d['label'] == self.name, amg_objs)[0] |
| 209 |
except IndexError: |
| 210 |
self.module.fail_json( |
| 211 |
msg="There is no async mirror group %s associated with storage array %s" % (self.name, self.ssid)) |
| 212 |
return amg_id, amg_obj |
| 213 |
|
| 214 |
@property |
| 215 |
def current_state(self): |
| 216 |
amg_id, amg_obj = self.get_amg() |
| 217 |
return amg_obj['syncActivity'] |
| 218 |
|
| 219 |
def run_sync_action(self): |
| 220 |
# If we get to this point we know that the states differ, and there is no 'err' state, |
| 221 |
# so no need to revalidate |
| 222 |
|
| 223 |
post_body = dict() |
| 224 |
if self.state == 'running': |
| 225 |
if self.current_state == 'idle': |
| 226 |
if self.delete_recovery_point: |
| 227 |
post_body.update(dict(deleteRecoveryPointIfNecessary=self.delete_recovery_point)) |
| 228 |
suffix = 'sync' |
| 229 |
else: |
| 230 |
# In a suspended state |
| 231 |
suffix = 'resume' |
| 232 |
else: |
| 233 |
suffix = 'suspend' |
| 234 |
|
| 235 |
endpoint = self.url + "/storage-systems/%s/async-mirrors/%s/%s" % (self.ssid, self.amg_id, suffix) |
| 236 |
|
| 237 |
(rc, resp) = request(endpoint, method='POST', url_username=self.user, url_password=self.pwd, |
| 238 |
validate_certs=self.certs, data=json.dumps(post_body), headers=self.post_headers, |
| 239 |
ignore_errors=True) |
| 240 |
|
| 241 |
if not str(rc).startswith('2'): |
| 242 |
self.module.fail_json(msg=str(resp['errorMessage'])) |
| 243 |
|
| 244 |
return resp |
| 245 |
|
| 246 |
def apply(self): |
| 247 |
state_map = dict( |
| 248 |
running=['active'], |
| 249 |
suspended=['userSuspended', 'internallySuspended', 'paused'], |
| 250 |
err=['unkown', '_UNDEFINED']) |
| 251 |
|
| 252 |
if self.current_state not in state_map[self.state]: |
| 253 |
if self.current_state in state_map['err']: |
| 254 |
self.module.fail_json( |
| 255 |
msg="The sync is a state of '%s', this requires manual intervention. " |
| 256 |
"Please investigate and try again" % self.current_state |
| 257 |
) |
| 258 |
else: |
| 259 |
self.amg_obj = self.run_sync_action() |
| 260 |
|
| 261 |
(ret, amg) = self.get_amg() |
| 262 |
self.module.exit_json(changed=False, **amg) |
| 263 |
|
| 264 |
|
| 265 |
def main(): |
| 266 |
sync = AMGsync() |
| 267 |
sync.apply() |
| 268 |
|
| 269 |
|
| 270 |
if __name__ == '__main__': |
| 271 |
main() |