| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# (c) 2016, 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_role |
| 18 |
short_description: NetApp E-Series update the role of a storage array within an Asynchronous Mirror Group (AMG). |
| 19 |
description: |
| 20 |
- Update a storage array to become the primary or secondary instance in an asynchronous mirror group |
| 21 |
version_added: '2.2.0' |
| 22 |
author: Kevin Hulquest (@hulquest) |
| 23 |
options: |
| 24 |
api_username: |
| 25 |
required: true |
| 26 |
type: str |
| 27 |
description: |
| 28 |
- The username to authenticate with the SANtricity WebServices Proxy or embedded REST API. |
| 29 |
api_password: |
| 30 |
required: true |
| 31 |
type: str |
| 32 |
description: |
| 33 |
- The password to authenticate with the SANtricity WebServices Proxy or embedded REST API. |
| 34 |
api_url: |
| 35 |
required: true |
| 36 |
type: str |
| 37 |
description: |
| 38 |
- The url to the SANtricity WebServices Proxy or embedded REST API. |
| 39 |
validate_certs: |
| 40 |
required: false |
| 41 |
default: true |
| 42 |
description: |
| 43 |
- Should https certificates be validated? |
| 44 |
type: bool |
| 45 |
ssid: |
| 46 |
description: |
| 47 |
- The ID of the primary storage array for the async mirror action |
| 48 |
required: yes |
| 49 |
type: str |
| 50 |
name: |
| 51 |
description: |
| 52 |
- Name of the role |
| 53 |
required: yes |
| 54 |
type: str |
| 55 |
role: |
| 56 |
description: |
| 57 |
- Whether the array should be the primary or secondary array for the AMG |
| 58 |
required: yes |
| 59 |
type: str |
| 60 |
choices: ['primary', 'secondary'] |
| 61 |
noSync: |
| 62 |
description: |
| 63 |
- Whether to avoid synchronization prior to role reversal |
| 64 |
required: no |
| 65 |
default: no |
| 66 |
type: bool |
| 67 |
force: |
| 68 |
description: |
| 69 |
- Whether to force the role reversal regardless of the online-state of the primary |
| 70 |
required: no |
| 71 |
default: no |
| 72 |
type: bool |
| 73 |
""" |
| 74 |
|
| 75 |
EXAMPLES = """ |
| 76 |
- name: Update the role of a storage array |
| 77 |
netapp_e_amg_role: |
| 78 |
name: updating amg role |
| 79 |
role: primary |
| 80 |
ssid: "{{ ssid }}" |
| 81 |
api_url: "{{ netapp_api_url }}" |
| 82 |
api_username: "{{ netapp_api_username }}" |
| 83 |
api_password: "{{ netapp_api_password }}" |
| 84 |
validate_certs: "{{ netapp_api_validate_certs }}" |
| 85 |
""" |
| 86 |
|
| 87 |
RETURN = """ |
| 88 |
msg: |
| 89 |
description: Failure message |
| 90 |
returned: failure |
| 91 |
type: str |
| 92 |
sample: "No Async Mirror Group with the name." |
| 93 |
""" |
| 94 |
import json |
| 95 |
import traceback |
| 96 |
|
| 97 |
from ansible.module_utils.api import basic_auth_argument_spec |
| 98 |
from ansible.module_utils.basic import AnsibleModule |
| 99 |
from ansible.module_utils.six.moves.urllib.error import HTTPError |
| 100 |
from ansible.module_utils._text import to_native |
| 101 |
from ansible.module_utils.urls import open_url |
| 102 |
|
| 103 |
|
| 104 |
HEADERS = { |
| 105 |
"Content-Type": "application/json", |
| 106 |
"Accept": "application/json", |
| 107 |
} |
| 108 |
|
| 109 |
|
| 110 |
def request(url, data=None, headers=None, method='GET', use_proxy=True, |
| 111 |
force=False, last_mod_time=None, timeout=10, validate_certs=True, |
| 112 |
url_username=None, url_password=None, http_agent=None, force_basic_auth=True, ignore_errors=False): |
| 113 |
try: |
| 114 |
r = open_url(url=url, data=data, headers=headers, method=method, use_proxy=use_proxy, |
| 115 |
force=force, last_mod_time=last_mod_time, timeout=timeout, validate_certs=validate_certs, |
| 116 |
url_username=url_username, url_password=url_password, http_agent=http_agent, |
| 117 |
force_basic_auth=force_basic_auth) |
| 118 |
except HTTPError as e: |
| 119 |
r = e.fp |
| 120 |
|
| 121 |
try: |
| 122 |
raw_data = r.read() |
| 123 |
if raw_data: |
| 124 |
data = json.loads(raw_data) |
| 125 |
else: |
| 126 |
raw_data = None |
| 127 |
except Exception: |
| 128 |
if ignore_errors: |
| 129 |
pass |
| 130 |
else: |
| 131 |
raise Exception(raw_data) |
| 132 |
|
| 133 |
resp_code = r.getcode() |
| 134 |
|
| 135 |
if resp_code >= 400 and not ignore_errors: |
| 136 |
raise Exception(resp_code, data) |
| 137 |
else: |
| 138 |
return resp_code, data |
| 139 |
|
| 140 |
|
| 141 |
def has_match(module, ssid, api_url, api_pwd, api_usr, body, name): |
| 142 |
amg_exists = False |
| 143 |
has_desired_role = False |
| 144 |
amg_id = None |
| 145 |
amg_data = None |
| 146 |
get_amgs = 'storage-systems/%s/async-mirrors' % ssid |
| 147 |
url = api_url + get_amgs |
| 148 |
try: |
| 149 |
amg_rc, amgs = request(url, url_username=api_usr, url_password=api_pwd, |
| 150 |
headers=HEADERS) |
| 151 |
except Exception: |
| 152 |
module.fail_json(msg="Failed to find AMGs on storage array. Id [%s]" % (ssid)) |
| 153 |
|
| 154 |
for amg in amgs: |
| 155 |
if amg['label'] == name: |
| 156 |
amg_exists = True |
| 157 |
amg_id = amg['id'] |
| 158 |
amg_data = amg |
| 159 |
if amg['localRole'] == body.get('role'): |
| 160 |
has_desired_role = True |
| 161 |
|
| 162 |
return amg_exists, has_desired_role, amg_id, amg_data |
| 163 |
|
| 164 |
|
| 165 |
def update_amg(module, ssid, api_url, api_usr, api_pwd, body, amg_id): |
| 166 |
endpoint = 'storage-systems/%s/async-mirrors/%s/role' % (ssid, amg_id) |
| 167 |
url = api_url + endpoint |
| 168 |
post_data = json.dumps(body) |
| 169 |
try: |
| 170 |
request(url, data=post_data, method='POST', url_username=api_usr, |
| 171 |
url_password=api_pwd, headers=HEADERS) |
| 172 |
except Exception as e: |
| 173 |
module.fail_json( |
| 174 |
msg="Failed to change role of AMG. Id [%s]. AMG Id [%s]. Error [%s]" % (ssid, amg_id, to_native(e)), |
| 175 |
exception=traceback.format_exc()) |
| 176 |
|
| 177 |
status_endpoint = 'storage-systems/%s/async-mirrors/%s' % (ssid, amg_id) |
| 178 |
status_url = api_url + status_endpoint |
| 179 |
try: |
| 180 |
rc, status = request(status_url, method='GET', url_username=api_usr, |
| 181 |
url_password=api_pwd, headers=HEADERS) |
| 182 |
except Exception as e: |
| 183 |
module.fail_json( |
| 184 |
msg="Failed to check status of AMG after role reversal. " |
| 185 |
"Id [%s]. AMG Id [%s]. Error [%s]" % (ssid, amg_id, to_native(e)), |
| 186 |
exception=traceback.format_exc()) |
| 187 |
|
| 188 |
# Here we wait for the role reversal to complete |
| 189 |
if 'roleChangeProgress' in status: |
| 190 |
while status['roleChangeProgress'] != "none": |
| 191 |
try: |
| 192 |
rc, status = request(status_url, method='GET', |
| 193 |
url_username=api_usr, url_password=api_pwd, headers=HEADERS) |
| 194 |
except Exception as e: |
| 195 |
module.fail_json( |
| 196 |
msg="Failed to check status of AMG after role reversal. " |
| 197 |
"Id [%s]. AMG Id [%s]. Error [%s]" % (ssid, amg_id, to_native(e)), |
| 198 |
exception=traceback.format_exc()) |
| 199 |
return status |
| 200 |
|
| 201 |
|
| 202 |
def main(): |
| 203 |
argument_spec = basic_auth_argument_spec() |
| 204 |
argument_spec.update(dict( |
| 205 |
api_username=dict(required=True, type='str'), |
| 206 |
api_password=dict(required=True, type='str', no_log=True), |
| 207 |
name=dict(required=True, type='str'), |
| 208 |
role=dict(required=True, choices=['primary', 'secondary']), |
| 209 |
noSync=dict(required=False, type='bool', default=False), |
| 210 |
force=dict(required=False, type='bool', default=False), |
| 211 |
ssid=dict(required=True, type='str'), |
| 212 |
api_url=dict(required=True), |
| 213 |
)) |
| 214 |
|
| 215 |
module = AnsibleModule(argument_spec=argument_spec) |
| 216 |
|
| 217 |
p = module.params |
| 218 |
|
| 219 |
ssid = p.pop('ssid') |
| 220 |
api_url = p.pop('api_url') |
| 221 |
user = p.pop('api_username') |
| 222 |
pwd = p.pop('api_password') |
| 223 |
name = p.pop('name') |
| 224 |
|
| 225 |
if not api_url.endswith('/'): |
| 226 |
api_url += '/' |
| 227 |
|
| 228 |
agm_exists, has_desired_role, async_id, amg_data = has_match(module, ssid, api_url, pwd, user, p, name) |
| 229 |
|
| 230 |
if not agm_exists: |
| 231 |
module.fail_json(msg="No Async Mirror Group with the name: '%s' was found" % name) |
| 232 |
elif has_desired_role: |
| 233 |
module.exit_json(changed=False, **amg_data) |
| 234 |
|
| 235 |
else: |
| 236 |
amg_data = update_amg(module, ssid, api_url, user, pwd, p, async_id) |
| 237 |
if amg_data: |
| 238 |
module.exit_json(changed=True, **amg_data) |
| 239 |
else: |
| 240 |
module.exit_json(changed=True, msg="AMG role changed.") |
| 241 |
|
| 242 |
|
| 243 |
if __name__ == '__main__': |
| 244 |
main() |