11# -*- coding: utf-8 -*-
22"""
3- * TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-蓝鲸 PaaS 平台(BlueKing-PaaS) available.
4- * Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved.
5- * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
6- * You may obtain a copy of the License at http://opensource.org/licenses/MIT
7- * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8- * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9- * specific language governing permissions and limitations under the License.
3+ * TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-蓝鲸 PaaS 平台(BlueKing-PaaS) available.
4+ * Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved.
5+ * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at http://opensource.org/licenses/MIT
7+ * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+ * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+ * specific language governing permissions and limitations under the License.
1010"""
11+
1112import json
1213import logging
14+ import os
1315import typing
1416
1517from bkapi .bk_apigateway .client import Client as BKAPIGatewayClient
16- from bkapi_client_core .exceptions import ResponseError
1718
1819from apigw_manager .core .exceptions import ApiException , ApiResponseError , ApiResultError
20+ from bkapi_client_core .exceptions import ResponseError
1921
2022if typing .TYPE_CHECKING :
2123 from apigw_manager .core import configuration # noqa
@@ -75,6 +77,28 @@ def _call_with_cache(self, operation, **kwargs):
7577
7678 return result
7779
80+ def _get_tenant_id (self ):
81+ """
82+ 获取应用所属的租户 ID
83+ Note: BKPAAS_APP_TENANT_ID 和 BK_APP_TENANT_ID 的含义不一样
84+ BKPAAS_APP_TENANT_ID 是应用的租户模式标识,表示应用是全租户还是单租户
85+ BK_APP_TENANT_ID 是应用所属的租户 ID,表示应用是属于哪个租户的,即由哪个租户产生的
86+ """
87+ # [大多数是外部 SaaS 场景] PaaS 平台上部署运行的应用,会自动内置 BKPAAS_APP_TENANT_ID 环境变量,表示应用是全租户的还是单租户的
88+ paas_app_tenant_id = os .environ .get ("BKPAAS_APP_TENANT_ID" )
89+ if paas_app_tenant_id is not None :
90+ # 空字符串表示全租户应用,则返回 system,因为全租户应用只能在运营租户 (system) 下创建
91+ return paas_app_tenant_id or "system"
92+
93+ # 对于单租户应用,BK_APP_TENANT_ID 可以不设置; 对于全租户应用,BK_APP_TENANT_ID 必须设置,建议设置为 system
94+ bk_app_tenant_id = self .config .bk_app_tenant_id or ""
95+ logger .warning (
96+ "the [X-Bk-Tenant-Id=%s], if the syncing to apigateway failed, and your app(%s) is a global tenant app, please set the environment variable BK_APP_TENANT_ID to `system` (or set django settings.BK_APP_TENANT_ID to `system`) and try again" ,
97+ bk_app_tenant_id ,
98+ self .config .bk_app_code ,
99+ )
100+ return bk_app_tenant_id
101+
78102 def _call (self , operation , files = None , ** kwargs ):
79103 """Call the API instance"""
80104 data = {
@@ -84,7 +108,7 @@ def _call(self, operation, files=None, **kwargs):
84108 "X-Bkapi-Authorization" : kwargs .pop ("x_bkapi_authorization" , self ._get_bkapi_authorization ()),
85109 # the header is required by the API gateway plugin bk-tenant-validate, for global tenant app!
86110 # so we set it to system, it would not be used in the gateway
87- "X-Bk-Tenant-Id" : "system" ,
111+ "X-Bk-Tenant-Id" : self . _get_tenant_id () ,
88112 },
89113 "files" : files ,
90114 }
0 commit comments