Repository navigation
Expand file tree
/
Copy pathsource.py
More file actions
107 lines (87 loc) · 3.7 KB
/
Copy pathsource.py
File metadata and controls
107 lines (87 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# Copyright (c) 2026, René Moser <mail@renemoser.net>
# SPDX-License-Identifier: Apache-2.0
"""Obtaining a ``listApis`` catalogue, from a live endpoint or from a file."""
import json
import logging
from os import PathLike
from typing import Any
from ._types import Json
from .errors import DependencyError, SourceError
logger = logging.getLogger(__name__)
def load_listapis(path: str | PathLike[str]) -> list[Json]:
"""Read a catalogue from a captured ``listApis`` response.
Accepts both the raw response (``{"count": 1, "api": [...]}``) and a bare
list of command objects.
"""
with open(path, encoding="utf-8") as handle:
payload = json.load(handle)
apis = payload.get("api", payload) if isinstance(payload, dict) else payload
if not isinstance(apis, list) or not apis:
raise SourceError(f"{path} does not contain a listApis response")
return apis
def create_client(endpoint: str | None = None) -> Any:
"""Build a ``cs.CloudStack`` client from the usual CloudStack configuration.
The configuration is read by the ``cs`` library from ``~/.cloudstack.ini`` or
the ``CLOUDSTACK_*`` environment variables; *endpoint* overrides the endpoint
it resolves to.
"""
try:
from cs import CloudStack, read_config
except ImportError as exc: # pragma: no cover - depends on the installed extras
raise DependencyError(
"the 'cs' library is required to query a live endpoint; "
"install it with: pip install 'cloudstack-openapi-ngine[live]'"
) from exc
config = read_config()
if endpoint:
config["endpoint"] = endpoint
return CloudStack(**config)
def fetch_listapis(client: Any) -> list[Json]:
"""Fetch the catalogue of every command the calling account may see."""
apis = client.listApis().get("api") or []
if not apis:
raise SourceError("listApis returned no commands")
return apis
def fetch_api_version(client: Any) -> str:
"""The CloudStack version of the endpoint, or ``"unknown"``."""
try:
version = client.listCapabilities()["capability"]["cloudstackversion"]
except Exception:
# The version is cosmetic, and listCapabilities may be denied to the caller.
logger.debug("listCapabilities did not yield a version", exc_info=True)
return "unknown"
return str(version)
def probe_response_keys(client: Any, apis: list[Json]) -> dict[str, tuple[str, bool]]:
"""Resolve the per-command payload key by calling read-only list commands.
listApis does not expose the key CloudStack nests a payload under, so the
only way to learn it is to look at a real response. Only ``list*`` commands
are probed, with no arguments; commands that error or come back empty are
left unresolved.
"""
keys: dict[str, tuple[str, bool]] = {}
for api in apis:
command = api["name"]
if not command.startswith("list") or api.get("isasync"):
continue
try:
result = getattr(client, command)()
except Exception:
# A failing probe simply teaches us nothing about this command.
logger.debug("probing %s failed", command, exc_info=True)
continue
if not isinstance(result, dict):
continue
candidates = [key for key in result if key != "count"]
if len(candidates) == 1:
# Not every list command returns a collection: listCapabilities
# nests a single object under "capability".
key = candidates[0]
keys[command] = (key, isinstance(result[key], list))
return keys
__all__ = [
"create_client",
"fetch_api_version",
"fetch_listapis",
"load_listapis",
"probe_response_keys",
]