| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
""" Ansible module for managing Snapshots on Unity""" |
| 6 |
|
| 7 |
from __future__ import (absolute_import, division, print_function) |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
DOCUMENTATION = r''' |
| 12 |
--- |
| 13 |
module: snapshot |
| 14 |
short_description: Manage snapshots on the Unity storage system |
| 15 |
description: |
| 16 |
- Managing snapshots on the Unity storage system includes create snapshot, |
| 17 |
delete snapshot, update snapshot, get snapshot, map host and unmap host. |
| 18 |
version_added: '1.1.0' |
| 19 |
|
| 20 |
extends_documentation_fragment: |
| 21 |
- dellemc.unity.unity |
| 22 |
|
| 23 |
author: |
| 24 |
- P Srinivas Rao (@srinivas-rao5) <[email protected]> |
| 25 |
options: |
| 26 |
snapshot_name: |
| 27 |
description: |
| 28 |
- The name of the snapshot. |
| 29 |
- Mandatory parameter for creating a snapshot. |
| 30 |
- For all other operations either I(snapshot_name) or I(snapshot_id) is |
| 31 |
required. |
| 32 |
type: str |
| 33 |
vol_name: |
| 34 |
description: |
| 35 |
- The name of the volume for which snapshot is created. |
| 36 |
- For creation of a snapshot either I(vol_name) or I(cg_name) is required. |
| 37 |
- Not required for other operations. |
| 38 |
type: str |
| 39 |
cg_name: |
| 40 |
description: |
| 41 |
- The name of the Consistency Group for which snapshot is created. |
| 42 |
- For creation of a snapshot either I(vol_name) or I(cg_name) is required. |
| 43 |
- Not required for other operations. |
| 44 |
type: str |
| 45 |
snapshot_id: |
| 46 |
description: |
| 47 |
- The id of the snapshot. |
| 48 |
- For all operations other than creation either I(snapshot_name) or |
| 49 |
I(snapshot_id) is required. |
| 50 |
type: str |
| 51 |
auto_delete: |
| 52 |
description: |
| 53 |
- This option specifies whether the snapshot is auto deleted or not. |
| 54 |
- If set to C(true), snapshot will expire based on the pool auto deletion |
| 55 |
policy. |
| 56 |
- If set to (false), snapshot will not be auto deleted |
| 57 |
based on the pool auto deletion policy. |
| 58 |
- Option I(auto_delete) can not be set to C(true), if I(expiry_time) is specified. |
| 59 |
- If during creation neither I(auto_delete) nor I(expiry_time) is mentioned |
| 60 |
then snapshot will be created keeping I(auto_delete) as C(true). |
| 61 |
- Once the I(expiry_time) is set then snapshot cannot be assigned |
| 62 |
to the auto delete policy. |
| 63 |
type: bool |
| 64 |
expiry_time: |
| 65 |
description: |
| 66 |
- This option is for specifying the date and time after which the |
| 67 |
snapshot will expire. |
| 68 |
- The time is to be mentioned in UTC timezone. |
| 69 |
- The format is "MM/DD/YYYY HH:MM". Year must be in 4 digits. |
| 70 |
type: str |
| 71 |
description: |
| 72 |
description: |
| 73 |
- The additional information about the snapshot can be provided using |
| 74 |
this option. |
| 75 |
type: str |
| 76 |
new_snapshot_name: |
| 77 |
description: |
| 78 |
- New name for the snapshot. |
| 79 |
type: str |
| 80 |
state: |
| 81 |
description: |
| 82 |
- The I(state) option is used to mention the existence of |
| 83 |
the snapshot. |
| 84 |
type: str |
| 85 |
required: true |
| 86 |
choices: [ 'absent', 'present' ] |
| 87 |
host_name: |
| 88 |
description: |
| 89 |
- The name of the host. |
| 90 |
- Either I(host_name) or I(host_id) is required to map or unmap a snapshot from |
| 91 |
a host. |
| 92 |
- Snapshot can be attached to multiple hosts. |
| 93 |
type: str |
| 94 |
host_id: |
| 95 |
description: |
| 96 |
- The id of the host. |
| 97 |
- Either I(host_name) or I(host_id) is required to map or unmap a snapshot from |
| 98 |
a host. |
| 99 |
- Snapshot can be attached to multiple hosts. |
| 100 |
type: str |
| 101 |
host_state: |
| 102 |
description: |
| 103 |
- The I(host_state) option is used to mention the existence of the host |
| 104 |
for snapshot. |
| 105 |
- It is required when a snapshot is mapped or unmapped from host. |
| 106 |
type: str |
| 107 |
choices: ['mapped', 'unmapped'] |
| 108 |
|
| 109 |
notes: |
| 110 |
- The I(check_mode) is not supported. |
| 111 |
''' |
| 112 |
|
| 113 |
EXAMPLES = r''' |
| 114 |
- name: Create a Snapshot for a CG |
| 115 |
snapshot: |
| 116 |
unispherehost: "{{unispherehost}}" |
| 117 |
username: "{{username}}" |
| 118 |
password: "{{password}}" |
| 119 |
validate_certs: "{{validate_certs}}" |
| 120 |
port: "{{port}}" |
| 121 |
cg_name: "{{cg_name}}" |
| 122 |
snapshot_name: "{{cg_snapshot_name}}" |
| 123 |
description: "{{description}}" |
| 124 |
auto_delete: false |
| 125 |
state: "present" |
| 126 |
|
| 127 |
- name: Create a Snapshot for a volume with Host attached |
| 128 |
snapshot: |
| 129 |
unispherehost: "{{unispherehost}}" |
| 130 |
username: "{{username}}" |
| 131 |
password: "{{password}}" |
| 132 |
validate_certs: "{{validate_certs}}" |
| 133 |
port: "{{port}}" |
| 134 |
vol_name: "{{vol_name}}" |
| 135 |
snapshot_name: "{{vol_snapshot_name}}" |
| 136 |
description: "{{description}}" |
| 137 |
expiry_time: "04/15/2025 16:30" |
| 138 |
host_name: "{{host_name}}" |
| 139 |
host_state: "mapped" |
| 140 |
state: "present" |
| 141 |
|
| 142 |
- name: Unmap a host for a Snapshot |
| 143 |
snapshot: |
| 144 |
unispherehost: "{{unispherehost}}" |
| 145 |
username: "{{username}}" |
| 146 |
password: "{{password}}" |
| 147 |
validate_certs: "{{validate_certs}}" |
| 148 |
port: "{{port}}" |
| 149 |
snapshot_name: "{{vol_snapshot_name}}" |
| 150 |
host_name: "{{host_name}}" |
| 151 |
host_state: "unmapped" |
| 152 |
state: "present" |
| 153 |
|
| 154 |
- name: Map snapshot to a host |
| 155 |
snapshot: |
| 156 |
unispherehost: "{{unispherehost}}" |
| 157 |
username: "{{username}}" |
| 158 |
password: "{{password}}" |
| 159 |
validate_certs: "{{validate_certs}}" |
| 160 |
port: "{{port}}" |
| 161 |
snapshot_name: "{{vol_snapshot_name}}" |
| 162 |
host_name: "{{host_name}}" |
| 163 |
host_state: "mapped" |
| 164 |
state: "present" |
| 165 |
|
| 166 |
- name: Update attributes of a Snapshot for a volume |
| 167 |
snapshot: |
| 168 |
unispherehost: "{{unispherehost}}" |
| 169 |
username: "{{username}}" |
| 170 |
password: "{{password}}" |
| 171 |
validate_certs: "{{validate_certs}}" |
| 172 |
snapshot_name: "{{vol_snapshot_name}}" |
| 173 |
new_snapshot_name: "{{new_snapshot_name}}" |
| 174 |
description: "{{new_description}}" |
| 175 |
host_name: "{{host_name}}" |
| 176 |
host_state: "unmapped" |
| 177 |
state: "present" |
| 178 |
|
| 179 |
- name: Delete Snapshot of CG |
| 180 |
snapshot: |
| 181 |
unispherehost: "{{unispherehost}}" |
| 182 |
username: "{{username}}" |
| 183 |
password: "{{password}}" |
| 184 |
validate_certs: "{{validate_certs}}" |
| 185 |
snapshot_name: "{{cg_snapshot_name}}" |
| 186 |
state: "absent" |
| 187 |
''' |
| 188 |
|
| 189 |
RETURN = r''' |
| 190 |
changed: |
| 191 |
description: Whether or not the resource has changed. |
| 192 |
returned: always |
| 193 |
type: bool |
| 194 |
sample: True |
| 195 |
|
| 196 |
snapshot_details: |
| 197 |
description: Details of the snapshot. |
| 198 |
returned: When snapshot exists |
| 199 |
type: dict |
| 200 |
contains: |
| 201 |
is_auto_delete: |
| 202 |
description: Additional information mentioned for snapshot. |
| 203 |
type: str |
| 204 |
expiration_time: |
| 205 |
description: Date and time after which the snapshot |
| 206 |
will expire. |
| 207 |
type: str |
| 208 |
hosts_list: |
| 209 |
description: Contains the name and id of the associated |
| 210 |
hosts. |
| 211 |
type: dict |
| 212 |
id: |
| 213 |
description: Unique identifier of the snapshot instance. |
| 214 |
type: str |
| 215 |
name: |
| 216 |
description: The name of the snapshot. |
| 217 |
type: str |
| 218 |
storage_resource_name: |
| 219 |
description: Name of the storage resource for which the |
| 220 |
snapshot exists. |
| 221 |
type: str |
| 222 |
storage_resource_id: |
| 223 |
description: Id of the storage resource for which the snapshot |
| 224 |
exists. |
| 225 |
type: str |
| 226 |
sample: { |
| 227 |
"access_type": null, |
| 228 |
"attached_wwn": null, |
| 229 |
"creation_time": "2022-10-21 08:20:25.803000+00:00", |
| 230 |
"creator_schedule": null, |
| 231 |
"creator_type": "SnapCreatorTypeEnum.USER_CUSTOM", |
| 232 |
"creator_user": { |
| 233 |
"id": "user_admin" |
| 234 |
}, |
| 235 |
"description": "Test snap creation", |
| 236 |
"existed": true, |
| 237 |
"expiration_time": null, |
| 238 |
"hash": 8756689457056, |
| 239 |
"hosts_list": [], |
| 240 |
"id": "85899355291", |
| 241 |
"io_limit_policy": null, |
| 242 |
"is_auto_delete": true, |
| 243 |
"is_modifiable": false, |
| 244 |
"is_modified": false, |
| 245 |
"is_read_only": true, |
| 246 |
"is_system_snap": false, |
| 247 |
"last_writable_time": null, |
| 248 |
"lun": null, |
| 249 |
"name": "ansible_snap_cg_1_1", |
| 250 |
"parent_snap": null, |
| 251 |
"size": null, |
| 252 |
"snap_group": null, |
| 253 |
"state": "SnapStateEnum.READY", |
| 254 |
"storage_resource_id": "res_95", |
| 255 |
"storage_resource_name": "CG_ansible_test_2_new" |
| 256 |
} |
| 257 |
''' |
| 258 |
|
| 259 |
from ansible.module_utils.basic import AnsibleModule |
| 260 |
from ansible_collections.dellemc.unity.plugins.module_utils.storage.dell \ |
| 261 |
import utils |
| 262 |
from datetime import datetime |
| 263 |
|
| 264 |
LOG = utils.get_logger('snapshot') |
| 265 |
|
| 266 |
application_type = "Ansible/1.7.1" |
| 267 |
|
| 268 |
|
| 269 |
class Snapshot(object): |
| 270 |
"""Class with Snapshot operations""" |
| 271 |
|
| 272 |
def __init__(self): |
| 273 |
""" Define all parameters required by this module""" |
| 274 |
|
| 275 |
self.module_params = utils.get_unity_management_host_parameters() |
| 276 |
self.module_params.update(get_snapshot_parameters()) |
| 277 |
|
| 278 |
mutually_exclusive = [['snapshot_name', 'snapshot_id'], |
| 279 |
['vol_name', 'cg_name'], |
| 280 |
['host_name', 'host_id']] |
| 281 |
|
| 282 |
required_one_of = [['snapshot_name', 'snapshot_id']] |
| 283 |
|
| 284 |
self.module = AnsibleModule(argument_spec=self.module_params, |
| 285 |
supports_check_mode=False, |
| 286 |
mutually_exclusive=mutually_exclusive, |
| 287 |
required_one_of=required_one_of) |
| 288 |
utils.ensure_required_libs(self.module) |
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
self.result = {"changed": False, |
| 293 |
'snapshot_details': {}} |
| 294 |
|
| 295 |
self.unity_conn = utils.get_unity_unisphere_connection( |
| 296 |
self.module.params, application_type) |
| 297 |
self.snap_obj = utils.snap.UnitySnap(self.unity_conn) |
| 298 |
LOG.info('Connection established with the Unity Array') |
| 299 |
|
| 300 |
def validate_expiry_time(self, expiry_time): |
| 301 |
"""Validates the specified expiry_time""" |
| 302 |
try: |
| 303 |
datetime.strptime(expiry_time, '%m/%d/%Y %H:%M') |
| 304 |
except ValueError: |
| 305 |
error_msg = "expiry_time not in MM/DD/YYYY HH:MM format" |
| 306 |
LOG.error(error_msg) |
| 307 |
self.module.fail_json(msg=error_msg) |
| 308 |
|
| 309 |
def to_update(self, snapshot, new_name=None, description=None, |
| 310 |
auto_del=None, expiry_time=None, host=None, |
| 311 |
host_state=None): |
| 312 |
"""Determines whether to update the snapshot or not""" |
| 313 |
|
| 314 |
|
| 315 |
if expiry_time and snapshot.is_auto_delete and \ |
| 316 |
(auto_del is None or auto_del): |
| 317 |
self.module.fail_json(msg="expiry_time can be assigned " |
| 318 |
"when auto delete is False") |
| 319 |
if auto_del and snapshot.expiration_time: |
| 320 |
error_msg = "expiry_time for snapshot is set." \ |
| 321 |
" Once it is set then snapshot cannot" \ |
| 322 |
" be assigned to auto_delete policy" |
| 323 |
self.module.fail_json(msg=error_msg) |
| 324 |
if new_name and new_name != snapshot.name: |
| 325 |
return True |
| 326 |
if description and description != snapshot.description: |
| 327 |
return True |
| 328 |
if auto_del and auto_del != snapshot.is_auto_delete: |
| 329 |
return True |
| 330 |
if to_update_expiry_time(snapshot, expiry_time): |
| 331 |
return True |
| 332 |
if host and to_update_host_list(snapshot, host, host_state): |
| 333 |
return True |
| 334 |
return False |
| 335 |
|
| 336 |
def update_snapshot(self, snapshot, new_name=None, |
| 337 |
description=None, auto_del=None, expiry_time=None, |
| 338 |
host_access_list=None): |
| 339 |
try: |
| 340 |
duration = None |
| 341 |
if expiry_time: |
| 342 |
duration = convert_timestamp_to_sec( |
| 343 |
expiry_time, self.unity_conn.system_time) |
| 344 |
if duration and duration <= 0: |
| 345 |
self.module.fail_json(msg="expiry_time should be after" |
| 346 |
" the current system time") |
| 347 |
snapshot.modify(name=new_name, retentionDuration=duration, |
| 348 |
isAutoDelete=auto_del, description=description, |
| 349 |
hostAccess=host_access_list) |
| 350 |
snapshot.update() |
| 351 |
except Exception as e: |
| 352 |
error_msg = "Failed to modify snapshot" \ |
| 353 |
" [name: %s , id: %s] with error %s"\ |
| 354 |
% (snapshot.name, snapshot.id, str(e)) |
| 355 |
LOG.error(error_msg) |
| 356 |
self.module.fail_json(msg=error_msg) |
| 357 |
|
| 358 |
def create_snapshot(self, snap_name, storage_id, description=None, |
| 359 |
auto_del=None, expiry_time=None): |
| 360 |
try: |
| 361 |
duration = None |
| 362 |
if expiry_time: |
| 363 |
duration = convert_timestamp_to_sec( |
| 364 |
expiry_time, self.unity_conn.system_time) |
| 365 |
if duration <= 0: |
| 366 |
self.module.fail_json(msg="expiry_time should be after" |
| 367 |
" the current system time") |
| 368 |
snapshot = self.snap_obj.create( |
| 369 |
cli=self.unity_conn._cli, storage_resource=storage_id, |
| 370 |
name=snap_name, description=description, |
| 371 |
is_auto_delete=auto_del, retention_duration=duration) |
| 372 |
return snapshot |
| 373 |
except Exception as e: |
| 374 |
error_msg = "Failed to create snapshot" \ |
| 375 |
" %s with error %s" % (snap_name, str(e)) |
| 376 |
LOG.error(error_msg) |
| 377 |
self.module.fail_json(msg=error_msg) |
| 378 |
|
| 379 |
def delete_snapshot(self, snapshot): |
| 380 |
try: |
| 381 |
if not bool(get_hosts_dict(snapshot)): |
| 382 |
snapshot.detach_from(None) |
| 383 |
snapshot.delete() |
| 384 |
else: |
| 385 |
snapshot.delete() |
| 386 |
return None |
| 387 |
|
| 388 |
except Exception as e: |
| 389 |
error_msg = "Failed to delete snapshot" \ |
| 390 |
" [name: %s, id: %s] with error %s" \ |
| 391 |
% (snapshot.name, snapshot.id, str(e)) |
| 392 |
LOG.error(error_msg) |
| 393 |
self.module.fail_json(msg=error_msg) |
| 394 |
|
| 395 |
def get_snapshot_obj(self, name=None, id=None): |
| 396 |
snapshot = id if id else name |
| 397 |
msg = "Failed to get details of snapshot %s with error %s " |
| 398 |
try: |
| 399 |
return self.unity_conn.get_snap(name=name, _id=id) |
| 400 |
|
| 401 |
except utils.HttpError as e: |
| 402 |
if e.http_status == 401: |
| 403 |
cred_err = "Incorrect username or password , {0}".format( |
| 404 |
e.message) |
| 405 |
self.module.fail_json(msg=cred_err) |
| 406 |
else: |
| 407 |
err_msg = msg % (snapshot, str(e)) |
| 408 |
LOG.error(err_msg) |
| 409 |
self.module.fail_json(msg=err_msg) |
| 410 |
|
| 411 |
except utils.UnityResourceNotFoundError as e: |
| 412 |
err_msg = msg % (snapshot, str(e)) |
| 413 |
LOG.error(err_msg) |
| 414 |
return None |
| 415 |
|
| 416 |
except Exception as e: |
| 417 |
err_msg = msg % (snapshot, str(e)) |
| 418 |
LOG.error(err_msg) |
| 419 |
self.module.fail_json(msg=err_msg) |
| 420 |
|
| 421 |
def get_volume_obj(self, name): |
| 422 |
try: |
| 423 |
return self.unity_conn.get_lun(name=name) |
| 424 |
except Exception as e: |
| 425 |
error_msg = "Failed to get volume %s with error %s"\ |
| 426 |
% (name, str(e)) |
| 427 |
LOG.error(error_msg) |
| 428 |
self.module.fail_json(msg=error_msg) |
| 429 |
|
| 430 |
def get_cg_obj(self, name): |
| 431 |
try: |
| 432 |
return self.unity_conn.get_cg(name=name) |
| 433 |
except Exception as e: |
| 434 |
error_msg = "Failed to get cg %s with error %s" % (name, str(e)) |
| 435 |
LOG.error(error_msg) |
| 436 |
self.module.fail_json(msg=error_msg) |
| 437 |
|
| 438 |
def get_host_obj(self, name=None, id=None): |
| 439 |
""" Get the Host object""" |
| 440 |
try: |
| 441 |
return self.unity_conn.get_host(name=name, _id=id) |
| 442 |
except Exception as e: |
| 443 |
host = id if id else name |
| 444 |
error_msg = "Failed to get host %s with error %s"\ |
| 445 |
% (host, str(e)) |
| 446 |
LOG.error(error_msg) |
| 447 |
self.module.fail_json(msg=error_msg) |
| 448 |
|
| 449 |
def attach_to_snap(self, snapshot, host): |
| 450 |
""" Attach snapshot to a host """ |
| 451 |
try: |
| 452 |
if not get_hosts_dict(snapshot): |
| 453 |
snapshot.detach_from(None) |
| 454 |
snapshot.attach_to(host) |
| 455 |
snapshot.update() |
| 456 |
except Exception as e: |
| 457 |
error_msg = "Failed to attach snapshot [name: %s, id: %s]" \ |
| 458 |
" to host [%s, %s] with error %s"\ |
| 459 |
% (snapshot.name, snapshot.id, |
| 460 |
host.name, host.id, str(e)) |
| 461 |
LOG.error(error_msg) |
| 462 |
self.module.fail_json(msg=error_msg) |
| 463 |
|
| 464 |
def perform_module_operation(self): |
| 465 |
""" |
| 466 |
Perform different actions on snapshot module based on parameters |
| 467 |
chosen in playbook |
| 468 |
""" |
| 469 |
snapshot_name = self.module.params['snapshot_name'] |
| 470 |
snapshot_id = self.module.params['snapshot_id'] |
| 471 |
vol_name = self.module.params['vol_name'] |
| 472 |
cg_name = self.module.params['cg_name'] |
| 473 |
auto_delete = self.module.params['auto_delete'] |
| 474 |
expiry_time = self.module.params['expiry_time'] |
| 475 |
description = self.module.params['description'] |
| 476 |
new_snapshot_name = self.module.params['new_snapshot_name'] |
| 477 |
host_name = self.module.params['host_name'] |
| 478 |
host_id = self.module.params['host_id'] |
| 479 |
host_state = self.module.params['host_state'] |
| 480 |
state = self.module.params['state'] |
| 481 |
host = None |
| 482 |
storage_resource = None |
| 483 |
changed = False |
| 484 |
|
| 485 |
LOG.info("Getting Snapshot details") |
| 486 |
snapshot = self.get_snapshot_obj(name=snapshot_name, id=snapshot_id) |
| 487 |
|
| 488 |
if snapshot and not snapshot.existed: |
| 489 |
snapshot = None |
| 490 |
msg = "snapshot details: %s" % str(snapshot) |
| 491 |
LOG.info(msg) |
| 492 |
|
| 493 |
|
| 494 |
if vol_name is not None: |
| 495 |
if vol_name == "" or vol_name.isspace(): |
| 496 |
self.module.fail_json(msg="Invalid vol_name given, Please" |
| 497 |
" provide a valid vol_name") |
| 498 |
storage_resource = self.get_volume_obj(name=vol_name) |
| 499 |
|
| 500 |
|
| 501 |
if cg_name is not None: |
| 502 |
if cg_name == "" or cg_name.isspace(): |
| 503 |
self.module.fail_json(msg="Invalid cg_name given, Please" |
| 504 |
" provide a valid cg_name") |
| 505 |
storage_resource = self.get_cg_obj(name=cg_name) |
| 506 |
|
| 507 |
|
| 508 |
if host_id or host_name: |
| 509 |
if cg_name: |
| 510 |
self.module.fail_json(msg="Mapping CG snapshot to host" |
| 511 |
" is not supported.") |
| 512 |
host = self.get_host_obj(name=host_name, id=host_id) |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
if (host and not host_state) or (not host and host_state): |
| 517 |
self.module.fail_json( |
| 518 |
msg="Either host_name or host_id along with host_state " |
| 519 |
"is required to map or unmap a snapshot from a host") |
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
if snapshot and storage_resource and\ |
| 524 |
(snapshot.storage_resource.id != storage_resource.id): |
| 525 |
self.module.fail_json( |
| 526 |
msg="Snapshot %s is of %s storage resource. Cannot create new" |
| 527 |
" snapshot with same name for %s storage resource" |
| 528 |
% (snapshot.name, snapshot.storage_resource.name, |
| 529 |
storage_resource.name)) |
| 530 |
|
| 531 |
|
| 532 |
if expiry_time is not None and \ |
| 533 |
(expiry_time == "" or expiry_time.isspace()): |
| 534 |
self.module.fail_json(msg="Please provide valid expiry_time," |
| 535 |
" empty expiry_time given") |
| 536 |
|
| 537 |
if expiry_time and auto_delete: |
| 538 |
error_msg = "Cannot set expiry_time if auto_delete given as True" |
| 539 |
LOG.info(error_msg) |
| 540 |
self.module.fail_json(msg=error_msg) |
| 541 |
|
| 542 |
|
| 543 |
update_flag = False |
| 544 |
if snapshot: |
| 545 |
update_flag = self.to_update(snapshot, |
| 546 |
new_name=new_snapshot_name, |
| 547 |
description=description, |
| 548 |
auto_del=auto_delete, |
| 549 |
expiry_time=expiry_time, |
| 550 |
host=host, host_state=host_state) |
| 551 |
msg = "update_flag for snapshot %s" % str(update_flag) |
| 552 |
LOG.info(msg) |
| 553 |
|
| 554 |
|
| 555 |
if not snapshot and state == "present": |
| 556 |
LOG.info("Creating a snapshot") |
| 557 |
if snapshot_id: |
| 558 |
self.module.fail_json(msg="Creation of Snapshot is allowed" |
| 559 |
" using snapshot_name only, " |
| 560 |
"snapshot_id given") |
| 561 |
if snapshot_name == "" or snapshot_name.isspace(): |
| 562 |
self.module.fail_json(msg="snapshot_name is required for" |
| 563 |
" creation of a snapshot," |
| 564 |
" empty snapshot_name given") |
| 565 |
if not storage_resource: |
| 566 |
self.module.fail_json(msg="vol_name or cg_name required to" |
| 567 |
" create a snapshot") |
| 568 |
|
| 569 |
if new_snapshot_name: |
| 570 |
self.module.fail_json( |
| 571 |
msg="new_snapshot_name can not be assigned" |
| 572 |
" during creation of a snapshot") |
| 573 |
|
| 574 |
snapshot = self.create_snapshot(snapshot_name, |
| 575 |
storage_resource.id, |
| 576 |
description, auto_delete, |
| 577 |
expiry_time) |
| 578 |
if host and host_state == "mapped": |
| 579 |
self.attach_to_snap(snapshot, host) |
| 580 |
changed = True |
| 581 |
|
| 582 |
|
| 583 |
if snapshot and state == "present" and update_flag: |
| 584 |
|
| 585 |
LOG.info("Updating the Snapshot details") |
| 586 |
|
| 587 |
if host_state == 'mapped': |
| 588 |
self.attach_to_snap(snapshot, host) |
| 589 |
self.update_snapshot( |
| 590 |
snapshot, new_name=new_snapshot_name, |
| 591 |
description=description, auto_del=auto_delete, |
| 592 |
expiry_time=expiry_time) |
| 593 |
|
| 594 |
elif host_state == 'unmapped': |
| 595 |
host_access_list = create_host_access_list(snapshot, |
| 596 |
host, |
| 597 |
host_state) |
| 598 |
self.update_snapshot( |
| 599 |
snapshot, new_name=new_snapshot_name, |
| 600 |
description=description, auto_del=auto_delete, |
| 601 |
expiry_time=expiry_time, |
| 602 |
host_access_list=host_access_list) |
| 603 |
|
| 604 |
else: |
| 605 |
self.update_snapshot( |
| 606 |
snapshot, new_name=new_snapshot_name, |
| 607 |
description=description, auto_del=auto_delete, |
| 608 |
expiry_time=expiry_time) |
| 609 |
changed = True |
| 610 |
|
| 611 |
|
| 612 |
if state == "absent" and snapshot: |
| 613 |
snapshot = self.delete_snapshot(snapshot) |
| 614 |
changed = True |
| 615 |
|
| 616 |
|
| 617 |
if snapshot: |
| 618 |
snapshot.update() |
| 619 |
self.result["snapshot_details"] = \ |
| 620 |
create_snapshot_details_dict(snapshot) |
| 621 |
else: |
| 622 |
self.result["snapshot_details"] = {} |
| 623 |
|
| 624 |
self.result["changed"] = changed |
| 625 |
self.module.exit_json(**self.result) |
| 626 |
|
| 627 |
|
| 628 |
def create_snapshot_details_dict(snapshot): |
| 629 |
""" Add name and id of storage resource and hosts to snapshot details """ |
| 630 |
snapshot_dict = snapshot._get_properties() |
| 631 |
del snapshot_dict['storage_resource'] |
| 632 |
del snapshot_dict['host_access'] |
| 633 |
snapshot_dict['hosts_list'] = get_hosts_list( |
| 634 |
get_hosts_dict(snapshot)) |
| 635 |
snapshot_dict['storage_resource_name'] = \ |
| 636 |
snapshot.storage_resource.name |
| 637 |
snapshot_dict['storage_resource_id'] = \ |
| 638 |
snapshot.storage_resource.id |
| 639 |
return snapshot_dict |
| 640 |
|
| 641 |
|
| 642 |
def get_hosts_list(hosts_dict): |
| 643 |
""" Get the host name and host id of all the associated hosts """ |
| 644 |
hosts_list = [] |
| 645 |
if not hosts_dict: |
| 646 |
return hosts_list |
| 647 |
|
| 648 |
for host in list(hosts_dict.keys()): |
| 649 |
hosts_list.append( |
| 650 |
{ |
| 651 |
"host_name": host.name, |
| 652 |
"host_id": host.id |
| 653 |
} |
| 654 |
) |
| 655 |
return hosts_list |
| 656 |
|
| 657 |
|
| 658 |
def create_host_access_list(snapshot, host, host_state): |
| 659 |
""" This method creates a List of dictionaries which will be used |
| 660 |
to modify the list of hosts mapped to a snapshot """ |
| 661 |
host_access_list = [] |
| 662 |
hosts_dict = get_hosts_dict(snapshot) |
| 663 |
|
| 664 |
if not hosts_dict: |
| 665 |
return None |
| 666 |
if to_update_host_list(snapshot, host, host_state): |
| 667 |
if host_state == "mapped": |
| 668 |
return None |
| 669 |
for snap_host in list(hosts_dict.keys()): |
| 670 |
if snap_host != host: |
| 671 |
access_dict = {'host': snap_host, |
| 672 |
'allowedAccess': hosts_dict[snap_host]} |
| 673 |
host_access_list.append(access_dict) |
| 674 |
return host_access_list |
| 675 |
|
| 676 |
|
| 677 |
def get_hosts_dict(snapshot): |
| 678 |
""" This method creates a dictionary, with host as key and |
| 679 |
allowed access as value """ |
| 680 |
hosts_dict = {} |
| 681 |
LOG.info("Inside get_hosts_dict") |
| 682 |
if not snapshot.host_access: |
| 683 |
return hosts_dict |
| 684 |
for host_access_obj in snapshot.host_access: |
| 685 |
hosts_dict[host_access_obj.host] = \ |
| 686 |
host_access_obj.allowed_access |
| 687 |
return hosts_dict |
| 688 |
|
| 689 |
|
| 690 |
def to_update_host_list(snapshot, host, host_state): |
| 691 |
""" Determines whether to update hosts list or not""" |
| 692 |
hosts_dict = get_hosts_dict(snapshot) |
| 693 |
if (not hosts_dict or host not in list(hosts_dict.keys()))\ |
| 694 |
and host_state == "mapped": |
| 695 |
return True |
| 696 |
if (hosts_dict and host in list(hosts_dict.keys())) \ |
| 697 |
and host_state == "unmapped": |
| 698 |
return True |
| 699 |
return False |
| 700 |
|
| 701 |
|
| 702 |
def to_update_expiry_time(snapshot, expiry_time=None): |
| 703 |
""" Check whether to update expiry_time or not""" |
| 704 |
if not expiry_time: |
| 705 |
return False |
| 706 |
if snapshot.expiration_time is None: |
| 707 |
return True |
| 708 |
if convert_timestamp_to_sec(expiry_time, snapshot.expiration_time) != 0: |
| 709 |
return True |
| 710 |
return False |
| 711 |
|
| 712 |
|
| 713 |
def convert_timestamp_to_sec(expiry_time, snap_time): |
| 714 |
"""Converts the time difference to seconds""" |
| 715 |
snap_time_str = snap_time.strftime('%m/%d/%Y %H:%M') |
| 716 |
snap_timestamp = datetime.strptime(snap_time_str, '%m/%d/%Y %H:%M') |
| 717 |
expiry_timestamp = datetime.strptime(expiry_time, "%m/%d/%Y %H:%M") |
| 718 |
return int((expiry_timestamp - snap_timestamp).total_seconds()) |
| 719 |
|
| 720 |
|
| 721 |
def get_snapshot_parameters(): |
| 722 |
"""This method provide parameter required for the ansible snapshot |
| 723 |
module on Unity""" |
| 724 |
return dict( |
| 725 |
snapshot_name=dict(required=False, type='str'), |
| 726 |
snapshot_id=dict(required=False, type='str'), |
| 727 |
vol_name=dict(required=False, type='str'), |
| 728 |
cg_name=dict(required=False, type='str'), |
| 729 |
auto_delete=dict(required=False, type='bool'), |
| 730 |
expiry_time=dict(required=False, type='str'), |
| 731 |
description=dict(required=False, type='str'), |
| 732 |
new_snapshot_name=dict(required=False, type='str'), |
| 733 |
host_name=dict(required=False, type='str'), |
| 734 |
host_id=dict(required=False, type='str'), |
| 735 |
host_state=dict(required=False, type='str', |
| 736 |
choices=['mapped', 'unmapped']), |
| 737 |
state=dict(required=True, type='str', choices=['present', 'absent']) |
| 738 |
) |
| 739 |
|
| 740 |
|
| 741 |
def main(): |
| 742 |
""" Create Unity Snapshot object and perform actions on it |
| 743 |
based on user input from playbook""" |
| 744 |
obj = Snapshot() |
| 745 |
obj.perform_module_operation() |
| 746 |
|
| 747 |
|
| 748 |
if __name__ == '__main__': |
| 749 |
main() |