Skip to content

Commit

Permalink
Merge pull request #1072 from TencentBlueKing/master
Browse files Browse the repository at this point in the history
merge: master-> release_4.3.4
  • Loading branch information
liuwenping authored Jul 1, 2022
2 parents 068a932 + 96e538d commit 15f151d
Show file tree
Hide file tree
Showing 128 changed files with 4,203 additions and 795 deletions.
14 changes: 11 additions & 3 deletions apps/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,17 @@ def new_api_module(module_name, api_name, module_dir="modules"):
# ITSM
BkItsmApi = SimpleLazyObject(lambda: new_api_module("bk_itsm", "_BkItsm"))

# BcsCC
BcsCcApi = SimpleLazyObject(lambda: new_api_module("bcs_cc", "_BcsCcApi"))

# BcsApi
BcsApi = SimpleLazyObject(lambda: new_api_module("bcs", "_BcsApi"))

# BkSSM
BkSSMApi = SimpleLazyObject(lambda: new_api_module("bk_ssm", "_BkSSM"))
# wework
WeWorkApi = SimpleLazyObject(lambda: new_api_module("wework", "_WeWork"))

PaasCcApi = SimpleLazyObject(lambda: new_api_module("paascc", "_PaasCcApi"))

# AIOPS
BkDataAIOPSApi = SimpleLazyObject(lambda: new_api_module("bkdata_aiops", "_BkDataAIOPSApi"))

