| 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 |
DOCUMENTATION = """ |
| 15 |
--- |
| 16 |
module: netapp_e_volume_copy |
| 17 |
short_description: NetApp E-Series create volume copy pairs |
| 18 |
description: |
| 19 |
- Create and delete snapshots images on volume groups for NetApp E-series storage arrays. |
| 20 |
version_added: '2.2.0' |
| 21 |
author: Kevin Hulquest (@hulquest) |
| 22 |
extends_documentation_fragment: |
| 23 |
- netapp_eseries.santricity.santricity.netapp.eseries |
| 24 |
options: |
| 25 |
ssid: |
| 26 |
description: |
| 27 |
- Storage system identifier |
| 28 |
type: str |
| 29 |
default: '1' |
| 30 |
api_username: |
| 31 |
required: true |
| 32 |
description: |
| 33 |
- The username to authenticate with the SANtricity WebServices Proxy or embedded REST API. |
| 34 |
type: str |
| 35 |
api_password: |
| 36 |
required: true |
| 37 |
description: |
| 38 |
- The password to authenticate with the SANtricity WebServices Proxy or embedded REST API. |
| 39 |
type: str |
| 40 |
api_url: |
| 41 |
required: true |
| 42 |
description: |
| 43 |
- The url to the SANtricity WebServices Proxy or embedded REST API, for example C(https://prod-1.wahoo.acme.com/devmgr/v2). |
| 44 |
type: str |
| 45 |
validate_certs: |
| 46 |
required: false |
| 47 |
default: true |
| 48 |
type: bool |
| 49 |
description: |
| 50 |
- Should https certificates be validated? |
| 51 |
source_volume_id: |
| 52 |
description: |
| 53 |
- The id of the volume copy source. |
| 54 |
- If used, must be paired with destination_volume_id |
| 55 |
- Mutually exclusive with volume_copy_pair_id, and search_volume_id |
| 56 |
type: str |
| 57 |
destination_volume_id: |
| 58 |
description: |
| 59 |
- The id of the volume copy destination. |
| 60 |
- If used, must be paired with source_volume_id |
| 61 |
- Mutually exclusive with volume_copy_pair_id, and search_volume_id |
| 62 |
type: str |
| 63 |
volume_copy_pair_id: |
| 64 |
description: |
| 65 |
- The id of a given volume copy pair |
| 66 |
- Mutually exclusive with destination_volume_id, source_volume_id, and search_volume_id |
| 67 |
- Can use to delete or check presence of volume pairs |
| 68 |
- Must specify this or (destination_volume_id and source_volume_id) |
| 69 |
type: str |
| 70 |
state: |
| 71 |
description: |
| 72 |
- Whether the specified volume copy pair should exist or not. |
| 73 |
required: True |
| 74 |
choices: ['present', 'absent'] |
| 75 |
type: str |
| 76 |
create_copy_pair_if_does_not_exist: |
| 77 |
description: |
| 78 |
- Defines if a copy pair will be created if it does not exist. |
| 79 |
- If set to True destination_volume_id and source_volume_id are required. |
| 80 |
type: bool |
| 81 |
default: True |
| 82 |
start_stop_copy: |
| 83 |
description: |
| 84 |
- starts a re-copy or stops a copy in progress |
| 85 |
- "Note: If you stop the initial file copy before it it done the copy pair will be destroyed" |
| 86 |
- Requires volume_copy_pair_id |
| 87 |
type: str |
| 88 |
choices: ['start', 'stop'] |
| 89 |
search_volume_id: |
| 90 |
description: |
| 91 |
- Searches for all valid potential target and source volumes that could be used in a copy_pair |
| 92 |
- Mutually exclusive with volume_copy_pair_id, destination_volume_id and source_volume_id |
| 93 |
type: str |
| 94 |
copy_priority: |
| 95 |
description: |
| 96 |
- Copy priority level |
| 97 |
required: False |
| 98 |
default: 0 |
| 99 |
type: int |
| 100 |
onlineCopy: |
| 101 |
description: |
| 102 |
- Whether copy should be online |
| 103 |
required: False |
| 104 |
default: False |
| 105 |
type: bool |
| 106 |
targetWriteProtected: |
| 107 |
description: |
| 108 |
- Whether target should be write protected |
| 109 |
required: False |
| 110 |
default: True |
| 111 |
type: bool |
| 112 |
""" |
| 113 |
EXAMPLES = """ |
| 114 |
--- |
| 115 |
msg: |
| 116 |
description: Success message |
| 117 |
returned: success |
| 118 |
type: str |
| 119 |
sample: Json facts for the volume copy that was created. |
| 120 |
""" |
| 121 |
RETURN = """ |
| 122 |
msg: |
| 123 |
description: Success message |
| 124 |
returned: success |
| 125 |
type: str |
| 126 |
sample: Created Volume Copy Pair with ID |
| 127 |
""" |
| 128 |
|
| 129 |
import json |
| 130 |
|
| 131 |
from ansible.module_utils.basic import AnsibleModule |
| 132 |
from ansible.module_utils._text import to_native |
| 133 |
from ansible_collections.netapp_eseries.santricity.plugins.module_utils.netapp import request |
| 134 |
|
| 135 |
HEADERS = { |
| 136 |
"Content-Type": "application/json", |
| 137 |
"Accept": "application/json", |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
def find_volume_copy_pair_id_from_source_volume_id_and_destination_volume_id(params): |
| 142 |
get_status = 'storage-systems/%s/volume-copy-jobs' % params['ssid'] |
| 143 |
url = params['api_url'] + get_status |
| 144 |
|
| 145 |
(rc, resp) = request(url, method='GET', url_username=params['api_username'], |
| 146 |
url_password=params['api_password'], headers=HEADERS, |
| 147 |
validate_certs=params['validate_certs']) |
| 148 |
|
| 149 |
volume_copy_pair_id = None |
| 150 |
for potential_copy_pair in resp: |
| 151 |
if potential_copy_pair['sourceVolume'] == params['source_volume_id']: |
| 152 |
if potential_copy_pair['sourceVolume'] == params['source_volume_id']: |
| 153 |
volume_copy_pair_id = potential_copy_pair['id'] |
| 154 |
|
| 155 |
return volume_copy_pair_id |
| 156 |
|
| 157 |
|
| 158 |
def create_copy_pair(params): |
| 159 |
get_status = 'storage-systems/%s/volume-copy-jobs' % params['ssid'] |
| 160 |
url = params['api_url'] + get_status |
| 161 |
|
| 162 |
rData = { |
| 163 |
"sourceId": params['source_volume_id'], |
| 164 |
"targetId": params['destination_volume_id'] |
| 165 |
} |
| 166 |
|
| 167 |
(rc, resp) = request(url, data=json.dumps(rData), ignore_errors=True, method='POST', |
| 168 |
url_username=params['api_username'], url_password=params['api_password'], headers=HEADERS, |
| 169 |
validate_certs=params['validate_certs']) |
| 170 |
if rc != 200: |
| 171 |
return False, (rc, resp) |
| 172 |
else: |
| 173 |
return True, (rc, resp) |
| 174 |
|
| 175 |
|
| 176 |
def delete_copy_pair_by_copy_pair_id(params): |
| 177 |
get_status = 'storage-systems/%s/volume-copy-jobs/%s?retainRepositories=false' % ( |
| 178 |
params['ssid'], params['volume_copy_pair_id']) |
| 179 |
url = params['api_url'] + get_status |
| 180 |
|
| 181 |
(rc, resp) = request(url, ignore_errors=True, method='DELETE', |
| 182 |
url_username=params['api_username'], url_password=params['api_password'], headers=HEADERS, |
| 183 |
validate_certs=params['validate_certs']) |
| 184 |
if rc != 204: |
| 185 |
return False, (rc, resp) |
| 186 |
else: |
| 187 |
return True, (rc, resp) |
| 188 |
|
| 189 |
|
| 190 |
def find_volume_copy_pair_id_by_volume_copy_pair_id(params): |
| 191 |
get_status = 'storage-systems/%s/volume-copy-jobs/%s?retainRepositories=false' % ( |
| 192 |
params['ssid'], params['volume_copy_pair_id']) |
| 193 |
url = params['api_url'] + get_status |
| 194 |
|
| 195 |
(rc, resp) = request(url, ignore_errors=True, method='DELETE', |
| 196 |
url_username=params['api_username'], url_password=params['api_password'], headers=HEADERS, |
| 197 |
validate_certs=params['validate_certs']) |
| 198 |
if rc != 200: |
| 199 |
return False, (rc, resp) |
| 200 |
else: |
| 201 |
return True, (rc, resp) |
| 202 |
|
| 203 |
|
| 204 |
def start_stop_copy(params): |
| 205 |
get_status = 'storage-systems/%s/volume-copy-jobs-control/%s?control=%s' % ( |
| 206 |
params['ssid'], params['volume_copy_pair_id'], params['start_stop_copy']) |
| 207 |
url = params['api_url'] + get_status |
| 208 |
|
| 209 |
(response_code, response_data) = request(url, ignore_errors=True, method='POST', |
| 210 |
url_username=params['api_username'], url_password=params['api_password'], |
| 211 |
headers=HEADERS, |
| 212 |
validate_certs=params['validate_certs']) |
| 213 |
|
| 214 |
if response_code == 200: |
| 215 |
return True, response_data[0]['percentComplete'] |
| 216 |
else: |
| 217 |
return False, response_data |
| 218 |
|
| 219 |
|
| 220 |
def check_copy_status(params): |
| 221 |
get_status = 'storage-systems/%s/volume-copy-jobs-control/%s' % ( |
| 222 |
params['ssid'], params['volume_copy_pair_id']) |
| 223 |
url = params['api_url'] + get_status |
| 224 |
|
| 225 |
(response_code, response_data) = request(url, ignore_errors=True, method='GET', |
| 226 |
url_username=params['api_username'], url_password=params['api_password'], |
| 227 |
headers=HEADERS, |
| 228 |
validate_certs=params['validate_certs']) |
| 229 |
|
| 230 |
if response_code == 200: |
| 231 |
if response_data['percentComplete'] != -1: |
| 232 |
|
| 233 |
return True, response_data['percentComplete'] |
| 234 |
else: |
| 235 |
return False, response_data['percentComplete'] |
| 236 |
else: |
| 237 |
return False, response_data |
| 238 |
|
| 239 |
|
| 240 |
def find_valid_copy_pair_targets_and_sources(params): |
| 241 |
get_status = 'storage-systems/%s/volumes' % params['ssid'] |
| 242 |
url = params['api_url'] + get_status |
| 243 |
|
| 244 |
(response_code, response_data) = request(url, ignore_errors=True, method='GET', |
| 245 |
url_username=params['api_username'], url_password=params['api_password'], |
| 246 |
headers=HEADERS, |
| 247 |
validate_certs=params['validate_certs']) |
| 248 |
|
| 249 |
if response_code == 200: |
| 250 |
source_capacity = None |
| 251 |
candidates = [] |
| 252 |
for volume in response_data: |
| 253 |
if volume['id'] == params['search_volume_id']: |
| 254 |
source_capacity = volume['capacity'] |
| 255 |
else: |
| 256 |
candidates.append(volume) |
| 257 |
|
| 258 |
potential_sources = [] |
| 259 |
potential_targets = [] |
| 260 |
|
| 261 |
for volume in candidates: |
| 262 |
if volume['capacity'] > source_capacity: |
| 263 |
if volume['volumeCopyTarget'] is False: |
| 264 |
if volume['volumeCopySource'] is False: |
| 265 |
potential_targets.append(volume['id']) |
| 266 |
else: |
| 267 |
if volume['volumeCopyTarget'] is False: |
| 268 |
if volume['volumeCopySource'] is False: |
| 269 |
potential_sources.append(volume['id']) |
| 270 |
|
| 271 |
return potential_targets, potential_sources |
| 272 |
|
| 273 |
else: |
| 274 |
raise Exception("Response [%s]" % response_code) |
| 275 |
|
| 276 |
|
| 277 |
def main(): |
| 278 |
module = AnsibleModule(argument_spec=dict( |
| 279 |
source_volume_id=dict(type='str'), |
| 280 |
destination_volume_id=dict(type='str'), |
| 281 |
copy_priority=dict(required=False, default=0, type='int'), |
| 282 |
ssid=dict(type='str', default='1'), |
| 283 |
api_url=dict(required=True), |
| 284 |
api_username=dict(type='str', required=True), |
| 285 |
api_password=dict(type='str', required=True, no_log=True), |
| 286 |
validate_certs=dict(required=False, default=True, type='bool'), |
| 287 |
targetWriteProtected=dict(required=False, default=True, type='bool'), |
| 288 |
onlineCopy=dict(required=False, default=False, type='bool'), |
| 289 |
volume_copy_pair_id=dict(type='str'), |
| 290 |
state=dict(required=True, choices=['present', 'absent'], type='str'), |
| 291 |
create_copy_pair_if_does_not_exist=dict(required=False, default=True, type='bool'), |
| 292 |
start_stop_copy=dict(required=False, choices=['start', 'stop'], type='str'), |
| 293 |
search_volume_id=dict(type='str'), |
| 294 |
), |
| 295 |
mutually_exclusive=[['volume_copy_pair_id', 'destination_volume_id'], |
| 296 |
['volume_copy_pair_id', 'source_volume_id'], |
| 297 |
['volume_copy_pair_id', 'search_volume_id'], |
| 298 |
['search_volume_id', 'destination_volume_id'], |
| 299 |
['search_volume_id', 'source_volume_id'], |
| 300 |
], |
| 301 |
required_together=[['source_volume_id', 'destination_volume_id'], |
| 302 |
], |
| 303 |
required_if=[["create_copy_pair_if_does_not_exist", True, ['source_volume_id', 'destination_volume_id'], ], |
| 304 |
["start_stop_copy", 'stop', ['volume_copy_pair_id'], ], |
| 305 |
["start_stop_copy", 'start', ['volume_copy_pair_id'], ], |
| 306 |
] |
| 307 |
|
| 308 |
) |
| 309 |
params = module.params |
| 310 |
|
| 311 |
if not params['api_url'].endswith('/'): |
| 312 |
params['api_url'] += '/' |
| 313 |
|
| 314 |
# Check if we want to search |
| 315 |
if params['search_volume_id'] is not None: |
| 316 |
try: |
| 317 |
potential_targets, potential_sources = find_valid_copy_pair_targets_and_sources(params) |
| 318 |
except Exception as e: |
| 319 |
module.fail_json(msg="Failed to find valid copy pair candidates. Error [%s]" % to_native(e)) |
| 320 |
|
| 321 |
module.exit_json(changed=False, |
| 322 |
msg=' Valid source devices found: %s Valid target devices found: %s' % (len(potential_sources), len(potential_targets)), |
| 323 |
search_volume_id=params['search_volume_id'], |
| 324 |
valid_targets=potential_targets, |
| 325 |
valid_sources=potential_sources) |
| 326 |
|
| 327 |
# Check if we want to start or stop a copy operation |
| 328 |
if params['start_stop_copy'] == 'start' or params['start_stop_copy'] == 'stop': |
| 329 |
|
| 330 |
# Get the current status info |
| 331 |
currenty_running, status_info = check_copy_status(params) |
| 332 |
|
| 333 |
# If we want to start |
| 334 |
if params['start_stop_copy'] == 'start': |
| 335 |
|
| 336 |
# If we have already started |
| 337 |
if currenty_running is True: |
| 338 |
module.exit_json(changed=False, msg='Volume Copy Pair copy has started.', |
| 339 |
volume_copy_pair_id=params['volume_copy_pair_id'], percent_done=status_info) |
| 340 |
# If we need to start |
| 341 |
else: |
| 342 |
|
| 343 |
start_status, info = start_stop_copy(params) |
| 344 |
|
| 345 |
if start_status is True: |
| 346 |
module.exit_json(changed=True, msg='Volume Copy Pair copy has started.', |
| 347 |
volume_copy_pair_id=params['volume_copy_pair_id'], percent_done=info) |
| 348 |
else: |
| 349 |
module.fail_json(msg="Could not start volume copy pair Error: %s" % info) |
| 350 |
|
| 351 |
# If we want to stop |
| 352 |
else: |
| 353 |
# If it has already stopped |
| 354 |
if currenty_running is False: |
| 355 |
module.exit_json(changed=False, msg='Volume Copy Pair copy is stopped.', |
| 356 |
volume_copy_pair_id=params['volume_copy_pair_id']) |
| 357 |
|
| 358 |
# If we need to stop it |
| 359 |
else: |
| 360 |
start_status, info = start_stop_copy(params) |
| 361 |
|
| 362 |
if start_status is True: |
| 363 |
module.exit_json(changed=True, msg='Volume Copy Pair copy has been stopped.', |
| 364 |
volume_copy_pair_id=params['volume_copy_pair_id']) |
| 365 |
else: |
| 366 |
module.fail_json(msg="Could not stop volume copy pair Error: %s" % info) |
| 367 |
|
| 368 |
# If we want the copy pair to exist we do this stuff |
| 369 |
if params['state'] == 'present': |
| 370 |
|
| 371 |
# We need to check if it exists first |
| 372 |
if params['volume_copy_pair_id'] is None: |
| 373 |
params['volume_copy_pair_id'] = find_volume_copy_pair_id_from_source_volume_id_and_destination_volume_id( |
| 374 |
params) |
| 375 |
|
| 376 |
# If no volume copy pair is found we need need to make it. |
| 377 |
if params['volume_copy_pair_id'] is None: |
| 378 |
|
| 379 |
# In order to create we can not do so with just a volume_copy_pair_id |
| 380 |
|
| 381 |
copy_began_status, (rc, resp) = create_copy_pair(params) |
| 382 |
|
| 383 |
if copy_began_status is True: |
| 384 |
module.exit_json(changed=True, msg='Created Volume Copy Pair with ID: %s' % resp['id']) |
| 385 |
else: |
| 386 |
module.fail_json(msg="Could not create volume copy pair Code: %s Error: %s" % (rc, resp)) |
| 387 |
|
| 388 |
# If it does exist we do nothing |
| 389 |
else: |
| 390 |
# We verify that it exists |
| 391 |
exist_status, (exist_status_code, exist_status_data) = find_volume_copy_pair_id_by_volume_copy_pair_id( |
| 392 |
params) |
| 393 |
|
| 394 |
if exist_status: |
| 395 |
module.exit_json(changed=False, |
| 396 |
msg=' Volume Copy Pair with ID: %s exists' % params['volume_copy_pair_id']) |
| 397 |
else: |
| 398 |
if exist_status_code == 404: |
| 399 |
module.fail_json( |
| 400 |
msg=' Volume Copy Pair with ID: %s does not exist. Can not create without source_volume_id and destination_volume_id' % |
| 401 |
params['volume_copy_pair_id']) |
| 402 |
else: |
| 403 |
module.fail_json(msg="Could not find volume copy pair Code: %s Error: %s" % ( |
| 404 |
exist_status_code, exist_status_data)) |
| 405 |
|
| 406 |
module.fail_json(msg="Done") |
| 407 |
|
| 408 |
# If we want it to not exist we do this |
| 409 |
else: |
| 410 |
|
| 411 |
if params['volume_copy_pair_id'] is None: |
| 412 |
params['volume_copy_pair_id'] = find_volume_copy_pair_id_from_source_volume_id_and_destination_volume_id( |
| 413 |
params) |
| 414 |
|
| 415 |
# We delete it by the volume_copy_pair_id |
| 416 |
delete_status, (delete_status_code, delete_status_data) = delete_copy_pair_by_copy_pair_id(params) |
| 417 |
|
| 418 |
if delete_status is True: |
| 419 |
module.exit_json(changed=True, |
| 420 |
msg=' Volume Copy Pair with ID: %s was deleted' % params['volume_copy_pair_id']) |
| 421 |
else: |
| 422 |
if delete_status_code == 404: |
| 423 |
module.exit_json(changed=False, |
| 424 |
msg=' Volume Copy Pair with ID: %s does not exist' % params['volume_copy_pair_id']) |
| 425 |
else: |
| 426 |
module.fail_json(msg="Could not delete volume copy pair Code: %s Error: %s" % ( |
| 427 |
delete_status_code, delete_status_data)) |
| 428 |
|
| 429 |
|
| 430 |
if __name__ == '__main__': |
| 431 |
main() |