codemie-cdk-python 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- codemie_cdk_python-0.1.0.dist-info/METADATA +892 -0
- codemie_cdk_python-0.1.0.dist-info/RECORD +40 -0
- codemie_cdk_python-0.1.0.dist-info/WHEEL +4 -0
- codemie_cdk_python-0.1.0.dist-info/entry_points.txt +3 -0
- codemie_iac/__init__.py +49 -0
- codemie_iac/cli/__init__.py +17 -0
- codemie_iac/cli/__main__.py +24 -0
- codemie_iac/cli/commands/__init__.py +17 -0
- codemie_iac/cli/commands/apply.py +119 -0
- codemie_iac/cli/commands/plan.py +72 -0
- codemie_iac/cli/config_model.py +136 -0
- codemie_iac/cli/documents.py +59 -0
- codemie_iac/cli/env_file.py +62 -0
- codemie_iac/cli/exit_status.py +80 -0
- codemie_iac/cli/init_command.py +101 -0
- codemie_iac/cli/main.py +152 -0
- codemie_iac/cli/render.py +206 -0
- codemie_iac/cli/target_config.py +258 -0
- codemie_iac/credentials.py +99 -0
- codemie_iac/envelope.py +188 -0
- codemie_iac/plan_apply/__init__.py +70 -0
- codemie_iac/plan_apply/bulk_create.py +561 -0
- codemie_iac/plan_apply/credentials.py +306 -0
- codemie_iac/plan_apply/errors.py +223 -0
- codemie_iac/plan_apply/field_policy.py +188 -0
- codemie_iac/plan_apply/guards.py +221 -0
- codemie_iac/plan_apply/kinds.py +815 -0
- codemie_iac/plan_apply/lookup.py +313 -0
- codemie_iac/plan_apply/ordering.py +177 -0
- codemie_iac/plan_apply/single_apply.py +751 -0
- codemie_iac/resolver.py +61 -0
- codemie_iac/schema.py +106 -0
- codemie_iac/serializer.py +64 -0
- codemie_iac/specs/__init__.py +13 -0
- codemie_iac/specs/assistant.py +509 -0
- codemie_iac/specs/category.py +36 -0
- codemie_iac/specs/datasource.py +595 -0
- codemie_iac/specs/integration.py +103 -0
- codemie_iac/specs/skill.py +51 -0
- codemie_iac/specs/workflow.py +154 -0
|
@@ -0,0 +1,561 @@
|
|
|
1
|
+
# Copyright 2026 EPAM Systems, Inc. ("EPAM")
|
|
2
|
+
#
|
|
3
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
# you may not use this file except in compliance with the License.
|
|
5
|
+
# You may obtain a copy of the License at
|
|
6
|
+
#
|
|
7
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
#
|
|
9
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
# See the License for the specific language governing permissions and
|
|
13
|
+
# limitations under the License.
|
|
14
|
+
|
|
15
|
+
"""Create-only, dependency-ordered bulk apply.
|
|
16
|
+
|
|
17
|
+
``BulkCreator.create_all`` first runs every preflight check
|
|
18
|
+
(``PREFLIGHT_CHECKS``: cycles, supported kinds, target project, adapter
|
|
19
|
+
support, reference resolution, credential resolution) and only then performs
|
|
20
|
+
writes, in dependency order. There is no dedupe, update, delete or scoped
|
|
21
|
+
apply: an entity whose name or content equals an existing one is still
|
|
22
|
+
created. Platform ids are held in memory for the duration of one run only,
|
|
23
|
+
and only substituted into request bodies; they are never written anywhere.
|
|
24
|
+
|
|
25
|
+
Datasource ``create`` returns no id, so ``created_id`` looks the new
|
|
26
|
+
datasource up by project and name in the target project and reports its id
|
|
27
|
+
(none when the platform does not list it yet). Assistants reference
|
|
28
|
+
datasources by ``context`` name (the SDK ``Context`` has no id), but a
|
|
29
|
+
workflow agent lists them by id, so resolving such a reference waits a bounded
|
|
30
|
+
time for the listing to show the new datasource (code and provider datasources
|
|
31
|
+
are indexed in a background task) and fails with a clear error if it never does.
|
|
32
|
+
|
|
33
|
+
``build_resolved`` (and ``ResolveContext``) are reusable by single-entity
|
|
34
|
+
apply: they turn a document's name references and credential refs into the
|
|
35
|
+
``resolved`` request-body overrides that ``KindAdapter`` expects.
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
import logging
|
|
39
|
+
import traceback
|
|
40
|
+
import uuid
|
|
41
|
+
from collections.abc import Callable, Iterable
|
|
42
|
+
from dataclasses import dataclass, field
|
|
43
|
+
from typing import Any
|
|
44
|
+
|
|
45
|
+
import yaml
|
|
46
|
+
from pydantic import BaseModel, ConfigDict
|
|
47
|
+
|
|
48
|
+
from ..credentials import CredentialRef
|
|
49
|
+
from ..envelope import Document, DocumentKind
|
|
50
|
+
from .credentials import (
|
|
51
|
+
MCP_REF_FIELDS,
|
|
52
|
+
EnvCredentialResolver,
|
|
53
|
+
ProviderSecretLookup,
|
|
54
|
+
)
|
|
55
|
+
from .errors import (
|
|
56
|
+
PLATFORM_ERRORS,
|
|
57
|
+
ApplyError,
|
|
58
|
+
FieldError,
|
|
59
|
+
PlatformRejectionError,
|
|
60
|
+
UnresolvedReferenceError,
|
|
61
|
+
scrub,
|
|
62
|
+
)
|
|
63
|
+
from .guards import guard_name, guard_project, guard_supported, with_default_project
|
|
64
|
+
from .kinds import adapter_for, created_id
|
|
65
|
+
from .lookup import EntityKey, NameLookup, Reference
|
|
66
|
+
from .ordering import EDGE_EXTRACTORS, DependencyGraph, value_edges
|
|
67
|
+
|
|
68
|
+
_LOG = logging.getLogger(__name__)
|
|
69
|
+
|
|
70
|
+
__all__ = [
|
|
71
|
+
"PREFLIGHT_CHECKS",
|
|
72
|
+
"BulkCreateReport",
|
|
73
|
+
"BulkCreator",
|
|
74
|
+
"CreatedEntity",
|
|
75
|
+
"FailedEntity",
|
|
76
|
+
"ResolveContext",
|
|
77
|
+
"build_resolved",
|
|
78
|
+
"check_adapters",
|
|
79
|
+
"check_credentials",
|
|
80
|
+
"check_cycles",
|
|
81
|
+
"check_projects",
|
|
82
|
+
"check_references",
|
|
83
|
+
"check_supported",
|
|
84
|
+
]
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
class CreatedEntity(BaseModel):
|
|
88
|
+
model_config = ConfigDict(extra="forbid")
|
|
89
|
+
|
|
90
|
+
kind: DocumentKind
|
|
91
|
+
name: str
|
|
92
|
+
id: str | None = None
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
class FailedEntity(BaseModel):
|
|
96
|
+
model_config = ConfigDict(extra="forbid")
|
|
97
|
+
|
|
98
|
+
kind: DocumentKind
|
|
99
|
+
name: str
|
|
100
|
+
error_type: str
|
|
101
|
+
reason: str
|
|
102
|
+
structured_errors: list[FieldError] | None = None
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
class BulkCreateReport(BaseModel):
|
|
106
|
+
model_config = ConfigDict(extra="forbid")
|
|
107
|
+
|
|
108
|
+
created: list[CreatedEntity]
|
|
109
|
+
failed: FailedEntity | None = None
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
@dataclass
|
|
113
|
+
class ResolveContext:
|
|
114
|
+
"""Everything needed to substitute references and credentials in memory."""
|
|
115
|
+
|
|
116
|
+
lookup: NameLookup
|
|
117
|
+
credentials: EnvCredentialResolver
|
|
118
|
+
created: dict[EntityKey, str] = field(default_factory=dict)
|
|
119
|
+
|
|
120
|
+
def ref(self, kind: DocumentKind, name: str) -> Reference:
|
|
121
|
+
return self.lookup.resolve(kind, name, self.created)
|
|
122
|
+
|
|
123
|
+
def ids(self, kind: DocumentKind, names: Iterable[str]) -> list[str]:
|
|
124
|
+
return [self.ref(kind, n).id for n in names]
|
|
125
|
+
|
|
126
|
+
def value_ids(
|
|
127
|
+
self, kind: DocumentKind, values: Iterable[str], owner: Document
|
|
128
|
+
) -> list[str]:
|
|
129
|
+
"""Platform ids for reference values that may be a name or a raw id.
|
|
130
|
+
|
|
131
|
+
Per value: a bundle name wins (even if id-shaped), then a live name in
|
|
132
|
+
the target project, then an id-shaped value passes through; anything
|
|
133
|
+
else (or an ambiguous name) is an ``UnresolvedReferenceError``.
|
|
134
|
+
"""
|
|
135
|
+
return [self._value_id(kind, v, owner) for v in values]
|
|
136
|
+
|
|
137
|
+
def _value_id(self, kind: DocumentKind, value: str, owner: Document) -> str:
|
|
138
|
+
if self.lookup.in_bundle(kind, value) is not None:
|
|
139
|
+
return self.ref(kind, value).id
|
|
140
|
+
return self._live_or_raw(kind, value, owner)
|
|
141
|
+
|
|
142
|
+
def check_value(self, kind: DocumentKind, value: str, owner: Document) -> None:
|
|
143
|
+
"""Preflight twin of ``value_ids``: raises exactly when it would."""
|
|
144
|
+
if self.lookup.in_bundle(kind, value) is None:
|
|
145
|
+
self._live_or_raw(kind, value, owner)
|
|
146
|
+
|
|
147
|
+
def _live_or_raw(self, kind: DocumentKind, value: str, owner: Document) -> str:
|
|
148
|
+
live = self.lookup.find_live(kind, value)
|
|
149
|
+
if live is not None:
|
|
150
|
+
return live.id
|
|
151
|
+
if _is_uuid(value):
|
|
152
|
+
_LOG.warning(
|
|
153
|
+
"%s %r is not in the bundle or in project %r; sending it as a "
|
|
154
|
+
"platform id without checking that it exists (referenced by %s)",
|
|
155
|
+
kind.value,
|
|
156
|
+
value,
|
|
157
|
+
self.lookup.target,
|
|
158
|
+
_label(owner),
|
|
159
|
+
)
|
|
160
|
+
return value
|
|
161
|
+
raise UnresolvedReferenceError(
|
|
162
|
+
"unresolved_reference",
|
|
163
|
+
kind.value,
|
|
164
|
+
value,
|
|
165
|
+
f"no {kind.value} named {value!r} in the bundle or in project "
|
|
166
|
+
f"{self.lookup.target!r}, and it is not a platform id "
|
|
167
|
+
f"(referenced by {_label(owner)})",
|
|
168
|
+
)
|
|
169
|
+
|
|
170
|
+
def integration(self, name: str | None) -> dict | None:
|
|
171
|
+
"""An ``Integration`` payload (no secrets) carrying the platform id."""
|
|
172
|
+
if not name:
|
|
173
|
+
return None
|
|
174
|
+
ref = self.ref(DocumentKind.INTEGRATION, name)
|
|
175
|
+
return _integration_payload(ref)
|
|
176
|
+
|
|
177
|
+
def reveal(self, value: Any) -> Any:
|
|
178
|
+
"""Replace credential-ref strings (recursively) with secret values."""
|
|
179
|
+
if isinstance(value, str):
|
|
180
|
+
return self._reveal_str(value)
|
|
181
|
+
if isinstance(value, list):
|
|
182
|
+
return [self.reveal(v) for v in value]
|
|
183
|
+
if isinstance(value, dict):
|
|
184
|
+
return {k: self.reveal(v) for k, v in value.items()}
|
|
185
|
+
return value
|
|
186
|
+
|
|
187
|
+
def _reveal_str(self, value: str) -> str:
|
|
188
|
+
try:
|
|
189
|
+
ref = CredentialRef(value)
|
|
190
|
+
except ValueError:
|
|
191
|
+
return value
|
|
192
|
+
return self.credentials.resolve(ref).reveal()
|
|
193
|
+
|
|
194
|
+
|
|
195
|
+
def _is_uuid(value: str) -> bool:
|
|
196
|
+
try:
|
|
197
|
+
return str(uuid.UUID(value)) == value.lower()
|
|
198
|
+
except ValueError:
|
|
199
|
+
return False
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def _integration_payload(ref: Reference) -> dict:
|
|
203
|
+
source = ref.source
|
|
204
|
+
spec = source.spec if isinstance(source, Document) else source
|
|
205
|
+
setting_type = getattr(spec, "setting_type", None)
|
|
206
|
+
return {
|
|
207
|
+
"id": ref.id,
|
|
208
|
+
"project_name": getattr(spec, "project_name", None),
|
|
209
|
+
"alias": getattr(spec, "alias", None),
|
|
210
|
+
"credential_type": getattr(spec, "credential_type", None),
|
|
211
|
+
"credential_values": [],
|
|
212
|
+
"setting_type": setting_type,
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
# --- resolved-body builders (one per kind) ----------------------------------
|
|
217
|
+
|
|
218
|
+
|
|
219
|
+
def _toolkit_body(toolkit: dict, ctx: ResolveContext) -> dict:
|
|
220
|
+
out = dict(toolkit)
|
|
221
|
+
out["settings"] = ctx.integration(toolkit.get("settings"))
|
|
222
|
+
out["tools"] = [
|
|
223
|
+
{**t, "settings": ctx.integration(t.get("settings"))}
|
|
224
|
+
for t in toolkit.get("tools", [])
|
|
225
|
+
]
|
|
226
|
+
return out
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
def _mcp_body(server: dict, ctx: ResolveContext) -> dict:
|
|
230
|
+
out = dict(server)
|
|
231
|
+
out["settings"] = ctx.integration(server.get("settings"))
|
|
232
|
+
out["mcp_connect_auth_token"] = ctx.integration(
|
|
233
|
+
server.get("mcp_connect_auth_token")
|
|
234
|
+
)
|
|
235
|
+
for key in MCP_REF_FIELDS:
|
|
236
|
+
if out.get(key) is not None:
|
|
237
|
+
out[key] = ctx.reveal(out[key])
|
|
238
|
+
return out
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
def _assistant_resolved(document: Document, ctx: ResolveContext) -> dict:
|
|
242
|
+
spec = document.spec
|
|
243
|
+
dumped = spec.model_dump(mode="python")
|
|
244
|
+
resolved = {
|
|
245
|
+
"assistant_ids": ctx.ids(DocumentKind.ASSISTANT, spec.assistant_ids),
|
|
246
|
+
"categories": ctx.ids(DocumentKind.CATEGORY, spec.categories),
|
|
247
|
+
"toolkits": [_toolkit_body(t, ctx) for t in dumped["toolkits"]],
|
|
248
|
+
"mcp_servers": [_mcp_body(s, ctx) for s in dumped["mcp_servers"]],
|
|
249
|
+
}
|
|
250
|
+
# An omitted ``skill_ids`` is neither diffed nor sent, so skills attached
|
|
251
|
+
# outside the document survive; an explicit empty list clears them.
|
|
252
|
+
if spec.skill_ids is not None:
|
|
253
|
+
resolved["skill_ids"] = ctx.value_ids(
|
|
254
|
+
DocumentKind.SKILL, spec.skill_ids, document
|
|
255
|
+
)
|
|
256
|
+
return resolved
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
def _integration_resolved(document: Document, ctx: ResolveContext) -> dict:
|
|
260
|
+
return {
|
|
261
|
+
"credential_values": [
|
|
262
|
+
{"key": e.key, "value": ctx.credentials.resolve(e.ref).reveal()}
|
|
263
|
+
for e in document.spec.credential_values
|
|
264
|
+
]
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
def _agent_refs(agent: Any, ctx: ResolveContext, owner: Document) -> Any:
|
|
269
|
+
"""``agent`` with its assistant, skill and datasource references as ids."""
|
|
270
|
+
update: dict[str, Any] = {}
|
|
271
|
+
if agent.assistant_id:
|
|
272
|
+
update["assistant_id"] = ctx.ref(DocumentKind.ASSISTANT, agent.assistant_id).id
|
|
273
|
+
if agent.skill_ids:
|
|
274
|
+
update["skill_ids"] = ctx.value_ids(DocumentKind.SKILL, agent.skill_ids, owner)
|
|
275
|
+
if agent.datasource_ids:
|
|
276
|
+
update["datasource_ids"] = ctx.value_ids(
|
|
277
|
+
DocumentKind.DATASOURCE, agent.datasource_ids, owner
|
|
278
|
+
)
|
|
279
|
+
return agent.model_copy(update=update) if update else agent
|
|
280
|
+
|
|
281
|
+
|
|
282
|
+
def _workflow_assistants(document: Document, ctx: ResolveContext) -> dict:
|
|
283
|
+
assistants = document.spec.workflow.assistants
|
|
284
|
+
return {key: _agent_refs(agent, ctx, document) for key, agent in assistants.items()}
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
def _yaml_error(document: Document) -> ApplyError:
|
|
288
|
+
return ApplyError(
|
|
289
|
+
"invalid_workflow_yaml",
|
|
290
|
+
document.meta.kind.value,
|
|
291
|
+
document.meta.name,
|
|
292
|
+
"rendered workflow config is not a mapping of assistants",
|
|
293
|
+
)
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
def _yaml_mapping(document: Document, rendered: str) -> dict:
|
|
297
|
+
try:
|
|
298
|
+
payload = yaml.safe_load(rendered)
|
|
299
|
+
except yaml.YAMLError:
|
|
300
|
+
raise _yaml_error(document) from None
|
|
301
|
+
if payload is None:
|
|
302
|
+
return {}
|
|
303
|
+
if not isinstance(payload, dict):
|
|
304
|
+
raise _yaml_error(document)
|
|
305
|
+
return payload
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
def _dict_items(document: Document, value: Any) -> list[dict]:
|
|
309
|
+
if value is None:
|
|
310
|
+
return []
|
|
311
|
+
if not isinstance(value, list) or not all(isinstance(v, dict) for v in value):
|
|
312
|
+
raise _yaml_error(document)
|
|
313
|
+
return value
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
def _workflow_yaml(document: Document, ctx: ResolveContext) -> str:
|
|
317
|
+
definition = document.spec.workflow.model_copy(
|
|
318
|
+
update={"assistants": _workflow_assistants(document, ctx)}
|
|
319
|
+
)
|
|
320
|
+
rendered = definition.to_yaml_config(
|
|
321
|
+
lambda name: ctx.ref(DocumentKind.ASSISTANT, name).id
|
|
322
|
+
)
|
|
323
|
+
payload = _yaml_mapping(document, rendered)
|
|
324
|
+
servers = [
|
|
325
|
+
server
|
|
326
|
+
for assistant in _dict_items(document, payload.get("assistants"))
|
|
327
|
+
for server in _dict_items(document, assistant.get("mcp_servers"))
|
|
328
|
+
]
|
|
329
|
+
# A tool declared once per workflow may carry an MCP server of its own.
|
|
330
|
+
servers += [
|
|
331
|
+
tool["mcp_server"]
|
|
332
|
+
for tool in _dict_items(document, payload.get("tools"))
|
|
333
|
+
if isinstance(tool.get("mcp_server"), dict)
|
|
334
|
+
]
|
|
335
|
+
for server in servers:
|
|
336
|
+
for key in MCP_REF_FIELDS:
|
|
337
|
+
if server.get(key) is not None:
|
|
338
|
+
server[key] = ctx.reveal(server[key])
|
|
339
|
+
return yaml.safe_dump(
|
|
340
|
+
payload, sort_keys=False, default_flow_style=False, allow_unicode=True
|
|
341
|
+
)
|
|
342
|
+
|
|
343
|
+
|
|
344
|
+
def _workflow_resolved(document: Document, ctx: ResolveContext) -> dict:
|
|
345
|
+
return {
|
|
346
|
+
"project": ctx.lookup.target,
|
|
347
|
+
"yaml_config": _workflow_yaml(document, ctx),
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
|
|
351
|
+
def _datasource_resolved(document: Document, ctx: ResolveContext) -> dict:
|
|
352
|
+
resolved: dict = {}
|
|
353
|
+
alias = document.spec.setting_id
|
|
354
|
+
if alias:
|
|
355
|
+
resolved["setting_id"] = ctx.ref(DocumentKind.INTEGRATION, alias).id
|
|
356
|
+
# Provider datasources carry free-form settings whose secret entries are
|
|
357
|
+
# ``env:`` references; reveal them only here, for the request body.
|
|
358
|
+
settings = getattr(document.spec, "settings", None)
|
|
359
|
+
if settings:
|
|
360
|
+
resolved["settings"] = ctx.reveal(settings)
|
|
361
|
+
token = getattr(document.spec, "access_token", None)
|
|
362
|
+
if token is not None:
|
|
363
|
+
resolved["access_token"] = ctx.reveal(token.root)
|
|
364
|
+
return resolved
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
def _nothing(_document: Document, _ctx: ResolveContext) -> dict:
|
|
368
|
+
return {}
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
_RESOLVED_BUILDERS: dict[DocumentKind, Callable[[Document, ResolveContext], dict]] = {
|
|
372
|
+
DocumentKind.ASSISTANT: _assistant_resolved,
|
|
373
|
+
# Skill categories are a fixed list of values, sent as authored.
|
|
374
|
+
DocumentKind.SKILL: _nothing,
|
|
375
|
+
DocumentKind.INTEGRATION: _integration_resolved,
|
|
376
|
+
DocumentKind.WORKFLOW: _workflow_resolved,
|
|
377
|
+
DocumentKind.CATEGORY: _nothing,
|
|
378
|
+
DocumentKind.DATASOURCE: _datasource_resolved,
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
def build_resolved(document: Document, ctx: ResolveContext) -> dict:
|
|
383
|
+
"""Request-body overrides for ``KindAdapter.create/update``: references
|
|
384
|
+
replaced by platform ids, credential refs replaced by secret values."""
|
|
385
|
+
return _RESOLVED_BUILDERS[document.meta.kind](document, ctx)
|
|
386
|
+
|
|
387
|
+
|
|
388
|
+
# --- preflight ----------------------------------------------------------------
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
def _label(document: Document) -> str:
|
|
392
|
+
return f"{document.meta.kind.value}/{document.meta.name}"
|
|
393
|
+
|
|
394
|
+
|
|
395
|
+
def check_cycles(documents: list[Document], _ctx: ResolveContext) -> None:
|
|
396
|
+
DependencyGraph(documents).order()
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
def check_supported(documents: list[Document], _ctx: ResolveContext) -> None:
|
|
400
|
+
for document in documents:
|
|
401
|
+
guard_supported(document)
|
|
402
|
+
|
|
403
|
+
|
|
404
|
+
def check_names(documents: list[Document], _ctx: ResolveContext) -> None:
|
|
405
|
+
for document in documents:
|
|
406
|
+
guard_name(document)
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
def check_projects(documents: list[Document], ctx: ResolveContext) -> None:
|
|
410
|
+
for document in documents:
|
|
411
|
+
guard_project(document, ctx.lookup.target)
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
def check_adapters(documents: list[Document], _ctx: ResolveContext) -> None:
|
|
415
|
+
for document in documents:
|
|
416
|
+
adapter_for(document)
|
|
417
|
+
|
|
418
|
+
|
|
419
|
+
def check_references(documents: list[Document], ctx: ResolveContext) -> None:
|
|
420
|
+
for document in documents:
|
|
421
|
+
extractor = EDGE_EXTRACTORS.get(document.meta.kind)
|
|
422
|
+
values = value_edges(document)
|
|
423
|
+
for edge in extractor(document) if extractor else ():
|
|
424
|
+
kind, name = edge
|
|
425
|
+
if edge in values:
|
|
426
|
+
ctx.check_value(kind, name, document)
|
|
427
|
+
else:
|
|
428
|
+
ctx.lookup.require(kind, name, _label(document))
|
|
429
|
+
|
|
430
|
+
|
|
431
|
+
def check_skill_categories(documents: list[Document], ctx: ResolveContext) -> None:
|
|
432
|
+
"""Skill categories are a fixed list on the platform: refuse an unknown one
|
|
433
|
+
before anything is written."""
|
|
434
|
+
skills = [d for d in documents if d.meta.kind is DocumentKind.SKILL]
|
|
435
|
+
wanted = {c for d in skills for c in d.spec.categories or []}
|
|
436
|
+
if not wanted or ctx.lookup.client is None:
|
|
437
|
+
return
|
|
438
|
+
known = {c.value for c in ctx.lookup.client.skills.list_categories()}
|
|
439
|
+
for document in skills:
|
|
440
|
+
unknown = sorted(set(document.spec.categories or []) - known)
|
|
441
|
+
if unknown:
|
|
442
|
+
raise ApplyError(
|
|
443
|
+
"unknown_skill_category",
|
|
444
|
+
document.meta.kind.value,
|
|
445
|
+
document.meta.name,
|
|
446
|
+
f"unknown skill categories {unknown}; the platform accepts "
|
|
447
|
+
f"{sorted(known)}",
|
|
448
|
+
)
|
|
449
|
+
|
|
450
|
+
|
|
451
|
+
def check_credentials(documents: list[Document], ctx: ResolveContext) -> None:
|
|
452
|
+
for document in documents:
|
|
453
|
+
for ref in ctx.credentials.collect_refs(document.spec):
|
|
454
|
+
ctx.credentials.resolve(ref)
|
|
455
|
+
|
|
456
|
+
|
|
457
|
+
#: Checks run in order before the first write; add new checks here.
|
|
458
|
+
PREFLIGHT_CHECKS: list[Callable[[list[Document], ResolveContext], None]] = [
|
|
459
|
+
check_cycles,
|
|
460
|
+
check_supported,
|
|
461
|
+
check_names,
|
|
462
|
+
check_projects,
|
|
463
|
+
check_adapters,
|
|
464
|
+
check_references,
|
|
465
|
+
check_skill_categories,
|
|
466
|
+
check_credentials,
|
|
467
|
+
]
|
|
468
|
+
|
|
469
|
+
|
|
470
|
+
# --- creator --------------------------------------------------------------------
|
|
471
|
+
|
|
472
|
+
|
|
473
|
+
class BulkCreator:
|
|
474
|
+
"""Creates every document of a bundle in dependency order."""
|
|
475
|
+
|
|
476
|
+
def __init__(
|
|
477
|
+
self,
|
|
478
|
+
client: Any,
|
|
479
|
+
credentials: EnvCredentialResolver | None = None,
|
|
480
|
+
provider_secret_lookup: ProviderSecretLookup | None = None,
|
|
481
|
+
):
|
|
482
|
+
self._client = client
|
|
483
|
+
self._credentials = credentials or EnvCredentialResolver()
|
|
484
|
+
if provider_secret_lookup is not None:
|
|
485
|
+
self._credentials = self._credentials.with_provider_secret_lookup(
|
|
486
|
+
provider_secret_lookup
|
|
487
|
+
)
|
|
488
|
+
|
|
489
|
+
def create_all(
|
|
490
|
+
self, documents: Iterable[Document], target: str
|
|
491
|
+
) -> BulkCreateReport:
|
|
492
|
+
docs = [with_default_project(document, target) for document in documents]
|
|
493
|
+
ctx = ResolveContext(NameLookup(docs, target, self._client), self._credentials)
|
|
494
|
+
for check in PREFLIGHT_CHECKS:
|
|
495
|
+
check(docs, ctx)
|
|
496
|
+
created: list[CreatedEntity] = []
|
|
497
|
+
for document in DependencyGraph(docs).order():
|
|
498
|
+
try:
|
|
499
|
+
created.append(self._create_one(document, ctx))
|
|
500
|
+
except Exception as exc: # noqa: BLE001 - keep the created list
|
|
501
|
+
error = self._as_apply_error(document, exc)
|
|
502
|
+
return BulkCreateReport(
|
|
503
|
+
created=created, failed=_failed(document, error)
|
|
504
|
+
)
|
|
505
|
+
return BulkCreateReport(created=created)
|
|
506
|
+
|
|
507
|
+
def _as_apply_error(self, document: Document, exc: Exception) -> ApplyError:
|
|
508
|
+
if isinstance(exc, ApplyError):
|
|
509
|
+
return exc
|
|
510
|
+
secrets = self._credentials.revealed_values()
|
|
511
|
+
# Not an error the platform or a check reported: most likely a bug.
|
|
512
|
+
# The report keeps only the scrubbed message, so log the scrubbed
|
|
513
|
+
# traceback to keep the stack.
|
|
514
|
+
trace = "".join(traceback.format_exception(exc))
|
|
515
|
+
_LOG.error(
|
|
516
|
+
"unexpected error for %s/%s\n%s",
|
|
517
|
+
document.meta.kind.value,
|
|
518
|
+
document.meta.name,
|
|
519
|
+
scrub(trace, secrets),
|
|
520
|
+
)
|
|
521
|
+
return ApplyError(
|
|
522
|
+
"apply_failed",
|
|
523
|
+
document.meta.kind.value,
|
|
524
|
+
document.meta.name,
|
|
525
|
+
scrub(f"{type(exc).__name__}: {exc}", secrets),
|
|
526
|
+
)
|
|
527
|
+
|
|
528
|
+
def _create_one(self, document: Document, ctx: ResolveContext) -> CreatedEntity:
|
|
529
|
+
kind, name = document.meta.kind, document.meta.name
|
|
530
|
+
adapter = adapter_for(document)
|
|
531
|
+
resolved = build_resolved(document, ctx)
|
|
532
|
+
try:
|
|
533
|
+
response = adapter.create(self._client, document, resolved)
|
|
534
|
+
except PLATFORM_ERRORS as exc:
|
|
535
|
+
# Raised below, outside the handler: the SDK error carries the
|
|
536
|
+
# request body, resolved credentials included.
|
|
537
|
+
rejection = PlatformRejectionError(
|
|
538
|
+
"platform_rejection",
|
|
539
|
+
kind.value,
|
|
540
|
+
name,
|
|
541
|
+
exc,
|
|
542
|
+
self._credentials.revealed_values(),
|
|
543
|
+
)
|
|
544
|
+
else:
|
|
545
|
+
rejection = None
|
|
546
|
+
if rejection is not None:
|
|
547
|
+
raise rejection
|
|
548
|
+
entity_id = created_id(adapter, self._client, document, response)
|
|
549
|
+
if entity_id is not None:
|
|
550
|
+
ctx.created[(kind, name)] = entity_id
|
|
551
|
+
return CreatedEntity(kind=kind, name=name, id=entity_id)
|
|
552
|
+
|
|
553
|
+
|
|
554
|
+
def _failed(document: Document, error: ApplyError) -> FailedEntity:
|
|
555
|
+
return FailedEntity(
|
|
556
|
+
kind=document.meta.kind,
|
|
557
|
+
name=document.meta.name,
|
|
558
|
+
error_type=error.error_type,
|
|
559
|
+
reason=error.reason,
|
|
560
|
+
structured_errors=getattr(error, "structured_errors", None),
|
|
561
|
+
)
|