Mechanical: ruff format, and ruff check --fix with the rule set every NAPALM driver uses (E, F, I, UP, B at line length 100, Python 3.10). Mapping and Iterable come from collections.abc, imports are sorted. What ruff cannot fix comes in the next commit.
78 lines
3.2 KiB
Python
78 lines
3.2 KiB
Python
"""ComputerSystem (``Systems/{id}``): the managed server, its power state and reset action."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from collections.abc import Mapping
|
|
from typing import Any
|
|
|
|
from .common import ident, integer, link_list, links, number, status, text
|
|
|
|
|
|
def parse_system(system: Mapping[str, Any]) -> dict[str, Any]:
|
|
"""Identity, summaries and links of the managed server."""
|
|
state, health = status(system)
|
|
cpus = (
|
|
system.get("ProcessorSummary")
|
|
if isinstance(system.get("ProcessorSummary"), Mapping)
|
|
else {}
|
|
)
|
|
memory = system.get("MemorySummary") if isinstance(system.get("MemorySummary"), Mapping) else {}
|
|
return {
|
|
"id": text(system.get("Id")),
|
|
"name": text(system.get("Name")),
|
|
"manufacturer": ident(system.get("Manufacturer")),
|
|
"model": ident(system.get("Model")),
|
|
"serial_number": ident(system.get("SerialNumber")),
|
|
"uuid": ident(system.get("UUID")),
|
|
"hostname": text(system.get("HostName")),
|
|
"sku": ident(system.get("SKU")),
|
|
"part_number": ident(system.get("PartNumber")),
|
|
"asset_tag": ident(system.get("AssetTag")),
|
|
"bios_version": text(system.get("BiosVersion")),
|
|
"system_type": text(system.get("SystemType")),
|
|
"power_state": parse_power_state(system),
|
|
"state": state,
|
|
"health": health,
|
|
"processor_count": integer(cpus.get("Count")),
|
|
"processor_model": text(cpus.get("Model")),
|
|
"memory_gib": number(memory.get("TotalSystemMemoryGiB")),
|
|
"links": links(system),
|
|
"chassis": link_list(system, "Links", "Chassis"),
|
|
"managed_by": link_list(system, "Links", "ManagedBy"),
|
|
}
|
|
|
|
|
|
def parse_power_state(system: Mapping[str, Any]) -> str | None:
|
|
"""``PowerState``: On, Off, PoweringOn, PoweringOff or Paused; ``None`` if not reported."""
|
|
return text(system.get("PowerState"))
|
|
|
|
|
|
def parse_reset_action(
|
|
resource: Mapping[str, Any], action: str = "#ComputerSystem.Reset"
|
|
) -> dict[str, Any]:
|
|
"""Target and allowed ``ResetType`` values of a reset action.
|
|
|
|
``allowed`` is ``None`` when the service does not list the values inline. Then
|
|
``action_info`` may name the ActionInfo resource that does (see ``parse_action_info``).
|
|
Works for ``#Manager.Reset`` too.
|
|
"""
|
|
actions = resource.get("Actions")
|
|
entry = actions.get(action) if isinstance(actions, Mapping) else None
|
|
if not isinstance(entry, Mapping):
|
|
return {"target": None, "allowed": None, "action_info": None}
|
|
allowed = entry.get("ResetType@Redfish.AllowableValues")
|
|
return {
|
|
"target": text(entry.get("target")),
|
|
"allowed": [str(v) for v in allowed] if isinstance(allowed, list) else None,
|
|
"action_info": text(entry.get("@Redfish.ActionInfo")),
|
|
}
|
|
|
|
|
|
def parse_action_info(info: Mapping[str, Any], parameter: str = "ResetType") -> list[str] | None:
|
|
"""``AllowableValues`` of one parameter in an ActionInfo resource."""
|
|
for param in info.get("Parameters") or []:
|
|
if isinstance(param, Mapping) and param.get("Name") == parameter:
|
|
values = param.get("AllowableValues")
|
|
return [str(v) for v in values] if isinstance(values, list) else None
|
|
return None
|