Skip to content

Commit

Permalink
feat: 新增IP选择器后端搜索和分页
Browse files Browse the repository at this point in the history
  • Loading branch information
guohelu committed Oct 22, 2024
1 parent 4456151 commit 469553b
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
11 changes: 11 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,14 @@ repos:
- id: commitlint
stages: [commit-msg]
additional_dependencies: ['@commitlint/config-conventional']
- repo: local
hooks:
- id: check-migrate
name: check migrate
entry: python scripts/check_migrate/check_migrate.py
language: system
types: [python]
- id: check-sensitive-info
name: Check Sensitive Info
entry: sh scripts/check_sensitive_info.sh
language: system
8 changes: 7 additions & 1 deletion gcloud/tests/utils/cmdb/test_business_host_topo.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def setUp(self):
self.bk_biz_id = "biz_id_token"
self.supplier_account = "supplier_account_token"
self.host_fields = ["host_fields_token"]
self.operator = "in"
self.ip_list = "ip_list_token"
self.list_biz_hosts_topo_return = [
{
Expand Down Expand Up @@ -113,7 +114,12 @@ def test__get_with_ip_list(self):
mock_batch_request = MagicMock(return_value=self.list_biz_hosts_topo_return)
with patch("gcloud.utils.cmdb.batch_request", mock_batch_request):
hosts_topo = get_business_host_topo(
self.username, self.bk_biz_id, self.supplier_account, self.host_fields, self.ip_list
self.username,
self.bk_biz_id,
self.supplier_account,
self.host_fields,
self.ip_list,
operator=self.operator,
)

self.assertEqual(hosts_topo, self.get_business_host_topo_expect_return)
Expand Down
8 changes: 6 additions & 2 deletions gcloud/utils/cmdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@


def get_business_host_topo(
username, bk_biz_id, supplier_account, host_fields, ip_list=None, property_filters=None, page=None
username, bk_biz_id, supplier_account, host_fields, ip_list=None, property_filters=None, page=None, operator=None
):
"""获取业务下所有主机信息
:param username: 请求用户名
Expand All @@ -41,6 +41,10 @@ def get_business_host_topo(
:type ip_list: list
:param property_filters: 查询主机时的相关属性过滤条件, 当传递该参数时,ip_list参数生成的过滤条件失效
:type property_filters: dict
:param page: 分页参数,例如:{"start":0,"limit":2}
:type page: dict
:param operator: 查询方法,"in"代表精准匹配,"contains"代表模糊查询
:type operator: str
:return: [
{
"host": {
Expand Down Expand Up @@ -75,7 +79,7 @@ def get_business_host_topo(
elif ip_list:
kwargs["host_property_filter"] = {
"condition": "AND",
"rules": [{"field": "bk_host_innerip", "operator": "in", "value": ip_list}],
"rules": [{"field": "bk_host_innerip", "operator": operator, "value": ip_list}],
}

if page:
Expand Down
21 changes: 15 additions & 6 deletions pipeline_plugins/cmdb_ip_picker/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,20 @@ def cmdb_search_host(request, bk_biz_id, bk_supplier_account="", bk_supplier_id=
client = get_client_by_user(request.user.username)

topo_modules_id = set()
page = {
"start": int(request.GET.get("start", 0)),
"limit": int(request.GET.get("limit", 10)),
}
ip_list = json.loads(request.GET.get("ip_list", "[]"))

operator = request.GET.get("operator", None)
if operator == "in":
ip_list = json.loads(request.GET.get("ip_list", "[]"))
elif operator == "contains":
ip_list = json.loads(request.GET.get("ip_list", ""))
else:
ip_list = None

start = request.GET.get("start", None)
if start:
page = {"start": int(start), "limit": int(request.GET.get("limit", 10))}
else:
page = None

# get filter module id
if request.GET.get("topo", None):
Expand All @@ -115,7 +124,7 @@ def cmdb_search_host(request, bk_biz_id, bk_supplier_account="", bk_supplier_id=
return JsonResponse(result)

raw_host_info_list = cmdb.get_business_host_topo(
request.user.username, bk_biz_id, bk_supplier_account, fields, ip_list=ip_list, page=page
request.user.username, bk_biz_id, bk_supplier_account, fields, ip_list=ip_list, page=page, operator=operator
)

# map cloud_area_id to cloud_area
Expand Down

0 comments on commit 469553b

Please sign in to comment.