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,188 @@
|
|
|
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
|
+
"""Hand-written, explicit field policies for the single-entity diff.
|
|
16
|
+
|
|
17
|
+
For every kind (and every datasource ``type``) each field of the live SDK
|
|
18
|
+
response is classified exactly once:
|
|
19
|
+
|
|
20
|
+
- ``deny``: server-managed or otherwise not authored by the IaC document
|
|
21
|
+
(ids, audit stamps, counters, indexing state, ciphertext...). Ignored by
|
|
22
|
+
the diff.
|
|
23
|
+
- ``comparable``: a candidate for comparison against the document spec.
|
|
24
|
+
|
|
25
|
+
The lists are written by hand on purpose, not derived from the SDK write
|
|
26
|
+
models: a new server-managed response field must be classified explicitly
|
|
27
|
+
rather than silently become a perpetual "difference".
|
|
28
|
+
|
|
29
|
+
Names refer to the *flattened* live view built by ``single_apply`` (datasource
|
|
30
|
+
sub-objects such as ``jira``/``code`` are merged into the top level, and
|
|
31
|
+
``google_doc_link`` is also exposed as ``google_doc``). The sub-object keys
|
|
32
|
+
themselves are therefore in ``deny``.
|
|
33
|
+
"""
|
|
34
|
+
|
|
35
|
+
from pydantic import BaseModel, ConfigDict
|
|
36
|
+
|
|
37
|
+
from ..envelope import Document, DocumentKind
|
|
38
|
+
|
|
39
|
+
__all__ = ["FIELD_POLICIES", "FieldPolicy", "policy_for"]
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class FieldPolicy(BaseModel):
|
|
43
|
+
model_config = ConfigDict(extra="forbid", frozen=True)
|
|
44
|
+
|
|
45
|
+
deny: frozenset[str]
|
|
46
|
+
comparable: frozenset[str]
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def _policy(deny: str, comparable: str) -> FieldPolicy:
|
|
50
|
+
return FieldPolicy(
|
|
51
|
+
deny=frozenset(deny.split()), comparable=frozenset(comparable.split())
|
|
52
|
+
)
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
_ASSISTANT = _policy(
|
|
56
|
+
# Assistant response: ids, audit stamps, usage statistics, derived values.
|
|
57
|
+
deny="id created_by system_prompt_history created_date updated_date creator "
|
|
58
|
+
"user_abilities display_name version_count unique_users_count "
|
|
59
|
+
"unique_likes_count unique_dislikes_count clone_count "
|
|
60
|
+
# Marketplace review state is set by reviewers, not by the document.
|
|
61
|
+
# Response-only: the create and update requests do not accept them.
|
|
62
|
+
"is_verified marketplace_status marketplace_decision_reason",
|
|
63
|
+
comparable="name description icon_url system_prompt project llm_model_type "
|
|
64
|
+
"toolkits conversation_starters shared is_react is_global slug temperature "
|
|
65
|
+
"top_p tools_tokens_size_limit context mcp_servers assistant_ids "
|
|
66
|
+
"skill_ids enabled_builtin_subagents prompt_variables categories tool_permissions "
|
|
67
|
+
"hedging_config interactive_enabled enable_image_generation "
|
|
68
|
+
"file_attachment_enabled",
|
|
69
|
+
)
|
|
70
|
+
|
|
71
|
+
_SKILL = _policy(
|
|
72
|
+
# slug/icon_url/mcp_servers/display_name are not authored by SkillSpec;
|
|
73
|
+
# bundle_warning is a server note about the skill bundle.
|
|
74
|
+
deny="id display_name created_date created_by updated_date is_attached "
|
|
75
|
+
"reactions_count assistants_count mcp_servers slug icon_url bundle_warning",
|
|
76
|
+
comparable="name description project visibility categories "
|
|
77
|
+
"enabled_builtin_subagents content",
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
_WORKFLOW = _policy(
|
|
81
|
+
deny="id created_date update_date created_by display_name required_variables",
|
|
82
|
+
comparable="project name description yaml_config mode shared icon_url start_hint",
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
_CATEGORY = _policy(
|
|
86
|
+
deny="id marketplace_count project_count",
|
|
87
|
+
comparable="name description",
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
_INTEGRATION = _policy(
|
|
91
|
+
deny="id date update_date user_id",
|
|
92
|
+
comparable="project_name alias default is_global credential_type "
|
|
93
|
+
"credential_values setting_type",
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
# Server-managed datasource state shared by every type. ``error``,
|
|
97
|
+
# ``updated_by`` and ``ciphertext`` are raw backend keys the SDK model ignores
|
|
98
|
+
# but which may still reach a dict-based live view.
|
|
99
|
+
_DS_DENY = (
|
|
100
|
+
"id status created_date created_by update_date updated_date "
|
|
101
|
+
"last_reindex_date error_message user_abilities processing_info "
|
|
102
|
+
"processed_documents tokens_usage completed complete_state current_state "
|
|
103
|
+
"ciphertext code git_faq jira confluence azure_devops_wiki "
|
|
104
|
+
"azure_devops_work_item xwiki svn sharepoint xray provider"
|
|
105
|
+
)
|
|
106
|
+
# ``timezone`` is only exposed alongside a ``cron_expression`` (see
|
|
107
|
+
# ``_datasource_view``): without a schedule the backend reports a default.
|
|
108
|
+
_DS_COMMON = (
|
|
109
|
+
"project_name name description type embeddings_model setting_id "
|
|
110
|
+
"shared_with_project cron_expression timezone"
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def _datasource(
|
|
115
|
+
comparable: str = "", extra_deny: str = "", not_comparable: str = ""
|
|
116
|
+
) -> FieldPolicy:
|
|
117
|
+
"""``not_comparable`` names common fields the type's update cannot carry:
|
|
118
|
+
they leave the comparable set and join the deny list."""
|
|
119
|
+
dropped = set(not_comparable.split())
|
|
120
|
+
deny = f"{_DS_DENY} {extra_deny} {not_comparable}".split()
|
|
121
|
+
# Only Google datasources author a document link (and its ``google_doc``
|
|
122
|
+
# alias, which the flattened live view derives from ``google_doc_link``).
|
|
123
|
+
if "google_doc_link" not in comparable.split():
|
|
124
|
+
deny.extend(("google_doc_link", "google_doc"))
|
|
125
|
+
return _policy(
|
|
126
|
+
deny=" ".join(deny),
|
|
127
|
+
comparable=" ".join(
|
|
128
|
+
[t for t in _DS_COMMON.split() if t not in dropped] + comparable.split()
|
|
129
|
+
),
|
|
130
|
+
)
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
# Fields only the create request accepts (or the backend does not store) are
|
|
134
|
+
# not compared: an update cannot change them, so they would differ forever.
|
|
135
|
+
# That is the Confluence ``include_*``/``keep_*`` flags, the code
|
|
136
|
+
# ``index_type``/``summarization_model``, the Git FAQ ``embedding_model`` and
|
|
137
|
+
# the Azure DevOps ``organization``/``project``.
|
|
138
|
+
_CONFLUENCE = "cql"
|
|
139
|
+
|
|
140
|
+
FIELD_POLICIES: dict[str, FieldPolicy] = {
|
|
141
|
+
DocumentKind.ASSISTANT.value: _ASSISTANT,
|
|
142
|
+
DocumentKind.SKILL.value: _SKILL,
|
|
143
|
+
DocumentKind.WORKFLOW.value: _WORKFLOW,
|
|
144
|
+
DocumentKind.CATEGORY.value: _CATEGORY,
|
|
145
|
+
DocumentKind.INTEGRATION.value: _INTEGRATION,
|
|
146
|
+
# Datasource types, keyed by spec ``type``.
|
|
147
|
+
"code": _datasource("link branch files_filter prompt docs_generation"),
|
|
148
|
+
"knowledge_base_git_faq": _datasource(
|
|
149
|
+
"link branch files_filter", extra_deny="error"
|
|
150
|
+
),
|
|
151
|
+
"knowledge_base_jira": _datasource("jql custom_fields"),
|
|
152
|
+
"knowledge_base_confluence": _datasource(_CONFLUENCE),
|
|
153
|
+
"llm_routing_google": _datasource("google_doc google_doc_link", extra_deny="error"),
|
|
154
|
+
"knowledge_base_azure_devops_wiki": _datasource("wiki_query wiki_name"),
|
|
155
|
+
"knowledge_base_azure_devops_work_item": _datasource("wiql_query"),
|
|
156
|
+
"knowledge_base_xwiki": _datasource("space wiki"),
|
|
157
|
+
# The SVN update request cannot change the index type: create-only.
|
|
158
|
+
"svn": _datasource(
|
|
159
|
+
"link branch files_filter prompt docs_generation", extra_deny="index_type"
|
|
160
|
+
),
|
|
161
|
+
"knowledge_base_sharepoint": _datasource(
|
|
162
|
+
"site_url include_pages include_documents include_lists max_file_size_mb "
|
|
163
|
+
"files_filter embedding_model auth_type oauth_client_id oauth_tenant_id",
|
|
164
|
+
# The OAuth access token is transient: the platform never returns it.
|
|
165
|
+
extra_deny="access_token",
|
|
166
|
+
),
|
|
167
|
+
# The Xray update route does not store an embedding model: create-only.
|
|
168
|
+
"knowledge_base_xray": _datasource(
|
|
169
|
+
"jql", extra_deny="embedding_model", not_comparable="embeddings_model"
|
|
170
|
+
),
|
|
171
|
+
# ``settings`` is derived from the live provider base/create params (see
|
|
172
|
+
# ``single_apply._flatten_live``); the raw params and provider id are
|
|
173
|
+
# server-shaped and denied.
|
|
174
|
+
"provider": _datasource(
|
|
175
|
+
"settings",
|
|
176
|
+
extra_deny="toolkit_id provider_name provider_id base_params create_params",
|
|
177
|
+
# The provider request takes no schedule.
|
|
178
|
+
not_comparable="cron_expression timezone",
|
|
179
|
+
),
|
|
180
|
+
"knowledge_base_file": _datasource(extra_deny="error updated_by"),
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
def policy_for(document: Document) -> FieldPolicy:
|
|
185
|
+
"""The policy for a document: by datasource ``type``, else by kind."""
|
|
186
|
+
kind = document.meta.kind
|
|
187
|
+
key = document.spec.type if kind is DocumentKind.DATASOURCE else kind.value
|
|
188
|
+
return FIELD_POLICIES[key]
|
|
@@ -0,0 +1,221 @@
|
|
|
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
|
+
"""Guards for operations that are not supported on datasources.
|
|
16
|
+
|
|
17
|
+
``guard_supported`` rejects file datasources before any adapter lookup;
|
|
18
|
+
``guard_datasource_identity`` rejects a rename or a move to another project
|
|
19
|
+
by comparing the document with the live datasource, before any write.
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
from collections.abc import Mapping
|
|
23
|
+
from typing import Any
|
|
24
|
+
|
|
25
|
+
from codemie_sdk.models.assistant import (
|
|
26
|
+
Assistant,
|
|
27
|
+
AssistantCreateRequest,
|
|
28
|
+
AssistantUpdateRequest,
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
from ..envelope import Document, DocumentKind
|
|
32
|
+
from .errors import ApplyError
|
|
33
|
+
|
|
34
|
+
__all__ = [
|
|
35
|
+
"SPEC_PROJECT_FIELDS",
|
|
36
|
+
"with_default_project",
|
|
37
|
+
"guard_datasource_identity",
|
|
38
|
+
"guard_name",
|
|
39
|
+
"guard_project",
|
|
40
|
+
"guard_supported",
|
|
41
|
+
]
|
|
42
|
+
|
|
43
|
+
#: kind -> name of the spec field that carries the entity's project (``None``
|
|
44
|
+
#: when the document omits it). Workflows
|
|
45
|
+
#: take the target project when written and categories are global, so neither
|
|
46
|
+
#: has one.
|
|
47
|
+
SPEC_PROJECT_FIELDS = {
|
|
48
|
+
DocumentKind.ASSISTANT: "project",
|
|
49
|
+
DocumentKind.SKILL: "project",
|
|
50
|
+
DocumentKind.INTEGRATION: "project_name",
|
|
51
|
+
DocumentKind.DATASOURCE: "project_name",
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
#: kind -> the spec field the platform knows the entity by. References resolve
|
|
55
|
+
#: through ``meta.name``, so it has to be the same string.
|
|
56
|
+
_SPEC_NAME_FIELDS = {
|
|
57
|
+
DocumentKind.ASSISTANT: lambda spec: spec.name,
|
|
58
|
+
DocumentKind.SKILL: lambda spec: spec.name,
|
|
59
|
+
DocumentKind.CATEGORY: lambda spec: spec.name,
|
|
60
|
+
DocumentKind.DATASOURCE: lambda spec: spec.name,
|
|
61
|
+
DocumentKind.WORKFLOW: lambda spec: spec.workflow.name,
|
|
62
|
+
DocumentKind.INTEGRATION: lambda spec: spec.alias,
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
_FILE_TYPE = "knowledge_base_file"
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def _is_datasource(document: Document) -> bool:
|
|
69
|
+
return document.meta.kind is DocumentKind.DATASOURCE
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def _error(document: Document, error_type: str, reason: str) -> ApplyError:
|
|
73
|
+
return ApplyError(error_type, document.meta.kind.value, document.meta.name, reason)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _sdk_carries_assistant_skills() -> bool:
|
|
77
|
+
"""Whether the installed SDK's assistant requests and the live assistant
|
|
78
|
+
model have a ``skill_ids`` field. Older SDKs ignore unknown fields, so
|
|
79
|
+
skills would be dropped on write and missing from the plan diff."""
|
|
80
|
+
return all(
|
|
81
|
+
"skill_ids" in cls.model_fields
|
|
82
|
+
for cls in (Assistant, AssistantCreateRequest, AssistantUpdateRequest)
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def guard_supported(document: Document) -> None:
|
|
87
|
+
"""Raise ``ApplyError`` for what cannot be written: a file datasource (no
|
|
88
|
+
adapter is consulted), a workflow without a description (the platform
|
|
89
|
+
requires one, and the update request rejects an empty one), or an assistant
|
|
90
|
+
with ``skill_ids`` (even an empty list, which detaches skills) when the
|
|
91
|
+
installed SDK cannot send them."""
|
|
92
|
+
if document.meta.kind is DocumentKind.WORKFLOW:
|
|
93
|
+
if not (document.spec.workflow.description or "").strip():
|
|
94
|
+
raise _error(
|
|
95
|
+
document,
|
|
96
|
+
"invalid_workflow",
|
|
97
|
+
"a workflow needs a non-empty description",
|
|
98
|
+
)
|
|
99
|
+
if _is_datasource(document) and document.spec.type == _FILE_TYPE:
|
|
100
|
+
raise _error(
|
|
101
|
+
document,
|
|
102
|
+
"unsupported_datasource_type",
|
|
103
|
+
f"datasource type {_FILE_TYPE!r}: file datasources are not supported",
|
|
104
|
+
)
|
|
105
|
+
if (
|
|
106
|
+
document.meta.kind is DocumentKind.ASSISTANT
|
|
107
|
+
and document.spec.skill_ids is not None
|
|
108
|
+
and not _sdk_carries_assistant_skills()
|
|
109
|
+
):
|
|
110
|
+
raise _error(
|
|
111
|
+
document,
|
|
112
|
+
"unsupported_sdk",
|
|
113
|
+
"skill_ids: the installed codemie-sdk assistant request has no "
|
|
114
|
+
"skill_ids field, so the skills would be dropped silently; upgrade "
|
|
115
|
+
"codemie-sdk-python or remove skill_ids from the document",
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def _live_value(live: Any, key: str) -> Any:
|
|
120
|
+
if isinstance(live, Mapping):
|
|
121
|
+
return live.get(key)
|
|
122
|
+
return getattr(live, key, None)
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def _live_toolkit_id(live: Any) -> Any:
|
|
126
|
+
provider = _live_value(live, "provider")
|
|
127
|
+
return _live_value(provider, "toolkit_id") if provider is not None else None
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def guard_datasource_identity(document: Document, live: Any) -> None:
|
|
131
|
+
"""Raise ``ApplyError`` if the live datasource has another name, project
|
|
132
|
+
or (provider) toolkit."""
|
|
133
|
+
if not _is_datasource(document):
|
|
134
|
+
return
|
|
135
|
+
spec = document.spec
|
|
136
|
+
live_toolkit = _live_toolkit_id(live)
|
|
137
|
+
if (
|
|
138
|
+
spec.type == "provider"
|
|
139
|
+
and live_toolkit is not None
|
|
140
|
+
and live_toolkit != spec.toolkit_id
|
|
141
|
+
):
|
|
142
|
+
raise _error(
|
|
143
|
+
document,
|
|
144
|
+
"datasource_identity_mismatch",
|
|
145
|
+
f"toolkit_id cannot change (document {spec.toolkit_id!r}, live "
|
|
146
|
+
f"{live_toolkit!r}); recreate the datasource",
|
|
147
|
+
)
|
|
148
|
+
live_name = _live_value(live, "name")
|
|
149
|
+
live_project = _live_value(live, "project_name")
|
|
150
|
+
if live_name == spec.name and live_project == spec.project_name:
|
|
151
|
+
_guard_google_document(document, live)
|
|
152
|
+
return
|
|
153
|
+
raise _error(
|
|
154
|
+
document,
|
|
155
|
+
"datasource_identity_mismatch",
|
|
156
|
+
"renaming or moving a datasource is not supported: document is "
|
|
157
|
+
f"{spec.name!r} in project {spec.project_name!r}, live is "
|
|
158
|
+
f"{live_name!r} in project {live_project!r}",
|
|
159
|
+
)
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def _guard_google_document(document: Document, live: Any) -> None:
|
|
163
|
+
"""The Google update route reindexes from the stored link and cannot take a
|
|
164
|
+
new one, so a changed document would be reported as applied and never be."""
|
|
165
|
+
if document.spec.type != "llm_routing_google":
|
|
166
|
+
return
|
|
167
|
+
live_doc = _live_value(live, "google_doc_link")
|
|
168
|
+
if live_doc is not None and live_doc != document.spec.google_doc:
|
|
169
|
+
raise _error(
|
|
170
|
+
document,
|
|
171
|
+
"unsupported_datasource_change",
|
|
172
|
+
"changing the document of a Google datasource is not supported: "
|
|
173
|
+
"create a new datasource for the new document",
|
|
174
|
+
)
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def with_default_project(document: Document, target: str) -> Document:
|
|
178
|
+
"""Return ``document`` with an omitted project set to ``target``.
|
|
179
|
+
|
|
180
|
+
A document that names a project is returned as is, so ``guard_project``
|
|
181
|
+
still refuses one that differs from the target.
|
|
182
|
+
"""
|
|
183
|
+
field = SPEC_PROJECT_FIELDS.get(document.meta.kind)
|
|
184
|
+
if field is None or getattr(document.spec, field, None) is not None:
|
|
185
|
+
return document
|
|
186
|
+
spec = document.spec.model_copy(update={field: target})
|
|
187
|
+
return document.model_copy(update={"spec": spec})
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def guard_project(document: Document, target: str) -> None:
|
|
191
|
+
"""Raise ``ApplyError`` if the document names a project other than ``target``.
|
|
192
|
+
|
|
193
|
+
The request body takes its project from the document, so an entity would
|
|
194
|
+
otherwise be created in (or moved to) a project the run does not target.
|
|
195
|
+
"""
|
|
196
|
+
field = SPEC_PROJECT_FIELDS.get(document.meta.kind)
|
|
197
|
+
project = getattr(document.spec, field, None) if field else None
|
|
198
|
+
if project is not None and project != target:
|
|
199
|
+
raise _error(
|
|
200
|
+
document,
|
|
201
|
+
"project_mismatch",
|
|
202
|
+
f"document is in project {project!r}, not target {target!r}",
|
|
203
|
+
)
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def guard_name(document: Document) -> None:
|
|
207
|
+
"""Raise ``ApplyError`` if ``meta.name`` differs from the entity's own name.
|
|
208
|
+
|
|
209
|
+
Bundle matching and the live lookup use ``meta.name``, while the platform
|
|
210
|
+
(and the names other entities reference) use the spec name, so the two
|
|
211
|
+
must be equal or a reference by either would miss.
|
|
212
|
+
"""
|
|
213
|
+
spec_name = _SPEC_NAME_FIELDS[document.meta.kind](document.spec)
|
|
214
|
+
if spec_name != document.meta.name:
|
|
215
|
+
raise _error(
|
|
216
|
+
document,
|
|
217
|
+
"name_mismatch",
|
|
218
|
+
f"meta.name {document.meta.name!r} differs from the "
|
|
219
|
+
f"{'alias' if document.meta.kind is DocumentKind.INTEGRATION else 'name'}"
|
|
220
|
+
f" in the spec {spec_name!r}; they must be equal",
|
|
221
|
+
)
|