Skip to content

Commit

Permalink
feat: 新增IP选择器后端搜索和分页 #7550
Browse files Browse the repository at this point in the history
  • Loading branch information
guohelu committed Oct 23, 2024
1 parent a5b517f commit 8a86a20
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
70 changes: 70 additions & 0 deletions gcloud/utils/cmdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,76 @@ def get_business_host_topo(username, bk_biz_id, supplier_account, host_fields, i
return host_info_list


def get_filter_business_host_topo_(username, bk_biz_id, supplier_account, host_fields, **kwargs):
"""获取业务下所有主机信息
:param username: 请求用户名
:type username: str
:param bk_biz_id: 业务 CC ID
:type bk_biz_id: int
:param supplier_account: 开发商账号, defaults to 0
:type supplier_account: int
:param host_fields: 主机过滤字段
:type host_fields: list
:param kwargs: 其他参数
:param ip_str: IP 字符串
:type ip_str: str
:param start: 起始页数
:type start: str
:param limit: 每页数量
:type limit: str
:return: [
{
"host": {
"bk_host_id": 4,
"bk_host_innerip": "127.0.0.1",
"bk_cloud_id": 0,
...
},
"module": [
{
"bk_module_id": 2,
"bk_module_name": "module_name"
},
...
],
"set": [
{
"bk_set_name": "set_name",
"bk_set_id": 1
},
...
]
}
]
:rtype: list
"""
client = get_client_by_user(username)
params = {"bk_biz_id": bk_biz_id, "bk_supplier_account": supplier_account, "fields": list(host_fields or [])}
ip_str = kwargs.get("ip_str")
if ip_str:
rules = [{"field": "bk_host_innerip", "operator": "contains", "value": ip} for ip in ip_str.split(",")]

params["host_property_filter"] = {"condition": "OR", "rules": rules}

params["page"] = {"start": int(kwargs.get("start")), "limit": int(kwargs.get("limit"))}
data = client.cc.list_biz_hosts_topo(params)
result = data["data"]["info"]

host_info_list = []
for host_topo in result:
host_info = {"host": host_topo["host"], "module": [], "set": []}
for parent_set in host_topo["topo"]:
host_info["set"].append({"bk_set_id": parent_set["bk_set_id"], "bk_set_name": parent_set["bk_set_name"]})
for parent_module in parent_set["module"]:
host_info["module"].append(
{"bk_module_id": parent_module["bk_module_id"], "bk_module_name": parent_module["bk_module_name"]}
)

host_info_list.append(host_info)

return host_info_list


def get_business_host(username, bk_biz_id, supplier_account, host_fields, ip_list=None, bk_cloud_id=None):
"""根据主机内网 IP 过滤业务下的主机
:param username: 请求用户名
Expand Down
11 changes: 10 additions & 1 deletion pipeline_plugins/cmdb_ip_picker/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ def cmdb_search_host(request, bk_biz_id, bk_supplier_account="", bk_supplier_id=

topo_modules_id = set()

ip_str = request.GET.get("ip_str", "")
start = request.GET.get("start", None)
limit = request.GET.get("limit", None)

# get filter module id
if request.GET.get("topo", None):
topo = json.loads(request.GET.get("topo"))
Expand All @@ -109,7 +113,12 @@ def cmdb_search_host(request, bk_biz_id, bk_supplier_account="", bk_supplier_id=
result = {"result": False, "code": ERROR_CODES.API_GSE_ERROR, "message": message}
return JsonResponse(result)

raw_host_info_list = cmdb.get_business_host_topo(request.user.username, bk_biz_id, bk_supplier_account, fields)
if start and limit:
raw_host_info_list = cmdb.get_filter_business_host_topo_(
request.user.username, bk_biz_id, bk_supplier_account, fields, ip_str=ip_str, start=start, limit=limit
)
else:
raw_host_info_list = cmdb.get_business_host_topo(request.user.username, bk_biz_id, bk_supplier_account, fields)

# map cloud_area_id to cloud_area
cloud_area_dict = {}
Expand Down

0 comments on commit 8a86a20

Please sign in to comment.