Skip to content

Commit

Permalink
feat(backend): gse接口迁移至apigw节点管理agent查询接口 #3707
Browse files Browse the repository at this point in the history
  • Loading branch information
ygcyao authored and zhangzhw8 committed Mar 26, 2024
1 parent 5bd6031 commit 044b378
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 5 deletions.
5 changes: 5 additions & 0 deletions dbm-ui/backend/components/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 作为统一参数
Expand Down
4 changes: 2 additions & 2 deletions dbm-ui/backend/components/bklog/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
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):
MODULE = _("蓝鲸日志平台")
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/",
Expand Down
10 changes: 9 additions & 1 deletion dbm-ui/backend/components/bknodeman/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
4 changes: 2 additions & 2 deletions dbm-ui/backend/components/sops/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
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):
MODULE = _("标准运维")
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}/",
Expand Down
43 changes: 43 additions & 0 deletions dbm-ui/backend/db_services/ipchooser/query/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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状态"""
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 044b378

Please sign in to comment.