-
Notifications
You must be signed in to change notification settings - Fork 6
[CDF-27106] ✨ Add Data Product Versions (Alpha) #2536
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
bf6d4ae
[CDF-27106] ✨ Add Data Product Versions (Alpha)
ronpal 1a8bb52
trigger CI
ronpal 3c7c4b8
Skip DataProductVersionCRUD in integration tests
ronpal 8619445
Use bare list[] instead of builtins.list[] to match codebase style
ronpal 71d5dd4
Switch data product versions from auto-assigned versionId to semantic…
ronpal 1880b11
Refactor data product versions API: add _resolve() helper, use base c…
ronpal 1a97a04
Merge branch 'main' of https://github.com/cognitedata/toolkit into cd…
ronpal 8bcc440
Fix FakeCogniteResourceGenerator: generate valid semantic versions fo…
ronpal 62453d6
Update cognite_toolkit/_cdf_tk/client/api/data_product_versions.py
ronpal 0c53d86
Address PR review: refactor API signatures, add space validation, imp…
ronpal da2d42a
Merge branch 'main' of https://github.com/cognitedata/toolkit into cd…
ronpal 8d967c3
Merge branch 'cdf-27106/add-data-product-versions' of https://github.…
ronpal 3fec250
Reorder iterate/list methods and remove type: ignore comment
ronpal e56dd59
Fix CI: handle Annotated pattern constraints in fake generator, add v…
ronpal 1b61ae5
Update snapshot for DataProductVersionResponse in complete_org_alpha_…
ronpal 5a1663a
Add comment explaining why as_update builds the payload manually
ronpal d92b077
Refactor FakeCogniteResourceGenerator: extract constraint helpers for…
ronpal 5c5fd2b
Apply suggestion from @doctrino
ronpal 1bc39b9
Inline URL construction in DataProductVersionsAPI, remove _resolve he…
ronpal bddc651
Merge branch 'main' into cdf-27106/add-data-product-versions
ronpal 027073c
Fix ruff formatting issues
ronpal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
157 changes: 157 additions & 0 deletions
157
cognite_toolkit/_cdf_tk/client/api/data_product_versions.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| """Data Product Versions API for managing versions of CDF data products.""" | ||
|
|
||
| from collections import defaultdict | ||
| from collections.abc import Iterable, Sequence | ||
| from typing import Literal | ||
|
|
||
| from cognite_toolkit._cdf_tk.client.cdf_client import CDFResourceAPI, Endpoint, PagedResponse | ||
| from cognite_toolkit._cdf_tk.client.http_client import ( | ||
| HTTPClient, | ||
| ItemsSuccessResponse, | ||
| RequestMessage, | ||
| SuccessResponse, | ||
| ) | ||
| from cognite_toolkit._cdf_tk.client.resource_classes.data_product_version import ( | ||
| DataProductVersionRequest, | ||
| DataProductVersionResponse, | ||
| ) | ||
| from cognite_toolkit._cdf_tk.client.resource_classes.identifiers import DataProductVersionId | ||
|
|
||
|
|
||
| class DataProductVersionsAPI( | ||
| CDFResourceAPI[DataProductVersionId, DataProductVersionRequest, DataProductVersionResponse] | ||
| ): | ||
| """API for managing data product versions. | ||
|
|
||
| All endpoints are scoped under a parent data product: /dataproducts/{externalId}/versions. | ||
| Versions are identified by a user-specified semantic version string (major.minor.patch). | ||
| """ | ||
|
|
||
| def __init__(self, http_client: HTTPClient) -> None: | ||
| super().__init__( | ||
| http_client=http_client, | ||
| method_endpoint_map={ | ||
| "create": Endpoint(method="POST", path="/dataproducts/{externalId}/versions", item_limit=1), | ||
| "retrieve": Endpoint(method="GET", path="/dataproducts/{externalId}/versions/{version}", item_limit=1), | ||
| "update": Endpoint(method="POST", path="/dataproducts/{externalId}/versions/update", item_limit=1), | ||
| "delete": Endpoint(method="POST", path="/dataproducts/{externalId}/versions/delete", item_limit=10), | ||
| "list": Endpoint(method="GET", path="/dataproducts/{externalId}/versions", item_limit=10), | ||
| }, | ||
| api_version="alpha", | ||
| ) | ||
|
|
||
| def _validate_page_response( | ||
| self, response: SuccessResponse | ItemsSuccessResponse | ||
| ) -> PagedResponse[DataProductVersionResponse]: | ||
| return PagedResponse[DataProductVersionResponse].model_validate_json(response.body) | ||
|
|
||
| @staticmethod | ||
| def _group_by_parent( | ||
| items: Sequence[DataProductVersionRequest], | ||
| ) -> dict[str, list[DataProductVersionRequest]]: | ||
| by_parent: dict[str, list[DataProductVersionRequest]] = {} | ||
| for item in items: | ||
| by_parent.setdefault(item.data_product_external_id, []).append(item) | ||
| return by_parent | ||
|
|
||
| def create(self, items: Sequence[DataProductVersionRequest]) -> list[DataProductVersionResponse]: | ||
| results: list[DataProductVersionResponse] = [] | ||
| for dp_ext_id, group in self._group_by_parent(items).items(): | ||
| url = self._make_url(self._method_endpoint_map["create"].path.format(externalId=dp_ext_id)) | ||
| for item in group: | ||
| response = self._http_client.request_single_retries( | ||
| RequestMessage( | ||
| endpoint_url=url, | ||
| method="POST", | ||
| body_content={"items": [item.dump()]}, | ||
| api_version=self._api_version, | ||
| ) | ||
| ) | ||
| page = self._validate_page_response(response.get_success_or_raise()) | ||
| for version in page.items: | ||
| version.data_product_external_id = dp_ext_id | ||
| results.extend(page.items) | ||
| return results | ||
|
|
||
| def retrieve( | ||
| self, | ||
| items: Sequence[DataProductVersionId], | ||
| ignore_unknown_ids: bool = False, | ||
| ) -> list[DataProductVersionResponse]: | ||
| results: list[DataProductVersionResponse] = [] | ||
| for item in items: | ||
| url = self._make_url( | ||
| self._method_endpoint_map["retrieve"].path.format( | ||
| externalId=item.data_product_external_id, version=item.version | ||
| ) | ||
| ) | ||
| response = self._http_client.request_single_retries( | ||
| RequestMessage( | ||
| endpoint_url=url, | ||
| method="GET", | ||
| api_version=self._api_version, | ||
| ) | ||
| ) | ||
| if isinstance(response, SuccessResponse): | ||
| ver = DataProductVersionResponse.model_validate_json(response.body) | ||
| ver.data_product_external_id = item.data_product_external_id | ||
| results.append(ver) | ||
| elif ignore_unknown_ids: | ||
| continue | ||
| else: | ||
| _ = response.get_success_or_raise() | ||
| return results | ||
|
|
||
| def update( | ||
| self, | ||
| items: Sequence[DataProductVersionRequest], | ||
| mode: Literal["patch", "replace"] = "replace", | ||
| ) -> list[DataProductVersionResponse]: | ||
| results: list[DataProductVersionResponse] = [] | ||
| for dp_ext_id, group in self._group_by_parent(items).items(): | ||
| url = self._make_url(self._method_endpoint_map["update"].path.format(externalId=dp_ext_id)) | ||
| for item in group: | ||
| response = self._http_client.request_single_retries( | ||
| RequestMessage( | ||
| endpoint_url=url, | ||
| method="POST", | ||
| body_content={"items": [item.as_update(mode=mode)]}, | ||
| api_version=self._api_version, | ||
| ) | ||
| ) | ||
| page = self._validate_page_response(response.get_success_or_raise()) | ||
| for ver in page.items: | ||
| ver.data_product_external_id = dp_ext_id | ||
| results.extend(page.items) | ||
| return results | ||
|
|
||
| def delete(self, ids: Sequence[DataProductVersionId]) -> None: | ||
| by_parent: defaultdict[str, list[str]] = defaultdict(list) | ||
| for id_ in ids: | ||
| by_parent[id_.data_product_external_id].append(id_.version) | ||
| for dp_ext_id, versions in by_parent.items(): | ||
| url = self._make_url(self._method_endpoint_map["delete"].path.format(externalId=dp_ext_id)) | ||
| self._http_client.request_single_retries( | ||
| RequestMessage( | ||
| endpoint_url=url, | ||
| method="POST", | ||
| body_content={"items": [{"version": v} for v in versions]}, | ||
| api_version=self._api_version, | ||
| ) | ||
| ).get_success_or_raise() | ||
|
|
||
| def iterate( | ||
| self, data_product_external_id: str, limit: int | None = 10 | ||
| ) -> Iterable[list[DataProductVersionResponse]]: | ||
| path = self._method_endpoint_map["list"].path.format(externalId=data_product_external_id) | ||
| for batch in self._iterate(limit=limit, endpoint_path=path): | ||
| for item in batch: | ||
| item.data_product_external_id = data_product_external_id | ||
| yield batch | ||
|
|
||
| def list(self, data_product_external_id: str, limit: int | None = 10) -> list[DataProductVersionResponse]: | ||
| path = self._method_endpoint_map["list"].path.format(externalId=data_product_external_id) | ||
| items = self._list(limit=limit, endpoint_path=path) | ||
| for item in items: | ||
| item.data_product_external_id = data_product_external_id | ||
| return items |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
cognite_toolkit/_cdf_tk/client/resource_classes/data_product_version.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| from typing import Annotated, Any, ClassVar, Literal | ||
|
|
||
| from pydantic import Field | ||
|
|
||
| from cognite_toolkit._cdf_tk.client._resource_base import ( | ||
| BaseModelObject, | ||
| ResponseResource, | ||
| UpdatableRequestResource, | ||
| ) | ||
| from cognite_toolkit._cdf_tk.constants import SPACE_FORMAT_PATTERN | ||
|
|
||
| from .identifiers import DataProductVersionId, SemanticVersion | ||
|
|
||
| SpaceId = Annotated[str, Field(pattern=SPACE_FORMAT_PATTERN, max_length=43)] | ||
|
|
||
|
|
||
| class ViewInstanceSpaces(BaseModelObject): | ||
| read: list[SpaceId] = Field(default_factory=list) | ||
| write: list[SpaceId] = Field(default_factory=list) | ||
|
|
||
|
|
||
| class DataProductVersionView(BaseModelObject): | ||
| external_id: str | ||
| instance_spaces: ViewInstanceSpaces = Field(default_factory=ViewInstanceSpaces) | ||
|
|
||
|
|
||
| class DataProductVersionDataModel(BaseModelObject): | ||
| external_id: str | ||
| version: str | ||
| views: list[DataProductVersionView] = Field(default_factory=list) | ||
|
|
||
|
|
||
| class DataProductVersionTerms(BaseModelObject): | ||
| usage: str | None = None | ||
| limitations: str | None = None | ||
|
|
||
|
|
||
| class DataProductVersion(BaseModelObject): | ||
| data_product_external_id: str = Field(exclude=True) | ||
| version: SemanticVersion | ||
| data_model: DataProductVersionDataModel | ||
| status: Literal["draft", "published", "deprecated"] = "draft" | ||
| description: str | None = None | ||
| terms: DataProductVersionTerms | None = None | ||
|
|
||
| def as_id(self) -> DataProductVersionId: | ||
| return DataProductVersionId( | ||
| data_product_external_id=self.data_product_external_id, | ||
| version=self.version, | ||
| ) | ||
|
|
||
|
|
||
| class DataProductVersionRequest(DataProductVersion, UpdatableRequestResource): | ||
| container_fields: ClassVar[frozenset[str]] = frozenset() | ||
|
|
||
| def as_update(self, mode: Literal["patch", "replace"]) -> dict[str, Any]: | ||
| # The versions update API uses nested {set}/{setNull}/{modify} operators | ||
| # instead of a flat body, so we must build the payload manually. | ||
| update_item: dict[str, Any] = {"version": self.version} | ||
| update: dict[str, Any] = {} | ||
| exclude_unset = mode == "patch" | ||
| dumped = self.model_dump(mode="json", by_alias=True, exclude_unset=exclude_unset) | ||
|
|
||
| for key in ("status", "description"): | ||
| if key not in dumped: | ||
| continue | ||
| if dumped[key] is None: | ||
| update[key] = {"setNull": True} | ||
| else: | ||
| update[key] = {"set": dumped[key]} | ||
|
|
||
| if "terms" in dumped and dumped["terms"] is not None: | ||
| terms_modify: dict[str, Any] = {} | ||
| for sub_key in ("usage", "limitations"): | ||
| if sub_key in dumped["terms"]: | ||
| val = dumped["terms"][sub_key] | ||
| terms_modify[sub_key] = {"setNull": True} if val is None else {"set": val} | ||
| if terms_modify: | ||
| update["terms"] = {"modify": terms_modify} | ||
|
|
||
| if "dataModel" in dumped and dumped["dataModel"] is not None: | ||
| views = dumped["dataModel"].get("views") | ||
| if views is not None: | ||
| update["dataModel"] = {"modify": {"views": {"set": views}}} | ||
|
|
||
| update_item["update"] = update | ||
| return update_item | ||
|
|
||
|
|
||
| class DataProductVersionResponse(DataProductVersion, ResponseResource[DataProductVersionRequest]): | ||
| created_time: int | ||
| last_updated_time: int | ||
|
|
||
| def as_request_resource(self) -> DataProductVersionRequest: | ||
| data = self.dump(camel_case=False) | ||
| data["data_product_external_id"] = self.data_product_external_id | ||
| return DataProductVersionRequest.model_validate(data, extra="ignore") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is something we can give feeback on to the API team. This update is deviating significantly from other updates with very nested structure. For example, HostedExtractros have a similar issue and solves it in a more standardized way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feedback given!