| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
""" Ansible module for managing Snapshots on Dell Technologies (Dell) PowerFlex""" |
| 7 |
|
| 8 |
from __future__ import (absolute_import, division, print_function) |
| 9 |
|
| 10 |
__metaclass__ = type |
| 11 |
|
| 12 |
DOCUMENTATION = r''' |
| 13 |
module: snapshot |
| 14 |
version_added: '1.0.0' |
| 15 |
short_description: Manage Snapshots on Dell PowerFlex |
| 16 |
description: |
| 17 |
- Managing snapshots on PowerFlex Storage System includes |
| 18 |
creating, getting details, mapping/unmapping to/from SDC, |
| 19 |
modifying the attributes and deleting snapshot. |
| 20 |
|
| 21 |
author: |
| 22 |
- Akash Shendge (@shenda1) <[email protected]> |
| 23 |
|
| 24 |
extends_documentation_fragment: |
| 25 |
- dellemc.powerflex.powerflex |
| 26 |
|
| 27 |
options: |
| 28 |
snapshot_name: |
| 29 |
description: |
| 30 |
- The name of the snapshot. |
| 31 |
- Mandatory for create operation. |
| 32 |
- Specify either I(snapshot_name) or I(snapshot_id) (but not both) for any operation. |
| 33 |
type: str |
| 34 |
snapshot_id: |
| 35 |
description: |
| 36 |
- The ID of the Snapshot. |
| 37 |
type: str |
| 38 |
vol_name: |
| 39 |
description: |
| 40 |
- The name of the volume for which snapshot will be taken. |
| 41 |
- Specify either I(vol_name) or I(vol_id) while creating snapshot. |
| 42 |
type: str |
| 43 |
vol_id: |
| 44 |
description: |
| 45 |
- The ID of the volume. |
| 46 |
type: str |
| 47 |
read_only: |
| 48 |
description: |
| 49 |
- Specifies whether mapping of the created snapshot volume will have |
| 50 |
read-write access or limited to read-only access. |
| 51 |
- If C(true), snapshot is created with read-only access. |
| 52 |
- If C(false), snapshot is created with read-write access. |
| 53 |
type: bool |
| 54 |
size: |
| 55 |
description: |
| 56 |
- The size of the snapshot. |
| 57 |
type: int |
| 58 |
cap_unit: |
| 59 |
description: |
| 60 |
- The unit of the volume size. It defaults to C(GB), if not specified. |
| 61 |
choices: ['GB' , 'TB'] |
| 62 |
type: str |
| 63 |
snapshot_new_name: |
| 64 |
description: |
| 65 |
- New name of the snapshot. Used to rename the snapshot. |
| 66 |
type: str |
| 67 |
allow_multiple_mappings: |
| 68 |
description: |
| 69 |
- Specifies whether to allow multiple mappings or not. |
| 70 |
type: bool |
| 71 |
desired_retention: |
| 72 |
description: |
| 73 |
- The retention value for the Snapshot. |
| 74 |
- If the desired_retention is not mentioned during creation, snapshot |
| 75 |
will be created with unlimited retention. |
| 76 |
- Maximum supported desired retention is 31 days. |
| 77 |
type: int |
| 78 |
retention_unit: |
| 79 |
description: |
| 80 |
- The unit for retention. It defaults to C(hours), if not specified. |
| 81 |
choices: [hours, days] |
| 82 |
type: str |
| 83 |
sdc: |
| 84 |
description: |
| 85 |
- Specifies SDC parameters. |
| 86 |
type: list |
| 87 |
elements: dict |
| 88 |
suboptions: |
| 89 |
sdc_name: |
| 90 |
description: |
| 91 |
- Name of the SDC. |
| 92 |
- Specify either I(sdc_name), I(sdc_id) or I(sdc_ip). |
| 93 |
- Mutually exclusive with I(sdc_id) and I(sdc_ip). |
| 94 |
type: str |
| 95 |
sdc_id: |
| 96 |
description: |
| 97 |
- ID of the SDC. |
| 98 |
- Specify either I(sdc_name), I(sdc_id) or I(sdc_ip). |
| 99 |
- Mutually exclusive with I(sdc_name) and I(sdc_ip). |
| 100 |
type: str |
| 101 |
sdc_ip: |
| 102 |
description: |
| 103 |
- IP of the SDC. |
| 104 |
- Specify either I(sdc_name), I(sdc_id) or I(sdc_ip). |
| 105 |
- Mutually exclusive with I(sdc_id) and I(sdc_ip). |
| 106 |
type: str |
| 107 |
access_mode: |
| 108 |
description: |
| 109 |
- Define the access mode for all mappings of the snapshot. |
| 110 |
choices: ['READ_WRITE', 'READ_ONLY', 'NO_ACCESS'] |
| 111 |
type: str |
| 112 |
bandwidth_limit: |
| 113 |
description: |
| 114 |
- Limit of snapshot network bandwidth. |
| 115 |
- Need to mention in multiple of 1024 Kbps. |
| 116 |
- To set no limit, 0 is to be passed. |
| 117 |
type: int |
| 118 |
iops_limit: |
| 119 |
description: |
| 120 |
- Limit of snapshot IOPS. |
| 121 |
- Minimum IOPS limit is 11 and specify 0 for unlimited iops. |
| 122 |
type: int |
| 123 |
sdc_state: |
| 124 |
description: |
| 125 |
- Mapping state of the SDC. |
| 126 |
choices: ['mapped', 'unmapped'] |
| 127 |
type: str |
| 128 |
remove_mode: |
| 129 |
description: |
| 130 |
- Removal mode for the snapshot. |
| 131 |
- It defaults to C(ONLY_ME), if not specified. |
| 132 |
choices: ['ONLY_ME', 'INCLUDING_DESCENDANTS'] |
| 133 |
type: str |
| 134 |
state: |
| 135 |
description: |
| 136 |
- State of the snapshot. |
| 137 |
choices: ['present', 'absent'] |
| 138 |
required: true |
| 139 |
type: str |
| 140 |
notes: |
| 141 |
- The I(check_mode) is not supported. |
| 142 |
''' |
| 143 |
|
| 144 |
EXAMPLES = r''' |
| 145 |
- name: Create snapshot |
| 146 |
dellemc.powerflex.snapshot: |
| 147 |
hostname: "{{hostname}}" |
| 148 |
username: "{{username}}" |
| 149 |
password: "{{password}}" |
| 150 |
validate_certs: "{{validate_certs}}" |
| 151 |
snapshot_name: "ansible_snapshot" |
| 152 |
vol_name: "ansible_volume" |
| 153 |
read_only: false |
| 154 |
desired_retention: 2 |
| 155 |
state: "present" |
| 156 |
|
| 157 |
- name: Get snapshot details using snapshot id |
| 158 |
dellemc.powerflex.snapshot: |
| 159 |
hostname: "{{hostname}}" |
| 160 |
username: "{{username}}" |
| 161 |
password: "{{password}}" |
| 162 |
validate_certs: "{{validate_certs}}" |
| 163 |
snapshot_id: "fe6cb28200000007" |
| 164 |
state: "present" |
| 165 |
|
| 166 |
- name: Map snapshot to SDC |
| 167 |
dellemc.powerflex.snapshot: |
| 168 |
hostname: "{{hostname}}" |
| 169 |
username: "{{username}}" |
| 170 |
password: "{{password}}" |
| 171 |
validate_certs: "{{validate_certs}}" |
| 172 |
snapshot_id: "fe6cb28200000007" |
| 173 |
sdc: |
| 174 |
- sdc_ip: "198.10.xxx.xxx" |
| 175 |
- sdc_id: "663ac0d200000001" |
| 176 |
allow_multiple_mappings: true |
| 177 |
sdc_state: "mapped" |
| 178 |
state: "present" |
| 179 |
|
| 180 |
- name: Modify the attributes of SDC mapped to snapshot |
| 181 |
dellemc.powerflex.snapshot: |
| 182 |
hostname: "{{hostname}}" |
| 183 |
username: "{{username}}" |
| 184 |
password: "{{password}}" |
| 185 |
validate_certs: "{{validate_certs}}" |
| 186 |
snapshot_id: "fe6cb28200000007" |
| 187 |
sdc: |
| 188 |
- sdc_ip: "198.10.xxx.xxx" |
| 189 |
iops_limit: 11 |
| 190 |
bandwidth_limit: 4096 |
| 191 |
- sdc_id: "663ac0d200000001" |
| 192 |
iops_limit: 20 |
| 193 |
bandwidth_limit: 2048 |
| 194 |
allow_multiple_mappings: true |
| 195 |
sdc_state: "mapped" |
| 196 |
state: "present" |
| 197 |
|
| 198 |
- name: Extend the size of snapshot |
| 199 |
dellemc.powerflex.snapshot: |
| 200 |
hostname: "{{hostname}}" |
| 201 |
username: "{{username}}" |
| 202 |
password: "{{password}}" |
| 203 |
validate_certs: "{{validate_certs}}" |
| 204 |
snapshot_id: "fe6cb28200000007" |
| 205 |
size: 16 |
| 206 |
state: "present" |
| 207 |
|
| 208 |
- name: Unmap SDCs from snapshot |
| 209 |
dellemc.powerflex.snapshot: |
| 210 |
hostname: "{{hostname}}" |
| 211 |
username: "{{username}}" |
| 212 |
password: "{{password}}" |
| 213 |
validate_certs: "{{validate_certs}}" |
| 214 |
snapshot_id: "fe6cb28200000007" |
| 215 |
sdc: |
| 216 |
- sdc_ip: "198.10.xxx.xxx" |
| 217 |
- sdc_id: "663ac0d200000001" |
| 218 |
sdc_state: "unmapped" |
| 219 |
state: "present" |
| 220 |
|
| 221 |
- name: Rename snapshot |
| 222 |
dellemc.powerflex.snapshot: |
| 223 |
hostname: "{{hostname}}" |
| 224 |
username: "{{username}}" |
| 225 |
password: "{{password}}" |
| 226 |
validate_certs: "{{validate_certs}}" |
| 227 |
snapshot_id: "fe6cb28200000007" |
| 228 |
snapshot_new_name: "ansible_renamed_snapshot_10" |
| 229 |
state: "present" |
| 230 |
|
| 231 |
- name: Delete snapshot |
| 232 |
dellemc.powerflex.snapshot: |
| 233 |
hostname: "{{hostname}}" |
| 234 |
username: "{{username}}" |
| 235 |
password: "{{password}}" |
| 236 |
validate_certs: "{{validate_certs}}" |
| 237 |
snapshot_id: "fe6cb28200000007" |
| 238 |
remove_mode: "ONLY_ME" |
| 239 |
state: "absent" |
| 240 |
''' |
| 241 |
|
| 242 |
RETURN = r''' |
| 243 |
changed: |
| 244 |
description: Whether or not the resource has changed. |
| 245 |
returned: always |
| 246 |
type: bool |
| 247 |
sample: 'false' |
| 248 |
|
| 249 |
snapshot_details: |
| 250 |
description: Details of the snapshot. |
| 251 |
returned: When snapshot exists |
| 252 |
type: dict |
| 253 |
contains: |
| 254 |
ancestorVolumeId: |
| 255 |
description: The ID of the root of the specified volume's V-Tree. |
| 256 |
type: str |
| 257 |
ancestorVolumeName: |
| 258 |
description: The name of the root of the specified volume's V-Tree. |
| 259 |
type: str |
| 260 |
creationTime: |
| 261 |
description: The creation time of the snapshot. |
| 262 |
type: int |
| 263 |
id: |
| 264 |
description: The ID of the snapshot. |
| 265 |
type: str |
| 266 |
mappedSdcInfo: |
| 267 |
description: The details of the mapped SDC. |
| 268 |
type: dict |
| 269 |
contains: |
| 270 |
sdcId: |
| 271 |
description: ID of the SDC. |
| 272 |
type: str |
| 273 |
sdcName: |
| 274 |
description: Name of the SDC. |
| 275 |
type: str |
| 276 |
sdcIp: |
| 277 |
description: IP of the SDC. |
| 278 |
type: str |
| 279 |
accessMode: |
| 280 |
description: Mapping access mode for the specified snapshot. |
| 281 |
type: str |
| 282 |
limitIops: |
| 283 |
description: IOPS limit for the SDC. |
| 284 |
type: int |
| 285 |
limitBwInMbps: |
| 286 |
description: Bandwidth limit for the SDC. |
| 287 |
type: int |
| 288 |
name: |
| 289 |
description: Name of the snapshot. |
| 290 |
type: str |
| 291 |
secureSnapshotExpTime: |
| 292 |
description: Expiry time of the snapshot. |
| 293 |
type: int |
| 294 |
sizeInKb: |
| 295 |
description: Size of the snapshot. |
| 296 |
type: int |
| 297 |
sizeInGb: |
| 298 |
description: Size of the snapshot. |
| 299 |
type: int |
| 300 |
retentionInHours: |
| 301 |
description: Retention of the snapshot in hours. |
| 302 |
type: int |
| 303 |
storagePoolId: |
| 304 |
description: The ID of the Storage pool in which snapshot resides. |
| 305 |
type: str |
| 306 |
storagePoolName: |
| 307 |
description: The name of the Storage pool in which snapshot resides. |
| 308 |
type: str |
| 309 |
sample: { |
| 310 |
"accessModeLimit": "ReadOnly", |
| 311 |
"ancestorVolumeId": "cdd883cf00000002", |
| 312 |
"ancestorVolumeName": "ansible-volume-1", |
| 313 |
"autoSnapshotGroupId": null, |
| 314 |
"compressionMethod": "Invalid", |
| 315 |
"consistencyGroupId": "22f1e80c00000001", |
| 316 |
"creationTime": 1631619229, |
| 317 |
"dataLayout": "MediumGranularity", |
| 318 |
"id": "cdd883d000000004", |
| 319 |
"links": [ |
| 320 |
{ |
| 321 |
"href": "/api/instances/Volume::cdd883d000000004", |
| 322 |
"rel": "self" |
| 323 |
}, |
| 324 |
{ |
| 325 |
"href": "/api/instances/Volume::cdd883d000000004/relationships |
| 326 |
/Statistics", |
| 327 |
"rel": "/api/Volume/relationship/Statistics" |
| 328 |
}, |
| 329 |
{ |
| 330 |
"href": "/api/instances/Volume::cdd883cf00000002", |
| 331 |
"rel": "/api/parent/relationship/ancestorVolumeId" |
| 332 |
}, |
| 333 |
{ |
| 334 |
"href": "/api/instances/VTree::6e86255c00000001", |
| 335 |
"rel": "/api/parent/relationship/vtreeId" |
| 336 |
}, |
| 337 |
{ |
| 338 |
"href": "/api/instances/StoragePool::e0d8f6c900000000", |
| 339 |
"rel": "/api/parent/relationship/storagePoolId" |
| 340 |
} |
| 341 |
], |
| 342 |
"lockedAutoSnapshot": false, |
| 343 |
"lockedAutoSnapshotMarkedForRemoval": false, |
| 344 |
"managedBy": "ScaleIO", |
| 345 |
"mappedSdcInfo": null, |
| 346 |
"name": "ansible_vol_snap_1", |
| 347 |
"notGenuineSnapshot": false, |
| 348 |
"originalExpiryTime": 0, |
| 349 |
"pairIds": null, |
| 350 |
"replicationJournalVolume": false, |
| 351 |
"replicationTimeStamp": 0, |
| 352 |
"retentionInHours": 0, |
| 353 |
"retentionLevels": [], |
| 354 |
"secureSnapshotExpTime": 0, |
| 355 |
"sizeInGb": 16, |
| 356 |
"sizeInKb": 16777216, |
| 357 |
"snplIdOfAutoSnapshot": null, |
| 358 |
"snplIdOfSourceVolume": null, |
| 359 |
"storagePoolId": "e0d8f6c900000000", |
| 360 |
"storagePoolName": "pool1", |
| 361 |
"timeStampIsAccurate": false, |
| 362 |
"useRmcache": false, |
| 363 |
"volumeReplicationState": "UnmarkedForReplication", |
| 364 |
"volumeType": "Snapshot", |
| 365 |
"vtreeId": "6e86255c00000001" |
| 366 |
} |
| 367 |
''' |
| 368 |
|
| 369 |
from ansible.module_utils.basic import AnsibleModule |
| 370 |
from ansible_collections.dellemc.powerflex.plugins.module_utils.storage.dell\ |
| 371 |
import utils |
| 372 |
from ansible_collections.dellemc.powerflex.plugins.module_utils.storage.dell.libraries.powerflex_base \ |
| 373 |
import powerflex_compatibility |
| 374 |
from ansible_collections.dellemc.powerflex.plugins.module_utils.storage.dell.libraries.powerflex_base \ |
| 375 |
import PowerFlexBase |
| 376 |
from datetime import datetime, timedelta |
| 377 |
import time |
| 378 |
import copy |
| 379 |
|
| 380 |
LOG = utils.get_logger('snapshot') |
| 381 |
|
| 382 |
|
| 383 |
@powerflex_compatibility(min_ver='3.6', max_ver='5.0', successor='snapshot_v2') |
| 384 |
class PowerFlexSnapshot(PowerFlexBase): |
| 385 |
"""Class with Snapshot operations""" |
| 386 |
|
| 387 |
def __init__(self): |
| 388 |
""" Define all parameters required by this module""" |
| 389 |
argument_spec = get_powerflex_snapshot_parameters() |
| 390 |
|
| 391 |
mutually_exclusive = [['snapshot_name', 'snapshot_id'], |
| 392 |
['vol_name', 'vol_id'], |
| 393 |
['snapshot_id', 'vol_name'], |
| 394 |
['snapshot_id', 'vol_id']] |
| 395 |
|
| 396 |
required_together = [['sdc', 'sdc_state']] |
| 397 |
|
| 398 |
required_one_of = [['snapshot_name', 'snapshot_id']] |
| 399 |
|
| 400 |
module_params = { |
| 401 |
'argument_spec': argument_spec, |
| 402 |
'supports_check_mode': False, |
| 403 |
'mutually_exclusive': mutually_exclusive, |
| 404 |
'required_one_of': required_one_of, |
| 405 |
'required_together': required_together |
| 406 |
} |
| 407 |
|
| 408 |
super().__init__(AnsibleModule, module_params) |
| 409 |
super().check_module_compatibility() |
| 410 |
|
| 411 |
def get_storage_pool(self, storage_pool_id): |
| 412 |
"""Get storage pool details |
| 413 |
:param storage_pool_id: The storage pool id |
| 414 |
:return: Storage pool details |
| 415 |
""" |
| 416 |
|
| 417 |
try: |
| 418 |
return self.powerflex_conn.storage_pool.get( |
| 419 |
filter_fields={'id': storage_pool_id}) |
| 420 |
|
| 421 |
except Exception as e: |
| 422 |
errormsg = "Failed to get the storage pool %s with error " \ |
| 423 |
"%s" % (storage_pool_id, str(e)) |
| 424 |
LOG.error(errormsg) |
| 425 |
self.module.fail_json(msg=errormsg) |
| 426 |
|
| 427 |
def get_snapshot(self, snapshot_name=None, snapshot_id=None): |
| 428 |
"""Get snapshot details |
| 429 |
:param snapshot_name: Name of the snapshot |
| 430 |
:param snapshot_id: ID of the snapshot |
| 431 |
:return: Details of snapshot if exist. |
| 432 |
""" |
| 433 |
|
| 434 |
id_or_name = snapshot_id if snapshot_id else snapshot_name |
| 435 |
|
| 436 |
try: |
| 437 |
filters = {'id': snapshot_id} |
| 438 |
if snapshot_name: |
| 439 |
filters = {'name': snapshot_name} |
| 440 |
snapshot_details = self.powerflex_conn.volume.get( |
| 441 |
filter_fields=filters) |
| 442 |
|
| 443 |
if len(snapshot_details) == 0: |
| 444 |
msg = "Snapshot with identifier %s is not found" % id_or_name |
| 445 |
LOG.error(msg) |
| 446 |
return None |
| 447 |
|
| 448 |
if len(snapshot_details) > 1: |
| 449 |
errormsg = "Multiple instances of snapshot " \ |
| 450 |
"exist with name {0}".format(snapshot_name) |
| 451 |
self.module.fail_json(msg=errormsg) |
| 452 |
|
| 453 |
|
| 454 |
self.add_ancestor(snapshot_details) |
| 455 |
|
| 456 |
|
| 457 |
self.add_size_in_kb(snapshot_details) |
| 458 |
|
| 459 |
|
| 460 |
self.add_storage_pool_name(snapshot_details) |
| 461 |
|
| 462 |
|
| 463 |
self.add_retention_in_hours(snapshot_details) |
| 464 |
|
| 465 |
|
| 466 |
if any([self.module.params['vol_name'], |
| 467 |
self.module.params['vol_id']]): |
| 468 |
self.match_vol_details(snapshot_details[0]) |
| 469 |
return snapshot_details[0] |
| 470 |
except Exception as e: |
| 471 |
errormsg = "Failed to get the snapshot %s with error %s" % ( |
| 472 |
id_or_name, str(e)) |
| 473 |
LOG.error(errormsg) |
| 474 |
self.module.fail_json(msg=errormsg) |
| 475 |
|
| 476 |
def add_retention_in_hours(self, snapshot_details): |
| 477 |
if 'secureSnapshotExpTime' in snapshot_details[0] and\ |
| 478 |
'creationTime' in snapshot_details[0]: |
| 479 |
if snapshot_details[0]['secureSnapshotExpTime'] != 0: |
| 480 |
expiry_obj = datetime.fromtimestamp( |
| 481 |
snapshot_details[0]['secureSnapshotExpTime']) |
| 482 |
creation_obj = datetime.fromtimestamp( |
| 483 |
snapshot_details[0]['creationTime']) |
| 484 |
|
| 485 |
td_hour = int(round(get_datetime_diff_in_minuets(expiry_obj, creation_obj) / 60)) |
| 486 |
snapshot_details[0]['retentionInHours'] = td_hour |
| 487 |
else: |
| 488 |
snapshot_details[0]['retentionInHours'] = 0 |
| 489 |
|
| 490 |
def add_storage_pool_name(self, snapshot_details): |
| 491 |
if 'storagePoolId' in snapshot_details[0] and \ |
| 492 |
snapshot_details[0]['storagePoolId']: |
| 493 |
sp = self.get_storage_pool(snapshot_details[0]['storagePoolId']) |
| 494 |
if len(sp) > 0: |
| 495 |
snapshot_details[0]['storagePoolName'] = sp[0]['name'] |
| 496 |
|
| 497 |
def add_size_in_kb(self, snapshot_details): |
| 498 |
if 'sizeInKb' in snapshot_details[0] and \ |
| 499 |
snapshot_details[0]['sizeInKb']: |
| 500 |
snapshot_details[0]['sizeInGb'] = utils.get_size_in_gb( |
| 501 |
snapshot_details[0]['sizeInKb'], 'KB') |
| 502 |
|
| 503 |
def add_ancestor(self, snapshot_details): |
| 504 |
if 'ancestorVolumeId' in snapshot_details[0] and \ |
| 505 |
snapshot_details[0]['ancestorVolumeId']: |
| 506 |
vol = self.get_volume( |
| 507 |
vol_id=snapshot_details[0]['ancestorVolumeId']) |
| 508 |
snapshot_details[0]['ancestorVolumeName'] = vol['name'] |
| 509 |
|
| 510 |
def match_vol_details(self, snapshot): |
| 511 |
"""Match the given volume details with the response |
| 512 |
:param snapshot: The snapshot details |
| 513 |
""" |
| 514 |
vol_name = self.module.params['vol_name'] |
| 515 |
vol_id = self.module.params['vol_id'] |
| 516 |
|
| 517 |
try: |
| 518 |
if vol_name and vol_name != snapshot['ancestorVolumeName']: |
| 519 |
errormsg = "Given volume name do not match with the " \ |
| 520 |
"corresponding snapshot details." |
| 521 |
self.module.fail_json(msg=errormsg) |
| 522 |
|
| 523 |
if vol_id and vol_id != snapshot['ancestorVolumeId']: |
| 524 |
errormsg = "Given volume ID do not match with the " \ |
| 525 |
"corresponding snapshot details." |
| 526 |
self.module.fail_json(msg=errormsg) |
| 527 |
except Exception as e: |
| 528 |
errormsg = "Failed to match volume details with the snapshot " \ |
| 529 |
"with error %s" % str(e) |
| 530 |
LOG.error(errormsg) |
| 531 |
self.module.fail_json(msg=errormsg) |
| 532 |
|
| 533 |
def get_volume(self, vol_name=None, vol_id=None): |
| 534 |
"""Get the volume id |
| 535 |
:param vol_name: The name of the volume |
| 536 |
:param vol_id: The ID of the volume |
| 537 |
:return: The volume details |
| 538 |
""" |
| 539 |
|
| 540 |
try: |
| 541 |
if vol_name: |
| 542 |
vol_details = self.powerflex_conn.volume.get( |
| 543 |
filter_fields={'name': vol_name}) |
| 544 |
else: |
| 545 |
vol_details = self.powerflex_conn.volume.get( |
| 546 |
filter_fields={'id': vol_id}) |
| 547 |
|
| 548 |
if len(vol_details) == 0: |
| 549 |
error_msg = "Unable to find volume with name {0}".format( |
| 550 |
vol_name) |
| 551 |
self.module.fail_json(msg=error_msg) |
| 552 |
return vol_details[0] |
| 553 |
except Exception as e: |
| 554 |
errormsg = "Failed to get the volume %s with error " \ |
| 555 |
"%s" % (vol_name, str(e)) |
| 556 |
LOG.error(errormsg) |
| 557 |
self.module.fail_json(msg=errormsg) |
| 558 |
|
| 559 |
def get_sdc_id(self, sdc_name=None, sdc_ip=None, sdc_id=None): |
| 560 |
"""Get the SDC ID |
| 561 |
:param sdc_name: The name of the SDC |
| 562 |
:param sdc_ip: The IP of the SDC |
| 563 |
:param sdc_id: The ID of the SDC |
| 564 |
:return: The ID of the SDC |
| 565 |
""" |
| 566 |
|
| 567 |
if sdc_name: |
| 568 |
id_ip_name = sdc_name |
| 569 |
elif sdc_ip: |
| 570 |
id_ip_name = sdc_ip |
| 571 |
else: |
| 572 |
id_ip_name = sdc_id |
| 573 |
|
| 574 |
try: |
| 575 |
if sdc_name: |
| 576 |
sdc_details = self.powerflex_conn.sdc.get( |
| 577 |
filter_fields={'name': sdc_name}) |
| 578 |
elif sdc_ip: |
| 579 |
sdc_details = self.powerflex_conn.sdc.get( |
| 580 |
filter_fields={'sdcIp': sdc_ip}) |
| 581 |
else: |
| 582 |
sdc_details = self.powerflex_conn.sdc.get( |
| 583 |
filter_fields={'id': sdc_id}) |
| 584 |
|
| 585 |
if len(sdc_details) == 0: |
| 586 |
error_msg = "Unable to find SDC with identifier {0}".format( |
| 587 |
id_ip_name) |
| 588 |
self.module.fail_json(msg=error_msg) |
| 589 |
return sdc_details[0]['id'] |
| 590 |
except Exception as e: |
| 591 |
errormsg = "Failed to get the SDC %s with error " \ |
| 592 |
"%s" % (id_ip_name, str(e)) |
| 593 |
LOG.error(errormsg) |
| 594 |
self.module.fail_json(msg=errormsg) |
| 595 |
|
| 596 |
def get_system_id(self): |
| 597 |
"""Get system id""" |
| 598 |
|
| 599 |
try: |
| 600 |
resp = self.powerflex_conn.system.get() |
| 601 |
|
| 602 |
if len(resp) == 0: |
| 603 |
self.module.fail_json(msg="No system exist on the given host.") |
| 604 |
|
| 605 |
if len(resp) > 1: |
| 606 |
self.module.fail_json(msg="Multiple systems exist on the " |
| 607 |
"given host.") |
| 608 |
return resp[0]['id'] |
| 609 |
except Exception as e: |
| 610 |
msg = "Failed to get system id with error %s" % str(e) |
| 611 |
LOG.error(msg) |
| 612 |
self.module.fail_json(msg=msg) |
| 613 |
|
| 614 |
def create_snapshot(self, snapshot_name, vol_id, system_id, |
| 615 |
access_mode, retention): |
| 616 |
"""Create snapshot |
| 617 |
:param snapshot_name: The name of the snapshot |
| 618 |
:param vol_id: The ID of the source volume |
| 619 |
:param system_id: The system id |
| 620 |
:param access_mode: Access mode for the snapshot |
| 621 |
:param retention: The retention for the snapshot |
| 622 |
:return: Boolean indicating if create operation is successful |
| 623 |
""" |
| 624 |
LOG.debug("Creating Snapshot") |
| 625 |
|
| 626 |
try: |
| 627 |
self.powerflex_conn.system.snapshot_volumes( |
| 628 |
system_id=system_id, |
| 629 |
snapshot_defs=[utils.SnapshotDef(vol_id, snapshot_name)], |
| 630 |
access_mode=access_mode, |
| 631 |
retention_period=retention |
| 632 |
) |
| 633 |
|
| 634 |
return True |
| 635 |
except Exception as e: |
| 636 |
errormsg = "Create snapshot %s operation failed with " \ |
| 637 |
"error %s" % (snapshot_name, str(e)) |
| 638 |
LOG.error(errormsg) |
| 639 |
self.module.fail_json(msg=errormsg) |
| 640 |
|
| 641 |
def modify_retention(self, snapshot_id, new_retention): |
| 642 |
"""Modify snapshot retention |
| 643 |
:param snapshot_id: The snapshot id |
| 644 |
:param new_retention: Desired retention of the snapshot |
| 645 |
:return: Boolean indicating if modifying retention is successful |
| 646 |
""" |
| 647 |
|
| 648 |
try: |
| 649 |
self.powerflex_conn.volume.set_retention_period(snapshot_id, |
| 650 |
new_retention) |
| 651 |
return True |
| 652 |
except Exception as e: |
| 653 |
errormsg = "Modify retention of snapshot %s operation failed " \ |
| 654 |
"with error %s" % (snapshot_id, str(e)) |
| 655 |
LOG.error(errormsg) |
| 656 |
self.module.fail_json(msg=errormsg) |
| 657 |
|
| 658 |
def modify_size(self, snapshot_id, new_size): |
| 659 |
"""Modify snapshot size |
| 660 |
:param snapshot_id: The snapshot id |
| 661 |
:param new_size: Size of the snapshot |
| 662 |
:return: Boolean indicating if extend operation is successful |
| 663 |
""" |
| 664 |
|
| 665 |
try: |
| 666 |
self.powerflex_conn.volume.extend(snapshot_id, new_size) |
| 667 |
return True |
| 668 |
except Exception as e: |
| 669 |
errormsg = "Extend snapshot %s operation failed with " \ |
| 670 |
"error %s" % (snapshot_id, str(e)) |
| 671 |
LOG.error(errormsg) |
| 672 |
self.module.fail_json(msg=errormsg) |
| 673 |
|
| 674 |
def modify_snap_access_mode(self, snapshot_id, snap_access_mode): |
| 675 |
"""Modify access mode of snapshot |
| 676 |
:param snapshot_id: The snapshot id |
| 677 |
:param snap_access_mode: Access mode of the snapshot |
| 678 |
:return: Boolean indicating if modifying access mode of |
| 679 |
snapshot is successful |
| 680 |
""" |
| 681 |
|
| 682 |
try: |
| 683 |
self.powerflex_conn.volume.set_volume_access_mode_limit( |
| 684 |
volume_id=snapshot_id, access_mode_limit=snap_access_mode) |
| 685 |
return True |
| 686 |
except Exception as e: |
| 687 |
errormsg = "Modify access mode of snapshot %s operation " \ |
| 688 |
"failed with error %s" % (snapshot_id, str(e)) |
| 689 |
LOG.error(errormsg) |
| 690 |
self.module.fail_json(msg=errormsg) |
| 691 |
|
| 692 |
def modify_access_mode(self, snapshot_id, access_mode_list): |
| 693 |
"""Modify access mode of SDCs mapped to snapshot |
| 694 |
:param snapshot_id: The snapshot id |
| 695 |
:param access_mode_list: List containing SDC ID's whose access mode |
| 696 |
is to modified |
| 697 |
:return: Boolean indicating if modifying access mode is successful |
| 698 |
""" |
| 699 |
|
| 700 |
try: |
| 701 |
changed = False |
| 702 |
for temp in access_mode_list: |
| 703 |
if temp['accessMode']: |
| 704 |
self.powerflex_conn.volume.set_access_mode_for_sdc( |
| 705 |
volume_id=snapshot_id, sdc_id=temp['sdc_id'], |
| 706 |
access_mode=temp['accessMode']) |
| 707 |
changed = True |
| 708 |
return changed |
| 709 |
except Exception as e: |
| 710 |
errormsg = "Modify access mode of SDC %s operation failed " \ |
| 711 |
"with error %s" % (temp['sdc_id'], str(e)) |
| 712 |
LOG.error(errormsg) |
| 713 |
self.module.fail_json(msg=errormsg) |
| 714 |
|
| 715 |
def modify_limits(self, payload): |
| 716 |
"""Modify IOPS and bandwidth limits of SDC's mapped to snapshot |
| 717 |
:param snapshot_id: The snapshot id |
| 718 |
:param limits_dict: Dict containing SDC ID's whose bandwidth and |
| 719 |
IOPS is to modified |
| 720 |
:return: Boolean indicating if modifying limits is successful |
| 721 |
""" |
| 722 |
|
| 723 |
try: |
| 724 |
changed = False |
| 725 |
if payload['bandwidth_limit'] is not None or \ |
| 726 |
payload['iops_limit'] is not None: |
| 727 |
self.powerflex_conn.volume.set_mapped_sdc_limits(**payload) |
| 728 |
changed = True |
| 729 |
return changed |
| 730 |
except Exception as e: |
| 731 |
errormsg = "Modify bandwidth/iops limits of SDC %s operation " \ |
| 732 |
"failed with error %s" % (payload['sdc_id'], str(e)) |
| 733 |
LOG.error(errormsg) |
| 734 |
self.module.fail_json(msg=errormsg) |
| 735 |
|
| 736 |
def rename_snapshot(self, snapshot_id, new_name): |
| 737 |
"""Rename snapshot |
| 738 |
:param snapshot_id: The snapshot id |
| 739 |
:param new_name: The new name of the snapshot |
| 740 |
:return: Boolean indicating if rename operation is successful |
| 741 |
""" |
| 742 |
|
| 743 |
try: |
| 744 |
self.powerflex_conn.volume.rename(snapshot_id, new_name) |
| 745 |
return True |
| 746 |
except Exception as e: |
| 747 |
errormsg = "Rename snapshot %s operation failed with " \ |
| 748 |
"error %s" % (snapshot_id, str(e)) |
| 749 |
LOG.error(errormsg) |
| 750 |
self.module.fail_json(msg=errormsg) |
| 751 |
|
| 752 |
def delete_snapshot(self, snapshot_id, remove_mode): |
| 753 |
"""Delete snapshot |
| 754 |
:param snapshot_id: The snapshot id |
| 755 |
:param remove_mode: Removal mode for the snapshot |
| 756 |
:return: Boolean indicating if delete operation is successful |
| 757 |
""" |
| 758 |
|
| 759 |
try: |
| 760 |
self.powerflex_conn.volume.delete(snapshot_id, remove_mode) |
| 761 |
return True |
| 762 |
except Exception as e: |
| 763 |
errormsg = "Delete snapshot %s operation failed with " \ |
| 764 |
"error %s" % (snapshot_id, str(e)) |
| 765 |
LOG.error(errormsg) |
| 766 |
self.module.fail_json(msg=errormsg) |
| 767 |
|
| 768 |
def validate_desired_retention(self, desired_retention, retention_unit): |
| 769 |
"""Validates the specified desired retention. |
| 770 |
:param desired_retention: Desired retention of the snapshot |
| 771 |
:param retention_unit: Retention unit for snapshot |
| 772 |
""" |
| 773 |
|
| 774 |
if desired_retention is not None: |
| 775 |
if retention_unit == 'hours' and (desired_retention < 1 or |
| 776 |
desired_retention > 744): |
| 777 |
self.module.fail_json(msg="Please provide a valid integer as the" |
| 778 |
" desired retention between 1 and 744.") |
| 779 |
elif retention_unit == 'days' and (desired_retention < 1 or |
| 780 |
desired_retention > 31): |
| 781 |
self.module.fail_json(msg="Please provide a valid integer as the" |
| 782 |
" desired retention between 1 and 31.") |
| 783 |
|
| 784 |
def unmap_snapshot_from_sdc(self, snapshot, sdc): |
| 785 |
"""Unmap SDC's from snapshot |
| 786 |
:param snapshot: Snapshot details |
| 787 |
:param sdc: List of SDCs to be unmapped |
| 788 |
:return: Boolean indicating if unmap operation is successful |
| 789 |
""" |
| 790 |
|
| 791 |
current_sdcs = snapshot['mappedSdcInfo'] |
| 792 |
current_sdc_ids = [] |
| 793 |
sdc_id_list = [] |
| 794 |
|
| 795 |
if current_sdcs: |
| 796 |
for temp in current_sdcs: |
| 797 |
current_sdc_ids.append(temp['sdcId']) |
| 798 |
|
| 799 |
for temp in sdc: |
| 800 |
if 'sdc_name' in temp and temp['sdc_name']: |
| 801 |
sdc_id = self.get_sdc_id(sdc_name=temp['sdc_name']) |
| 802 |
elif 'sdc_ip' in temp and temp['sdc_ip']: |
| 803 |
sdc_id = self.get_sdc_id(sdc_ip=temp['sdc_ip']) |
| 804 |
else: |
| 805 |
sdc_id = self.get_sdc_id(sdc_id=temp['sdc_id']) |
| 806 |
if sdc_id in current_sdc_ids: |
| 807 |
sdc_id_list.append(sdc_id) |
| 808 |
|
| 809 |
LOG.info("SDC IDs to remove %s", sdc_id_list) |
| 810 |
|
| 811 |
if len(sdc_id_list) == 0: |
| 812 |
return False |
| 813 |
|
| 814 |
try: |
| 815 |
for sdc_id in sdc_id_list: |
| 816 |
self.powerflex_conn.volume.remove_mapped_sdc( |
| 817 |
snapshot['id'], sdc_id) |
| 818 |
return True |
| 819 |
except Exception as e: |
| 820 |
errormsg = "Unmap SDC %s from snapshot %s failed with error " \ |
| 821 |
"%s" % (sdc_id, snapshot['id'], str(e)) |
| 822 |
LOG.error(errormsg) |
| 823 |
self.module.fail_json(msg=errormsg) |
| 824 |
|
| 825 |
def map_snapshot_to_sdc(self, snapshot, sdc): |
| 826 |
"""Map SDC's to snapshot |
| 827 |
:param snapshot: Snapshot details |
| 828 |
:param sdc: List of SDCs |
| 829 |
:return: Boolean indicating if mapping operation is successful |
| 830 |
""" |
| 831 |
|
| 832 |
current_sdcs = snapshot['mappedSdcInfo'] |
| 833 |
sdc_id_list = [] |
| 834 |
sdc_map_list = [] |
| 835 |
sdc_modify_list1 = [] |
| 836 |
sdc_modify_list2 = [] |
| 837 |
|
| 838 |
current_sdc_ids = self.populate_current_sdcs_ids(current_sdcs) |
| 839 |
|
| 840 |
for temp in sdc: |
| 841 |
sdc_id = self.get_sdc_id_from(temp) |
| 842 |
if sdc_id not in current_sdc_ids: |
| 843 |
sdc_id_list.append(sdc_id) |
| 844 |
self.update_sdc_details(temp, sdc_id) |
| 845 |
sdc_map_list.append(temp) |
| 846 |
else: |
| 847 |
access_mode_dict, limits_dict = check_for_sdc_modification( |
| 848 |
snapshot, sdc_id, temp) |
| 849 |
self.update_sdc_modify_lists( |
| 850 |
sdc_modify_list1, sdc_modify_list2, access_mode_dict, limits_dict) |
| 851 |
|
| 852 |
LOG.info("SDC to add: %s", sdc_map_list) |
| 853 |
|
| 854 |
if not sdc_map_list: |
| 855 |
return False, sdc_modify_list1, sdc_modify_list2 |
| 856 |
|
| 857 |
try: |
| 858 |
changed = False |
| 859 |
for sdc in sdc_map_list: |
| 860 |
payload = { |
| 861 |
"volume_id": snapshot['id'], |
| 862 |
"sdc_id": sdc['sdc_id'], |
| 863 |
"access_mode": sdc['access_mode'], |
| 864 |
"allow_multiple_mappings": self.module.params['allow_multiple_mappings'] |
| 865 |
} |
| 866 |
self.powerflex_conn.volume.add_mapped_sdc(**payload) |
| 867 |
|
| 868 |
if sdc['bandwidth_limit'] or sdc['iops_limit']: |
| 869 |
payload = { |
| 870 |
"volume_id": snapshot['id'], |
| 871 |
"sdc_id": sdc['sdc_id'], |
| 872 |
"bandwidth_limit": sdc['bandwidth_limit'], |
| 873 |
"iops_limit": sdc['iops_limit'] |
| 874 |
} |
| 875 |
|
| 876 |
self.powerflex_conn.volume.set_mapped_sdc_limits(**payload) |
| 877 |
changed = True |
| 878 |
return changed, sdc_modify_list1, sdc_modify_list2 |
| 879 |
|
| 880 |
except Exception as e: |
| 881 |
errormsg = "Mapping snapshot %s to SDC %s " \ |
| 882 |
"failed with error %s" % (snapshot['name'], |
| 883 |
sdc['sdc_id'], str(e)) |
| 884 |
LOG.error(errormsg) |
| 885 |
self.module.fail_json(msg=errormsg) |
| 886 |
|
| 887 |
def update_sdc_modify_lists(self, sdc_modify_list1, sdc_modify_list2, |
| 888 |
access_mode_dict, limits_dict): |
| 889 |
if access_mode_dict: |
| 890 |
sdc_modify_list1.append(access_mode_dict) |
| 891 |
if limits_dict: |
| 892 |
sdc_modify_list2.append(limits_dict) |
| 893 |
|
| 894 |
def update_sdc_details(self, temp, sdc_id): |
| 895 |
temp['sdc_id'] = sdc_id |
| 896 |
if 'access_mode' in temp: |
| 897 |
temp['access_mode'] = get_access_mode(temp['access_mode']) |
| 898 |
if 'bandwidth_limit' not in temp: |
| 899 |
temp['bandwidth_limit'] = None |
| 900 |
if 'iops_limit' not in temp: |
| 901 |
temp['iops_limit'] = None |
| 902 |
|
| 903 |
def get_sdc_id_from(self, temp): |
| 904 |
sdc_id = None |
| 905 |
if 'sdc_name' in temp and temp['sdc_name']: |
| 906 |
sdc_id = self.get_sdc_id(sdc_name=temp['sdc_name']) |
| 907 |
elif 'sdc_ip' in temp and temp['sdc_ip']: |
| 908 |
sdc_id = self.get_sdc_id(sdc_ip=temp['sdc_ip']) |
| 909 |
else: |
| 910 |
sdc_id = self.get_sdc_id(sdc_id=temp['sdc_id']) |
| 911 |
return sdc_id |
| 912 |
|
| 913 |
def populate_current_sdcs_ids(self, current_sdcs): |
| 914 |
current_sdc_ids = [] |
| 915 |
if current_sdcs: |
| 916 |
for temp in current_sdcs: |
| 917 |
current_sdc_ids.append(temp['sdcId']) |
| 918 |
return current_sdc_ids |
| 919 |
|
| 920 |
def validate_parameters(self): |
| 921 |
"""Validate the input parameters""" |
| 922 |
|
| 923 |
sdc = self.module.params['sdc'] |
| 924 |
cap_unit = self.module.params['cap_unit'] |
| 925 |
size = self.module.params['size'] |
| 926 |
desired_retention = self.module.params['desired_retention'] |
| 927 |
retention_unit = self.module.params['retention_unit'] |
| 928 |
|
| 929 |
param_list = ['snapshot_name', 'snapshot_id', 'vol_name', 'vol_id'] |
| 930 |
for param in param_list: |
| 931 |
if self.module.params[param] is not None and \ |
| 932 |
len(self.module.params[param].strip()) == 0: |
| 933 |
error_msg = "Please provide valid %s" % param |
| 934 |
self.module.fail_json(msg=error_msg) |
| 935 |
|
| 936 |
if sdc: |
| 937 |
for temp in sdc: |
| 938 |
if (all([temp['sdc_id'], temp['sdc_ip']]) or |
| 939 |
all([temp['sdc_id'], temp['sdc_name']]) or |
| 940 |
all([temp['sdc_ip'], temp['sdc_name']])): |
| 941 |
self.module.fail_json(msg="sdc_id, sdc_ip and sdc_name " |
| 942 |
"are mutually exclusive") |
| 943 |
|
| 944 |
if (cap_unit is not None) and not size: |
| 945 |
self.module.fail_json(msg="cap_unit can be specified along " |
| 946 |
"with size") |
| 947 |
|
| 948 |
if (retention_unit is not None) and not desired_retention: |
| 949 |
self.module.fail_json(msg="retention_unit can be specified along " |
| 950 |
"with desired_retention") |
| 951 |
|
| 952 |
def perform_module_operation(self): |
| 953 |
""" |
| 954 |
Perform different actions on snapshot based on parameters passed in |
| 955 |
the playbook |
| 956 |
""" |
| 957 |
snapshot_name = self.module.params['snapshot_name'] |
| 958 |
snapshot_id = self.module.params['snapshot_id'] |
| 959 |
vol_name = self.module.params['vol_name'] |
| 960 |
vol_id = self.module.params['vol_id'] |
| 961 |
read_only = self.module.params['read_only'] |
| 962 |
size = self.module.params['size'] |
| 963 |
cap_unit = self.module.params['cap_unit'] |
| 964 |
snapshot_new_name = self.module.params['snapshot_new_name'] |
| 965 |
sdc = copy.deepcopy(self.module.params['sdc']) |
| 966 |
sdc_state = self.module.params['sdc_state'] |
| 967 |
desired_retention = self.module.params['desired_retention'] |
| 968 |
retention_unit = self.module.params['retention_unit'] |
| 969 |
remove_mode = self.module.params['remove_mode'] |
| 970 |
state = self.module.params['state'] |
| 971 |
|
| 972 |
|
| 973 |
changed = False |
| 974 |
is_modified = False |
| 975 |
result = dict( |
| 976 |
changed=False, |
| 977 |
snapshot_details={} |
| 978 |
) |
| 979 |
|
| 980 |
self.validate_parameters() |
| 981 |
|
| 982 |
cap_unit = self.get_cap_unit(size, cap_unit) |
| 983 |
|
| 984 |
retention_unit = self.get_retention_unit( |
| 985 |
desired_retention, retention_unit) |
| 986 |
|
| 987 |
self.validate_desired_retention(desired_retention, retention_unit) |
| 988 |
|
| 989 |
snapshot_details = self.get_snapshot(snapshot_name, snapshot_id) |
| 990 |
|
| 991 |
if snapshot_details: |
| 992 |
snap_access_mode = None |
| 993 |
if read_only is not None: |
| 994 |
snap_access_mode = self.get_mode(read_only) |
| 995 |
is_modified, flag1, flag2, flag3 = check_snapshot_modified( |
| 996 |
snapshot_details, desired_retention, retention_unit, size, |
| 997 |
cap_unit, snap_access_mode) |
| 998 |
|
| 999 |
if state == 'present' and not snapshot_details: |
| 1000 |
self.validate_create(snapshot_name, snapshot_id, vol_name, |
| 1001 |
vol_id, snapshot_new_name, remove_mode) |
| 1002 |
changed = self.create_snapshot_with_detail(snapshot_name, vol_name, |
| 1003 |
read_only, size, |
| 1004 |
cap_unit, |
| 1005 |
desired_retention, |
| 1006 |
retention_unit) |
| 1007 |
if changed: |
| 1008 |
snapshot_details = self.get_snapshot(snapshot_name) |
| 1009 |
|
| 1010 |
if is_modified: |
| 1011 |
changed = self.modify_val(size, cap_unit, desired_retention, |
| 1012 |
retention_unit, snapshot_details, |
| 1013 |
snap_access_mode, flag1, flag2, flag3) |
| 1014 |
|
| 1015 |
if state == 'present' and snapshot_details and sdc and sdc_state == 'mapped': |
| 1016 |
changed = self.sdc_state_mapped(sdc, snapshot_details) |
| 1017 |
|
| 1018 |
if state == 'present' and snapshot_details and sdc and \ |
| 1019 |
sdc_state == 'unmapped': |
| 1020 |
changed = self.unmap_snapshot_from_sdc(snapshot_details, sdc) |
| 1021 |
|
| 1022 |
if state == 'present' and snapshot_details and \ |
| 1023 |
snapshot_new_name is not None: |
| 1024 |
self.validate_snap_shot_new_name(snapshot_new_name) |
| 1025 |
changed = self.rename_snapshot(snapshot_details['id'], |
| 1026 |
snapshot_new_name) |
| 1027 |
snapshot_name = self.assign_snapshot_name( |
| 1028 |
snapshot_new_name, changed) |
| 1029 |
|
| 1030 |
if state == 'absent' and snapshot_details: |
| 1031 |
remove_mode = self.get_remove_mode(remove_mode) |
| 1032 |
changed = self.delete_snapshot(snapshot_details['id'], remove_mode) |
| 1033 |
|
| 1034 |
if state == 'present': |
| 1035 |
snapshot_details = self.get_snapshot(snapshot_name, snapshot_id) |
| 1036 |
result['snapshot_details'] = snapshot_details |
| 1037 |
|
| 1038 |
result['changed'] = changed |
| 1039 |
self.module.exit_json(**result) |
| 1040 |
|
| 1041 |
def assign_snapshot_name(self, snapshot_new_name, changed): |
| 1042 |
if changed: |
| 1043 |
snapshot_name = snapshot_new_name |
| 1044 |
return snapshot_name |
| 1045 |
|
| 1046 |
def get_remove_mode(self, remove_mode): |
| 1047 |
if remove_mode is None: |
| 1048 |
remove_mode = "ONLY_ME" |
| 1049 |
return remove_mode |
| 1050 |
|
| 1051 |
def get_retention_unit(self, desired_retention, retention_unit): |
| 1052 |
if desired_retention and not retention_unit: |
| 1053 |
retention_unit = 'hours' |
| 1054 |
return retention_unit |
| 1055 |
|
| 1056 |
def get_cap_unit(self, size, cap_unit): |
| 1057 |
if size and not cap_unit: |
| 1058 |
cap_unit = 'GB' |
| 1059 |
return cap_unit |
| 1060 |
|
| 1061 |
def validate_snap_shot_new_name(self, snapshot_new_name): |
| 1062 |
if len(snapshot_new_name.strip()) == 0: |
| 1063 |
self.module.fail_json(msg="Please provide valid snapshot " |
| 1064 |
"name.") |
| 1065 |
|
| 1066 |
def sdc_state_mapped(self, sdc, snapshot_details): |
| 1067 |
changed_mode = False |
| 1068 |
changed_limits = False |
| 1069 |
|
| 1070 |
map_changed, access_mode_list, limits_list = \ |
| 1071 |
self.map_snapshot_to_sdc(snapshot_details, sdc) |
| 1072 |
|
| 1073 |
if len(access_mode_list) > 0: |
| 1074 |
changed_mode = self.modify_access_mode( |
| 1075 |
snapshot_details['id'], access_mode_list) |
| 1076 |
|
| 1077 |
if len(limits_list) > 0: |
| 1078 |
for temp in limits_list: |
| 1079 |
payload = { |
| 1080 |
"volume_id": snapshot_details['id'], |
| 1081 |
"sdc_id": temp['sdc_id'], |
| 1082 |
"bandwidth_limit": temp['bandwidth_limit'], |
| 1083 |
"iops_limit": temp['iops_limit'] |
| 1084 |
} |
| 1085 |
changed_limits = self.modify_limits(payload) |
| 1086 |
return changed_mode or changed_limits or map_changed |
| 1087 |
|
| 1088 |
def modify_val(self, size, cap_unit, desired_retention, retention_unit, |
| 1089 |
snapshot_details, snap_access_mode, flag1, flag2, flag3): |
| 1090 |
if flag1: |
| 1091 |
retention = calculate_retention(desired_retention, |
| 1092 |
retention_unit) |
| 1093 |
changed = self.modify_retention(snapshot_details['id'], |
| 1094 |
retention) |
| 1095 |
if flag2: |
| 1096 |
new_size = size |
| 1097 |
if cap_unit == 'TB': |
| 1098 |
new_size = size * 1024 |
| 1099 |
changed = self.modify_size(snapshot_details['id'], new_size) |
| 1100 |
|
| 1101 |
if flag3: |
| 1102 |
changed = self.modify_snap_access_mode( |
| 1103 |
snapshot_details['id'], snap_access_mode) |
| 1104 |
return changed |
| 1105 |
|
| 1106 |
def create_snapshot_with_detail(self, snapshot_name, vol_name, read_only, |
| 1107 |
size, cap_unit, desired_retention, |
| 1108 |
retention_unit): |
| 1109 |
if vol_name: |
| 1110 |
vol = self.get_volume(vol_name=vol_name) |
| 1111 |
vol_id = vol['id'] |
| 1112 |
|
| 1113 |
retention = 0 |
| 1114 |
if desired_retention: |
| 1115 |
retention = calculate_retention(desired_retention, |
| 1116 |
retention_unit) |
| 1117 |
|
| 1118 |
system_id = self.get_system_id() |
| 1119 |
access_mode = self.get_mode(read_only) |
| 1120 |
|
| 1121 |
changed = self.create_snapshot(snapshot_name, vol_id, system_id, |
| 1122 |
access_mode, retention) |
| 1123 |
if changed: |
| 1124 |
snapshot_details = self.get_snapshot(snapshot_name) |
| 1125 |
|
| 1126 |
if size: |
| 1127 |
if cap_unit == 'GB': |
| 1128 |
new_size = size * 1024 * 1024 |
| 1129 |
else: |
| 1130 |
new_size = size * 1024 * 1024 * 1024 |
| 1131 |
|
| 1132 |
if new_size != snapshot_details['sizeInKb']: |
| 1133 |
if cap_unit == 'TB': |
| 1134 |
size = size * 1024 |
| 1135 |
changed = self.modify_size(snapshot_details['id'], size) |
| 1136 |
return changed |
| 1137 |
|
| 1138 |
def get_mode(self, read_only): |
| 1139 |
ret_mode = None |
| 1140 |
if read_only: |
| 1141 |
ret_mode = 'ReadOnly' |
| 1142 |
else: |
| 1143 |
ret_mode = 'ReadWrite' |
| 1144 |
return ret_mode |
| 1145 |
|
| 1146 |
def validate_create(self, snapshot_name, snapshot_id, vol_name, vol_id, |
| 1147 |
snapshot_new_name, remove_mode): |
| 1148 |
if snapshot_id: |
| 1149 |
self.module.fail_json(msg="Creation of snapshot is allowed " |
| 1150 |
"using snapshot_name only, " |
| 1151 |
"snapshot_id given.") |
| 1152 |
|
| 1153 |
if snapshot_name is None or len(snapshot_name.strip()) == 0: |
| 1154 |
self.module.fail_json(msg="Please provide valid snapshot " |
| 1155 |
"name.") |
| 1156 |
|
| 1157 |
if vol_name is None and vol_id is None: |
| 1158 |
self.module.fail_json(msg="Please provide volume details to " |
| 1159 |
"create new snapshot") |
| 1160 |
|
| 1161 |
if snapshot_new_name is not None: |
| 1162 |
self.module.fail_json(msg="snapshot_new_name is not required" |
| 1163 |
" while creating snapshot") |
| 1164 |
|
| 1165 |
if remove_mode: |
| 1166 |
self.module.fail_json(msg="remove_mode is not required while " |
| 1167 |
"creating snapshot") |
| 1168 |
|
| 1169 |
|
| 1170 |
def check_snapshot_modified(snapshot=None, desired_retention=None, |
| 1171 |
retention_unit=None, size=None, cap_unit=None, |
| 1172 |
access_mode=None): |
| 1173 |
"""Check if snapshot modification is required |
| 1174 |
:param snapshot: Snapshot details |
| 1175 |
:param desired_retention: Desired retention of the snapshot |
| 1176 |
:param retention_unit: Retention unit for snapshot |
| 1177 |
:param size: Size of the snapshot |
| 1178 |
:param cap_unit: Capacity unit for the snapshot |
| 1179 |
:param access_mode: Access mode of the snapshot |
| 1180 |
:return: Boolean indicating if modification is needed |
| 1181 |
""" |
| 1182 |
|
| 1183 |
expiration_timestamp = None |
| 1184 |
is_timestamp_modified = False |
| 1185 |
is_size_modified = False |
| 1186 |
is_access_modified = False |
| 1187 |
is_modified = False |
| 1188 |
|
| 1189 |
snap_creation_timestamp = get_snap_creation_time(snapshot) |
| 1190 |
|
| 1191 |
if desired_retention: |
| 1192 |
expiration_timestamp = get_expiration_timestamp(desired_retention, |
| 1193 |
retention_unit, |
| 1194 |
snap_creation_timestamp) |
| 1195 |
|
| 1196 |
if 'secureSnapshotExpTime' in snapshot and expiration_timestamp and \ |
| 1197 |
snapshot['secureSnapshotExpTime'] != expiration_timestamp: |
| 1198 |
existing_timestamp = snapshot['secureSnapshotExpTime'] |
| 1199 |
new_timestamp = expiration_timestamp |
| 1200 |
|
| 1201 |
info_message = 'The existing timestamp is: %s and the new ' \ |
| 1202 |
'timestamp is: %s' % (existing_timestamp, |
| 1203 |
new_timestamp) |
| 1204 |
LOG.info(info_message) |
| 1205 |
|
| 1206 |
existing_time_obj = datetime.fromtimestamp(existing_timestamp) |
| 1207 |
new_time_obj = datetime.fromtimestamp(new_timestamp) |
| 1208 |
|
| 1209 |
td = get_datetime_diff_in_minuets(existing_time_obj, new_time_obj) |
| 1210 |
|
| 1211 |
LOG.info("Time difference: %s", td) |
| 1212 |
|
| 1213 |
|
| 1214 |
if td > 2: |
| 1215 |
is_timestamp_modified = True |
| 1216 |
|
| 1217 |
if size: |
| 1218 |
new_size = get_new_size(size, cap_unit) |
| 1219 |
if new_size != snapshot['sizeInKb']: |
| 1220 |
is_size_modified = True |
| 1221 |
|
| 1222 |
if access_mode and snapshot['accessModeLimit'] != access_mode: |
| 1223 |
is_access_modified = True |
| 1224 |
|
| 1225 |
if is_timestamp_modified or is_size_modified or is_access_modified: |
| 1226 |
is_modified = True |
| 1227 |
return is_modified, is_timestamp_modified, is_size_modified, is_access_modified |
| 1228 |
|
| 1229 |
|
| 1230 |
def get_datetime_diff_in_minuets(dt1, dt2): |
| 1231 |
""" |
| 1232 |
Calculates the difference in two datetime objects. |
| 1233 |
Args: |
| 1234 |
dt1 (datetime): The first datetime object. |
| 1235 |
dt2 (datetime): The second datetime object. |
| 1236 |
Returns: |
| 1237 |
int: The difference in minutes between dt1 and dt2. |
| 1238 |
Raises: |
| 1239 |
TypeError: If dt1 or dt2 are None. |
| 1240 |
""" |
| 1241 |
if dt1 is None: |
| 1242 |
raise ValueError("First datetime cannot be None") |
| 1243 |
if dt2 is None: |
| 1244 |
raise ValueError("Second datetime cannot be None") |
| 1245 |
if not isinstance(dt1, datetime): |
| 1246 |
raise TypeError(f"First parameter is not a datetime object, it is {type(dt1).__name__}.") |
| 1247 |
if not isinstance(dt2, datetime): |
| 1248 |
raise TypeError(f"Second parameter not a datetime object, it is {type(dt2).__name__}.") |
| 1249 |
|
| 1250 |
if dt1 > dt2: |
| 1251 |
td = dt1 - dt2 |
| 1252 |
else: |
| 1253 |
td = dt2 - dt1 |
| 1254 |
return int(round(td.total_seconds() / 60)) |
| 1255 |
|
| 1256 |
|
| 1257 |
def get_new_size(size, cap_unit): |
| 1258 |
if cap_unit == 'GB': |
| 1259 |
new_size = size * 1024 * 1024 |
| 1260 |
else: |
| 1261 |
new_size = size * 1024 * 1024 * 1024 |
| 1262 |
return new_size |
| 1263 |
|
| 1264 |
|
| 1265 |
def get_expiration_timestamp(desired_retention, retention_unit, |
| 1266 |
snap_creation_timestamp): |
| 1267 |
if retention_unit == 'hours': |
| 1268 |
expiration_timestamp = \ |
| 1269 |
datetime.fromtimestamp(snap_creation_timestamp) + \ |
| 1270 |
timedelta(hours=desired_retention) |
| 1271 |
expiration_timestamp = time.mktime(expiration_timestamp.timetuple()) |
| 1272 |
else: |
| 1273 |
expiration_timestamp = \ |
| 1274 |
datetime.fromtimestamp(snap_creation_timestamp) + \ |
| 1275 |
timedelta(days=desired_retention) |
| 1276 |
expiration_timestamp = time.mktime(expiration_timestamp.timetuple()) |
| 1277 |
return expiration_timestamp |
| 1278 |
|
| 1279 |
|
| 1280 |
def get_snap_creation_time(snapshot): |
| 1281 |
snap_creation_timestamp = None |
| 1282 |
if 'creationTime' in snapshot: |
| 1283 |
snap_creation_timestamp = snapshot['creationTime'] |
| 1284 |
return snap_creation_timestamp |
| 1285 |
|
| 1286 |
|
| 1287 |
def calculate_retention(desired_retention=None, retention_unit=None): |
| 1288 |
""" |
| 1289 |
:param desired_retention: Desired retention of the snapshot |
| 1290 |
:param retention_unit: Retention unit for snapshot |
| 1291 |
:return: Retention in minutes |
| 1292 |
""" |
| 1293 |
|
| 1294 |
retention = 0 |
| 1295 |
if retention_unit == 'days': |
| 1296 |
retention = desired_retention * 24 * 60 |
| 1297 |
else: |
| 1298 |
retention = desired_retention * 60 |
| 1299 |
return retention |
| 1300 |
|
| 1301 |
|
| 1302 |
def check_for_sdc_modification(snapshot, sdc_id, sdc_details): |
| 1303 |
""" |
| 1304 |
:param snapshot: The snapshot details |
| 1305 |
:param sdc_id: The ID of the SDC |
| 1306 |
:param sdc_details: The details of SDC |
| 1307 |
:return: Dictionary with SDC attributes to be modified |
| 1308 |
""" |
| 1309 |
access_mode_dict = dict() |
| 1310 |
limits_dict = dict() |
| 1311 |
|
| 1312 |
for sdc in snapshot['mappedSdcInfo']: |
| 1313 |
if sdc['sdcId'] == sdc_id: |
| 1314 |
update_access_mode(sdc_id, sdc_details, access_mode_dict, sdc) |
| 1315 |
if sdc['limitIops'] != sdc_details['iops_limit'] or \ |
| 1316 |
sdc['limitBwInMbps'] != sdc_details['bandwidth_limit']: |
| 1317 |
limits_dict['sdc_id'] = sdc_id |
| 1318 |
limits_dict['iops_limit'] = None |
| 1319 |
limits_dict['bandwidth_limit'] = None |
| 1320 |
if sdc['limitIops'] != sdc_details['iops_limit']: |
| 1321 |
limits_dict['iops_limit'] = sdc_details['iops_limit'] |
| 1322 |
if sdc['limitBwInMbps'] != get_limits_in_mb(sdc_details['bandwidth_limit']): |
| 1323 |
limits_dict['bandwidth_limit'] = \ |
| 1324 |
sdc_details['bandwidth_limit'] |
| 1325 |
break |
| 1326 |
return access_mode_dict, limits_dict |
| 1327 |
|
| 1328 |
|
| 1329 |
def update_access_mode(sdc_id, sdc_details, access_mode_dict, sdc): |
| 1330 |
if sdc['accessMode'] != get_access_mode(sdc_details['access_mode']): |
| 1331 |
access_mode_dict['sdc_id'] = sdc_id |
| 1332 |
access_mode_dict['accessMode'] = get_access_mode( |
| 1333 |
sdc_details['access_mode']) |
| 1334 |
|
| 1335 |
|
| 1336 |
def get_limits_in_mb(limits): |
| 1337 |
""" |
| 1338 |
:param limits: Limits in KB |
| 1339 |
:return: Limits in MB |
| 1340 |
""" |
| 1341 |
|
| 1342 |
if limits: |
| 1343 |
return limits / 1024 |
| 1344 |
|
| 1345 |
|
| 1346 |
def get_access_mode(access_mode): |
| 1347 |
""" |
| 1348 |
:param access_mode: Access mode of the SDC |
| 1349 |
:return: The enum for the access mode |
| 1350 |
""" |
| 1351 |
|
| 1352 |
access_mode_dict = { |
| 1353 |
"READ_WRITE": "ReadWrite", |
| 1354 |
"READ_ONLY": "ReadOnly", |
| 1355 |
"NO_ACCESS": "NoAccess" |
| 1356 |
} |
| 1357 |
return access_mode_dict.get(access_mode) |
| 1358 |
|
| 1359 |
|
| 1360 |
def get_powerflex_snapshot_parameters(): |
| 1361 |
"""This method provide parameter required for the Ansible snapshot |
| 1362 |
module on PowerFlex""" |
| 1363 |
return dict( |
| 1364 |
snapshot_name=dict(), snapshot_id=dict(), |
| 1365 |
vol_name=dict(), vol_id=dict(), |
| 1366 |
read_only=dict(required=False, type='bool'), |
| 1367 |
size=dict(required=False, type='int'), |
| 1368 |
cap_unit=dict(choices=['GB', 'TB']), |
| 1369 |
snapshot_new_name=dict(), |
| 1370 |
allow_multiple_mappings=dict(required=False, type='bool'), |
| 1371 |
sdc=dict( |
| 1372 |
type='list', elements='dict', options=dict( |
| 1373 |
sdc_id=dict(), sdc_ip=dict(), |
| 1374 |
sdc_name=dict(), |
| 1375 |
access_mode=dict(choices=['READ_WRITE', 'READ_ONLY', |
| 1376 |
'NO_ACCESS']), |
| 1377 |
bandwidth_limit=dict(type='int'), |
| 1378 |
iops_limit=dict(type='int') |
| 1379 |
) |
| 1380 |
), |
| 1381 |
desired_retention=dict(type='int'), |
| 1382 |
retention_unit=dict(choices=['hours', 'days']), |
| 1383 |
remove_mode=dict(choices=['ONLY_ME', 'INCLUDING_DESCENDANTS']), |
| 1384 |
sdc_state=dict(choices=['mapped', 'unmapped']), |
| 1385 |
state=dict(required=True, type='str', choices=['present', 'absent']) |
| 1386 |
) |
| 1387 |
|
| 1388 |
|
| 1389 |
def main(): |
| 1390 |
""" Create PowerFlex Snapshot object and perform actions on it |
| 1391 |
based on user input from playbook""" |
| 1392 |
obj = PowerFlexSnapshot() |
| 1393 |
obj.perform_module_operation() |
| 1394 |
|
| 1395 |
|
| 1396 |
if __name__ == '__main__': |
| 1397 |
main() |