Move error responses to API routers.

This commit is contained in:
grossmj committed 2021-04-13 16:19:56 +09:30
1 parent 6b0d0de1bc
commit a9eb035b7d
18 files changed
+263 -487

No files matched your search

+6 -10
View File
@@ -27,12 +27,12 @@ from uuid import UUID
from gns3server.controller import Controller
from gns3server import schemas
router = APIRouter()
responses = {
404: {"model": schemas.ErrorMessage, "description": "Project or drawing not found"}
}
router = APIRouter(responses=responses)
@router.get("",
response_model=List[schemas.Drawing],
@@ -48,8 +48,7 @@ async def get_drawings(project_id: UUID):
@router.post("",
status_code=status.HTTP_201_CREATED,
response_model=schemas.Drawing,
responses=responses)
response_model=schemas.Drawing)
async def create_drawing(project_id: UUID, drawing_data: schemas.Drawing):
"""
Create a new drawing.
@@ -62,8 +61,7 @@ async def create_drawing(project_id: UUID, drawing_data: schemas.Drawing):
@router.get("/{drawing_id}",
response_model=schemas.Drawing,
response_model_exclude_unset=True,
responses=responses)
response_model_exclude_unset=True)
async def get_drawing(project_id: UUID, drawing_id: UUID):
"""
Return a drawing.
@@ -76,8 +74,7 @@ async def get_drawing(project_id: UUID, drawing_id: UUID):
@router.put("/{drawing_id}",
response_model=schemas.Drawing,
response_model_exclude_unset=True,
responses=responses)
response_model_exclude_unset=True)
async def update_drawing(project_id: UUID, drawing_id: UUID, drawing_data: schemas.Drawing):
"""
Update a drawing.
@@ -90,8 +87,7 @@ async def update_drawing(project_id: UUID, drawing_id: UUID, drawing_data: schem
@router.delete("/{drawing_id}",
status_code=status.HTTP_204_NO_CONTENT,
responses=responses)
status_code=status.HTTP_204_NO_CONTENT)
async def delete_drawing(project_id: UUID, drawing_id: UUID):
"""
Delete a drawing.
+10 -18
View File
@@ -37,12 +37,12 @@ from gns3server import schemas
import logging
log = logging.getLogger(__name__)
router = APIRouter()
responses = {
404: {"model": schemas.ErrorMessage, "description": "Could not find project or link"}
}
router = APIRouter(responses=responses)
async def dep_link(project_id: UUID, link_id: UUID):
"""
@@ -95,8 +95,7 @@ async def create_link(project_id: UUID, link_data: schemas.Link):
return link.__json__()
@router.get("/{link_id}/available_filters",
responses=responses)
@router.get("/{link_id}/available_filters")
async def get_filters(link: Link = Depends(dep_link)):
"""
Return all filters available for a given link.
@@ -107,8 +106,7 @@ async def get_filters(link: Link = Depends(dep_link)):
@router.get("/{link_id}",
response_model=schemas.Link,
response_model_exclude_unset=True,
responses=responses)
response_model_exclude_unset=True)
async def get_link(link: Link = Depends(dep_link)):
"""
Return a link.
@@ -119,8 +117,7 @@ async def get_link(link: Link = Depends(dep_link)):
@router.put("/{link_id}",
response_model=schemas.Link,
response_model_exclude_unset=True,
responses=responses)
response_model_exclude_unset=True)
async def update_link(link_data: schemas.Link, link: Link = Depends(dep_link)):
"""
Update a link.
@@ -137,8 +134,7 @@ async def update_link(link_data: schemas.Link, link: Link = Depends(dep_link)):
@router.delete("/{link_id}",
status_code=status.HTTP_204_NO_CONTENT,
responses=responses)
status_code=status.HTTP_204_NO_CONTENT)
async def delete_link(project_id: UUID, link: Link = Depends(dep_link)):
"""
Delete a link.
@@ -149,8 +145,7 @@ async def delete_link(project_id: UUID, link: Link = Depends(dep_link)):
@router.post("/{link_id}/reset",
response_model=schemas.Link,
responses=responses)
response_model=schemas.Link)
async def reset_link(link: Link = Depends(dep_link)):
"""
Reset a link.
@@ -162,8 +157,7 @@ async def reset_link(link: Link = Depends(dep_link)):
@router.post("/{link_id}/capture/start",
status_code=status.HTTP_201_CREATED,
response_model=schemas.Link,
responses=responses)
response_model=schemas.Link)
async def start_capture(capture_data: dict, link: Link = Depends(dep_link)):
"""
Start packet capture on the link.
@@ -175,8 +169,7 @@ async def start_capture(capture_data: dict, link: Link = Depends(dep_link)):
@router.post("/{link_id}/capture/stop",
status_code=status.HTTP_204_NO_CONTENT,
responses=responses)
status_code=status.HTTP_204_NO_CONTENT)
async def stop_capture(link: Link = Depends(dep_link)):
"""
Stop packet capture on the link.
@@ -185,8 +178,7 @@ async def stop_capture(link: Link = Depends(dep_link)):
await link.stop_capture()
@router.get("/{link_id}/capture/stream",
responses=responses)
@router.get("/{link_id}/capture/stream")
async def stream_pcap(request: Request, link: Link = Depends(dep_link)):
"""
Stream the PCAP capture file from compute.
+20 -38
View File
@@ -76,12 +76,12 @@ class NodeConcurrency(APIRoute):
return custom_route_handler
router = APIRouter(route_class=NodeConcurrency)
responses = {
404: {"model": schemas.ErrorMessage, "description": "Could not find project or node"}
}
router = APIRouter(route_class=NodeConcurrency, responses=responses)
async def dep_project(project_id: UUID):
"""
@@ -133,8 +133,7 @@ async def get_nodes(project: Project = Depends(dep_project)):
@router.post("/start",
status_code=status.HTTP_204_NO_CONTENT,
responses=responses)
status_code=status.HTTP_204_NO_CONTENT)
async def start_all_nodes(project: Project = Depends(dep_project)):
"""
Start all nodes belonging to a given project.
@@ -144,8 +143,7 @@ async def start_all_nodes(project: Project = Depends(dep_project)):
@router.post("/stop",
status_code=status.HTTP_204_NO_CONTENT,
responses=responses)
status_code=status.HTTP_204_NO_CONTENT)
async def stop_all_nodes(project: Project = Depends(dep_project)):
"""
Stop all nodes belonging to a given project.
@@ -155,8 +153,7 @@ async def stop_all_nodes(project: Project = Depends(dep_project)):
@router.post("/suspend",
status_code=status.HTTP_204_NO_CONTENT,
responses=responses)
status_code=status.HTTP_204_NO_CONTENT)
async def suspend_all_nodes(project: Project = Depends(dep_project)):
"""
Suspend all nodes belonging to a given project.
@@ -166,8 +163,7 @@ async def suspend_all_nodes(project: Project = Depends(dep_project)):
@router.post("/reload",
status_code=status.HTTP_204_NO_CONTENT,
responses=responses)
status_code=status.HTTP_204_NO_CONTENT)
async def reload_all_nodes(project: Project = Depends(dep_project)):
"""
Reload all nodes belonging to a given project.
@@ -178,8 +174,7 @@ async def reload_all_nodes(project: Project = Depends(dep_project)):
@router.get("/{node_id}",
response_model=schemas.Node,
responses=responses)
response_model=schemas.Node)
def get_node(node: Node = Depends(dep_node)):
"""
Return a node from a given project.
@@ -190,8 +185,7 @@ def get_node(node: Node = Depends(dep_node)):
@router.put("/{node_id}",
response_model=schemas.Node,
response_model_exclude_unset=True,
responses=responses)
response_model_exclude_unset=True)
async def update_node(node_data: schemas.NodeUpdate, node: Node = Depends(dep_node)):
"""
Update a node.
@@ -222,8 +216,7 @@ async def delete_node(node_id: UUID, project: Project = Depends(dep_project)):
@router.post("/{node_id}/duplicate",
response_model=schemas.Node,
status_code=status.HTTP_201_CREATED,
responses=responses)
status_code=status.HTTP_201_CREATED)
async def duplicate_node(duplicate_data: schemas.NodeDuplicate, node: Node = Depends(dep_node)):
"""
Duplicate a node.
@@ -237,8 +230,7 @@ async def duplicate_node(duplicate_data: schemas.NodeDuplicate, node: Node = Dep
@router.post("/{node_id}/start",
status_code=status.HTTP_204_NO_CONTENT,
responses=responses)
status_code=status.HTTP_204_NO_CONTENT)
async def start_node(start_data: dict, node: Node = Depends(dep_node)):
"""
Start a node.
@@ -248,8 +240,7 @@ async def start_node(start_data: dict, node: Node = Depends(dep_node)):
@router.post("/{node_id}/stop",
status_code=status.HTTP_204_NO_CONTENT,
responses=responses)
status_code=status.HTTP_204_NO_CONTENT)
async def stop_node(node: Node = Depends(dep_node)):
"""
Stop a node.
@@ -259,8 +250,7 @@ async def stop_node(node: Node = Depends(dep_node)):
@router.post("/{node_id}/suspend",
status_code=status.HTTP_204_NO_CONTENT,
responses=responses)
status_code=status.HTTP_204_NO_CONTENT)
async def suspend_node(node: Node = Depends(dep_node)):
"""
Suspend a node.
@@ -270,8 +260,7 @@ async def suspend_node(node: Node = Depends(dep_node)):
@router.post("/{node_id}/reload",
status_code=status.HTTP_204_NO_CONTENT,
responses=responses)
status_code=status.HTTP_204_NO_CONTENT)
async def reload_node(node: Node = Depends(dep_node)):
"""
Reload a node.
@@ -294,8 +283,7 @@ async def get_node_links(node: Node = Depends(dep_node)):
return links
@router.get("/{node_id}/dynamips/auto_idlepc",
responses=responses)
@router.get("/{node_id}/dynamips/auto_idlepc")
async def auto_idlepc(node: Node = Depends(dep_node)):
"""
Compute an Idle-PC value for a Dynamips node
@@ -304,8 +292,7 @@ async def auto_idlepc(node: Node = Depends(dep_node)):
return await node.dynamips_auto_idlepc()
@router.get("/{node_id}/dynamips/idlepc_proposals",
responses=responses)
@router.get("/{node_id}/dynamips/idlepc_proposals")
async def idlepc_proposals(node: Node = Depends(dep_node)):
"""
Compute a list of potential idle-pc values for a Dynamips node
@@ -315,8 +302,7 @@ async def idlepc_proposals(node: Node = Depends(dep_node)):
@router.post("/{node_id}/resize_disk",
status_code=status.HTTP_201_CREATED,
responses=responses)
status_code=status.HTTP_201_CREATED)
async def resize_disk(resize_data: dict, node: Node = Depends(dep_node)):
"""
Resize a disk image.
@@ -324,8 +310,7 @@ async def resize_disk(resize_data: dict, node: Node = Depends(dep_node)):
await node.post("/resize_disk", **resize_data)
@router.get("/{node_id}/files/{file_path:path}",
responses=responses)
@router.get("/{node_id}/files/{file_path:path}")
async def get_file(file_path: str, node: Node = Depends(dep_node)):
"""
Return a file in the node directory
@@ -347,8 +332,7 @@ async def get_file(file_path: str, node: Node = Depends(dep_node)):
@router.post("/{node_id}/files/{file_path:path}",
status_code=status.HTTP_201_CREATED,
responses=responses)
status_code=status.HTTP_201_CREATED)
async def post_file(file_path: str, request: Request, node: Node = Depends(dep_node)):
"""
Write a file in the node directory.
@@ -414,8 +398,7 @@ async def ws_console(websocket: WebSocket, node: Node = Depends(dep_node)):
@router.post("/console/reset",
status_code=status.HTTP_204_NO_CONTENT,
responses=responses)
status_code=status.HTTP_204_NO_CONTENT)
async def reset_console_all_nodes(project: Project = Depends(dep_project)):
"""
Reset console for all nodes belonging to the project.
@@ -425,8 +408,7 @@ async def reset_console_all_nodes(project: Project = Depends(dep_project)):
@router.post("/{node_id}/console/reset",
status_code=status.HTTP_204_NO_CONTENT,
responses=responses)
status_code=status.HTTP_204_NO_CONTENT)
async def console_reset(node: Node = Depends(dep_node)):
await node.post("/console/reset")#, request.json)
+8 -15
View File
@@ -46,13 +46,12 @@ from gns3server.controller.export_project import export_project as export_contro
from gns3server.utils.asyncio import aiozipstream
from gns3server.config import Config
router = APIRouter()
responses = {
404: {"model": schemas.ErrorMessage, "description": "Could not find project"}
}
router = APIRouter(responses=responses)
def dep_project(project_id: UUID):
"""
@@ -94,8 +93,7 @@ async def create_project(project_data: schemas.ProjectCreate):
@router.get("/{project_id}",
response_model=schemas.Project,
responses=responses)
response_model=schemas.Project)
def get_project(project: Project = Depends(dep_project)):
"""
Return a project.
@@ -106,8 +104,7 @@ def get_project(project: Project = Depends(dep_project)):
@router.put("/{project_id}",
response_model=schemas.Project,
response_model_exclude_unset=True,
responses=responses)
response_model_exclude_unset=True)
async def update_project(project_data: schemas.ProjectUpdate, project: Project = Depends(dep_project)):
"""
Update a project.
@@ -118,8 +115,7 @@ async def update_project(project_data: schemas.ProjectUpdate, project: Project =
@router.delete("/{project_id}",
status_code=status.HTTP_204_NO_CONTENT,
responses=responses)
status_code=status.HTTP_204_NO_CONTENT)
async def delete_project(project: Project = Depends(dep_project)):
"""
Delete a project.
@@ -130,8 +126,7 @@ async def delete_project(project: Project = Depends(dep_project)):
controller.remove_project(project)
@router.get("/{project_id}/stats",
responses=responses)
@router.get("/{project_id}/stats")
def get_project_stats(project: Project = Depends(dep_project)):
"""
Return a project statistics.
@@ -253,8 +248,7 @@ async def notification_ws(project_id: UUID, websocket: WebSocket):
await project.close()
@router.get("/{project_id}/export",
responses=responses)
@router.get("/{project_id}/export")
async def export_project(project: Project = Depends(dep_project),
include_snapshots: bool = False,
include_images: bool = False,
@@ -304,8 +298,7 @@ async def export_project(project: Project = Depends(dep_project),
@router.post("/{project_id}/import",
status_code=status.HTTP_201_CREATED,
response_model=schemas.Project,
responses=responses)
response_model=schemas.Project)
async def import_project(project_id: UUID, request: Request, path: Optional[Path] = None, name: Optional[str] = None):
"""
Import a project from a portable archive.
+6 -10
View File
@@ -30,12 +30,12 @@ from gns3server.controller.project import Project
from gns3server import schemas
from gns3server.controller import Controller
router = APIRouter()
responses = {
404: {"model": schemas.ErrorMessage, "description": "Could not find project or snapshot"}
}
router = APIRouter(responses=responses)
def dep_project(project_id: UUID):
"""
@@ -48,8 +48,7 @@ def dep_project(project_id: UUID):
@router.post("",
status_code=status.HTTP_201_CREATED,
response_model=schemas.Snapshot,
responses=responses)
response_model=schemas.Snapshot)
async def create_snapshot(snapshot_data: schemas.SnapshotCreate, project: Project = Depends(dep_project)):
"""
Create a new snapshot of a project.
@@ -61,8 +60,7 @@ async def create_snapshot(snapshot_data: schemas.SnapshotCreate, project: Projec
@router.get("",
response_model=List[schemas.Snapshot],
response_model_exclude_unset=True,
responses=responses)
response_model_exclude_unset=True)
def get_snapshots(project: Project = Depends(dep_project)):
"""
Return all snapshots belonging to a given project.
@@ -73,8 +71,7 @@ def get_snapshots(project: Project = Depends(dep_project)):
@router.delete("/{snapshot_id}",
status_code=status.HTTP_204_NO_CONTENT,
responses=responses)
status_code=status.HTTP_204_NO_CONTENT)
async def delete_snapshot(snapshot_id: UUID, project: Project = Depends(dep_project)):
"""
Delete a snapshot.
@@ -85,8 +82,7 @@ async def delete_snapshot(snapshot_id: UUID, project: Project = Depends(dep_proj
@router.post("/{snapshot_id}/restore",
status_code=status.HTTP_201_CREATED,
response_model=schemas.Project,
responses=responses)
response_model=schemas.Project)
async def restore_snapshot(snapshot_id: UUID, project: Project = Depends(dep_project)):
"""
Restore a snapshot.