Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 新增IP选择器后端搜索和分页 #7550 #7591

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_paged_business_host_topo(..., start:int, limit:int, ip_str=None)
格式转换的动作在 query 里面做
单一使用场景,可以把相关参数列出来,这里几乎不涉及兼容

"""获取业务下所有主机信息
: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
Loading