| 1 |
# Copyright (c) 2022 Western Digital Corporation |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
# Note that this module util is **PRIVATE** to the collection. It can have breaking changes at any time. |
| 6 |
# Do not use this from other collections or standalone plugins/modules! |
| 7 |
|
| 8 |
from __future__ import annotations |
| 9 |
|
| 10 |
import datetime |
| 11 |
import os |
| 12 |
import re |
| 13 |
import tarfile |
| 14 |
import time |
| 15 |
import typing as t |
| 16 |
from urllib.parse import urlparse, urlunparse |
| 17 |
|
| 18 |
from ansible.module_utils.urls import fetch_file |
| 19 |
|
| 20 |
from ansible_collections.community.general.plugins.module_utils._redfish_utils import RedfishUtils |
| 21 |
|
| 22 |
if t.TYPE_CHECKING: |
| 23 |
from ansible.module_utils.basic import AnsibleModule |
| 24 |
|
| 25 |
|
| 26 |
class WdcRedfishUtils(RedfishUtils): |
| 27 |
"""Extension to RedfishUtils to support WDC enclosures.""" |
| 28 |
|
| 29 |
# Status codes returned by WDC FW Update Status |
| 30 |
UPDATE_STATUS_CODE_READY_FOR_FW_UPDATE = 0 |
| 31 |
UPDATE_STATUS_CODE_FW_UPDATE_IN_PROGRESS = 1 |
| 32 |
UPDATE_STATUS_CODE_FW_UPDATE_COMPLETED_WAITING_FOR_ACTIVATION = 2 |
| 33 |
UPDATE_STATUS_CODE_FW_UPDATE_FAILED = 3 |
| 34 |
|
| 35 |
# Status messages returned by WDC FW Update Status |
| 36 |
UPDATE_STATUS_MESSAGE_READY_FOR_FW_UDPATE = "Ready for FW update" |
| 37 |
UDPATE_STATUS_MESSAGE_FW_UPDATE_IN_PROGRESS = "FW update in progress" |
| 38 |
UPDATE_STATUS_MESSAGE_FW_UPDATE_COMPLETED_WAITING_FOR_ACTIVATION = "FW update completed. Waiting for activation." |
| 39 |
UPDATE_STATUS_MESSAGE_FW_UPDATE_FAILED = "FW update failed." |
| 40 |
|
| 41 |
# Dict keys for resource bodies |
| 42 |
# Standard keys |
| 43 |
ACTIONS = "Actions" |
| 44 |
OEM = "Oem" |
| 45 |
WDC = "WDC" |
| 46 |
TARGET = "target" |
| 47 |
|
| 48 |
# Keys for specific operations |
| 49 |
CHASSIS_LOCATE = "#Chassis.Locate" |
| 50 |
CHASSIS_POWER_MODE = "#Chassis.PowerMode" |
| 51 |
|
| 52 |
def __init__(self, creds, root_uris, timeout, module: AnsibleModule, resource_id, data_modification) -> None: |
| 53 |
super().__init__( |
| 54 |
creds=creds, |
| 55 |
root_uri=root_uris[0], |
| 56 |
timeout=timeout, |
| 57 |
module=module, |
| 58 |
resource_id=resource_id, |
| 59 |
data_modification=data_modification, |
| 60 |
) |
| 61 |
# Update the root URI if we cannot perform a Redfish GET to the first one |
| 62 |
self._set_root_uri(root_uris) |
| 63 |
|
| 64 |
def _set_root_uri(self, root_uris): |
| 65 |
"""Set the root URI from a list of options. |
| 66 |
|
| 67 |
If the current root URI is good, just keep it. Else cycle through our options until we find a good one. |
| 68 |
A URI is considered good if we can GET uri/redfish/v1. |
| 69 |
""" |
| 70 |
for root_uri in root_uris: |
| 71 |
uri = f"{root_uri}/redfish/v1" |
| 72 |
response = self.get_request(uri) |
| 73 |
if response["ret"]: |
| 74 |
self.root_uri = root_uri |
| 75 |
break |
| 76 |
|
| 77 |
def _find_updateservice_resource(self): |
| 78 |
"""Find the update service resource as well as additional WDC-specific resources.""" |
| 79 |
response = super()._find_updateservice_resource() |
| 80 |
if not response["ret"]: |
| 81 |
return response |
| 82 |
return self._find_updateservice_additional_uris() |
| 83 |
|
| 84 |
def _is_enclosure_multi_tenant_and_fetch_gen(self): |
| 85 |
"""Determine if the enclosure is multi-tenant. |
| 86 |
|
| 87 |
The serial number of a multi-tenant enclosure will end in "-A" or "-B". |
| 88 |
Fetching enclsoure generation. |
| 89 |
|
| 90 |
:return: True/False if the enclosure is multi-tenant or not and return enclosure generation; |
| 91 |
None if unable to determine. |
| 92 |
""" |
| 93 |
response = self.get_request(f"{self.root_uri}{self.service_root}Chassis/Enclosure") |
| 94 |
if response["ret"] is False: |
| 95 |
return None |
| 96 |
pattern = r".*-[A,B]" |
| 97 |
data = response["data"] |
| 98 |
if "EnclVersion" not in data: |
| 99 |
enc_version = "G1" |
| 100 |
else: |
| 101 |
enc_version = data["EnclVersion"] |
| 102 |
return re.match(pattern, data["SerialNumber"]) is not None, enc_version |
| 103 |
|
| 104 |
def _find_updateservice_additional_uris(self): |
| 105 |
"""Find & set WDC-specific update service URIs""" |
| 106 |
response = self.get_request(self.root_uri + self._update_uri()) |
| 107 |
if response["ret"] is False: |
| 108 |
return response |
| 109 |
data = response["data"] |
| 110 |
if "Actions" not in data: |
| 111 |
return {"ret": False, "msg": "Service does not support SimpleUpdate"} |
| 112 |
if "#UpdateService.SimpleUpdate" not in data["Actions"]: |
| 113 |
return {"ret": False, "msg": "Service does not support SimpleUpdate"} |
| 114 |
action = data["Actions"]["#UpdateService.SimpleUpdate"] |
| 115 |
if "target" not in action: |
| 116 |
return {"ret": False, "msg": "Service does not support SimpleUpdate"} |
| 117 |
self.simple_update_uri = action["target"] |
| 118 |
|
| 119 |
# Simple update status URI is not provided via GET /redfish/v1/UpdateService |
| 120 |
# So we have to hard code it. |
| 121 |
self.simple_update_status_uri = f"{self.simple_update_uri}/Status" |
| 122 |
|
| 123 |
# FWActivate URI |
| 124 |
if "Oem" not in data["Actions"]: |
| 125 |
return {"ret": False, "msg": "Service does not support OEM operations"} |
| 126 |
if "WDC" not in data["Actions"]["Oem"]: |
| 127 |
return {"ret": False, "msg": "Service does not support WDC operations"} |
| 128 |
if "#UpdateService.FWActivate" not in data["Actions"]["Oem"]["WDC"]: |
| 129 |
return {"ret": False, "msg": "Service does not support FWActivate"} |
| 130 |
action = data["Actions"]["Oem"]["WDC"]["#UpdateService.FWActivate"] |
| 131 |
if "target" not in action: |
| 132 |
return {"ret": False, "msg": "Service does not support FWActivate"} |
| 133 |
self.firmware_activate_uri = action["target"] |
| 134 |
return {"ret": True} |
| 135 |
|
| 136 |
def _simple_update_status_uri(self): |
| 137 |
return self.simple_update_status_uri |
| 138 |
|
| 139 |
def _firmware_activate_uri(self): |
| 140 |
return self.firmware_activate_uri |
| 141 |
|
| 142 |
def _update_uri(self): |
| 143 |
return self.update_uri |
| 144 |
|
| 145 |
def get_simple_update_status(self): |
| 146 |
"""Issue Redfish HTTP GET to return the simple update status""" |
| 147 |
result = {} |
| 148 |
response = self.get_request(self.root_uri + self._simple_update_status_uri()) |
| 149 |
if response["ret"] is False: |
| 150 |
return response |
| 151 |
result["ret"] = True |
| 152 |
data = response["data"] |
| 153 |
result["entries"] = data |
| 154 |
return result |
| 155 |
|
| 156 |
def firmware_activate(self, update_opts): |
| 157 |
"""Perform FWActivate using Redfish HTTP API.""" |
| 158 |
creds = update_opts.get("update_creds") |
| 159 |
payload = {} |
| 160 |
if creds: |
| 161 |
if creds.get("username"): |
| 162 |
payload["Username"] = creds.get("username") |
| 163 |
if creds.get("password"): |
| 164 |
payload["Password"] = creds.get("password") |
| 165 |
|
| 166 |
# Make sure the service supports FWActivate |
| 167 |
response = self.get_request(self.root_uri + self._update_uri()) |
| 168 |
if response["ret"] is False: |
| 169 |
return response |
| 170 |
data = response["data"] |
| 171 |
if "Actions" not in data: |
| 172 |
return {"ret": False, "msg": "Service does not support FWActivate"} |
| 173 |
|
| 174 |
response = self.post_request(self.root_uri + self._firmware_activate_uri(), payload) |
| 175 |
if response["ret"] is False: |
| 176 |
return response |
| 177 |
return {"ret": True, "changed": True, "msg": "FWActivate requested"} |
| 178 |
|
| 179 |
def _get_bundle_version(self, bundle_uri): |
| 180 |
"""Get the firmware version from a bundle file, and whether or not it is multi-tenant. |
| 181 |
|
| 182 |
Only supports HTTP at this time. Assumes URI exists and is a tarfile. |
| 183 |
Looks for a file oobm-[version].pkg, such as 'oobm-4.0.13.pkg`. Extracts the version number |
| 184 |
from that filename (in the above example, the version number is "4.0.13". |
| 185 |
|
| 186 |
To determine if the bundle is multi-tenant or not, it looks inside the .bin file within the tarfile, |
| 187 |
and checks the appropriate byte in the file. |
| 188 |
|
| 189 |
If not tarfile, the bundle is checked for 2048th byte to determine whether it is Gen2 bundle. |
| 190 |
Gen2 is always single tenant at this time. |
| 191 |
|
| 192 |
:param str bundle_uri: HTTP URI of the firmware bundle. |
| 193 |
:return: Firmware version number contained in the bundle, whether or not the bundle is multi-tenant |
| 194 |
and bundle generation. Either value will be None if unable to determine. |
| 195 |
:rtype: str or None, bool or None |
| 196 |
""" |
| 197 |
bundle_temp_filename = fetch_file(module=self.module, url=bundle_uri) |
| 198 |
bundle_version = None |
| 199 |
is_multi_tenant = None |
| 200 |
gen = None |
| 201 |
|
| 202 |
# If not tarfile, then if the file has "MMG2" or "DPG2" at 2048th byte |
| 203 |
# then the bundle is for MM or DP G2 |
| 204 |
if not tarfile.is_tarfile(bundle_temp_filename): |
| 205 |
cookie1 = None |
| 206 |
with open(bundle_temp_filename, "rb") as bundle_file: |
| 207 |
file_size = os.path.getsize(bundle_temp_filename) |
| 208 |
if file_size >= 2052: |
| 209 |
bundle_file.seek(2048) |
| 210 |
cookie1 = bundle_file.read(4) |
| 211 |
# It is anticipated that DP firmware bundle will be having the value "DPG2" |
| 212 |
# for cookie1 in the header |
| 213 |
if cookie1 and cookie1.decode("utf8") == "MMG2" or cookie1.decode("utf8") == "DPG2": |
| 214 |
file_name, ext = os.path.splitext(str(bundle_uri.rsplit("/", 1)[1])) |
| 215 |
# G2 bundle file name: Ultrastar-Data102_3000_SEP_1010-032_2.1.12 |
| 216 |
parsedFileName = file_name.split("_") |
| 217 |
if len(parsedFileName) == 5: |
| 218 |
bundle_version = parsedFileName[4] |
| 219 |
# MM G2 is always single tanant |
| 220 |
is_multi_tenant = False |
| 221 |
gen = "G2" |
| 222 |
|
| 223 |
return bundle_version, is_multi_tenant, gen |
| 224 |
|
| 225 |
# Bundle is for MM or DP G1 |
| 226 |
tf = tarfile.open(bundle_temp_filename) |
| 227 |
pattern_pkg = r"oobm-(.+)\.pkg" |
| 228 |
pattern_bin = r"(.*\.bin)" |
| 229 |
bundle_version = None |
| 230 |
is_multi_tenant = None |
| 231 |
for filename in tf.getnames(): |
| 232 |
match_pkg = re.match(pattern_pkg, filename) |
| 233 |
if match_pkg is not None: |
| 234 |
bundle_version = match_pkg.group(1) |
| 235 |
match_bin = re.match(pattern_bin, filename) |
| 236 |
if match_bin is not None: |
| 237 |
bin_filename = match_bin.group(1) |
| 238 |
bin_file = tf.extractfile(bin_filename) |
| 239 |
bin_file.seek(11) |
| 240 |
byte_11 = bin_file.read(1) |
| 241 |
is_multi_tenant = byte_11 == b"\x80" |
| 242 |
gen = "G1" |
| 243 |
|
| 244 |
return bundle_version, is_multi_tenant, gen |
| 245 |
|
| 246 |
@staticmethod |
| 247 |
def uri_is_http(uri): |
| 248 |
"""Return True if the specified URI is http or https. |
| 249 |
|
| 250 |
:param str uri: A URI. |
| 251 |
:return: True if the URI is http or https, else False |
| 252 |
:rtype: bool |
| 253 |
""" |
| 254 |
parsed_bundle_uri = urlparse(uri) |
| 255 |
return parsed_bundle_uri.scheme.lower() in ["http", "https"] |
| 256 |
|
| 257 |
def update_and_activate(self, update_opts): |
| 258 |
"""Update and activate the firmware in a single action. |
| 259 |
|
| 260 |
Orchestrates the firmware update so that everything can be done in a single command. |
| 261 |
Compares the update version with the already-installed version -- skips update if they are the same. |
| 262 |
Performs retries, handles timeouts as needed. |
| 263 |
|
| 264 |
""" |
| 265 |
# Convert credentials to standard HTTP format |
| 266 |
if ( |
| 267 |
update_opts.get("update_creds") is not None |
| 268 |
and "username" in update_opts["update_creds"] |
| 269 |
and "password" in update_opts["update_creds"] |
| 270 |
): |
| 271 |
update_creds = update_opts["update_creds"] |
| 272 |
parsed_url = urlparse(update_opts["update_image_uri"]) |
| 273 |
if update_creds: |
| 274 |
original_netloc = parsed_url.netloc |
| 275 |
parsed_url = parsed_url._replace( |
| 276 |
netloc=f"{update_creds.get('username')}:{update_creds.get('password')}@{original_netloc}" |
| 277 |
) |
| 278 |
update_opts["update_image_uri"] = urlunparse(parsed_url) |
| 279 |
del update_opts["update_creds"] |
| 280 |
|
| 281 |
# Make sure bundle URI is HTTP(s) |
| 282 |
bundle_uri = update_opts["update_image_uri"] |
| 283 |
|
| 284 |
if not self.uri_is_http(bundle_uri): |
| 285 |
return {"ret": False, "msg": "Bundle URI must be HTTP or HTTPS"} |
| 286 |
# Make sure IOM is ready for update |
| 287 |
result = self.get_simple_update_status() |
| 288 |
if result["ret"] is False: |
| 289 |
return result |
| 290 |
update_status = result["entries"] |
| 291 |
status_code = update_status["StatusCode"] |
| 292 |
status_description = update_status["Description"] |
| 293 |
if status_code not in [self.UPDATE_STATUS_CODE_READY_FOR_FW_UPDATE, self.UPDATE_STATUS_CODE_FW_UPDATE_FAILED]: |
| 294 |
return { |
| 295 |
"ret": False, |
| 296 |
"msg": f"Target is not ready for FW update. Current status: {status_code} ({status_description})", |
| 297 |
} |
| 298 |
|
| 299 |
# Check the FW version in the bundle file, and compare it to what is already on the IOMs |
| 300 |
|
| 301 |
# Bundle version number |
| 302 |
bundle_firmware_version, is_bundle_multi_tenant, bundle_gen = self._get_bundle_version(bundle_uri) |
| 303 |
if bundle_firmware_version is None or is_bundle_multi_tenant is None or bundle_gen is None: |
| 304 |
return { |
| 305 |
"ret": False, |
| 306 |
"msg": "Unable to extract bundle version or multi-tenant status or generation from update image file", |
| 307 |
} |
| 308 |
|
| 309 |
is_enclosure_multi_tenant, enclosure_gen = self._is_enclosure_multi_tenant_and_fetch_gen() |
| 310 |
|
| 311 |
# Verify that the bundle is correctly multi-tenant or not |
| 312 |
if is_enclosure_multi_tenant != is_bundle_multi_tenant: |
| 313 |
return { |
| 314 |
"ret": False, |
| 315 |
"msg": f"Enclosure multi-tenant is {is_enclosure_multi_tenant} but bundle multi-tenant is {is_bundle_multi_tenant}", |
| 316 |
} |
| 317 |
|
| 318 |
# Verify that the bundle is compliant with the target enclosure |
| 319 |
if enclosure_gen != bundle_gen: |
| 320 |
return {"ret": False, "msg": f"Enclosure generation is {enclosure_gen} but bundle is of {bundle_gen}"} |
| 321 |
|
| 322 |
# Version number installed on IOMs |
| 323 |
firmware_inventory = self.get_firmware_inventory() |
| 324 |
if not firmware_inventory["ret"]: |
| 325 |
return firmware_inventory |
| 326 |
firmware_inventory_dict = {} |
| 327 |
for entry in firmware_inventory["entries"]: |
| 328 |
firmware_inventory_dict[entry["Id"]] = entry |
| 329 |
iom_a_firmware_version = firmware_inventory_dict.get("IOModuleA_OOBM", {}).get("Version") |
| 330 |
iom_b_firmware_version = firmware_inventory_dict.get("IOModuleB_OOBM", {}).get("Version") |
| 331 |
# If version is None, we will proceed with the update, because we cannot tell |
| 332 |
# for sure that we have a full version match. |
| 333 |
if is_enclosure_multi_tenant: |
| 334 |
# For multi-tenant, only one of the IOMs will be affected by the firmware update, |
| 335 |
# so see if that IOM already has the same firmware version as the bundle. |
| 336 |
firmware_already_installed = ( |
| 337 |
bundle_firmware_version |
| 338 |
== self._get_installed_firmware_version_of_multi_tenant_system( |
| 339 |
iom_a_firmware_version, iom_b_firmware_version |
| 340 |
) |
| 341 |
) |
| 342 |
else: |
| 343 |
# For single-tenant, see if both IOMs already have the same firmware version as the bundle. |
| 344 |
firmware_already_installed = bundle_firmware_version == iom_a_firmware_version == iom_b_firmware_version |
| 345 |
# If this FW already installed, return changed: False, and do not update the firmware. |
| 346 |
if firmware_already_installed: |
| 347 |
return {"ret": True, "changed": False, "msg": f"Version {bundle_firmware_version} already installed"} |
| 348 |
|
| 349 |
# Version numbers don't match the bundle -- proceed with update (unless we are in check mode) |
| 350 |
if self.module.check_mode: |
| 351 |
return {"ret": True, "changed": True, "msg": "Update not performed in check mode."} |
| 352 |
update_successful = False |
| 353 |
retry_interval_seconds = 5 |
| 354 |
max_number_of_retries = 5 |
| 355 |
retry_number = 0 |
| 356 |
while retry_number < max_number_of_retries and not update_successful: |
| 357 |
if retry_number != 0: |
| 358 |
time.sleep(retry_interval_seconds) |
| 359 |
retry_number += 1 |
| 360 |
|
| 361 |
result = self.simple_update(update_opts) |
| 362 |
if result["ret"] is not True: |
| 363 |
# Sometimes a timeout error is returned even though the update actually was requested. |
| 364 |
# Check the update status to see if the update is in progress. |
| 365 |
status_result = self.get_simple_update_status() |
| 366 |
if status_result["ret"] is False: |
| 367 |
continue |
| 368 |
update_status = status_result["entries"] |
| 369 |
status_code = update_status["StatusCode"] |
| 370 |
if status_code != self.UPDATE_STATUS_CODE_FW_UPDATE_IN_PROGRESS: |
| 371 |
# Update is not in progress -- retry until max number of retries |
| 372 |
continue |
| 373 |
else: |
| 374 |
update_successful = True |
| 375 |
else: |
| 376 |
update_successful = True |
| 377 |
if not update_successful: |
| 378 |
# Unable to get SimpleUpdate to work. Return the failure from the SimpleUpdate |
| 379 |
return result |
| 380 |
|
| 381 |
# Wait for "ready to activate" |
| 382 |
max_wait_minutes = 30 |
| 383 |
polling_interval_seconds = 30 |
| 384 |
status_code = self.UPDATE_STATUS_CODE_READY_FOR_FW_UPDATE |
| 385 |
start_time = datetime.datetime.now() |
| 386 |
# For a short time, target will still say "ready for firmware update" before it transitions |
| 387 |
# to "update in progress" |
| 388 |
status_codes_for_update_incomplete = [ |
| 389 |
self.UPDATE_STATUS_CODE_FW_UPDATE_IN_PROGRESS, |
| 390 |
self.UPDATE_STATUS_CODE_READY_FOR_FW_UPDATE, |
| 391 |
] |
| 392 |
iteration = 0 |
| 393 |
while ( |
| 394 |
status_code in status_codes_for_update_incomplete |
| 395 |
and datetime.datetime.now() - start_time < datetime.timedelta(minutes=max_wait_minutes) |
| 396 |
): |
| 397 |
if iteration != 0: |
| 398 |
time.sleep(polling_interval_seconds) |
| 399 |
iteration += 1 |
| 400 |
result = self.get_simple_update_status() |
| 401 |
if result["ret"] is False: |
| 402 |
continue # We may get timeouts, just keep trying until we give up |
| 403 |
update_status = result["entries"] |
| 404 |
status_code = update_status["StatusCode"] |
| 405 |
status_description = update_status["Description"] |
| 406 |
if status_code == self.UPDATE_STATUS_CODE_FW_UPDATE_IN_PROGRESS: |
| 407 |
# Once it says update in progress, "ready for update" is no longer a valid status code |
| 408 |
status_codes_for_update_incomplete = [self.UPDATE_STATUS_CODE_FW_UPDATE_IN_PROGRESS] |
| 409 |
|
| 410 |
# Update no longer in progress -- verify that it finished |
| 411 |
if status_code != self.UPDATE_STATUS_CODE_FW_UPDATE_COMPLETED_WAITING_FOR_ACTIVATION: |
| 412 |
return { |
| 413 |
"ret": False, |
| 414 |
"msg": f"Target is not ready for FW activation after update. Current status: {status_code} ({status_description})", |
| 415 |
} |
| 416 |
|
| 417 |
self.firmware_activate(update_opts) |
| 418 |
return {"ret": True, "changed": True, "msg": "Firmware updated and activation initiated."} |
| 419 |
|
| 420 |
def _get_installed_firmware_version_of_multi_tenant_system(self, iom_a_firmware_version, iom_b_firmware_version): |
| 421 |
"""Return the version for the active IOM on a multi-tenant system. |
| 422 |
|
| 423 |
Only call this on a multi-tenant system. |
| 424 |
Given the installed firmware versions for IOM A, B, this method will determine which IOM is active |
| 425 |
for this tenanat, and return that IOM's firmware version. |
| 426 |
""" |
| 427 |
# To determine which IOM we are on, try to GET each IOM resource |
| 428 |
# The one we are on will return valid data. |
| 429 |
# The other will return an error with message "IOM Module A/B cannot be read" |
| 430 |
which_iom_is_this = None |
| 431 |
for iom_letter in ["A", "B"]: |
| 432 |
iom_uri = f"Chassis/IOModule{iom_letter}FRU" |
| 433 |
response = self.get_request(self.root_uri + self.service_root + iom_uri) |
| 434 |
if response["ret"] is False: |
| 435 |
continue |
| 436 |
data = response["data"] |
| 437 |
if "Id" in data: # Assume if there is an "Id", it is valid |
| 438 |
which_iom_is_this = iom_letter |
| 439 |
break |
| 440 |
if which_iom_is_this == "A": |
| 441 |
return iom_a_firmware_version |
| 442 |
elif which_iom_is_this == "B": |
| 443 |
return iom_b_firmware_version |
| 444 |
else: |
| 445 |
return None |
| 446 |
|
| 447 |
@staticmethod |
| 448 |
def _get_led_locate_uri(data): |
| 449 |
"""Get the LED locate URI given a resource body.""" |
| 450 |
if WdcRedfishUtils.ACTIONS not in data: |
| 451 |
return None |
| 452 |
if WdcRedfishUtils.OEM not in data[WdcRedfishUtils.ACTIONS]: |
| 453 |
return None |
| 454 |
if WdcRedfishUtils.WDC not in data[WdcRedfishUtils.ACTIONS][WdcRedfishUtils.OEM]: |
| 455 |
return None |
| 456 |
if ( |
| 457 |
WdcRedfishUtils.CHASSIS_LOCATE |
| 458 |
not in data[WdcRedfishUtils.ACTIONS][WdcRedfishUtils.OEM][WdcRedfishUtils.WDC] |
| 459 |
): |
| 460 |
return None |
| 461 |
if ( |
| 462 |
WdcRedfishUtils.TARGET |
| 463 |
not in data[WdcRedfishUtils.ACTIONS][WdcRedfishUtils.OEM][WdcRedfishUtils.WDC][ |
| 464 |
WdcRedfishUtils.CHASSIS_LOCATE |
| 465 |
] |
| 466 |
): |
| 467 |
return None |
| 468 |
return data[WdcRedfishUtils.ACTIONS][WdcRedfishUtils.OEM][WdcRedfishUtils.WDC][WdcRedfishUtils.CHASSIS_LOCATE][ |
| 469 |
WdcRedfishUtils.TARGET |
| 470 |
] |
| 471 |
|
| 472 |
@staticmethod |
| 473 |
def _get_power_mode_uri(data): |
| 474 |
"""Get the Power Mode URI given a resource body.""" |
| 475 |
if WdcRedfishUtils.ACTIONS not in data: |
| 476 |
return None |
| 477 |
if WdcRedfishUtils.OEM not in data[WdcRedfishUtils.ACTIONS]: |
| 478 |
return None |
| 479 |
if WdcRedfishUtils.WDC not in data[WdcRedfishUtils.ACTIONS][WdcRedfishUtils.OEM]: |
| 480 |
return None |
| 481 |
if ( |
| 482 |
WdcRedfishUtils.CHASSIS_POWER_MODE |
| 483 |
not in data[WdcRedfishUtils.ACTIONS][WdcRedfishUtils.OEM][WdcRedfishUtils.WDC] |
| 484 |
): |
| 485 |
return None |
| 486 |
if ( |
| 487 |
WdcRedfishUtils.TARGET |
| 488 |
not in data[WdcRedfishUtils.ACTIONS][WdcRedfishUtils.OEM][WdcRedfishUtils.WDC][ |
| 489 |
WdcRedfishUtils.CHASSIS_POWER_MODE |
| 490 |
] |
| 491 |
): |
| 492 |
return None |
| 493 |
return data[WdcRedfishUtils.ACTIONS][WdcRedfishUtils.OEM][WdcRedfishUtils.WDC][ |
| 494 |
WdcRedfishUtils.CHASSIS_POWER_MODE |
| 495 |
][WdcRedfishUtils.TARGET] |
| 496 |
|
| 497 |
def manage_indicator_led(self, command, resource_uri): |
| 498 |
key = "IndicatorLED" |
| 499 |
|
| 500 |
payloads = {"IndicatorLedOn": "On", "IndicatorLedOff": "Off"} |
| 501 |
current_led_status_map = {"IndicatorLedOn": "Blinking", "IndicatorLedOff": "Off"} |
| 502 |
|
| 503 |
result = {} |
| 504 |
response = self.get_request(self.root_uri + resource_uri) |
| 505 |
if response["ret"] is False: |
| 506 |
return response |
| 507 |
result["ret"] = True |
| 508 |
data = response["data"] |
| 509 |
if key not in data: |
| 510 |
return {"ret": False, "msg": f"Key {key} not found"} |
| 511 |
current_led_status = data[key] |
| 512 |
if current_led_status == current_led_status_map[command]: |
| 513 |
return {"ret": True, "changed": False} |
| 514 |
|
| 515 |
led_locate_uri = self._get_led_locate_uri(data) |
| 516 |
if led_locate_uri is None: |
| 517 |
return {"ret": False, "msg": "LED locate URI not found."} |
| 518 |
|
| 519 |
if command in payloads.keys(): |
| 520 |
payload = {"LocateState": payloads[command]} |
| 521 |
response = self.post_request(self.root_uri + led_locate_uri, payload) |
| 522 |
if response["ret"] is False: |
| 523 |
return response |
| 524 |
else: |
| 525 |
return {"ret": False, "msg": "Invalid command"} |
| 526 |
|
| 527 |
return result |
| 528 |
|
| 529 |
def manage_chassis_power_mode(self, command): |
| 530 |
return self.manage_power_mode(command, self.chassis_uri) |
| 531 |
|
| 532 |
def manage_power_mode(self, command, resource_uri=None): |
| 533 |
if resource_uri is None: |
| 534 |
resource_uri = self.chassis_uri |
| 535 |
|
| 536 |
payloads = {"PowerModeNormal": "Normal", "PowerModeLow": "Low"} |
| 537 |
requested_power_mode = payloads[command] |
| 538 |
|
| 539 |
result = {} |
| 540 |
response = self.get_request(self.root_uri + resource_uri) |
| 541 |
if response["ret"] is False: |
| 542 |
return response |
| 543 |
result["ret"] = True |
| 544 |
data = response["data"] |
| 545 |
|
| 546 |
# Make sure the response includes Oem.WDC.PowerMode, and get current power mode |
| 547 |
power_mode = "PowerMode" |
| 548 |
if ( |
| 549 |
WdcRedfishUtils.OEM not in data |
| 550 |
or WdcRedfishUtils.WDC not in data[WdcRedfishUtils.OEM] |
| 551 |
or power_mode not in data[WdcRedfishUtils.OEM][WdcRedfishUtils.WDC] |
| 552 |
): |
| 553 |
return {"ret": False, "msg": "Resource does not support Oem.WDC.PowerMode"} |
| 554 |
current_power_mode = data[WdcRedfishUtils.OEM][WdcRedfishUtils.WDC][power_mode] |
| 555 |
if current_power_mode == requested_power_mode: |
| 556 |
return {"ret": True, "changed": False} |
| 557 |
|
| 558 |
power_mode_uri = self._get_power_mode_uri(data) |
| 559 |
if power_mode_uri is None: |
| 560 |
return {"ret": False, "msg": "Power Mode URI not found."} |
| 561 |
|
| 562 |
if command in payloads.keys(): |
| 563 |
payload = {"PowerMode": payloads[command]} |
| 564 |
response = self.post_request(self.root_uri + power_mode_uri, payload) |
| 565 |
if response["ret"] is False: |
| 566 |
return response |
| 567 |
else: |
| 568 |
return {"ret": False, "msg": "Invalid command"} |
| 569 |
|
| 570 |
return result |