| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
""" Ansible module for managing device 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: device_v2 |
| 14 |
version_added: '3.0.0' |
| 15 |
short_description: Manage Device on Dell PowerFlex |
| 16 |
description: |
| 17 |
- Managing device on PowerFlex storage system includes |
| 18 |
adding new device, getting details of device, |
| 19 |
modifying attributes of device, and removing device. |
| 20 |
- Support only for Powerflex 5.0 versions and above. |
| 21 |
|
| 22 |
author: |
| 23 |
- Tao He (@taohe1012) <[email protected]> |
| 24 |
|
| 25 |
extends_documentation_fragment: |
| 26 |
- dellemc.powerflex.powerflex_v2 |
| 27 |
|
| 28 |
options: |
| 29 |
current_pathname: |
| 30 |
description: |
| 31 |
- Full path of the device to be added. |
| 32 |
- Required while adding a device. |
| 33 |
type: str |
| 34 |
device_name: |
| 35 |
description: |
| 36 |
- Device name. |
| 37 |
- Mutually exclusive with I(device_id). |
| 38 |
type: str |
| 39 |
device_id: |
| 40 |
description: |
| 41 |
- Device ID. |
| 42 |
- Mutually exclusive with I(device_name). |
| 43 |
type: str |
| 44 |
device_group_name: |
| 45 |
description: |
| 46 |
- The name of the device group. |
| 47 |
- Required while adding a device. |
| 48 |
- Mutually exclusive with I(device_group_id). |
| 49 |
type: str |
| 50 |
device_group_id: |
| 51 |
description: |
| 52 |
- The ID of the device group. |
| 53 |
- Required while adding a device. |
| 54 |
- Mutually exclusive with I(device_group_name). |
| 55 |
type: str |
| 56 |
storage_node_name: |
| 57 |
description: |
| 58 |
- The name of the storage node. |
| 59 |
- Required while adding a device. |
| 60 |
- Mutually exclusive with I(storage_node_id). |
| 61 |
type: str |
| 62 |
storage_node_id: |
| 63 |
description: |
| 64 |
- The ID of the storage node. |
| 65 |
- Required while adding a device. |
| 66 |
- Mutually exclusive with I(storage_node_name). |
| 67 |
type: str |
| 68 |
new_device_name: |
| 69 |
description: |
| 70 |
- New name of the device. |
| 71 |
type: str |
| 72 |
capacity_limit_gb: |
| 73 |
description: |
| 74 |
- Device capacity limit in GB. |
| 75 |
type: int |
| 76 |
media_type: |
| 77 |
description: |
| 78 |
- Device media types. |
| 79 |
- Required while adding a device. |
| 80 |
type: str |
| 81 |
choices: ['SSD', 'PMEM'] |
| 82 |
clear_error: |
| 83 |
description: |
| 84 |
- Using the flag to clear error on a device. |
| 85 |
- If the error continues to exist, the device will return |
| 86 |
to an error state as soon as it is accessed. |
| 87 |
type: bool |
| 88 |
state: |
| 89 |
description: |
| 90 |
- State of the device. |
| 91 |
choices: ['present', 'absent'] |
| 92 |
default: 'present' |
| 93 |
type: str |
| 94 |
force: |
| 95 |
description: |
| 96 |
- Using the Force flag to add a device. |
| 97 |
- Using the flag to clear device error state without checking. |
| 98 |
- Use this flag with caution, because all data on the device will be |
| 99 |
destroyed. |
| 100 |
type: bool |
| 101 |
attributes: |
| 102 |
check_mode: |
| 103 |
description: Runs task to validate without performing action on the target machine. |
| 104 |
support: full |
| 105 |
diff_mode: |
| 106 |
description: Runs the task to report the changes made or to be made. |
| 107 |
support: full |
| 108 |
''' |
| 109 |
|
| 110 |
EXAMPLES = r''' |
| 111 |
- name: Create device |
| 112 |
register: device1_result |
| 113 |
dellemc.powerflex.device_v2: |
| 114 |
hostname: "{{ hostname }}" |
| 115 |
username: "{{ username }}" |
| 116 |
password: "{{ password }}" |
| 117 |
validate_certs: "{{ validate_certs }}" |
| 118 |
current_pathname: "/dev/sdc" |
| 119 |
device_group_name: "DG1" |
| 120 |
media_type: "SSD" |
| 121 |
storage_node_name: "Node1" |
| 122 |
|
| 123 |
- name: Create device using name with force flag |
| 124 |
register: device2_result |
| 125 |
dellemc.powerflex.device_v2: |
| 126 |
hostname: "{{ hostname }}" |
| 127 |
username: "{{ username }}" |
| 128 |
password: "{{ password }}" |
| 129 |
validate_certs: "{{ validate_certs }}" |
| 130 |
current_pathname: "/dev/sdd" |
| 131 |
device_group_id: "39a898be00000000" |
| 132 |
storage_node_id: "03b589bf00000003" |
| 133 |
media_type: "SSD" |
| 134 |
device_name: "node1-d2" |
| 135 |
force: true |
| 136 |
state: "present" |
| 137 |
|
| 138 |
- name: Get device details using device_id |
| 139 |
dellemc.powerflex.device_v2: |
| 140 |
hostname: "{{ hostname }}" |
| 141 |
username: "{{ username }}" |
| 142 |
password: "{{ password }}" |
| 143 |
validate_certs: "{{ validate_certs }}" |
| 144 |
device_id: "{{ device1_result.device_details.id }}" |
| 145 |
state: "present" |
| 146 |
|
| 147 |
- name: Get device details using (current_pathname, storage_node_name) |
| 148 |
dellemc.powerflex.device_v2: |
| 149 |
hostname: "{{ hostname }}" |
| 150 |
username: "{{ username }}" |
| 151 |
password: "{{ password }}" |
| 152 |
validate_certs: "{{ validate_certs }}" |
| 153 |
current_pathname: "/dev/sdd" |
| 154 |
storage_node_name: "Node1" |
| 155 |
state: "present" |
| 156 |
|
| 157 |
- name: Get device details using (current_pathname, storage_node_id) |
| 158 |
dellemc.powerflex.device_v2: |
| 159 |
hostname: "{{ hostname }}" |
| 160 |
username: "{{ username }}" |
| 161 |
password: "{{ password }}" |
| 162 |
validate_certs: "{{ validate_certs }}" |
| 163 |
current_pathname: "/dev/sdd" |
| 164 |
storage_node_id: "03b589bf00000003" |
| 165 |
state: "present" |
| 166 |
|
| 167 |
- name: Rename device |
| 168 |
dellemc.powerflex.device_v2: |
| 169 |
hostname: "{{ hostname }}" |
| 170 |
username: "{{ username }}" |
| 171 |
password: "{{ password }}" |
| 172 |
validate_certs: "{{ validate_certs }}" |
| 173 |
device_id: "{{ device1_result.device_details.id }}" |
| 174 |
new_device_name: "node1-d3" |
| 175 |
state: "present" |
| 176 |
|
| 177 |
- name: Clear device error |
| 178 |
dellemc.powerflex.device_v2: |
| 179 |
hostname: "{{ hostname }}" |
| 180 |
username: "{{ username }}" |
| 181 |
password: "{{ password }}" |
| 182 |
validate_certs: "{{ validate_certs }}" |
| 183 |
device_id: "{{ device1_result.device_details.id }}" |
| 184 |
clear_error: true |
| 185 |
force: true |
| 186 |
state: "present" |
| 187 |
|
| 188 |
- name: Clear device error with force flag |
| 189 |
dellemc.powerflex.device_v2: |
| 190 |
hostname: "{{ hostname }}" |
| 191 |
username: "{{ username }}" |
| 192 |
password: "{{ password }}" |
| 193 |
validate_certs: "{{ validate_certs }}" |
| 194 |
device_name: "{{ device2_result.device_details.name }}" |
| 195 |
clear_error: true |
| 196 |
force: true |
| 197 |
state: "present" |
| 198 |
|
| 199 |
- name: Modify device capacity limit |
| 200 |
dellemc.powerflex.device_v2: |
| 201 |
hostname: "{{ hostname }}" |
| 202 |
username: "{{ username }}" |
| 203 |
password: "{{ password }}" |
| 204 |
validate_certs: "{{ validate_certs }}" |
| 205 |
device_name: "{{ device2_result.device_details.name }}" |
| 206 |
capacity_limit_gb: 500 |
| 207 |
state: "present" |
| 208 |
|
| 209 |
- name: Remove device using device_id |
| 210 |
dellemc.powerflex.device_v2: |
| 211 |
hostname: "{{ hostname }}" |
| 212 |
username: "{{ username }}" |
| 213 |
password: "{{ password }}" |
| 214 |
validate_certs: "{{ validate_certs }}" |
| 215 |
device_id: "{{ device1_result.device_details.id }}" |
| 216 |
state: "absent" |
| 217 |
|
| 218 |
- name: Remove device using (current_pathname, storage_node_name) |
| 219 |
dellemc.powerflex.device_v2: |
| 220 |
hostname: "{{ hostname }}" |
| 221 |
username: "{{ username }}" |
| 222 |
password: "{{ password }}" |
| 223 |
validate_certs: "{{ validate_certs }}" |
| 224 |
current_pathname: "/dev/sdd" |
| 225 |
storage_node_name: "Node1" |
| 226 |
state: "absent" |
| 227 |
''' |
| 228 |
|
| 229 |
RETURN = r''' |
| 230 |
changed: |
| 231 |
description: Whether or not the resource has changed. |
| 232 |
returned: always |
| 233 |
type: bool |
| 234 |
sample: 'false' |
| 235 |
device_details: |
| 236 |
description: Details of the device. |
| 237 |
returned: When device exists |
| 238 |
type: dict |
| 239 |
contains: |
| 240 |
id: |
| 241 |
description: Device ID. |
| 242 |
type: str |
| 243 |
name: |
| 244 |
description: Device name. |
| 245 |
type: str |
| 246 |
deviceCurrentPathname: |
| 247 |
description: Device current path name. |
| 248 |
type: str |
| 249 |
deviceOriginalPathname: |
| 250 |
description: Device original path name. |
| 251 |
type: str |
| 252 |
deviceState: |
| 253 |
description: Indicates device state. |
| 254 |
type: str |
| 255 |
errorState: |
| 256 |
description: Indicates error state. |
| 257 |
type: str |
| 258 |
capacityLimitInKb: |
| 259 |
description: Device capacity limit in KB. |
| 260 |
type: int |
| 261 |
maxCapacityInKb: |
| 262 |
description: Maximum device capacity in KB. |
| 263 |
type: int |
| 264 |
deviceGroupId: |
| 265 |
description: Device group ID. |
| 266 |
type: str |
| 267 |
longSuccessfulIos: |
| 268 |
description: Indicates long successful I/O operations. |
| 269 |
type: dict |
| 270 |
storageNodeId: |
| 271 |
description: Storage node ID. |
| 272 |
type: str |
| 273 |
updateConfiguration: |
| 274 |
description: Indicates whether configuration update is enabled. |
| 275 |
type: bool |
| 276 |
ledSetting: |
| 277 |
description: LED setting state. |
| 278 |
type: str |
| 279 |
aggregatedState: |
| 280 |
description: Indicates aggregated device state. |
| 281 |
type: str |
| 282 |
temperatureState: |
| 283 |
description: Indicates temperature state. |
| 284 |
type: str |
| 285 |
ssdEndOfLifeState: |
| 286 |
description: Indicates SSD end of life state. |
| 287 |
type: str |
| 288 |
modelName: |
| 289 |
description: Device model name. |
| 290 |
type: str |
| 291 |
serialNumber: |
| 292 |
description: Device serial number. |
| 293 |
type: str |
| 294 |
deviceType: |
| 295 |
description: Indicates device type. |
| 296 |
type: str |
| 297 |
mediaType: |
| 298 |
description: Indicates media type. |
| 299 |
type: str |
| 300 |
vendorName: |
| 301 |
description: Device vendor name. |
| 302 |
type: str |
| 303 |
raidControllerSerialNumber: |
| 304 |
description: RAID controller serial number. |
| 305 |
type: str |
| 306 |
firmwareVersion: |
| 307 |
description: Device firmware version. |
| 308 |
type: str |
| 309 |
cacheLookAheadActive: |
| 310 |
description: Indicates cache look-ahead active state. |
| 311 |
type: bool |
| 312 |
writeCacheActive: |
| 313 |
description: Indicates write cache active state. |
| 314 |
type: bool |
| 315 |
ataSecurityActive: |
| 316 |
description: Indicates ATA security active state. |
| 317 |
type: bool |
| 318 |
capacity: |
| 319 |
description: Device capacity in bytes. |
| 320 |
type: int |
| 321 |
logicalSectorSizeInBytes: |
| 322 |
description: Logical sector size in bytes. |
| 323 |
type: int |
| 324 |
physicalSectorSizeInBytes: |
| 325 |
description: Physical sector size in bytes. |
| 326 |
type: int |
| 327 |
mediaFailing: |
| 328 |
description: Indicates if media is failing. |
| 329 |
type: bool |
| 330 |
autoDetectMediaType: |
| 331 |
description: Auto-detection result of media type. |
| 332 |
type: str |
| 333 |
storageProps: |
| 334 |
description: Storage device properties. |
| 335 |
type: dict |
| 336 |
persistentChecksumState: |
| 337 |
description: Indicates persistent checksum state. |
| 338 |
type: str |
| 339 |
sample: { |
| 340 |
"fglNvdimmWriteCacheSize": null, |
| 341 |
"deviceCurrentPathName": "/dev/sdd", |
| 342 |
"rfcacheErrorDeviceDoesNotExist": false, |
| 343 |
"logicalSectorSizeInBytes": 0, |
| 344 |
"deviceOriginalPathName": "/dev/sdd", |
| 345 |
"fglNvdimmMetadataAmortizationX100": null, |
| 346 |
"capacity": 0, |
| 347 |
"name": null, |
| 348 |
"serialNumber": null, |
| 349 |
"mediaType": "SSD", |
| 350 |
"accelerationPoolId": null, |
| 351 |
"rfcacheProps": null, |
| 352 |
"sdsId": null, |
| 353 |
"storagePoolId": null, |
| 354 |
"capacityLimitInKb": 1073479680, |
| 355 |
"errorState": "None", |
| 356 |
"storageNodeId": "03b589bf00000003", |
| 357 |
"externalAccelerationType": "None", |
| 358 |
"accelerationProps": null, |
| 359 |
"ssdEndOfLifeState": "NeverFailed", |
| 360 |
"temperatureState": "NeverFailed", |
| 361 |
"aggregatedState": "NeverFailed", |
| 362 |
"spSdsId": null, |
| 363 |
"deviceState": "Normal", |
| 364 |
"storageProps": null, |
| 365 |
"autoDetectMediaType": null, |
| 366 |
"longSuccessfulIos": { |
| 367 |
"shortWindow": null, |
| 368 |
"mediumWindow": null, |
| 369 |
"longWindow": null |
| 370 |
}, |
| 371 |
"maxCapacityInKb": 1073479680, |
| 372 |
"ledSetting": "Off", |
| 373 |
"modelName": null, |
| 374 |
"deviceType": "Unknown", |
| 375 |
"vendorName": null, |
| 376 |
"raidControllerSerialNumber": null, |
| 377 |
"firmwareVersion": null, |
| 378 |
"cacheLookAheadActive": false, |
| 379 |
"writeCacheActive": false, |
| 380 |
"ataSecurityActive": false, |
| 381 |
"physicalSectorSizeInBytes": 0, |
| 382 |
"mediaFailing": false, |
| 383 |
"slotNumber": "N/A", |
| 384 |
"persistentChecksumState": "StateInvalid", |
| 385 |
"capacityInMb": 1048576, |
| 386 |
"usableCapacityInMb": 1048320, |
| 387 |
"deviceGroupId": "39a898be00000000", |
| 388 |
"id": "e7ffaabf00030002", |
| 389 |
"links": [ |
| 390 |
{ |
| 391 |
"rel": "self", |
| 392 |
"href": "/api/instances/Device::e7ffaabf00030002" |
| 393 |
}, |
| 394 |
{ |
| 395 |
"rel": "/dtapi/rest/v1/metrics/query", |
| 396 |
"href": "/dtapi/rest/v1/metrics/query", |
| 397 |
"body": { |
| 398 |
"resource_type": "device", |
| 399 |
"ids": [ |
| 400 |
"e7ffaabf00030002" |
| 401 |
] |
| 402 |
} |
| 403 |
}, |
| 404 |
{ |
| 405 |
"rel": "/api/parent/relationship/deviceGroupId", |
| 406 |
"href": "/api/instances/DeviceGroup::39a898be00000000" |
| 407 |
}, |
| 408 |
{ |
| 409 |
"rel": "/api/parent/relationship/storageNodeId", |
| 410 |
"href": "/api/instances/StorageNode::03b589bf00000003" |
| 411 |
} |
| 412 |
] |
| 413 |
} |
| 414 |
''' |
| 415 |
|
| 416 |
from ansible.module_utils.basic import AnsibleModule |
| 417 |
from ansible_collections.dellemc.powerflex.plugins.module_utils.storage.dell.libraries.powerflex_base \ |
| 418 |
import PowerFlexBase |
| 419 |
from ansible_collections.dellemc.powerflex.plugins.module_utils.storage.dell.libraries.powerflex_base \ |
| 420 |
import powerflex_compatibility |
| 421 |
from ansible_collections.dellemc.powerflex.plugins.module_utils.storage.dell import utils |
| 422 |
import copy |
| 423 |
|
| 424 |
LOG = utils.get_logger('device_v2') |
| 425 |
|
| 426 |
|
| 427 |
@powerflex_compatibility(min_ver='5.0', predecessor='device') |
| 428 |
class PowerFlexDeviceV2(PowerFlexBase): |
| 429 |
"""Class with device operations""" |
| 430 |
|
| 431 |
def __init__(self): |
| 432 |
""" Define all parameters required by this module""" |
| 433 |
mutually_exclusive = [["storage_node_name", "storage_node_id"], |
| 434 |
['device_group_name', 'device_group_id'], |
| 435 |
['device_name', 'device_id']] |
| 436 |
ansible_module_params = { |
| 437 |
'argument_spec': get_powerflex_device_parameters(), |
| 438 |
'supports_check_mode': True, |
| 439 |
'mutually_exclusive': mutually_exclusive |
| 440 |
} |
| 441 |
super().__init__(AnsibleModule, ansible_module_params) |
| 442 |
super().check_module_compatibility() |
| 443 |
|
| 444 |
self.result = dict( |
| 445 |
changed=False, |
| 446 |
device_details={}, |
| 447 |
diff={} |
| 448 |
) |
| 449 |
|
| 450 |
def validate_create_parameters(self, device_params): |
| 451 |
"""Validate the input parameters |
| 452 |
:param device_params: The dict of device parameters |
| 453 |
:type device_params: dict |
| 454 |
""" |
| 455 |
|
| 456 |
if device_params['device_id']: |
| 457 |
self.module.fail_json(msg="Should Not provide device_id for creation of device.") |
| 458 |
if device_params['media_type'] is None or len(device_params['media_type'].strip()) == 0: |
| 459 |
self.module.fail_json(msg="Provide media_type for creation of device.") |
| 460 |
if device_params['current_pathname'] is None or len(device_params['current_pathname'].strip()) == 0: |
| 461 |
self.module.fail_json(msg="Provide current_pathname for creation of device.") |
| 462 |
if device_params['device_name'] is not None and len(device_params['device_name'].strip()) == 0: |
| 463 |
self.module.fail_json(msg="Provide valid device_name for creation of device.") |
| 464 |
|
| 465 |
dg_identify = device_params["device_group_id"] \ |
| 466 |
if device_params["device_group_id"] else device_params["device_group_name"] |
| 467 |
if dg_identify is None or len(dg_identify.strip()) == 0: |
| 468 |
self.module.fail_json(msg="Either device_group_id or device_group_name " |
| 469 |
"needs to be provided for creation of device.") |
| 470 |
|
| 471 |
node_identify = device_params["storage_node_id"] \ |
| 472 |
if device_params["storage_node_id"] else device_params["storage_node_name"] |
| 473 |
if node_identify is None or len(node_identify.strip()) == 0: |
| 474 |
self.module.fail_json(msg="Either storage_node_id or storage_node_name " |
| 475 |
"needs to be provided for creation of device.") |
| 476 |
|
| 477 |
def get_storage_node(self, storage_node_name=None, storage_node_id=None): |
| 478 |
"""Get storage node details |
| 479 |
:param storage_node_name: Name of the storage node |
| 480 |
:param storage_node_id: ID of the storage node |
| 481 |
:return: storage node details |
| 482 |
:rtype: dict |
| 483 |
""" |
| 484 |
name_or_id = storage_node_name if storage_node_name \ |
| 485 |
else storage_node_id |
| 486 |
try: |
| 487 |
node_details = None |
| 488 |
if storage_node_id: |
| 489 |
node_details = self.powerflex_conn.storage_node.get( |
| 490 |
filter_fields={'id': storage_node_id}) |
| 491 |
|
| 492 |
if storage_node_name: |
| 493 |
node_details = self.powerflex_conn.storage_node.get( |
| 494 |
filter_fields={'name': storage_node_name}) |
| 495 |
|
| 496 |
if not node_details or len(node_details) == 0: |
| 497 |
error_msg = (f"Unable to find the storage node with {name_or_id}. " |
| 498 |
"Please enter a valid storage node name/id.") |
| 499 |
LOG.error(error_msg) |
| 500 |
self.module.fail_json(msg=error_msg) |
| 501 |
|
| 502 |
return node_details[0] |
| 503 |
|
| 504 |
except Exception as e: |
| 505 |
error_msg = (f"Failed to get the storage node {name_or_id} with " |
| 506 |
f"error {str(e)}") |
| 507 |
LOG.error(error_msg) |
| 508 |
self.module.fail_json(msg=error_msg) |
| 509 |
|
| 510 |
def get_device_group(self, device_group_id=None, device_group_name=None): |
| 511 |
"""Get device group details |
| 512 |
:param device_group_name: Name of the device group. |
| 513 |
:param device_group_id: ID of the device group. |
| 514 |
:return: device group details |
| 515 |
:rtype: dict |
| 516 |
""" |
| 517 |
name_or_id = device_group_name if device_group_name \ |
| 518 |
else device_group_id |
| 519 |
try: |
| 520 |
dg_details = None |
| 521 |
if device_group_id: |
| 522 |
dg_details = self.powerflex_conn.device_group.get( |
| 523 |
filter_fields={'id': device_group_id}) |
| 524 |
|
| 525 |
if device_group_name: |
| 526 |
dg_details = self.powerflex_conn.device_group.get( |
| 527 |
filter_fields={'name': device_group_name}) |
| 528 |
|
| 529 |
if not dg_details or len(dg_details) == 0: |
| 530 |
error_msg = (f"Unable to find the device group with {name_or_id}. " |
| 531 |
"Please enter a valid device group name/id.") |
| 532 |
LOG.error(error_msg) |
| 533 |
self.module.fail_json(msg=error_msg) |
| 534 |
|
| 535 |
return dg_details[0] |
| 536 |
|
| 537 |
except Exception as e: |
| 538 |
error_msg = (f"Failed to get the device group {name_or_id} with " |
| 539 |
f"error {str(e)}") |
| 540 |
LOG.error(error_msg) |
| 541 |
self.module.fail_json(msg=error_msg) |
| 542 |
|
| 543 |
def get_device(self, current_pathname=None, storage_node_name=None, |
| 544 |
storage_node_id=None, device_id=None, device_name=None): |
| 545 |
"""Get device details |
| 546 |
:param current_pathname: Device path name. |
| 547 |
:type current_pathname: str |
| 548 |
:param storage_node_name: Storage node name. |
| 549 |
:type storage_node_name: str |
| 550 |
:param storage_node_id: Storage node ID. |
| 551 |
:type storage_node_id: str |
| 552 |
:param device_name: Name of the device. |
| 553 |
:param device_id: ID of the device. |
| 554 |
:return: device details |
| 555 |
""" |
| 556 |
try: |
| 557 |
device_details = None |
| 558 |
if device_id: |
| 559 |
device_details = self.powerflex_conn.device.get( |
| 560 |
filter_fields={'id': device_id}) |
| 561 |
elif device_name: |
| 562 |
device_details = self.powerflex_conn.device.get( |
| 563 |
filter_fields={'name': device_name}) |
| 564 |
elif current_pathname: |
| 565 |
if storage_node_name and not storage_node_id: |
| 566 |
storage_node_id = self.get_storage_node(storage_node_name=storage_node_name)['id'] |
| 567 |
if storage_node_id: |
| 568 |
device_details = self.powerflex_conn.device.get( |
| 569 |
filter_fields={'deviceCurrentPathName': current_pathname, |
| 570 |
'storageNodeId': storage_node_id}) |
| 571 |
|
| 572 |
if not device_details or len(device_details) == 0: |
| 573 |
msg = "Unable to find the device." |
| 574 |
LOG.info(msg) |
| 575 |
return None |
| 576 |
|
| 577 |
node_details = self.get_storage_node(storage_node_id=device_details[0]['storageNodeId']) |
| 578 |
dg_details = self.get_device_group(device_group_id=device_details[0]['deviceGroupId']) |
| 579 |
device_details[0]['storageNodeName'] = node_details['name'] |
| 580 |
device_details[0]['deviceGroupName'] = dg_details['name'] |
| 581 |
return device_details[0] |
| 582 |
|
| 583 |
except Exception as e: |
| 584 |
errormsg = f'Failed to get the device with error {str(e)}' |
| 585 |
LOG.error(errormsg) |
| 586 |
self.module.fail_json(msg=errormsg) |
| 587 |
|
| 588 |
def create_device(self, media_type, device_group_id, storage_node_id, |
| 589 |
current_pathname, device_name=None, force=None): |
| 590 |
"""Create device |
| 591 |
:param media_type: Media type of device. |
| 592 |
:type media_type: str |
| 593 |
:param device_group_id: Device group ID. |
| 594 |
:type device_group_id: str |
| 595 |
:param storage_node_id: Storage node ID. |
| 596 |
:type storage_node_id: str |
| 597 |
:param device_name: Name of the device. |
| 598 |
:type device_name: str |
| 599 |
:param current_pathname: Pathname of the device. |
| 600 |
:type current_pathname: str |
| 601 |
:param force: Force adding the device. |
| 602 |
:type force: bool |
| 603 |
:return: Details of the device, if created. |
| 604 |
:rtype: dict |
| 605 |
""" |
| 606 |
|
| 607 |
try: |
| 608 |
device_id = None |
| 609 |
if not self.module.check_mode: |
| 610 |
device_id = self.powerflex_conn.device.create( |
| 611 |
current_pathname=current_pathname, |
| 612 |
media_type=media_type, |
| 613 |
device_group_id=device_group_id, |
| 614 |
node_id=storage_node_id, |
| 615 |
force=force, |
| 616 |
name=device_name)['id'] |
| 617 |
return self.get_device(device_id=device_id) |
| 618 |
|
| 619 |
except Exception as e: |
| 620 |
errormsg = f'Creation of device failed with error {str(e)}' |
| 621 |
LOG.error(errormsg) |
| 622 |
self.module.fail_json(msg=errormsg) |
| 623 |
|
| 624 |
def delete_device(self, device_id): |
| 625 |
"""Delete device |
| 626 |
:param device_id: The ID of the device. |
| 627 |
:return: Details of the device. |
| 628 |
""" |
| 629 |
try: |
| 630 |
if not self.module.check_mode: |
| 631 |
self.powerflex_conn.device.delete(device_id) |
| 632 |
return self.get_device(device_id=device_id) |
| 633 |
|
| 634 |
except Exception as e: |
| 635 |
errormsg = (f'Deletion of device {device_id} ' |
| 636 |
f'failed with error {str(e)}') |
| 637 |
LOG.error(errormsg) |
| 638 |
self.module.fail_json(msg=errormsg) |
| 639 |
|
| 640 |
def get_diff_after(self, device_params, device_details): |
| 641 |
"""Get diff between playbook input and device details |
| 642 |
:param device_details: Dictionary of device details |
| 643 |
:param device_params: Dictionary of parameters input from playbook |
| 644 |
:return: Dictionary of parameters of differences""" |
| 645 |
|
| 646 |
if device_params["state"] == "absent": |
| 647 |
return {} |
| 648 |
diff_dict = {} |
| 649 |
if device_details is None: |
| 650 |
diff_dict = { |
| 651 |
"media_type": device_params['media_type'], |
| 652 |
"device_group_id": device_params['device_group_id'], |
| 653 |
"storage_node_id": device_params['storage_node_id'], |
| 654 |
"current_pathname": device_params['current_pathname'] |
| 655 |
} |
| 656 |
if device_params["device_name"]: |
| 657 |
diff_dict["device_name"] = device_params["device_name"] |
| 658 |
if device_params["force"]: |
| 659 |
diff_dict["force"] = device_params["force"] |
| 660 |
else: |
| 661 |
diff_dict = copy.deepcopy(device_details) |
| 662 |
diff_dict.pop("links", None) |
| 663 |
modify_dict = self.to_modify(diff_dict, device_params) |
| 664 |
for key in modify_dict.keys(): |
| 665 |
diff_dict[key] = modify_dict[key] |
| 666 |
return diff_dict |
| 667 |
|
| 668 |
def to_modify(self, device_details, device_params): |
| 669 |
"""Whether to modify the device or not |
| 670 |
:param device_details: Dictionary of device details |
| 671 |
:param device_params: Dictionary of parameters input from playbook |
| 672 |
:return: Dictionary containing the attributes of device which are to be updated. |
| 673 |
""" |
| 674 |
modify_dict = {} |
| 675 |
|
| 676 |
new_name = device_params["new_device_name"] |
| 677 |
if new_name is not None: |
| 678 |
if len(new_name.strip()) == 0: |
| 679 |
self.module.fail_json(msg="Provide valid name.") |
| 680 |
if device_details["name"] is None or new_name != device_details['name']: |
| 681 |
modify_dict['new_device_name'] = new_name |
| 682 |
|
| 683 |
if device_params["capacity_limit_gb"] is not None and\ |
| 684 |
device_params["capacity_limit_gb"] * 1024 * 1024 < device_details['capacityLimitInKb']: |
| 685 |
modify_dict["capacity_limit_gb"] = device_params["capacity_limit_gb"] |
| 686 |
|
| 687 |
if device_params["clear_error"] is not None and device_params["clear_error"]: |
| 688 |
modify_dict["clear_error"] = True |
| 689 |
if device_params["force"] is not None and device_params["force"]: |
| 690 |
modify_dict["force"] = True |
| 691 |
|
| 692 |
return modify_dict |
| 693 |
|
| 694 |
def modify_device(self, device_id, modify_dict): |
| 695 |
""" |
| 696 |
Modify the device attributes |
| 697 |
:param device_details: Details of the device. |
| 698 |
:param modify_dict: Dictionary containing the attributes of device which are to be updated. |
| 699 |
:return: True, if the operation is successful. |
| 700 |
""" |
| 701 |
try: |
| 702 |
msg = (f"Dictionary containing attributes which are to be" |
| 703 |
f" updated is {str(modify_dict)}.") |
| 704 |
LOG.info(msg) |
| 705 |
|
| 706 |
if not self.module.check_mode: |
| 707 |
if "new_device_name" in modify_dict: |
| 708 |
self.powerflex_conn.device.rename(device_id, modify_dict["new_device_name"]) |
| 709 |
msg = f"The name of the Device is updated to {modify_dict['new_device_name']} successfully." |
| 710 |
LOG.info(msg) |
| 711 |
|
| 712 |
if "capacity_limit_gb" in modify_dict: |
| 713 |
self.powerflex_conn.device.set_capacity_limit(device_id, modify_dict["capacity_limit_gb"]) |
| 714 |
msg = f"The capacity limit is updated to {modify_dict['capacity_limit_gb']} successfully." |
| 715 |
LOG.info(msg) |
| 716 |
|
| 717 |
if "clear_error" in modify_dict: |
| 718 |
self.powerflex_conn.device.clear_errors(device_id, modify_dict["force"]) |
| 719 |
LOG.info("The device error is cleared successfully.") |
| 720 |
|
| 721 |
return True, self.get_device(device_id=device_id) |
| 722 |
|
| 723 |
except Exception as e: |
| 724 |
err_msg = (f"Failed to update the device {device_id} " |
| 725 |
f"with error {str(e)}") |
| 726 |
LOG.error(err_msg) |
| 727 |
self.module.fail_json(msg=err_msg) |
| 728 |
|
| 729 |
|
| 730 |
def get_powerflex_device_parameters(): |
| 731 |
"""This method provide parameter required for the device_v2 module on PowerFlex""" |
| 732 |
return dict( |
| 733 |
device_name=dict(type='str'), |
| 734 |
device_id=dict(type='str'), |
| 735 |
new_device_name=dict(type='str'), |
| 736 |
device_group_name=dict(type='str'), |
| 737 |
device_group_id=dict(type='str'), |
| 738 |
storage_node_id=dict(type='str'), |
| 739 |
storage_node_name=dict(type='str'), |
| 740 |
current_pathname=dict(type='str'), |
| 741 |
media_type=dict(type='str', choices=['SSD', 'PMEM']), |
| 742 |
capacity_limit_gb=dict(type='int'), |
| 743 |
force=dict(type='bool'), |
| 744 |
clear_error=dict(type='bool'), |
| 745 |
state=dict(default='present', choices=['present', 'absent']) |
| 746 |
) |
| 747 |
|
| 748 |
|
| 749 |
class DeviceCreateHandler(): |
| 750 |
def handle(self, device_object, device_params, device_details): |
| 751 |
if device_params['state'] == 'present' and not device_details: |
| 752 |
device_object.validate_create_parameters(device_params=device_params) |
| 753 |
device_group_id = device_params["device_group_id"] |
| 754 |
if device_params["device_group_name"]: |
| 755 |
device_group_id = device_object.get_device_group( |
| 756 |
device_group_name=device_params["device_group_name"])["id"] |
| 757 |
storage_node_id = device_params["storage_node_id"] |
| 758 |
if device_params["storage_node_name"]: |
| 759 |
storage_node_id = device_object.get_storage_node( |
| 760 |
storage_node_name=device_params["storage_node_name"])["id"] |
| 761 |
|
| 762 |
device_details = device_object.create_device( |
| 763 |
media_type=device_params["media_type"], |
| 764 |
device_group_id=device_group_id, |
| 765 |
storage_node_id=storage_node_id, |
| 766 |
current_pathname=device_params["current_pathname"], |
| 767 |
device_name=device_params["device_name"], |
| 768 |
force=device_params["force"]) |
| 769 |
device_object.result['changed'] = True |
| 770 |
DeviceModifyHandler().handle(device_object, device_params, device_details) |
| 771 |
|
| 772 |
|
| 773 |
class DeviceModifyHandler(): |
| 774 |
def handle(self, device_object, device_params, device_details): |
| 775 |
if device_params['state'] == 'present' and device_details: |
| 776 |
modify_dict = device_object.to_modify(device_details, device_params) |
| 777 |
if modify_dict: |
| 778 |
changed, device_details = device_object.modify_device(device_details['id'], modify_dict) |
| 779 |
device_object.result['changed'] |= changed |
| 780 |
DeviceDeleteHandler().handle(device_object, device_params, device_details) |
| 781 |
|
| 782 |
|
| 783 |
class DeviceDeleteHandler(): |
| 784 |
def handle(self, device_object, device_params, device_details): |
| 785 |
deleteFlag = False |
| 786 |
if device_params['state'] == 'absent' and device_details: |
| 787 |
device_details = device_object.delete_device(device_details['id']) |
| 788 |
device_object.result['changed'] = True |
| 789 |
deleteFlag = True |
| 790 |
DeviceExitHandler().handle(device_object, device_details, deleteFlag) |
| 791 |
|
| 792 |
|
| 793 |
class DeviceExitHandler(): |
| 794 |
def handle(self, device_object, device_details, deleteFlag=False): |
| 795 |
if device_object.module._diff: |
| 796 |
after_dict = copy.deepcopy(device_details) if device_details else {} |
| 797 |
after_dict = {} if deleteFlag else after_dict |
| 798 |
after_dict.pop("links", None) |
| 799 |
device_object.result["diff"]["after"].update(after_dict) |
| 800 |
device_object.result['device_details'] = device_details |
| 801 |
device_object.module.exit_json(**device_object.result) |
| 802 |
|
| 803 |
|
| 804 |
class DeviceHandler(): |
| 805 |
def handle(self, device_object, device_params): |
| 806 |
device_details = device_object.get_device( |
| 807 |
current_pathname=device_params['current_pathname'], |
| 808 |
storage_node_name=device_params['storage_node_name'], |
| 809 |
storage_node_id=device_params['storage_node_id'], |
| 810 |
device_name=device_params['device_name'], |
| 811 |
device_id=device_params['device_id']) |
| 812 |
|
| 813 |
before_dict = {} |
| 814 |
diff_dict = {} |
| 815 |
diff_dict = device_object.get_diff_after(device_params, device_details) |
| 816 |
if device_details is None: |
| 817 |
before_dict = {} |
| 818 |
else: |
| 819 |
before_dict = device_details |
| 820 |
before_dict.pop("links", None) |
| 821 |
if device_object.module._diff: |
| 822 |
device_object.result["diff"] = dict(before=before_dict, after=diff_dict) |
| 823 |
DeviceCreateHandler().handle(device_object, device_params, device_details) |
| 824 |
|
| 825 |
|
| 826 |
def main(): |
| 827 |
""" Create PowerFlex device object and perform action on it |
| 828 |
based on user input from playbook""" |
| 829 |
device_obj = PowerFlexDeviceV2() |
| 830 |
DeviceHandler().handle(device_obj, device_obj.module.params) |
| 831 |
|
| 832 |
|
| 833 |
if __name__ == '__main__': |
| 834 |
main() |