Skip to content

Commit

Permalink
feat: APIGW接入 (closed #370)
Browse files Browse the repository at this point in the history
  • Loading branch information
wyyalt committed Aug 26, 2024
1 parent 2d329e3 commit a7923db
Show file tree
Hide file tree
Showing 19 changed files with 871 additions and 13 deletions.
41 changes: 41 additions & 0 deletions apps/authentication.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# -*- coding: utf-8 -*-
"""
Tencent is pleased to support the open source community by making 蓝鲸 (Blueking) available.
Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved.
Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
You may obtain a copy of the License at https://opensource.org/licenses/MIT
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.
"""
from blueapps.account import get_user_model

from common.log import logger
from django.contrib.auth.backends import ModelBackend
from django.contrib.auth.models import AnonymousUser


class ApiGatewayJWTUserModelBackend(ModelBackend):
"""Get users by username"""

def user_maker(self, bk_username):
user_model = get_user_model()
try:
user, _ = user_model.objects.get_or_create(defaults={"nickname": bk_username}, username=bk_username)
except Exception:
logger.exception(f"[{self.__class__.__name__}] Failed to get_or_create user -> {bk_username}.")
return None
else:
logger.info(f"test-user{user.username}, {user.is_superuser}")
return user

def make_anonymous_user(self, bk_username=None):
user = AnonymousUser()
user.username = bk_username # type: ignore
return user

def authenticate(self, request, gateway_name, bk_username, verified, **credentials):
if not verified:
return self.make_anonymous_user(bk_username=bk_username)

return self.user_maker(bk_username)
6 changes: 6 additions & 0 deletions apps/core/gray/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,31 @@ class GrayViewSet(APIViewSet):
permission_classes = (permission.GrayPermission,)

@swagger_auto_schema(
operation_id="gray_build",
operation_summary="GSE 2.0灰度",
tags=GRAY_VIEW_TAGS,
extra_overrides={"is_register_apigw": True},
)
@action(detail=False, methods=["POST"], serializer_class=serializers.GraySerializer)
def build(self, request):
return Response(handlers.GrayHandler.build(self.validated_data))

@swagger_auto_schema(
operation_id="gray_rollback",
operation_summary="GSE 2.0灰度回滚",
tags=GRAY_VIEW_TAGS,
extra_overrides={"is_register_apigw": True},
)
@action(detail=False, methods=["POST"], serializer_class=serializers.GraySerializer)
def rollback(self, request):
return Response(handlers.GrayHandler.rollback(self.validated_data))

@swagger_auto_schema(
operation_id="gray_info",
operation_summary="获取GSE 2.0灰度信息",
tags=GRAY_VIEW_TAGS,
responses={status.HTTP_200_OK: serializers.GraySerializer},
extra_overrides={"is_register_apigw": True},
)
@action(detail=False, methods=["GET"])
def info(self, request):
Expand Down
2 changes: 2 additions & 0 deletions apps/gsekit/configfile/views/config_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,12 @@ def get_serializer_class(self, *args, **kwargs):
return serializer_class

@swagger_auto_schema(
operation_id="config_template_list",
operation_summary="获取配置模板列表",
tags=ConfigTemplateViewTags,
query_serializer=config_template_serializer.ListConfigTemplateRequestSerializer(),
responses={status.HTTP_200_OK: config_template_serializer.ListConfigTemplateResponseSerializer()},
extra_overrides={"is_register_apigw": True},
)
@insert_permission_field(
id_field=lambda d: d["config_template_id"],
Expand Down
8 changes: 7 additions & 1 deletion apps/gsekit/job/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,11 @@ def retrieve(self, request, *args, **kwargs):
return super().retrieve(request, *args, **kwargs)

@swagger_auto_schema(
operation_summary="创建任务", tags=JobViewTags, request_body=job_serializers.CreateJobRequestSerializer()
operation_id="create_job",
operation_summary="创建任务",
tags=JobViewTags,
request_body=job_serializers.CreateJobRequestSerializer(),
extra_overrides={"is_register_apigw": True},
)
def create(self, request, bk_biz_id, *args, **kwargs):
self.serializer_class = job_serializers.CreateJobRequestSerializer
Expand Down Expand Up @@ -120,9 +124,11 @@ def job_task_statistics(self, request, *args, **kwargs):
return Response(JobHandlers(bk_biz_id=kwargs.get("bk_biz_id"), job_id=kwargs["pk"]).job_task_statistics())

@swagger_auto_schema(
operation_id="job_status",
operation_summary="任务状态查询",
tags=JobViewTags,
responses={status.HTTP_200_OK: job_serializers.JobTaskResponseSerializer()},
extra_overrides={"is_register_apigw": True},
)
@action(methods=["POST"], detail=True, serializer_class=job_serializers.RetryRequestSerializer)
def job_status(self, request, *args, **kwargs):
Expand Down
Loading

0 comments on commit a7923db

Please sign in to comment.