From 044b378ee00c58e2789b73d66044e6d5ce845599 Mon Sep 17 00:00:00 2001 From: ygcyao Date: Tue, 26 Mar 2024 17:30:09 +0800 Subject: [PATCH] =?UTF-8?q?feat(backend):=20gse=E6=8E=A5=E5=8F=A3=E8=BF=81?= =?UTF-8?q?=E7=A7=BB=E8=87=B3apigw=E8=8A=82=E7=82=B9=E7=AE=A1=E7=90=86agen?= =?UTF-8?q?t=E6=9F=A5=E8=AF=A2=E6=8E=A5=E5=8F=A3=20#3707?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- dbm-ui/backend/components/base.py | 5 +++ dbm-ui/backend/components/bklog/client.py | 4 +- dbm-ui/backend/components/bknodeman/client.py | 10 ++++- dbm-ui/backend/components/sops/client.py | 4 +- .../db_services/ipchooser/query/resource.py | 43 +++++++++++++++++++ 5 files changed, 61 insertions(+), 5 deletions(-) diff --git a/dbm-ui/backend/components/base.py b/dbm-ui/backend/components/base.py index 3a6770e2f6..58d6924109 100644 --- a/dbm-ui/backend/components/base.py +++ b/dbm-ui/backend/components/base.py @@ -25,6 +25,7 @@ from backend import env from backend.components.constants import CLIENT_CRT_PATH, SSL_KEY, SSLEnum +from backend.components.domains import ESB_PREFIX from backend.components.exception import DataAPIException from backend.components.utils.params import add_esb_info_before_request, remove_auth_args from backend.configuration.models.system import SystemSettings @@ -540,6 +541,10 @@ class BaseApi(object): MODULE = "" BASE = "" + @classmethod + def is_esb(cls): + return ESB_PREFIX in cls.BASE + def generate_data_api(self, method, url, description, **kwargs): """ 生成 DataAPI,使用类变量 BASE,MODULE 作为统一参数 diff --git a/dbm-ui/backend/components/bklog/client.py b/dbm-ui/backend/components/bklog/client.py index 90385edb6c..798ba5d7de 100644 --- a/dbm-ui/backend/components/bklog/client.py +++ b/dbm-ui/backend/components/bklog/client.py @@ -11,7 +11,7 @@ from django.utils.translation import ugettext_lazy as _ from ..base import BaseApi -from ..domains import BKLOG_APIGW_DOMAIN, ESB_PREFIX +from ..domains import BKLOG_APIGW_DOMAIN class _BKLogApi(BaseApi): @@ -19,7 +19,7 @@ class _BKLogApi(BaseApi): BASE = BKLOG_APIGW_DOMAIN def __init__(self): - is_esb = ESB_PREFIX in BKLOG_APIGW_DOMAIN + is_esb = self.is_esb() self.esquery_search = self.generate_data_api( method="POST", url="esquery_search/", diff --git a/dbm-ui/backend/components/bknodeman/client.py b/dbm-ui/backend/components/bknodeman/client.py index 2a754d1a70..083be60068 100644 --- a/dbm-ui/backend/components/bknodeman/client.py +++ b/dbm-ui/backend/components/bknodeman/client.py @@ -27,8 +27,16 @@ class JobStatusType(object): PROCESSING_STATUS = [PENDING, RUNNING] def __init__(self): + is_esb = self.is_esb() self.operate_plugin = self.generate_data_api(method="POST", url="api/plugin/operate/", description=_("插件操作任务")) - self.job_details = self.generate_data_api(method="GET", url="api/job/details/", description=_("查询任务详情")) + self.job_details = self.generate_data_api( + method="GET" if is_esb else "POST", + url="api/job/details/" if is_esb else "api/job/{job_id}/details/", + description=_("查询任务详情"), + ) + self.ipchooser_host_details = self.generate_data_api( + method="POST", url="core/api/ipchooser_host/details/", description=_("查询agent状态") + ) BKNodeManApi = _BKNodeManApi() diff --git a/dbm-ui/backend/components/sops/client.py b/dbm-ui/backend/components/sops/client.py index 196e5cad79..e88268b794 100644 --- a/dbm-ui/backend/components/sops/client.py +++ b/dbm-ui/backend/components/sops/client.py @@ -13,7 +13,7 @@ from django.utils.translation import ugettext_lazy as _ from ..base import BaseApi -from ..domains import ESB_PREFIX, SOPS_APIGW_DOMAIN +from ..domains import SOPS_APIGW_DOMAIN class _BkSopsApi(BaseApi): @@ -21,7 +21,7 @@ class _BkSopsApi(BaseApi): BASE = SOPS_APIGW_DOMAIN def __init__(self): - is_esb = ESB_PREFIX in SOPS_APIGW_DOMAIN + is_esb = self.is_esb() self.create_task = self.generate_data_api( method="POST", url="create_task/" if is_esb else "create_task/{template_id}/{bk_biz_id}/", diff --git a/dbm-ui/backend/db_services/ipchooser/query/resource.py b/dbm-ui/backend/db_services/ipchooser/query/resource.py index 991a8af695..fe79d59f98 100644 --- a/dbm-ui/backend/db_services/ipchooser/query/resource.py +++ b/dbm-ui/backend/db_services/ipchooser/query/resource.py @@ -20,6 +20,7 @@ from backend import env from backend.bk_web.constants import CACHE_1D from backend.components import CCApi +from backend.components.bknodeman.client import BKNodeManApi from backend.components.gse.client import GseApi from backend.utils.batch_request import batch_request from backend.utils.cache import func_cache_decorator @@ -195,6 +196,44 @@ def query_cc_hosts( return resp + @staticmethod + def query_agent_status_from_nodeman(cc_hosts, fill_key="status"): + """查询agent状态""" + host_list, host_map, scope_list = [], {}, [] + # 构建bk_host_id对bk_biz_id的映射 + bk_host_id_to_biz_id = { + item["bk_host_id"]: item["bk_biz_id"] + for item in CCApi.find_host_biz_relations({"bk_host_id": [host["bk_host_id"] for host in cc_hosts]}) + } + + for index, cc_host in enumerate(cc_hosts): + bk_host_id = cc_host["bk_host_id"] + bk_biz_id = bk_host_id_to_biz_id.get(bk_host_id, env.DBA_APP_BK_BIZ_ID) + # 构建meta数据 + meta = {"scope_type": constants.ScopeType.BIZ.value, "scope_id": str(bk_biz_id), "bk_biz_id": bk_biz_id} + host_list.append({"host_id": bk_host_id, "meta": meta}) + + # 构建scope_list数据 + meta_bak = meta.copy() + del meta_bak["bk_biz_id"] + scope_list.append(meta_bak) + + # 使用index + host_map[str(bk_host_id)] = index + + # 准备request参数 + request_params = { + "host_list": host_list, + "scope_list": scope_list, + } + try: + status_map = BKNodeManApi.ipchooser_host_details(request_params) + for status in status_map: + # agent在线状态,0为不在线,1为在线 + cc_hosts[host_map[str(status["host_id"])]][fill_key] = status["alive"] + except KeyError as e: + logger.error("query_agent_one_status exception: %s", e) + @staticmethod def query_agent_one_status(cc_hosts, fill_key="status"): """查询agent1.0状态""" @@ -243,6 +282,10 @@ def fill_agent_status(cc_hosts, fill_key="status"): if not cc_hosts: return + if not BKNodeManApi.is_esb(): + ResourceQueryHelper.query_agent_status_from_nodeman(cc_hosts, fill_key) + return + if env.GSE_AGENT_VERSION == "1.0": ResourceQueryHelper.query_agent_one_status(cc_hosts, fill_key) return