fix(ipam): check for the default namespace when deleting a namespace by HFID

Merged
OG
11h ago
+72-27

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

  • InfrahubIPNamespaceMutation no longer overrides mutate_delete. It overrides _delete_obj and compares the ID of the node that the parent mutate_delete looked up by id or hfid with 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_obj the same way, to check a profile before it is deleted (profile.py).
  • The _delete_obj signature quotes "GraphqlContext", because ipam.py imports that name only for type checking and has no from __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 ns1 is 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 that ns1 still 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) and uv run invoke docs.validate pass.

What a reviewer should check

  • Moving the check from mutate_delete to _delete_obj: a rejected delete still raises ValidationError (422) before any node is deleted.

šŸ¤– Generated with Claude Code

Jira: IFC-3493
Generated by nisse, Claude session 57671fbc-4bdf-4664-8013-d3225230d0ab.