1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-11-25 09:48:09 +00:00
gns3-server/gns3server/api/routes/controller/snapshots.py

125 lines
3.5 KiB
Python
Raw Normal View History

2020-10-02 06:37:50 +00:00
#!/usr/bin/env python
#
# Copyright (C) 2020 GNS3 Technologies Inc.
2020-10-02 06:37:50 +00:00
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
API routes for snapshots.
2020-10-02 06:37:50 +00:00
"""
import logging
2021-04-13 09:16:50 +00:00
log = logging.getLogger()
from fastapi import APIRouter, Depends, status
2020-10-02 06:37:50 +00:00
from typing import List
from uuid import UUID
from gns3server.controller.project import Project
from gns3server.db.repositories.rbac import RbacRepository
2020-10-31 05:32:21 +00:00
from gns3server import schemas
2020-10-02 06:37:50 +00:00
from gns3server.controller import Controller
from .dependencies.database import get_repository
from .dependencies.rbac import has_privilege
2021-04-13 09:16:50 +00:00
responses = {404: {"model": schemas.ErrorMessage, "description": "Could not find project or snapshot"}}
2021-04-13 06:49:56 +00:00
router = APIRouter(responses=responses)
def dep_project(project_id: UUID) -> Project:
"""
Dependency to retrieve a project.
"""
project = Controller.instance().get_project(str(project_id))
return project
2020-10-02 06:37:50 +00:00
@router.post(
"",
status_code=status.HTTP_201_CREATED,
response_model=schemas.Snapshot,
dependencies=[Depends(has_privilege("Snapshot.Allocate"))]
)
async def create_snapshot(
snapshot_data: schemas.SnapshotCreate,
project: Project = Depends(dep_project)
) -> schemas.Snapshot:
2020-10-02 06:37:50 +00:00
"""
Create a new snapshot of a project.
Required privilege: Snapshot.Allocate
2020-10-02 06:37:50 +00:00
"""
snapshot = await project.snapshot(snapshot_data.name)
2021-04-17 14:04:28 +00:00
return snapshot.asdict()
2020-10-02 06:37:50 +00:00
@router.get(
"",
response_model=List[schemas.Snapshot],
response_model_exclude_unset=True,
dependencies=[Depends(has_privilege("Snapshot.Audit"))]
)
def get_snapshots(project: Project = Depends(dep_project)) -> List[schemas.Snapshot]:
2020-10-02 06:37:50 +00:00
"""
Return all snapshots belonging to a given project.
Required privilege: Snapshot.Audit
2020-10-02 06:37:50 +00:00
"""
snapshots = [s for s in project.snapshots.values()]
2021-04-17 14:04:28 +00:00
return [s.asdict() for s in sorted(snapshots, key=lambda s: (s.created_at, s.name))]
2020-10-02 06:37:50 +00:00
@router.delete(
"/{snapshot_id}",
status_code=status.HTTP_204_NO_CONTENT,
dependencies=[Depends(has_privilege("Snapshot.Allocate"))]
)
async def delete_snapshot(
snapshot_id: UUID,
project: Project = Depends(dep_project),
rbac_repo=Depends(get_repository(RbacRepository))
) -> None:
2020-10-02 06:37:50 +00:00
"""
Delete a snapshot.
Required privilege: Snapshot.Allocate
2020-10-02 06:37:50 +00:00
"""
await project.delete_snapshot(str(snapshot_id))
await rbac_repo.delete_all_ace_starting_with_path(f"/projects/{project.id}/snapshots/{snapshot_id}")
2020-10-02 06:37:50 +00:00
@router.post(
"/{snapshot_id}/restore",
status_code=status.HTTP_201_CREATED,
response_model=schemas.Project,
dependencies=[Depends(has_privilege("Snapshot.Restore"))]
)
async def restore_snapshot(snapshot_id: UUID, project: Project = Depends(dep_project)) -> schemas.Project:
2020-10-02 06:37:50 +00:00
"""
Restore a snapshot.
Required privilege: Snapshot.Restore
2020-10-02 06:37:50 +00:00
"""
snapshot = project.get_snapshot(str(snapshot_id))
project = await snapshot.restore()
2021-04-17 14:04:28 +00:00
return project.asdict()