Merge pull request #2646 from yueguobin/feature/symbol-delete-api

feat(symbols): add DELETE API for removing custom symbol files
This commit is contained in:
Jeremy Grossmann 2026-03-27 10:51:51 +08:00 committed by GitHub
commit 3b261ebbfc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -27,7 +27,8 @@ from typing import List
from gns3server.controller import Controller
from gns3server import schemas
from gns3server.controller.controller_error import ControllerError, ControllerNotFoundError
from gns3server.controller.controller_error import ControllerError, ControllerNotFoundError, ControllerForbiddenError
from gns3server.utils.get_resource import get_resource
from .dependencies.rbac import has_privilege
@ -131,3 +132,45 @@ async def upload_symbol(symbol_id: str, request: Request) -> None:
# Reset the symbol list
controller.symbols.list()
@router.delete(
"/{symbol_id:path}",
status_code=status.HTTP_204_NO_CONTENT,
dependencies=[Depends(has_privilege("Symbol.Allocate"))]
)
async def delete_symbol(symbol_id: str) -> None:
"""
Delete a custom symbol file.
Required privilege: Symbol.Allocate
"""
controller = Controller.instance()
# Cannot delete built-in symbols
if symbol_id.startswith(":/"):
raise ControllerForbiddenError("Cannot delete built-in symbols")
try:
symbol_path = controller.symbols.get_path(symbol_id)
except (KeyError, ControllerNotFoundError) as e:
raise ControllerNotFoundError(f"Symbol '{symbol_id}' not found: {e}")
# Check if it's a built-in symbol (in resource directory)
symbols_resource_dir = get_resource("symbols")
if symbols_resource_dir and os.path.commonprefix([symbols_resource_dir, symbol_path]) == symbols_resource_dir:
raise ControllerForbiddenError("Cannot delete built-in symbols")
# Delete the file
try:
os.remove(symbol_path)
log.info(f"Deleted symbol file '{symbol_path}'")
except OSError as e:
raise ControllerError(f"Could not delete symbol file '{symbol_path}': {e}")
# Clear the symbol size cache
controller.symbols._symbol_size_cache.pop(symbol_id, None)
# Reset the symbol list
controller.symbols.list()