2020-10-02 06:37:50 +00:00
|
|
|
#
|
2021-03-28 10:47:29 +00:00
|
|
|
# Copyright (C) 2021 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/>.
|
|
|
|
|
|
|
|
"""
|
2020-11-19 04:51:03 +00:00
|
|
|
API routes for templates.
|
2020-10-02 06:37:50 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
import hashlib
|
|
|
|
import json
|
|
|
|
|
2020-10-14 00:19:29 +00:00
|
|
|
import logging
|
2021-04-13 09:16:50 +00:00
|
|
|
|
2020-10-14 00:19:29 +00:00
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
2021-08-23 00:57:10 +00:00
|
|
|
from fastapi import APIRouter, Request, HTTPException, Depends, Response, status
|
|
|
|
from typing import List, Optional
|
2020-10-02 06:37:50 +00:00
|
|
|
from uuid import UUID
|
|
|
|
|
2020-10-31 05:32:21 +00:00
|
|
|
from gns3server import schemas
|
2021-03-28 00:45:08 +00:00
|
|
|
from gns3server.db.repositories.templates import TemplatesRepository
|
2021-03-28 10:47:29 +00:00
|
|
|
from gns3server.services.templates import TemplatesService
|
2021-06-03 06:10:12 +00:00
|
|
|
from gns3server.db.repositories.rbac import RbacRepository
|
2021-08-23 00:57:10 +00:00
|
|
|
from gns3server.db.repositories.images import ImagesRepository
|
2021-06-03 06:10:12 +00:00
|
|
|
|
|
|
|
from .dependencies.authentication import get_current_active_user
|
2023-09-02 10:54:24 +00:00
|
|
|
from .dependencies.rbac import has_privilege
|
2021-03-28 00:45:08 +00:00
|
|
|
from .dependencies.database import get_repository
|
2020-10-31 04:37:12 +00:00
|
|
|
|
2021-04-13 09:16:50 +00:00
|
|
|
responses = {404: {"model": schemas.ErrorMessage, "description": "Could not find template"}}
|
2020-10-14 00:19:29 +00:00
|
|
|
|
2021-03-28 10:47:29 +00:00
|
|
|
router = APIRouter(responses=responses)
|
|
|
|
|
2020-10-02 06:37:50 +00:00
|
|
|
|
2023-09-02 10:54:24 +00:00
|
|
|
@router.post(
|
|
|
|
"",
|
|
|
|
response_model=schemas.Template,
|
|
|
|
status_code=status.HTTP_201_CREATED,
|
|
|
|
dependencies=[Depends(has_privilege("Template.Allocate"))]
|
|
|
|
)
|
2021-03-28 00:45:08 +00:00
|
|
|
async def create_template(
|
2021-04-13 09:16:50 +00:00
|
|
|
template_create: schemas.TemplateCreate,
|
2023-08-27 08:20:42 +00:00
|
|
|
templates_repo: TemplatesRepository = Depends(get_repository(TemplatesRepository))
|
2021-04-18 06:10:38 +00:00
|
|
|
) -> schemas.Template:
|
2020-10-14 00:19:29 +00:00
|
|
|
"""
|
|
|
|
Create a new template.
|
2023-09-02 10:54:24 +00:00
|
|
|
|
|
|
|
Required privilege: Template.Allocate
|
2020-10-14 00:19:29 +00:00
|
|
|
"""
|
2020-10-02 06:37:50 +00:00
|
|
|
|
2021-06-03 06:10:12 +00:00
|
|
|
template = await TemplatesService(templates_repo).create_template(template_create)
|
|
|
|
return template
|
2020-10-02 06:37:50 +00:00
|
|
|
|
|
|
|
|
2023-09-02 10:54:24 +00:00
|
|
|
@router.get(
|
|
|
|
"/{template_id}",
|
|
|
|
response_model=schemas.Template,
|
|
|
|
response_model_exclude_unset=True,
|
2023-10-18 05:51:43 +00:00
|
|
|
dependencies=[Depends(get_current_active_user)],
|
|
|
|
#dependencies=[Depends(has_privilege("Template.Audit"))] # FIXME: this is a temporary workaround due to a bug in the web-ui
|
2023-09-02 10:54:24 +00:00
|
|
|
)
|
2021-03-28 00:45:08 +00:00
|
|
|
async def get_template(
|
2021-04-13 09:16:50 +00:00
|
|
|
template_id: UUID,
|
|
|
|
request: Request,
|
|
|
|
response: Response,
|
|
|
|
templates_repo: TemplatesRepository = Depends(get_repository(TemplatesRepository)),
|
2021-04-18 06:10:38 +00:00
|
|
|
) -> schemas.Template:
|
2020-10-14 00:19:29 +00:00
|
|
|
"""
|
|
|
|
Return a template.
|
2023-09-02 10:54:24 +00:00
|
|
|
|
|
|
|
Required privilege: Template.Audit
|
2020-10-14 00:19:29 +00:00
|
|
|
"""
|
2020-10-02 06:37:50 +00:00
|
|
|
|
|
|
|
request_etag = request.headers.get("If-None-Match", "")
|
2021-03-28 10:47:29 +00:00
|
|
|
template = await TemplatesService(templates_repo).get_template(template_id)
|
2021-03-28 00:45:08 +00:00
|
|
|
data = json.dumps(template)
|
2020-10-02 06:37:50 +00: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 00:45:08 +00:00
|
|
|
return template
|
2020-10-02 06:37:50 +00:00
|
|
|
|
|
|
|
|
2023-09-02 10:54:24 +00:00
|
|
|
@router.put(
|
|
|
|
"/{template_id}",
|
|
|
|
response_model=schemas.Template,
|
|
|
|
response_model_exclude_unset=True,
|
|
|
|
dependencies=[Depends(has_privilege("Template.Modify"))]
|
|
|
|
)
|
2021-03-28 00:45:08 +00:00
|
|
|
async def update_template(
|
2021-04-13 09:16:50 +00:00
|
|
|
template_id: UUID,
|
|
|
|
template_update: schemas.TemplateUpdate,
|
|
|
|
templates_repo: TemplatesRepository = Depends(get_repository(TemplatesRepository)),
|
2021-04-18 06:10:38 +00:00
|
|
|
) -> schemas.Template:
|
2020-10-14 00:19:29 +00:00
|
|
|
"""
|
|
|
|
Update a template.
|
2023-09-02 10:54:24 +00:00
|
|
|
|
|
|
|
Required privilege: Template.Modify
|
2020-10-14 00:19:29 +00:00
|
|
|
"""
|
2020-10-02 06:37:50 +00:00
|
|
|
|
2021-03-30 23:28:52 +00:00
|
|
|
return await TemplatesService(templates_repo).update_template(template_id, template_update)
|
2020-10-02 06:37:50 +00:00
|
|
|
|
|
|
|
|
2023-09-02 10:54:24 +00:00
|
|
|
@router.delete(
|
|
|
|
"/{template_id}",
|
|
|
|
status_code=status.HTTP_204_NO_CONTENT,
|
|
|
|
dependencies=[Depends(has_privilege("Template.Allocate"))]
|
|
|
|
)
|
2021-03-28 00:45:08 +00:00
|
|
|
async def delete_template(
|
2021-06-03 06:10:12 +00:00
|
|
|
template_id: UUID,
|
2021-08-23 00:57:10 +00:00
|
|
|
prune_images: Optional[bool] = False,
|
2021-06-03 06:10:12 +00:00
|
|
|
templates_repo: TemplatesRepository = Depends(get_repository(TemplatesRepository)),
|
2021-08-23 00:57:10 +00:00
|
|
|
images_repo: RbacRepository = Depends(get_repository(ImagesRepository)),
|
2021-06-03 06:10:12 +00:00
|
|
|
rbac_repo: RbacRepository = Depends(get_repository(RbacRepository))
|
2022-07-15 22:12:18 +00:00
|
|
|
) -> None:
|
2020-10-14 00:19:29 +00:00
|
|
|
"""
|
|
|
|
Delete a template.
|
2023-09-02 10:54:24 +00:00
|
|
|
|
|
|
|
Required privilege: Template.Allocate
|
2020-10-14 00:19:29 +00:00
|
|
|
"""
|
2020-10-02 06:37:50 +00:00
|
|
|
|
2021-03-28 10:47:29 +00:00
|
|
|
await TemplatesService(templates_repo).delete_template(template_id)
|
2023-09-02 10:54:24 +00:00
|
|
|
await rbac_repo.delete_all_ace_starting_with_path(f"/templates/{template_id}")
|
2021-08-23 00:57:10 +00:00
|
|
|
if prune_images:
|
|
|
|
await images_repo.prune_images()
|
2020-10-02 06:37:50 +00:00
|
|
|
|
|
|
|
|
2023-09-02 10:54:24 +00:00
|
|
|
@router.get(
|
|
|
|
"",
|
|
|
|
response_model=List[schemas.Template],
|
|
|
|
response_model_exclude_unset=True,
|
2023-10-18 05:51:43 +00:00
|
|
|
dependencies=[Depends(get_current_active_user)],
|
|
|
|
#dependencies=[Depends(has_privilege("Template.Audit"))] # FIXME: this is a temporary workaround due to a bug in the web-ui
|
2023-09-02 10:54:24 +00:00
|
|
|
)
|
2021-03-28 00:45:08 +00:00
|
|
|
async def get_templates(
|
2021-06-03 06:10:12 +00: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 06:10:38 +00:00
|
|
|
) -> List[schemas.Template]:
|
2020-10-14 00:19:29 +00:00
|
|
|
"""
|
|
|
|
Return all templates.
|
2023-09-02 10:54:24 +00:00
|
|
|
|
|
|
|
Required privilege: Template.Audit
|
2020-10-14 00:19:29 +00:00
|
|
|
"""
|
2020-10-02 06:37:50 +00:00
|
|
|
|
2021-06-03 06:10:12 +00:00
|
|
|
templates = await TemplatesService(templates_repo).get_templates()
|
|
|
|
if current_user.is_superadmin:
|
|
|
|
return templates
|
|
|
|
else:
|
|
|
|
user_templates = []
|
|
|
|
for template in templates:
|
2023-08-27 08:20:42 +00:00
|
|
|
# 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)
|
2021-06-03 06:10:12 +00:00
|
|
|
return user_templates
|
2020-10-02 06:37:50 +00:00
|
|
|
|
|
|
|
|
2023-09-02 10:54:24 +00:00
|
|
|
@router.post(
|
|
|
|
"/{template_id}/duplicate",
|
|
|
|
response_model=schemas.Template,
|
|
|
|
status_code=status.HTTP_201_CREATED,
|
|
|
|
dependencies=[Depends(has_privilege("Template.Allocate"))]
|
|
|
|
)
|
2021-03-28 00:45:08 +00:00
|
|
|
async def duplicate_template(
|
2023-08-27 08:20:42 +00:00
|
|
|
template_id: UUID, templates_repo: TemplatesRepository = Depends(get_repository(TemplatesRepository))
|
2021-04-18 06:10:38 +00:00
|
|
|
) -> schemas.Template:
|
2020-10-14 00:19:29 +00:00
|
|
|
"""
|
|
|
|
Duplicate a template.
|
2023-09-02 10:54:24 +00:00
|
|
|
|
|
|
|
Required privilege: Template.Allocate
|
2020-10-14 00:19:29 +00:00
|
|
|
"""
|
2020-10-02 06:37:50 +00:00
|
|
|
|
2021-06-03 06:10:12 +00:00
|
|
|
template = await TemplatesService(templates_repo).duplicate_template(template_id)
|
|
|
|
return template
|