2020-10-02 09:37:50 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
2020-12-02 10:09:08 +02:00
|
|
|
# Copyright (C) 2020 GNS3 Technologies Inc.
|
2020-10-02 09:37:50 +03: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/>.
|
|
|
|
|
|
|
|
"""
|
2020-11-19 06:51:03 +02:00
|
|
|
API routes for snapshots.
|
2020-10-02 09:37:50 +03:00
|
|
|
"""
|
|
|
|
|
2020-10-14 03:19:29 +03:00
|
|
|
import logging
|
|
|
|
log = logging.getLogger()
|
|
|
|
|
|
|
|
from fastapi import APIRouter, Depends, status
|
2020-10-02 09:37:50 +03:00
|
|
|
from typing import List
|
|
|
|
from uuid import UUID
|
|
|
|
|
2020-10-14 03:19:29 +03:00
|
|
|
from gns3server.controller.project import Project
|
2020-10-31 07:32:21 +02:00
|
|
|
from gns3server import schemas
|
2020-10-02 09:37:50 +03:00
|
|
|
from gns3server.controller import Controller
|
|
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
2020-10-14 03:19:29 +03:00
|
|
|
responses = {
|
2020-10-31 07:32:21 +02:00
|
|
|
404: {"model": schemas.ErrorMessage, "description": "Could not find project or snapshot"}
|
2020-10-14 03:19:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def dep_project(project_id: UUID):
|
|
|
|
"""
|
|
|
|
Dependency to retrieve a project.
|
|
|
|
"""
|
|
|
|
|
|
|
|
project = Controller.instance().get_project(str(project_id))
|
|
|
|
return project
|
2020-10-02 09:37:50 +03:00
|
|
|
|
|
|
|
|
2020-10-19 07:30:41 +03:00
|
|
|
@router.post("",
|
2020-10-02 09:37:50 +03:00
|
|
|
status_code=status.HTTP_201_CREATED,
|
|
|
|
response_model=schemas.Snapshot,
|
2020-10-14 03:19:29 +03:00
|
|
|
responses=responses)
|
|
|
|
async def create_snapshot(snapshot_data: schemas.SnapshotCreate, project: Project = Depends(dep_project)):
|
2020-10-02 09:37:50 +03:00
|
|
|
"""
|
2020-10-14 03:19:29 +03:00
|
|
|
Create a new snapshot of a project.
|
2020-10-02 09:37:50 +03:00
|
|
|
"""
|
|
|
|
|
|
|
|
snapshot = await project.snapshot(snapshot_data.name)
|
|
|
|
return snapshot.__json__()
|
|
|
|
|
|
|
|
|
2020-10-19 07:30:41 +03:00
|
|
|
@router.get("",
|
2020-10-02 09:37:50 +03:00
|
|
|
response_model=List[schemas.Snapshot],
|
|
|
|
response_model_exclude_unset=True,
|
2020-10-14 03:19:29 +03:00
|
|
|
responses=responses)
|
|
|
|
def get_snapshots(project: Project = Depends(dep_project)):
|
2020-10-02 09:37:50 +03:00
|
|
|
"""
|
2020-10-14 03:19:29 +03:00
|
|
|
Return all snapshots belonging to a given project.
|
2020-10-02 09:37:50 +03:00
|
|
|
"""
|
|
|
|
|
|
|
|
snapshots = [s for s in project.snapshots.values()]
|
|
|
|
return [s.__json__() for s in sorted(snapshots, key=lambda s: (s.created_at, s.name))]
|
|
|
|
|
|
|
|
|
2020-10-14 03:19:29 +03:00
|
|
|
@router.delete("/{snapshot_id}",
|
2020-10-02 09:37:50 +03:00
|
|
|
status_code=status.HTTP_204_NO_CONTENT,
|
2020-10-14 03:19:29 +03:00
|
|
|
responses=responses)
|
|
|
|
async def delete_snapshot(snapshot_id: UUID, project: Project = Depends(dep_project)):
|
2020-10-02 09:37:50 +03:00
|
|
|
"""
|
2020-10-14 03:19:29 +03:00
|
|
|
Delete a snapshot.
|
2020-10-02 09:37:50 +03:00
|
|
|
"""
|
|
|
|
|
|
|
|
await project.delete_snapshot(str(snapshot_id))
|
|
|
|
|
|
|
|
|
2020-10-14 03:19:29 +03:00
|
|
|
@router.post("/{snapshot_id}/restore",
|
2020-10-02 09:37:50 +03:00
|
|
|
status_code=status.HTTP_201_CREATED,
|
|
|
|
response_model=schemas.Project,
|
2020-10-14 03:19:29 +03:00
|
|
|
responses=responses)
|
|
|
|
async def restore_snapshot(snapshot_id: UUID, project: Project = Depends(dep_project)):
|
2020-10-02 09:37:50 +03:00
|
|
|
"""
|
2020-10-14 03:19:29 +03:00
|
|
|
Restore a snapshot.
|
2020-10-02 09:37:50 +03:00
|
|
|
"""
|
|
|
|
|
|
|
|
snapshot = project.get_snapshot(str(snapshot_id))
|
|
|
|
project = await snapshot.restore()
|
|
|
|
return project.__json__()
|