|
| 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) |
0 commit comments