from dataclasses import dataclass from typing import List import requests API_BASE_PATH: str = "/api2/json" @dataclass() class ProxmoxAuthInfo: instance_url: str token_id: str secret: str verify_cert: bool = True @dataclass(frozen=True) class ProxmoxRealm: type: str realm: str comment: str = None default: bool = False def get_realms(auth_info: ProxmoxAuthInfo) -> List['ProxmoxRealm']: realm_answer = requests.get( f"{auth_info.instance_url}{API_BASE_PATH}/access/domains", headers={"Authorization": _get_token_from_authinfo(auth_info)}, verify=auth_info.verify_cert ).json() return list(map( lambda r: ProxmoxRealm(r['type'], r['realm'], r.get('comment', None), r.get('default', False)), realm_answer['data'] )) def _get_token_from_authinfo(auth_info: ProxmoxAuthInfo) -> str: return f"PVEAPIToken={auth_info.token_id}={auth_info.secret}"