fix(ipam): check for the default namespace when deleting a namespace by HFID
Merged
OG
11h ago+72+72+01234567890123456789-27-27-01234567890123456789
Why
IpamNamespaceDelete accepts id or hfid. The namespace mutation read data["id"] to protect the default namespace, so a request that gave only hfid raised KeyError: 'id'. The response was HTTP 500 with the message 'id':
- a namespace that is not the default one could not be deleted by HFID;
- deleting the default namespace by HFID returned 500 instead of 422 with "Cannot delete default IPAM namespace".
Jira: IFC-3493. Related: #11232, which covers only the default namespace deleted by ID.
Fixes #11240.
What changed
InfrahubIPNamespaceMutationno longer overridesmutate_delete. It overrides_delete_objand compares the ID of the node that the parentmutate_deletelooked up byidorhfidwith the default namespace ID.- The check runs inside the parent's
@retry_db_transaction(name="object_delete"), so the retry stays. - Profile mutations already use
_delete_objthe same way, to check a profile before it is deleted (profile.py).
- The check runs inside the parent's
- The
_delete_objsignature quotes"GraphqlContext", becauseipam.pyimports that name only for type checking and has nofrom __future__ import annotations. - Each of the two existing namespace delete tests now runs four times: by ID and by HFID, each on the default branch and on a user branch.
- When the default namespace is deleted, the tests check the 422 error and that the namespace still exists on the branch where the delete ran and on the default branch.
- When
ns1is deleted, the tests check that it is gone from the branch where the delete ran. When the delete ran on a user branch, they also check thatns1still exists on the default branch.
- Changelog fragment:
changelog/11240.fixed.md.
Not covered
The work item says it is not known whether other delete paths, such as cascade deletes or deletes that workflows run, remove the default namespace without this check. This PR does not check or change those paths.
How it was tested
uv run pytest backend/tests/component/graphql/mutations/test_ipam.py -k ipnamespace: 10 passed.uv run pytest backend/tests/component/graphql/mutations/test_ipam.py backend/tests/component/graphql/test_mutation_delete.py: passed. This ran before the user-branch cases were added.- With the fix reverted, the four HFID cases fail with the
'id'error, on the default branch and on the user branch. uv run invoke format,uv run invoke lint(ruff, ty, mypy) anduv run invoke docs.validatepass.
What a reviewer should check
- Moving the check from
mutate_deleteto_delete_obj: a rejected delete still raisesValidationError(422) before any node is deleted.
š¤ Generated with Claude Code
Jira: IFC-3493
Generated by nisse, Claude session 57671fbc-4bdf-4664-8013-d3225230d0ab.