Skip to content

Commit 3c98e16

Browse files
committed
[doc] Add new python recipe to delete an element
Signed-off-by: Axel RICHARD <axel.richard@obeo.fr>
1 parent 69415bd commit 3c98e16

3 files changed

Lines changed: 171 additions & 0 deletions

File tree

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# These examples are adapted from the SysML v2 API Cookbook, available at
2+
# https://github.com/Systems-Modeling/SysML-v2-API-Cookbook, maintained by the
3+
# Object Management Group (OMG).
4+
# The original cookbook is designed for use with Jupyter Lab.
5+
# These examples have been adapted to run as standalone Python scripts, making
6+
# them suitable for use in various environments, including SysON.
7+
# They showcase practical usage scenarios and may include additional functionality
8+
# or modifications tailored to specific needs.
9+
10+
import requests # <1>
11+
from init_api import parse_arguments
12+
from init_api import init_sysmlv2_api
13+
from fetch_commits import get_last_commit_id
14+
15+
16+
def fetch_element(host, project_id, commit_id, element_id): # <2>
17+
element_url = f"{host}/projects/{project_id}/commits/{commit_id}/elements/{element_id}" # <3>
18+
response = requests.get(element_url) # <4>
19+
if response.status_code == 200:
20+
return response.json()
21+
22+
print(f"Unable to fetch element: {response.status_code} - {response.text}")
23+
return None
24+
25+
26+
def delete_element(host, project_id, element_id): # <5>
27+
commit_id = get_last_commit_id(host, project_id)
28+
if not commit_id:
29+
print("Deletion aborted: unable to determine the latest commit.")
30+
return False
31+
32+
element = fetch_element(host, project_id, commit_id, element_id)
33+
if not element:
34+
print(f"Deletion aborted: element '{element_id}' was not found.")
35+
return False
36+
37+
print(
38+
f"Deleting element '{element.get('name') or 'N/A'}' "
39+
f"(id = {element['@id']}, type = {element['@type']})"
40+
)
41+
42+
commit_url = f"{host}/projects/{project_id}/commits" # <6>
43+
commit_body = {
44+
"@type": "Commit",
45+
"change": [
46+
{
47+
"@type": "DataVersion",
48+
"identity": {
49+
"@id": element_id,
50+
"@type": "DataIdentity",
51+
},
52+
}
53+
],
54+
} # <7>
55+
56+
response = requests.post(commit_url, json=commit_body) # <8>
57+
if response.status_code != 201:
58+
print(f"Error deleting element: {response.status_code} - {response.text}")
59+
return False
60+
61+
print("Delete commit created successfully.")
62+
63+
new_commit_id = get_last_commit_id(host, project_id) # <9>
64+
if not new_commit_id:
65+
print("Deletion verification skipped: unable to determine the latest commit.")
66+
return True
67+
68+
verification_url = f"{host}/projects/{project_id}/commits/{new_commit_id}/elements/{element_id}"
69+
verification_response = requests.get(verification_url) # <10>
70+
if verification_response.status_code == 404:
71+
print(f"Element '{element_id}' deleted successfully.")
72+
return True
73+
74+
print(
75+
"Deletion verification failed: "
76+
f"the element is still accessible ({verification_response.status_code})."
77+
)
78+
return False
79+
80+
81+
if __name__ == "__main__":
82+
args = parse_arguments()
83+
host = init_sysmlv2_api()
84+
project_id = args.project_id
85+
element_id = args.element_id
86+
87+
if not element_id:
88+
raise SystemExit("Usage: python delete_element.py <project-id> <element-id>")
89+
90+
delete_element(host, project_id, element_id)

doc/content/modules/developer-guide/pages/api/api-cookbook.adoc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,5 @@ include::developer-guide:partial$create_element_recipe.adoc[leveloffset=+2]
6767
include::developer-guide:partial$new_objects_from_text.adoc[leveloffset=+2]
6868

6969
include::developer-guide:partial$import_sysml_file_recipe.adoc[leveloffset=+2]
70+
71+
include::developer-guide:partial$delete_element_recipe.adoc[leveloffset=+2]
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
= Delete element recipe (python script)
2+
3+
Learn how to delete an element programmatically.
4+
Each recipe includes a detailed explanation, step-by-step instructions, and sample code.
5+
6+
Recipes covered:
7+
8+
* <<delete_an_element>>: Delete an element from a project by creating a commit.
9+
10+
[#delete_an_element]
11+
== Delete an element
12+
This example demonstrates how to delete an existing element by using the {product} REST API.
13+
The script first fetches the latest commit of the target project, retrieves the element to confirm it exists, and then creates a new commit containing a deletion change for that element.
14+
After the commit is created, the script verifies that the element is no longer accessible in the latest commit.
15+
16+
[NOTE]
17+
====
18+
To delete an element through the commit API, send a `DataVersion` with its `identity` set to the target element ID and omit the `payload`.
19+
When an element is deleted, {product} also removes incoming relationships to maintain model integrity.
20+
====
21+
22+
Example script to delete an element:
23+
24+
[source,python]
25+
.delete_element.py
26+
----
27+
include::example$delete_element.py[]
28+
----
29+
30+
*What this code does*:
31+
32+
<1> *Import required libraries*:
33+
34+
* `requests`: Used for sending HTTP requests.
35+
<2> *Define the `fetch_element` function* with four parameters:
36+
37+
* `host`: The base API address.
38+
* `project_id`: The UUID of the project.
39+
* `commit_id`: The UUID of the commit in which the element is read.
40+
* `element_id`: The UUID of the element to fetch.
41+
42+
<3> Constructs the *API endpoint* URL for retrieving a single element.
43+
44+
<4> *Sends a `GET` request* to confirm the element exists before trying to delete it.
45+
46+
<5> *Define the `delete_element` function* with three parameters:
47+
48+
* `host`: The base API address.
49+
* `project_id`: The UUID of the project.
50+
* `element_id`: The UUID of the element to delete.
51+
52+
<6> Constructs the *API endpoint* URL for creating a commit.
53+
54+
<7> Builds the *commit payload* for deletion by setting `identity` and omitting `payload`.
55+
56+
<8> *Sends a `POST` request* to create the delete commit.
57+
58+
<9> Fetches the latest commit again after the delete operation.
59+
60+
<10> *Verifies the deletion* by checking that the element endpoint returns `404`.
61+
62+
Run the script:
63+
[source,bash]
64+
----
65+
$ python delete_element.py your-project-id your-element-id
66+
----
67+
68+
Output example:
69+
[source,bash]
70+
----
71+
Commit ID: 63a03bd8-a81a-4818-801a-01790ce8a086
72+
Last Commit ID: 63a03bd8-a81a-4818-801a-01790ce8a086
73+
Deleting element 'battery' (id = 7d5e52f4-6fd0-4f3b-9daa-9cb7f0c3e501, type = PartUsage)
74+
Delete commit created successfully.
75+
Commit ID: 63a03bd8-a81a-4818-801a-01790ce8a086
76+
Commit ID: 6f70be18-0117-4e2d-b947-a6bbf2981b63
77+
Last Commit ID: 6f70be18-0117-4e2d-b947-a6bbf2981b63
78+
Element '7d5e52f4-6fd0-4f3b-9daa-9cb7f0c3e501' deleted successfully.
79+
----

0 commit comments

Comments
 (0)