-
Notifications
You must be signed in to change notification settings - Fork 16
cli: delete datasets #38
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
9 commits
Select commit
Hold shift + click to select a range
19851a5
feat: databus api key for downloading
Integer-Ctrl c10c114
refactored README.md
Integer-Ctrl c62966b
refactored README.md
Integer-Ctrl 833872d
chore: readme examples - dev to prod databus
Integer-Ctrl cb91775
feat: cli delete to delete datasets from databus
Integer-Ctrl d86d06b
Merge branch 'main' into delete-datasets
Integer-Ctrl 9d6de50
fix: coderabbit
Integer-Ctrl 565ad59
fix: coderabbit
Integer-Ctrl b6eb33e
fix: coderabbit
Integer-Ctrl 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,190 @@ | ||
| import json | ||
| import requests | ||
| from typing import List | ||
|
|
||
| from databusclient.api.utils import get_databus_id_parts_from_uri, get_json_ld_from_databus | ||
|
|
||
| def _confirm_delete(databusURI: str) -> str: | ||
| """ | ||
| Confirm deletion of a Databus resource with the user. | ||
|
|
||
| Parameters: | ||
| - databusURI: The full databus URI of the resource to delete | ||
|
|
||
| Returns: | ||
| - "confirm" if the user confirms deletion | ||
| - "skip" if the user chooses to skip deletion | ||
| - "cancel" if the user chooses to cancel the entire deletion process | ||
| """ | ||
| print(f"Are you sure you want to delete: {databusURI}?") | ||
| print("\nThis action is irreversible and will permanently remove the resource and all its data.") | ||
| while True: | ||
| choice = input("Type 'yes'/'y' to confirm, 'skip'/'s' to skip this resource, or 'cancel'/'c' to abort: ").strip().lower() | ||
| if choice in ("yes", "y"): | ||
| return "confirm" | ||
| elif choice in ("skip", "s"): | ||
| return "skip" | ||
| elif choice in ("cancel", "c"): | ||
| return "cancel" | ||
| else: | ||
| print("Invalid input. Please type 'yes'/'y', 'skip'/'s', or 'cancel'/'c'.") | ||
|
|
||
|
|
||
| def _delete_resource(databusURI: str, databus_key: str, dry_run: bool = False, force: bool = False): | ||
| """ | ||
| Delete a single Databus resource (version, artifact, group). | ||
|
|
||
| Equivalent to: | ||
| curl -X DELETE "<databusURI>" -H "accept: */*" -H "X-API-KEY: <key>" | ||
|
|
||
| Parameters: | ||
| - databusURI: The full databus URI of the resource to delete | ||
| - databus_key: Databus API key to authenticate the deletion request | ||
| - dry_run: If True, do not perform the deletion but only print what would be deleted | ||
| - force: If True, skip confirmation prompt and proceed with deletion | ||
| """ | ||
|
|
||
| # Confirm the deletion request, skip the request or cancel deletion process | ||
| if not (dry_run or force): | ||
| action = _confirm_delete(databusURI) | ||
| if action == "skip": | ||
| print(f"Skipping: {databusURI}\n") | ||
| return | ||
| if action == "cancel": | ||
| raise KeyboardInterrupt("Deletion cancelled by user.") | ||
|
|
||
| if databus_key is None: | ||
| raise ValueError("Databus API key must be provided for deletion") | ||
|
|
||
| headers = { | ||
| "accept": "*/*", | ||
| "X-API-KEY": databus_key | ||
| } | ||
|
|
||
| if dry_run: | ||
| print(f"[DRY RUN] Would delete: {databusURI}") | ||
| return | ||
|
|
||
| response = requests.delete(databusURI, headers=headers, timeout=30) | ||
|
|
||
| if response.status_code in (200, 204): | ||
| print(f"Successfully deleted: {databusURI}") | ||
| else: | ||
| raise Exception(f"Failed to delete {databusURI}: {response.status_code} - {response.text}") | ||
|
Integer-Ctrl marked this conversation as resolved.
|
||
|
|
||
|
|
||
| def _delete_list(databusURIs: List[str], databus_key: str, dry_run: bool = False, force: bool = False): | ||
| """ | ||
| Delete a list of Databus resources. | ||
|
|
||
| Parameters: | ||
| - databusURIs: List of full databus URIs of the resources to delete | ||
| - databus_key: Databus API key to authenticate the deletion requests | ||
| """ | ||
| for databusURI in databusURIs: | ||
| _delete_resource(databusURI, databus_key, dry_run=dry_run, force=force) | ||
|
|
||
|
|
||
| def _delete_artifact(databusURI: str, databus_key: str, dry_run: bool = False, force: bool = False): | ||
| """ | ||
| Delete an artifact and all its versions. | ||
|
|
||
| This function first retrieves all versions of the artifact and then deletes them one by one. | ||
| Finally, it deletes the artifact itself. | ||
|
|
||
| Parameters: | ||
| - databusURI: The full databus URI of the artifact to delete | ||
| - databus_key: Databus API key to authenticate the deletion requests | ||
| - dry_run: If True, do not perform the deletion but only print what would be deleted | ||
| """ | ||
| artifact_body = get_json_ld_from_databus(databusURI, databus_key) | ||
|
|
||
| json_dict = json.loads(artifact_body) | ||
| versions = json_dict.get("databus:hasVersion") | ||
|
|
||
| # Single version case {} | ||
| if isinstance(versions, dict): | ||
| versions = [versions] | ||
| # Multiple versions case [{}, {}] | ||
|
|
||
| # If versions is None or empty skip | ||
| if versions is None: | ||
| print(f"No versions found for artifact: {databusURI}") | ||
| else: | ||
| version_uris = [v["@id"] for v in versions if "@id" in v] | ||
| if not version_uris: | ||
| print(f"No version URIs found in artifact JSON-LD for: {databusURI}") | ||
| else: | ||
| # Delete all versions | ||
| _delete_list(version_uris, databus_key, dry_run=dry_run, force=force) | ||
|
|
||
| # Finally, delete the artifact itself | ||
| _delete_resource(databusURI, databus_key, dry_run=dry_run, force=force) | ||
|
|
||
| def _delete_group(databusURI: str, databus_key: str, dry_run: bool = False, force: bool = False): | ||
| """ | ||
| Delete a group and all its artifacts and versions. | ||
|
|
||
| This function first retrieves all artifacts of the group, then deletes each artifact (which in turn deletes its versions). | ||
| Finally, it deletes the group itself. | ||
|
|
||
| Parameters: | ||
| - databusURI: The full databus URI of the group to delete | ||
| - databus_key: Databus API key to authenticate the deletion requests | ||
| - dry_run: If True, do not perform the deletion but only print what would be deleted | ||
| """ | ||
| group_body = get_json_ld_from_databus(databusURI, databus_key) | ||
|
|
||
| json_dict = json.loads(group_body) | ||
| artifacts = json_dict.get("databus:hasArtifact", []) | ||
|
|
||
| artifact_uris = [] | ||
| for item in artifacts: | ||
| uri = item.get("@id") | ||
| if not uri: | ||
| continue | ||
| _, _, _, _, version, _ = get_databus_id_parts_from_uri(uri) | ||
| if version is None: | ||
| artifact_uris.append(uri) | ||
|
|
||
| # Delete all artifacts (which deletes their versions) | ||
| for artifact_uri in artifact_uris: | ||
| _delete_artifact(artifact_uri, databus_key, dry_run=dry_run, force=force) | ||
|
|
||
| # Finally, delete the group itself | ||
| _delete_resource(databusURI, databus_key, dry_run=dry_run, force=force) | ||
|
|
||
| def delete(databusURIs: List[str], databus_key: str, dry_run: bool, force: bool): | ||
| """ | ||
| Delete a dataset from the databus. | ||
|
|
||
| Delete a group, artifact, or version identified by the given databus URI. | ||
| Will recursively delete all data associated with the dataset. | ||
|
|
||
| Parameters: | ||
| - databusURIs: List of full databus URIs of the resources to delete | ||
| - databus_key: Databus API key to authenticate the deletion requests | ||
| - dry_run: If True, will only print what would be deleted without performing actual deletions | ||
| - force: If True, skip confirmation prompt and proceed with deletion | ||
| """ | ||
|
|
||
| for databusURI in databusURIs: | ||
| _host, _account, group, artifact, version, file = get_databus_id_parts_from_uri(databusURI) | ||
|
|
||
| if group == "collections" and artifact is not None: | ||
| print(f"Deleting collection: {databusURI}") | ||
| _delete_resource(databusURI, databus_key, dry_run=dry_run, force=force) | ||
| elif file is not None: | ||
| print(f"Deleting file is not supported via API: {databusURI}") | ||
| continue # skip file deletions | ||
| elif version is not None: | ||
| print(f"Deleting version: {databusURI}") | ||
| _delete_resource(databusURI, databus_key, dry_run=dry_run, force=force) | ||
| elif artifact is not None: | ||
| print(f"Deleting artifact and all its versions: {databusURI}") | ||
| _delete_artifact(databusURI, databus_key, dry_run=dry_run, force=force) | ||
| elif group is not None and group != "collections": | ||
| print(f"Deleting group and all its artifacts and versions: {databusURI}") | ||
| _delete_group(databusURI, databus_key, dry_run=dry_run, force=force) | ||
| else: | ||
| print(f"Deleting {databusURI} is not supported.") | ||
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,37 @@ | ||
| import requests | ||
| from typing import Tuple, Optional | ||
|
|
||
| def get_databus_id_parts_from_uri(uri: str) -> Tuple[Optional[str], Optional[str], Optional[str], Optional[str], Optional[str], Optional[str]]: | ||
| """ | ||
| Extract databus ID parts from a given databus URI. | ||
|
|
||
| Parameters: | ||
| - uri: The full databus URI | ||
|
|
||
| Returns: | ||
| A tuple containing (host, accountId, groupId, artifactId, versionId, fileId). | ||
| Each element is a string or None if not present. | ||
| """ | ||
| uri = uri.removeprefix("https://").removeprefix("http://") | ||
| parts = uri.strip("/").split("/") | ||
| parts += [None] * (6 - len(parts)) # pad with None if less than 6 parts | ||
| return tuple(parts[:6]) # return only the first 6 parts | ||
|
|
||
| def get_json_ld_from_databus(uri: str, databus_key: str | None = None) -> str: | ||
| """ | ||
| Retrieve JSON-LD representation of a databus resource. | ||
|
|
||
| Parameters: | ||
| - uri: The full databus URI | ||
| - databus_key: Optional Databus API key for authentication on protected resources | ||
|
|
||
| Returns: | ||
| JSON-LD string representation of the databus resource. | ||
| """ | ||
| headers = {"Accept": "application/ld+json"} | ||
| if databus_key is not None: | ||
| headers["X-API-KEY"] = databus_key | ||
| response = requests.get(uri, headers=headers, timeout=30) | ||
| response.raise_for_status() | ||
|
|
||
| return response.text |
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.
Uh oh!
There was an error while loading. Please reload this page.