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.
42 lines
1.4 KiB
Python
42 lines
1.4 KiB
Python
"""AccountService accounts (``AccountService/Accounts/*``)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from collections.abc import Iterable, Mapping
|
|
from typing import Any
|
|
|
|
from .common import boolean, text
|
|
|
|
# Privilege level for NAPALM's get_users, from the Redfish standard roles.
|
|
ROLE_LEVELS = {"Administrator": 15, "Operator": 5, "ReadOnly": 1}
|
|
|
|
|
|
def parse_account(account: Mapping[str, Any]) -> dict[str, Any] | None:
|
|
"""One account; ``None`` for an unused slot (no UserName), as iDRAC and Supermicro list."""
|
|
username = text(account.get("UserName"))
|
|
if username is None:
|
|
return None
|
|
types = account.get("AccountTypes")
|
|
return {
|
|
"id": text(account.get("Id")),
|
|
"username": username,
|
|
"role_id": text(account.get("RoleId")),
|
|
"enabled": boolean(account.get("Enabled")),
|
|
"locked": boolean(account.get("Locked")),
|
|
"account_types": [str(t) for t in types] if isinstance(types, list) else [],
|
|
}
|
|
|
|
|
|
def to_napalm_users(accounts: Iterable[Mapping[str, Any] | None]) -> dict[str, dict[str, Any]]:
|
|
"""NAPALM ``get_users`` from parsed accounts. Unknown (OEM) roles get level 0."""
|
|
users = {}
|
|
for acc in accounts:
|
|
if not acc:
|
|
continue
|
|
users[acc["username"]] = {
|
|
"level": ROLE_LEVELS.get(acc.get("role_id") or "", 0),
|
|
"password": "",
|
|
"sshkeys": [],
|
|
}
|
|
return users
|