Expand All @@ -109,8 +115,10 @@ def new_api_module(module_name, api_name, module_dir="modules"):
"MonitorApi",
"GrafanaApi",
"IAMApi",
"PaasCcApi",
"BcsCcApi",
"BcsApi",
"BkItsmApi",
"BkSSMApi",
"BkDataAIOPSApi",
"BkDataDataFlowApi",
"WeWorkApi",
Expand Down
20 changes: 18 additions & 2 deletions apps/api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ def __init__(
max_query_params_record=5000,
method_override=None,
url_keys=None,
header_keys=None,
after_serializer=None,
cache_time=0,
default_timeout=60,
Expand Down Expand Up @@ -178,6 +179,13 @@ def __init__(

self.method_override = method_override
self.url_keys = url_keys
common_headers = [
"X-Bk-App-Code",
"X-Bk-App-Secret",
]
self.header_keys = common_headers
if header_keys:
self.header_keys = header_keys + common_headers

self.cache_time = cache_time
self.default_timeout = default_timeout
Expand Down Expand Up @@ -300,8 +308,10 @@ def _send_request(self, params, timeout, request_id, request_cookies):
raise DataAPIException(self, _("返回数据格式不正确,结果格式非json."), response=raw_response)
else:
# 只有正常返回才会调用 after_request 进行处理
if response_result["result"]:

if "result" not in response_result:
# 说明返回不是蓝鲸标准
response_result["result"] = True
if response_result.get("result"):
# 请求完成后的清洗处理
if self.after_request is not None:
response_result = self.after_request(response_result)
Expand Down Expand Up @@ -413,6 +423,12 @@ def _send(self, params, timeout, request_id, request_cookies):

session.headers.update({"blueking-language": translation.get_language(), "request-id": get_request_id()})

if self.header_keys:
headers = {key: params.get(key) for key in self.header_keys if key in params}
for key in self.header_keys:
params.pop(key, None)
session.headers.update(**headers)

url = self.build_actual_url(params)

# 发出请求并返回结果
Expand Down
32 changes: 18 additions & 14 deletions apps/api/modules/paascc.py → apps/api/modules/bcs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
"""
Tencent is pleased to support the open source community by making BK-LOG 蓝鲸日志平台 available.
Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
Expand All @@ -19,25 +18,30 @@
We undertake not to change the open source license (MIT license) applicable to the current version of
the project delivered to anyone in the future.
"""
from django.utils.translation import ugettext_lazy as _

from apps.api.modules.utils import add_esb_info_before_request
from config.domains import PAASCC_APIGATEWAY_ROOT
from django.conf import settings

from apps.api.base import DataAPI

from config.domains import BCS_APIGATEWAY_ROOT
from apps.api.modules.utils import add_esb_info_before_request


def bcs_before_request(params):
params = add_esb_info_before_request(params)
params["Authorization"] = f"Bearer {settings.BCS_API_GATEWAY_TOKEN}"
return params


class _PaasCcApi(object):
MODULE = _(u" 蓝鲸 PaaS 3.0 配置中心")
URL_PREFIX = PAASCC_APIGATEWAY_ROOT
class _BcsApi:
MODULE = "BCS"

def __init__(self):
self.get_cluster_by_cluster_id = DataAPI(
self.list_cluster_by_project_id = DataAPI(
method="GET",
url=PAASCC_APIGATEWAY_ROOT + "v1/clusters/{cluster_id}/",
url=f"{BCS_APIGATEWAY_ROOT}bcsapi/v4/clustermanager/v1/cluster",
module=self.MODULE,
url_keys=["cluster_id"],
description=u"根据集群id获取集群信息",
default_return_value=None,
before_request=add_esb_info_before_request,
after_request=None,
description="根据项目id获取集群信息",
header_keys=["Authorization"],
before_request=bcs_before_request,
)
88 changes: 88 additions & 0 deletions apps/api/modules/bcs_cc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# -*- coding: utf-8 -*-
"""
Tencent is pleased to support the open source community by making BK-LOG 蓝鲸日志平台 available.
Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
BK-LOG 蓝鲸日志平台 is licensed under the MIT License.
License for BK-LOG 蓝鲸日志平台:
--------------------------------------------------------------------
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import json

from django.conf import settings
from django.utils.translation import ugettext_lazy as _

from apps.api import BkSSMApi
from apps.api.modules.utils import add_esb_info_before_request
from config.domains import BCS_CC_APIGATEWAY_ROOT
from apps.api.base import DataAPI


def bcs_cc_before_request(params):
params = add_esb_info_before_request(params)
if settings.BCS_CC_SSM_SWITCH:
bkssm_access_token = BkSSMApi.get_access_token({"grant_type": "client_credentials", "id_provider": "client"})
access_token = bkssm_access_token["access_token"]
params["X-BKAPI-AUTHORIZATION"] = json.dumps({"access_token": access_token})
return params


def bcs_get_cluster_config_after(response):
response["data"] = json.loads(response["data"]["configure"])
return response


class _BcsCcApi(object):
MODULE = _(u"Bcs cc 配置中心")

def __init__(self):
self.get_cluster_config_by_cluster_id = DataAPI(
method="GET",
url=BCS_CC_APIGATEWAY_ROOT + "v1/clusters/{cluster_id}/cluster_config/",
module=self.MODULE,
url_keys=["cluster_id"],
description=u"根据集群id获取集群信息",
header_keys=["X-BKAPI-AUTHORIZATION"],
before_request=bcs_cc_before_request,
after_request=bcs_get_cluster_config_after,
)
self.get_cluster_by_cluster_id = DataAPI(
method="GET",
url=BCS_CC_APIGATEWAY_ROOT + "clusters/{cluster_id}/",
module=self.MODULE,
url_keys=["cluster_id"],
description=u"根据集群id获取集群信息",
header_keys=["X-BKAPI-AUTHORIZATION"],
before_request=bcs_cc_before_request,
)
self.list_cluster = DataAPI(
method="GET",
url=BCS_CC_APIGATEWAY_ROOT + "cluster_list/",
module=self.MODULE,
before_request=bcs_cc_before_request,
header_keys=["X-BKAPI-AUTHORIZATION"],
)
self.list_area = DataAPI(
method="GET",
url=BCS_CC_APIGATEWAY_ROOT + "areas/",
module=self.MODULE,
before_request=bcs_cc_before_request,
header_keys=["X-BKAPI-AUTHORIZATION"],
)
self.list_project = DataAPI(
method="GET",
url=BCS_CC_APIGATEWAY_ROOT + "projects/",
module=self.MODULE,
before_request=bcs_cc_before_request,
header_keys=["X-BKAPI-AUTHORIZATION"],
)
54 changes: 54 additions & 0 deletions apps/api/modules/bk_ssm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""
Tencent is pleased to support the open source community by making BK-LOG 蓝鲸日志平台 available.
Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
BK-LOG 蓝鲸日志平台 is licensed under the MIT License.
License for BK-LOG 蓝鲸日志平台:
--------------------------------------------------------------------
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial
portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""

from django.utils.translation import ugettext_lazy as _

from apps.api.modules.utils import add_esb_info_before_request
from apps.log_search.constants import TimeEnum
from config.domains import BK_SSM_ROOT

from apps.api.base import DataAPI


class _BkSSM:
MODULE = _("bkssm")

def __init__(self):
self.get_access_token = DataAPI(
method="POST",
url=BK_SSM_ROOT + "access-tokens",
module=self.MODULE,
description=_("获取access_token"),
before_request=add_esb_info_before_request,
cache_time=TimeEnum.ONE_DAY_SECOND.value,
)
self.verify_access_token = DataAPI(
method="POST",
url=BK_SSM_ROOT + "access-tokens/verify",
module=self.MODULE,
description=_("verify access_token"),
before_request=add_esb_info_before_request,
)
self.refresh_access_token = DataAPI(
method="POST",
url=BK_SSM_ROOT + "access-tokens/refresh",
module=self.MODULE,
description=_("refresh access_token"),
before_request=add_esb_info_before_request,
)
9 changes: 9 additions & 0 deletions apps/api/modules/bkdata_aiops.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,15 @@ def __init__(self):
after_request=None,
default_timeout=300,
)
self.aiops_get_model_storage_cluster = DataAPI(
method="GET",
url=AIOPS_APIGATEWAY_ROOT + "models/storage_clusters/",
module=self.MODULE,
description=u"获取模型存储集群列表",
before_request=add_esb_info_before_request_for_bkdata_user,
after_request=None,
default_timeout=300,
)


BkDataAIOPSApi = _BkDataAIOPSApi()
9 changes: 9 additions & 0 deletions apps/api/modules/iam.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ def __init__(self):
default_return_value=None,
before_request=add_esb_info_before_request,
)
self.share_system_info = DataAPI(
method="GET",
url=IAM_APIGATEWAY_ROOT_V2 + "share/systems/{system_id}/",
module=self.MODULE,
url_keys=["system_id"],
description="系统共享信息",
default_return_value=None,
before_request=add_esb_info_before_request,
)


IAMApi = _IAMApi()
7 changes: 7 additions & 0 deletions apps/api/modules/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ def __init__(self):
module=self.MODULE,
before_request=get_job_request_before,
)
self.get_public_script_list = DataAPI(
method="GET",
url=JOB_APIGATEWAY_ROOT_V2 + "get_public_script_list",
description=_("查询公共脚本列表"),
module=self.MODULE,
before_request=get_job_request_before,
)


JobApi = _JobApi()
18 changes: 13 additions & 5 deletions apps/api/modules/monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,39 +38,47 @@ def __init__(self):
method="POST",
url=MONITOR_APIGATEWAY_ROOT + "save_alarm_strategy/",
module=self.MODULE,
description=u"保存告警策略",
description="保存告警策略",
default_return_value=None,
before_request=add_esb_info_before_request,
)
self.save_notice_group = DataAPI(
method="POST",
url=MONITOR_APIGATEWAY_ROOT + "save_notice_group/",
module=self.MODULE,
description=u"保存通知组",
description="保存通知组",
default_return_value=None,
before_request=add_esb_info_before_request,
)
self.save_alarm_strategy_v2 = DataAPI(
method="POST",
url=MONITOR_APIGATEWAY_ROOT + "save_alarm_strategy_v2/",
module=self.MODULE,
description=u"保存告警策略V2",
description="保存告警策略V2",
default_return_value=None,
before_request=add_esb_info_before_request,
)
self.search_alarm_strategy_v2 = DataAPI(
method="POST",
url=MONITOR_APIGATEWAY_ROOT + "search_alarm_strategy_v2/",
module=self.MODULE,
description=u"查询告警策略V2",
description="查询告警策略V2",
default_return_value=None,
before_request=add_esb_info_before_request,
)
self.delete_alarm_strategy_v2 = DataAPI(
method="POST",
url=MONITOR_APIGATEWAY_ROOT + "delete_alarm_strategy_v2/",
module=self.MODULE,
description=u"删除告警策略V2",
description="删除告警策略V2",
default_return_value=None,
before_request=add_esb_info_before_request,
)
self.search_alarm_strategy_v3 = DataAPI(
method="POST",
url=MONITOR_APIGATEWAY_ROOT + "search_alarm_strategy_v3",
module=self.MODULE,
description="查询告警策略V3",
default_return_value=None,
before_request=add_esb_info_before_request,
)
Expand Down
Loading

0 comments on commit 15f151d

Please sign in to comment.