2020-10-02 09:37:50 +03:00
|
|
|
#
|
2021-03-28 13:47:29 +03:00
|
|
|
# Copyright (C) 2021 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 templates.
|
2020-10-02 09:37:50 +03:00
|
|
|
"""
|
|
|
|
|
|
|
|
import hashlib
|
|
|
|
import json
|
|
|
|
|
2020-10-14 03:19:29 +03:00
|
|
|
import logging
|
2021-04-13 12:16:50 +03:00
|
|
|
|
2020-10-14 03:19:29 +03:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2021-03-28 03:45:08 +03:00
|
|
|
from fastapi import APIRouter, Request, Response, HTTPException, Depends, status
|
2020-10-31 06:37:12 +02:00
|
|
|
from typing import List
|
2020-10-02 09:37:50 +03:00
|
|
|
from uuid import UUID
|
|
|
|
|
2020-10-31 07:32:21 +02:00
|
|
|
from gns3server import schemas
|
2021-03-28 03:45:08 +03:00
|
|
|
from gns3server.db.repositories.templates import TemplatesRepository
|
2021-03-28 13:47:29 +03:00
|
|
|
from gns3server.services.templates import TemplatesService
|
2021-06-03 09:10:12 +03:00
|
|
|
from gns3server.db.repositories.rbac import RbacRepository
|
|
|
|
|
|
|
|
from .dependencies.authentication import get_current_active_user
|
2021-03-28 03:45:08 +03:00
|
|
|
from .dependencies.database import get_repository
|
2020-10-31 06:37:12 +02:00
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
responses = {404: {"model": schemas.ErrorMessage, "description": "Could not find template"}}
|
2020-10-14 03:19:29 +03:00
|
|
|
|
2021-03-28 13:47:29 +03:00
|
|
|
router = APIRouter(responses=responses)
|
|
|
|
|
2020-10-02 09:37:50 +03:00
|
|
|
|
2021-03-28 03:45:08 +03:00
|
|
|
@router.post("/templates", response_model=schemas.Template, status_code=status.HTTP_201_CREATED)
|
|
|
|
async def create_template(
|
2021-04-13 12:16:50 +03:00
|
|
|
template_create: schemas.TemplateCreate,
|
|
|
|
templates_repo: TemplatesRepository = Depends(get_repository(TemplatesRepository)),
|
2021-06-03 09:10:12 +03:00
|
|
|
current_user: schemas.User = Depends(get_current_active_user),
|
|
|
|
rbac_repo: RbacRepository = Depends(get_repository(RbacRepository))
|
2021-04-18 09:10:38 +03:00
|
|
|
) -> schemas.Template:
|
2020-10-14 03:19:29 +03:00
|
|
|
"""
|
|
|
|
Create a new template.
|
|
|
|
"""
|
2020-10-02 09:37:50 +03:00
|
|
|
|
2021-06-03 09:10:12 +03:00
|
|
|
template = await TemplatesService(templates_repo).create_template(template_create)
|
|
|
|
template_id = template.get("template_id")
|
|
|
|
await rbac_repo.add_permission_to_user_with_path(current_user.user_id, f"/templates/{template_id}/*")
|
|
|
|
return template
|
2020-10-02 09:37:50 +03:00
|
|
|
|
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
@router.get("/templates/{template_id}", response_model=schemas.Template, response_model_exclude_unset=True)
|
2021-03-28 03:45:08 +03:00
|
|
|
async def get_template(
|
2021-04-13 12:16:50 +03:00
|
|
|
template_id: UUID,
|
|
|
|
request: Request,
|
|
|
|
response: Response,
|
|
|
|
templates_repo: TemplatesRepository = Depends(get_repository(TemplatesRepository)),
|
2021-04-18 09:10:38 +03:00
|
|
|
) -> schemas.Template:
|
2020-10-14 03:19:29 +03:00
|
|
|
"""
|
|
|
|
Return a template.
|
|
|
|
"""
|
2020-10-02 09:37:50 +03:00
|
|
|
|
|
|
|
request_etag = request.headers.get("If-None-Match", "")
|
2021-03-28 13:47:29 +03:00
|
|
|
template = await TemplatesService(templates_repo).get_template(template_id)
|
2021-03-28 03:45:08 +03:00
|
|
|
data = json.dumps(template)
|
2020-10-02 09:37:50 +03:00
|
|
|
template_etag = '"' + hashlib.md5(data.encode()).hexdigest() + '"'
|
|
|
|
if template_etag == request_etag:
|
|
|
|
raise HTTPException(status_code=status.HTTP_304_NOT_MODIFIED)
|
|
|
|
else:
|
|
|
|
response.headers["ETag"] = template_etag
|
2021-03-28 03:45:08 +03:00
|
|
|
return template
|
2020-10-02 09:37:50 +03:00
|
|
|
|
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
@router.put("/templates/{template_id}", response_model=schemas.Template, response_model_exclude_unset=True)
|
2021-03-28 03:45:08 +03:00
|
|
|
async def update_template(
|
2021-04-13 12:16:50 +03:00
|
|
|
template_id: UUID,
|
|
|
|
template_update: schemas.TemplateUpdate,
|
|
|
|
templates_repo: TemplatesRepository = Depends(get_repository(TemplatesRepository)),
|
2021-04-18 09:10:38 +03:00
|
|
|
) -> schemas.Template:
|
2020-10-14 03:19:29 +03:00
|
|
|
"""
|
|
|
|
Update a template.
|
|
|
|
"""
|
2020-10-02 09:37:50 +03:00
|
|
|
|
2021-03-31 02:28:52 +03:00
|
|
|
return await TemplatesService(templates_repo).update_template(template_id, template_update)
|
2020-10-02 09:37:50 +03:00
|
|
|
|
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
@router.delete(
|
|
|
|
"/templates/{template_id}",
|
|
|
|
status_code=status.HTTP_204_NO_CONTENT,
|
|
|
|
)
|
2021-03-28 03:45:08 +03:00
|
|
|
async def delete_template(
|
2021-06-03 09:10:12 +03:00
|
|
|
template_id: UUID,
|
|
|
|
templates_repo: TemplatesRepository = Depends(get_repository(TemplatesRepository)),
|
|
|
|
rbac_repo: RbacRepository = Depends(get_repository(RbacRepository))
|
2021-03-28 03:45:08 +03:00
|
|
|
) -> None:
|
2020-10-14 03:19:29 +03:00
|
|
|
"""
|
|
|
|
Delete a template.
|
|
|
|
"""
|
2020-10-02 09:37:50 +03:00
|
|
|
|
2021-03-28 13:47:29 +03:00
|
|
|
await TemplatesService(templates_repo).delete_template(template_id)
|
2021-06-03 09:10:12 +03:00
|
|
|
await rbac_repo.delete_all_permissions_with_path(f"/templates/{template_id}")
|
2020-10-02 09:37:50 +03:00
|
|
|
|
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
@router.get("/templates", response_model=List[schemas.Template], response_model_exclude_unset=True)
|
2021-03-28 03:45:08 +03:00
|
|
|
async def get_templates(
|
2021-06-03 09:10:12 +03:00
|
|
|
templates_repo: TemplatesRepository = Depends(get_repository(TemplatesRepository)),
|
|
|
|
current_user: schemas.User = Depends(get_current_active_user),
|
|
|
|
rbac_repo: RbacRepository = Depends(get_repository(RbacRepository))
|
2021-04-18 09:10:38 +03:00
|
|
|
) -> List[schemas.Template]:
|
2020-10-14 03:19:29 +03:00
|
|
|
"""
|
|
|
|
Return all templates.
|
|
|
|
"""
|
2020-10-02 09:37:50 +03:00
|
|
|
|
2021-06-03 09:10:12 +03:00
|
|
|
templates = await TemplatesService(templates_repo).get_templates()
|
|
|
|
if current_user.is_superadmin:
|
|
|
|
return templates
|
|
|
|
else:
|
|
|
|
user_templates = []
|
|
|
|
for template in templates:
|
|
|
|
if template.get("builtin") is True:
|
|
|
|
user_templates.append(template)
|
|
|
|
continue
|
|
|
|
template_id = template.get("template_id")
|
|
|
|
authorized = await rbac_repo.check_user_is_authorized(
|
|
|
|
current_user.user_id, "GET", f"/templates/{template_id}")
|
|
|
|
if authorized:
|
|
|
|
user_templates.append(template)
|
|
|
|
return user_templates
|
2020-10-02 09:37:50 +03:00
|
|
|
|
|
|
|
|
2021-04-13 12:16:50 +03:00
|
|
|
@router.post("/templates/{template_id}/duplicate", response_model=schemas.Template, status_code=status.HTTP_201_CREATED)
|
2021-03-28 03:45:08 +03:00
|
|
|
async def duplicate_template(
|
2021-06-03 09:10:12 +03:00
|
|
|
template_id: UUID, templates_repo: TemplatesRepository = Depends(get_repository(TemplatesRepository)),
|
|
|
|
current_user: schemas.User = Depends(get_current_active_user),
|
|
|
|
rbac_repo: RbacRepository = Depends(get_repository(RbacRepository))
|
2021-04-18 09:10:38 +03:00
|
|
|
) -> schemas.Template:
|
2020-10-14 03:19:29 +03:00
|
|
|
"""
|
|
|
|
Duplicate a template.
|
|
|
|
"""
|
2020-10-02 09:37:50 +03:00
|
|
|
|
2021-06-03 09:10:12 +03:00
|
|
|
template = await TemplatesService(templates_repo).duplicate_template(template_id)
|
|
|
|
await rbac_repo.add_permission_to_user_with_path(current_user.user_id, f"/templates/{template_id}/*")
|
|
|
|
return template
|