Repository navigation
Expand file tree
/
Copy pathdocument.py
More file actions
361 lines (319 loc) · 13.2 KB
/
Copy pathdocument.py
File metadata and controls
361 lines (319 loc) · 13.2 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# Copyright (c) 2026, René Moser <mail@renemoser.net>
# SPDX-License-Identifier: Apache-2.0
"""Assembly of the OpenAPI document from a ``listApis`` catalogue."""
import logging
from collections import Counter
from collections.abc import Iterable
from dataclasses import dataclass, field
from ._types import Json, Schema
from .naming import build_tagger, envelope_key, first_sentence, related_commands
from .schemas import SchemaRegistry
logger = logging.getLogger(__name__)
OPENAPI_VERSION = "3.2.0"
DEFAULT_SERVER_URL = "https://localhost:8443/client/api"
DEFAULT_TITLE = "Apache CloudStack API"
# Injected by this generator, so a command may not redefine them.
RESERVED_PARAMETERS = ("command", "response", "apiKey", "signature", "sessionkey")
COMMON_PARAMETERS: dict[str, Json] = {
"response": {
"name": "response",
"in": "query",
"description": "Format of the response body. This document only describes the JSON rendering.",
"required": False,
"schema": {"type": "string", "enum": ["json", "xml"], "default": "json"},
},
"page": {
"name": "page",
"in": "query",
"description": "Page to return, used together with `pagesize`.",
"required": False,
"schema": {"type": "integer", "format": "int32", "minimum": 1},
},
}
SECURITY_SCHEMES: dict[str, Json] = {
"apiKey": {
"type": "apiKey",
"in": "query",
"name": "apiKey",
"description": "API key of the CloudStack user. Must be sent together with `signature`.",
},
"signature": {
"type": "apiKey",
"in": "query",
"name": "signature",
"description": (
"Base64-encoded HMAC-SHA1 of the lower-cased, alphabetically sorted, "
"URL-encoded query string, signed with the user's secret key."
),
},
"sessionKey": {
"type": "apiKey",
"in": "query",
"name": "sessionkey",
"description": "Session key obtained from `login`, used instead of `apiKey` plus `signature`.",
},
}
ERROR_SCHEMA: Schema = {
"type": "object",
"description": "Error body CloudStack returns in place of the documented payload.",
"properties": {
"errorcode": {"type": "integer", "description": "Error code, mirrored in the HTTP status."},
"cserrorcode": {"type": "integer", "description": "CloudStack specific error code."},
"errortext": {"type": "string", "description": "Human readable error message."},
"uuidList": {
"type": "array",
"description": "UUIDs of the entities the error refers to.",
"items": {"type": "object"},
},
},
}
ASYNC_SCHEMA: Schema = {
"type": "object",
"description": (
"Acknowledgement of an asynchronous command. Poll `queryAsyncJobResult` with `jobid` to obtain the documented payload."
),
"properties": {
"jobid": {"type": "string", "format": "uuid", "description": "ID of the async job."},
"id": {"type": "string", "description": "ID of the entity the job acts on, when already known."},
},
"additionalProperties": True,
}
DESCRIPTION_HEADER = """\
OpenAPI rendering of the Apache CloudStack API, generated from the `listApis`
command.
**Paths in this document are synthetic.** CloudStack exposes a single endpoint
and selects the command with a query parameter, which OpenAPI cannot express:
every request actually goes to `{server}?command=<operationId>`. Each operation
is therefore keyed by a path named after its command, and carries a required
`command` parameter pinned to that command. A client generated from this
document must send the `command` parameter and ignore the path segment.
Every response body is wrapped by CloudStack in a single `<command>response`
key, which is reflected in the schemas. `listApis` describes the *entity* a
command returns rather than that envelope, so for non-list commands the payload
is modelled as either the entity itself or the entity nested under a
command-specific key.
Requests are authenticated with `apiKey` plus a `signature` computed from the
request, or with a `sessionkey` obtained from `login`.\
"""
@dataclass
class GeneratorOptions:
"""Everything that influences the document but does not come from ``listApis``."""
server_url: str = DEFAULT_SERVER_URL
title: str = DEFAULT_TITLE
api_version: str = "unknown"
self_uri: str | None = None
#: Command -> (payload key, whether the payload is an array), as resolved by
#: :func:`cloudstack_openapi.source.probe_response_keys`.
response_keys: dict[str, tuple[str, bool]] = field(default_factory=dict)
def build_parameter(param: Json, registry: SchemaRegistry) -> Json:
"""Translate one ``listApis`` request parameter into an OpenAPI query parameter."""
schema = registry.map_type(param, response=False)
description = schema.pop("description", None)
encoding = schema.pop("x-cloudstack-encoding", None)
parameter: Json = {"name": param["name"], "in": "query"}
if description:
parameter["description"] = description
if param.get("required"):
parameter["required"] = True
if (param.get("type") or "string").lower() in ("list", "set"):
# ids=a,b,c rather than ids=a&ids=b&ids=c
parameter["style"] = "form"
parameter["explode"] = False
if encoding:
parameter["x-cloudstack-encoding"] = encoding
if (length := param.get("length")) and schema.get("type") == "string":
schema["maxLength"] = length
parameter["schema"] = schema
if since := param.get("since"):
parameter["x-cloudstack-since"] = since
if related := related_commands(param.get("related")):
parameter["x-cloudstack-related"] = related
return parameter
def dedupe_params(api: Json) -> list[Json]:
"""listApis lists some parameters twice; OpenAPI requires them to be unique."""
seen: dict[str, Json] = {}
for param in api.get("params") or []:
name = param["name"]
if name in RESERVED_PARAMETERS:
logger.warning("%s declares the reserved parameter %r, skipping it", api["name"], name)
continue
if name in seen:
# Keep the first description, but never lose a "required" flag.
seen[name]["required"] = seen[name].get("required") or param.get("required")
continue
seen[name] = dict(param)
return list(seen.values())
def build_payload_schema(
api: Json,
signature: str,
registry: SchemaRegistry,
response_key: tuple[str, bool] | None,
) -> Schema:
"""Schema for the body inside the "<command>response" envelope.
listApis documents the *entity* a command deals with, never the envelope
around it. CloudStack then returns that entity either directly (delete-style
commands), nested under a command specific key (``{"zone": {...}}``), or as a
counted array (``{"count": 1, "zone": [...]}``). The key is not part of
listApis, so unless it was resolved by probing a live endpoint the schema
stays open enough to validate all three renderings.
"""
payload = registry.ref(signature)
count_property: Schema = {"type": "integer", "description": "Total number of matching entities."}
if response_key:
key, is_array = response_key
properties: dict[str, Schema] = {}
if is_array:
properties["count"] = count_property
properties[key] = {"type": "array", "items": payload}
else:
properties[key] = payload
return {"type": "object", "properties": properties, "additionalProperties": True}
nested: Schema = {
"type": "object",
"properties": {"count": count_property},
"additionalProperties": {"oneOf": [payload, {"type": "array", "items": payload}]},
}
if api["name"].startswith("list"):
return nested
return {"oneOf": [payload, nested]}
def build_operation(
api: Json,
signature: str,
registry: SchemaRegistry,
response_key: tuple[str, bool] | None,
tag: str,
) -> Json:
"""Build the ``get`` operation object for one CloudStack command."""
command = api["name"]
key = envelope_key(command)
is_async = bool(api.get("isasync"))
parameters: list[Json] = [{"$ref": "#/components/parameters/response"}]
parameters += [
build_parameter(param, registry)
for param in sorted(dedupe_params(api), key=lambda p: (not p.get("required"), p["name"]))
]
if is_async:
payload: Schema = {"$ref": "#/components/schemas/AsyncJobStart"}
else:
payload = build_payload_schema(api, signature, registry, response_key)
description = (api.get("description") or "").strip()
if is_async:
description = (
f"{description}\n\nThis command is asynchronous: it returns a `jobid`. "
"The documented entity is delivered in the `jobresult` of `queryAsyncJobResult`."
).strip()
operation: Json = {"operationId": command, "summary": first_sentence(api.get("description")) or command}
if description and description != operation["summary"]:
operation["description"] = description
operation["tags"] = [tag]
operation["parameters"] = parameters
operation["responses"] = {
"200": {
"description": f"Successful invocation of `{command}`.",
"content": {"application/json": {"schema": {"type": "object", "properties": {key: payload}, "required": [key]}}},
},
"default": {
"description": "CloudStack error. The HTTP status repeats the `errorcode` of the body.",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {key: {"$ref": "#/components/schemas/CloudStackError"}},
"required": [key],
}
}
},
},
}
operation["x-cloudstack-command"] = command
operation["x-cloudstack-async"] = is_async
if since := api.get("since"):
operation["x-cloudstack-since"] = since
if related := related_commands(api.get("related")):
operation["x-cloudstack-related"] = related
if is_async:
operation["x-cloudstack-jobresult-schema"] = registry.ref(signature)["$ref"]
return operation
def build_document(apis: Iterable[Json], options: GeneratorOptions | None = None) -> Json:
"""Build the complete OpenAPI document from a ``listApis`` catalogue.
The result is deterministic: the same catalogue and options always produce an
identical document.
"""
options = options or GeneratorOptions()
apis = sorted(apis, key=lambda api: api["name"])
registry = SchemaRegistry()
signatures = {api["name"]: registry.add(api.get("response"), api["name"], "top") for api in apis}
schemas = registry.finalize()
paths: dict[str, Json] = {}
tags: Counter[str] = Counter()
tagger = build_tagger([api["name"] for api in apis])
for api in apis:
command = api["name"]
operation = build_operation(
api,
signatures[command],
registry,
options.response_keys.get(command),
tagger(command),
)
operation["parameters"].insert(
0,
{
"name": "command",
"in": "query",
"description": "API command to invoke.",
"required": True,
# "default" repeats "const" so that documentation browsers and
# generated clients prefill the value instead of leaving the
# required parameter empty.
"schema": {"type": "string", "const": command, "default": command},
},
)
tags[operation["tags"][0]] += 1
paths[f"/{command}"] = {"get": operation}
document: Json = {"openapi": OPENAPI_VERSION}
if options.self_uri:
document["$self"] = options.self_uri
document["info"] = {
"title": options.title,
"summary": "Apache CloudStack API, generated from listApis.",
"description": DESCRIPTION_HEADER,
"license": {"name": "Apache License 2.0", "identifier": "Apache-2.0"},
"version": options.api_version,
}
document["servers"] = [
{
"url": options.server_url,
"name": "cloudstack",
"description": "CloudStack API endpoint, usually the management server plus `/client/api`.",
}
]
document["security"] = [{"apiKey": [], "signature": []}, {"sessionKey": []}]
document["tags"] = [
{"name": name, "description": f"{count} command{'' if count == 1 else 's'} acting on {name}."}
for name, count in sorted(tags.items())
]
document["paths"] = paths
document["components"] = {
"securitySchemes": SECURITY_SCHEMES,
"parameters": COMMON_PARAMETERS,
"schemas": {"CloudStackError": ERROR_SCHEMA, "AsyncJobStart": ASYNC_SCHEMA}
| {name: schemas[name] for name in sorted(schemas)},
}
return document
__all__ = [
"ASYNC_SCHEMA",
"COMMON_PARAMETERS",
"DEFAULT_SERVER_URL",
"DEFAULT_TITLE",
"ERROR_SCHEMA",
"OPENAPI_VERSION",
"RESERVED_PARAMETERS",
"SECURITY_SCHEMES",
"GeneratorOptions",
"build_document",
"build_operation",
"build_parameter",
"build_payload_schema",
"dedupe_params",
]