Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 修复申请组件 API 时未返回 record_id 导致报错 #1535

Merged
merged 4 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 36 additions & 18 deletions apiserver/paasng/paasng/accessories/cloudapi/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from paasng.misc.audit.service import DataDetail, add_app_audit_record
from paasng.platform.applications.mixins import ApplicationCodeInPathMixin
from paasng.platform.applications.models import Application
from paasng.utils.dictx import get_items
from paasng.utils.error_codes import error_codes

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -151,7 +152,7 @@ def apply_component_permissions(self, request, *args, **kwargs):
app = self.get_application()
operation_type = OperationEnum.APPLY
apigw_url = self._trans_request_path_to_apigw_url(request.path, app.code)
return self._post(request, apigw_url, operation_type, app)
return self._post(request, apigw_url, operation_type, app, DataType.ESB_API_RECORD)

@swagger_auto_schema(
request_body=serializers.ESBPermissionRenewSLZ,
Expand All @@ -164,7 +165,7 @@ def renew_component_permissions(self, request, *args, **kwargs):
app = self.get_application()
operation_type = OperationEnum.RENEW
apigw_url = self._trans_request_path_to_apigw_url(request.path, app.code)
return self._post(request, apigw_url, operation_type, app)
return self._post(request, apigw_url, operation_type, app, DataType.ESB_API_RECORD)

@swagger_auto_schema(
response_serializer=serializers.APIGWPermissionSLZ(many=True),
Expand Down Expand Up @@ -207,7 +208,22 @@ def _get(self, request, apigw_url: str, app: Application):
result = bk_apigateway_inner_component.get(apigw_url, params=params, bk_username=request.user.username)
return Response(result)

def _post(self, request, apigw_url: str, operation_type: str, app: Application):
def _post(
self,
request,
apigw_url: str,
operation_type: str,
app: Application,
data_type: DataType = DataType.CLOUD_API_RECORD,
):
"""
申请/续期网关/组件 API 权限

@param: apigw_url: 请求 API 路径
@param: operation_type: 操作的类型,如申请、续期
@param: app: 应用
@param: data_type: 操作前后数据中记录数据的类型,如网关、组件。通过 record_id 获取申请的详情需要根据这个类型来调用不同的 API 获取申请单据详情
jiayuan929 marked this conversation as resolved.
Show resolved Hide resolved
"""
logger.debug("[cloudapi] posting %s", apigw_url)
data = copy.copy(request.data)
data.update(
Expand All @@ -219,21 +235,23 @@ def _post(self, request, apigw_url: str, operation_type: str, app: Application):

result = bk_apigateway_inner_component.post(apigw_url, json=data, bk_username=request.user.username)

# 云 API 申请记录 ID,用于操作详情的展示
record_id = result.get("data", {}).get("record_id", "")
# 记录操作记录
gateway_name = data.get("gateway_name", "")
add_app_audit_record(
app_code=app.code,
user=request.user.pk,
action_id=AppAction.MANAGE_CLOUD_API,
operation=operation_type,
target=OperationTarget.CLOUD_API,
attribute=gateway_name,
# 仅提交了申请记录,需要审批后才算操作成功
result_code=ResultCode.ONGOING,
data_after=DataDetail(type=DataType.CLOUD_API_RECORD, data=record_id),
)
try:
# 云 API 申请记录 ID,用于操作详情的展示
record_id = get_items(result, ["data", "record_id"], "")
gateway_name = data.get("gateway_name", "")
jiayuan929 marked this conversation as resolved.
Show resolved Hide resolved
jamesgetx marked this conversation as resolved.
Show resolved Hide resolved
add_app_audit_record(
app_code=app.code,
user=request.user.pk,
action_id=AppAction.MANAGE_CLOUD_API,
operation=operation_type,
target=OperationTarget.CLOUD_API,
attribute=gateway_name,
# 仅提交了申请记录,需要审批后才算操作成功
result_code=ResultCode.ONGOING,
data_after=DataDetail(type=data_type, data=record_id),
)
except Exception:
logger.exception("An exception occurred in the operation record of adding cloud API permissions")
return Response(result)

@staticmethod
Expand Down
3 changes: 3 additions & 0 deletions apiserver/paasng/paasng/misc/audit/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class DataType(str, StructuredEnum):
CLOUD_API_RECORD = EnumField(
"cloud_api_record", label="云 API 权限申请记录 ID, 需要通过 ID 查询具体的申请单据详情展示"
)
ESB_API_RECORD = EnumField(
"esb_api_record", label="组件 API 权限申请记录 ID, 需要通过 ID 查询具体的申请单据详情展示"
)


class OperationTarget(str, StructuredEnum):
Expand Down