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

Hotfix release master #7594

Merged
Show file tree
Hide file tree
Changes from 7 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
120 changes: 108 additions & 12 deletions gcloud/tests/utils/cmdb/test_business_host_topo.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ def setUp(self):
self.ip_list = "ip_list_token"
self.ip_str = "ip_str_token"
self.ip_strs = "ip1_str_token, ip2_str_token"
self.host_id = "225"
self.host_ids = "225,286"
self.list_biz_hosts_topo_return = [
{
"host": {
Expand Down Expand Up @@ -111,7 +113,8 @@ def setUp(self):
},
],
},
]
],
"count": 2,
},
}
self.get_business_host_topo_expect_return = [
Expand Down Expand Up @@ -142,12 +145,42 @@ def setUp(self):
],
},
]
self.get_filter_business_host_topo_expect_return = (
[
{
"host": {
"bk_cloud_id": 0,
"bk_host_id": 1,
"bk_host_innerip": "127.0.0.1",
"bk_mac": "",
"bk_os_type": None,
},
"set": [{"bk_set_id": 11, "bk_set_name": "set1"}],
"module": [{"bk_module_id": 56, "bk_module_name": "m1"}],
},
{
"host": {
"bk_cloud_id": 0,
"bk_host_id": 3,
"bk_host_innerip": "127.0.0.3",
"bk_mac": "",
"bk_os_type": None,
},
"set": [{"bk_set_id": 10, "bk_set_name": "空闲机池"}, {"bk_set_id": 11, "bk_set_name": "set1"}],
"module": [
{"bk_module_id": 54, "bk_module_name": "空闲机"},
{"bk_module_id": 55, "bk_module_name": "空闲机1"},
{"bk_module_id": 56, "bk_module_name": "m1"},
],
},
],
2,
)

def tearDown(self):
self.get_client_by_user_patcher.stop()

def test__list_biz_hosts_topo_return_empty(self):

mock_batch_request = MagicMock(return_value=[])
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)
Expand Down Expand Up @@ -197,20 +230,21 @@ def test__get_contains_with_ip_list(self):
self.bk_biz_id,
self.supplier_account,
self.host_fields,
start=0,
limit=10,
start="0",
limit="10",
ip_str=self.ip_str,
)

self.assertEqual(hosts_topo, self.get_business_host_topo_expect_return)
self.assertEqual(hosts_topo, self.get_filter_business_host_topo_expect_return)
self.mock_client.cc.list_biz_hosts_topo.assert_called_once_with(
{
"bk_biz_id": self.bk_biz_id,
"bk_supplier_account": self.supplier_account,
"fields": self.host_fields,
"host_property_filter": {
"condition": "OR",
"rules": [{"field": "bk_host_innerip", "operator": "contains", "value": self.ip_str}],
"rules": [{"field": "bk_host_innerip_v6", "operator": "contains", "value": self.ip_str}]
+ [{"field": "bk_host_innerip", "operator": "contains", "value": self.ip_str}],
},
"page": {"start": 0, "limit": 10},
},
Expand All @@ -223,12 +257,12 @@ def test__get_many_contains_with_ip_list(self):
self.bk_biz_id,
self.supplier_account,
self.host_fields,
start=0,
limit=10,
start="0",
limit="10",
ip_str=self.ip_strs,
)

self.assertEqual(hosts_topo, self.get_business_host_topo_expect_return)
self.assertEqual(hosts_topo, self.get_filter_business_host_topo_expect_return)
self.mock_client.cc.list_biz_hosts_topo.assert_called_once_with(
{
"bk_biz_id": self.bk_biz_id,
Expand All @@ -237,6 +271,10 @@ def test__get_many_contains_with_ip_list(self):
"host_property_filter": {
"condition": "OR",
"rules": [
{"field": "bk_host_innerip_v6", "operator": "contains", "value": self.ip_str}
for self.ip_str in self.ip_strs.split(",")
]
+ [
{"field": "bk_host_innerip", "operator": "contains", "value": self.ip_str}
for self.ip_str in self.ip_strs.split(",")
],
Expand All @@ -248,15 +286,73 @@ def test__get_many_contains_with_ip_list(self):
def test__get_with_page_list(self):
self.mock_client.cc.list_biz_hosts_topo = MagicMock(return_value=self.list_biz_hosts_page_topo_return)
hosts_topo = get_filter_business_host_topo(
self.username, self.bk_biz_id, self.supplier_account, self.host_fields, start=0, limit=10
self.username, self.bk_biz_id, self.supplier_account, self.host_fields, start="0", limit="10"
)

self.assertEqual(hosts_topo, self.get_business_host_topo_expect_return)
self.assertEqual(hosts_topo, self.get_filter_business_host_topo_expect_return)
self.mock_client.cc.list_biz_hosts_topo.assert_called_once_with(
{
"bk_biz_id": self.bk_biz_id,
"bk_supplier_account": self.supplier_account,
"fields": self.host_fields,
"page": {"start": 0, "limit": 10},
},
}
)

def test_get_equal_host_list(self):
self.mock_client.cc.list_biz_hosts_topo = MagicMock(return_value=self.list_biz_hosts_page_topo_return)
hosts_topo = get_filter_business_host_topo(
self.username,
self.bk_biz_id,
self.supplier_account,
self.host_fields,
start="0",
limit="10",
host_id=self.host_id,
)

self.assertEqual(hosts_topo, self.get_filter_business_host_topo_expect_return)
self.mock_client.cc.list_biz_hosts_topo.assert_called_once_with(
{
"bk_biz_id": self.bk_biz_id,
"bk_supplier_account": self.supplier_account,
"fields": self.host_fields,
"host_property_filter": {
"condition": "OR",
"rules": [{"field": "bk_host_id", "operator": "in", "value": [int(self.host_id)]}],
},
"page": {"start": 0, "limit": 10},
}
)

def test_get_many_equal_host_list(self):
self.mock_client.cc.list_biz_hosts_topo = MagicMock(return_value=self.list_biz_hosts_page_topo_return)
hosts_topo = get_filter_business_host_topo(
self.username,
self.bk_biz_id,
self.supplier_account,
self.host_fields,
start="0",
limit="10",
host_id=self.host_ids,
)

self.assertEqual(hosts_topo, self.get_filter_business_host_topo_expect_return)
self.mock_client.cc.list_biz_hosts_topo.assert_called_once_with(
{
"bk_biz_id": self.bk_biz_id,
"bk_supplier_account": self.supplier_account,
"fields": self.host_fields,
"host_property_filter": {
"condition": "OR",
"rules": [
{
"field": "bk_host_id",
"operator": "in",
"value": [int(host_id) for host_id in self.host_ids.split(",")],
}
],
},
"page": {"start": 0, "limit": 10},
}
)
43 changes: 30 additions & 13 deletions gcloud/utils/cmdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,10 @@ 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, start, limit, ip_str=None):
"""获取业务下所有主机信息
def get_filter_business_host_topo(
username, bk_biz_id, supplier_account, host_fields, start=None, limit=None, ip_str=None, host_id=None
):
"""获取业务下所有符合条件的主机信息
:param username: 请求用户名
:type username: str
:param bk_biz_id: 业务 CC ID
Expand All @@ -104,11 +106,13 @@ def get_filter_business_host_topo(username, bk_biz_id, supplier_account, host_fi
:param host_fields: 主机过滤字段
:type host_fields: list
:param start: 起始页数
:type start: int
:type start: str
guohelu marked this conversation as resolved.
Show resolved Hide resolved
:param limit: 每页数量,最大数量限制为 500
:type limit: int
:type limit: str
:param ip_str: 主机内网 IP
:type ip_str: str
:param host_id: 主机 id
guohelu marked this conversation as resolved.
Show resolved Hide resolved
:type host_id: str
:return: [
{
"host": {
Expand Down Expand Up @@ -137,16 +141,29 @@ def get_filter_business_host_topo(username, bk_biz_id, supplier_account, host_fi
"""
client = get_client_by_user(username)
params = {"bk_biz_id": bk_biz_id, "bk_supplier_account": supplier_account, "fields": list(host_fields or [])}
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": start, "limit": limit}
data = client.cc.list_biz_hosts_topo(params)
if not data["result"]:
raise Exception(_("查询主机列表失败, 请确认业务[{}]是否存在!".format(bk_biz_id)))
rules = []
# 根据host_id进行精准匹配
if host_id:
host_id_list = [int(id) for id in host_id.split(",")]
guohelu marked this conversation as resolved.
Show resolved Hide resolved
rules.extend([{"field": "bk_host_id", "operator": "in", "value": host_id_list}])
# 根据ip_str进行模糊匹配
elif ip_str:
guohelu marked this conversation as resolved.
Show resolved Hide resolved
rules.extend([{"field": "bk_host_innerip_v6", "operator": "contains", "value": ip} for ip in ip_str.split(",")])
rules.extend([{"field": "bk_host_innerip", "operator": "contains", "value": ip} for ip in ip_str.split(",")])
if rules:
params["host_property_filter"] = {"condition": "OR", "rules": rules}

result = data["data"]["info"]
count = None
if start and limit:
params["page"] = {"start": int(start), "limit": int(limit)}
data = client.cc.list_biz_hosts_topo(params)
if not data["result"]:
raise Exception(_("查询主机列表失败, 请确认业务[{}]是否存在!".format(bk_biz_id)))
count = data["data"]["count"]
result = data["data"]["info"]
else:
result = batch_request(client.cc.list_biz_hosts_topo, params)

host_info_list = []
for host_topo in result:
Expand All @@ -160,7 +177,7 @@ def get_filter_business_host_topo(username, bk_biz_id, supplier_account, host_fi

host_info_list.append(host_info)

return host_info_list
return host_info_list, count


def get_business_host(username, bk_biz_id, supplier_account, host_fields, ip_list=None, bk_cloud_id=None):
Expand Down
30 changes: 16 additions & 14 deletions pipeline_plugins/cmdb_ip_picker/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,19 @@ def cmdb_search_host(request, bk_biz_id, bk_supplier_account="", bk_supplier_id=
if settings.ENABLE_IPV6 or settings.ENABLE_GSE_V2:
# IPV6环境下或者开启了GSE 2.0 版本
default_host_fields.append("bk_agent_id")
fields = set(default_host_fields + json.loads(request.GET.get("fields", "[]")))
fields = set(default_host_fields + json.loads(request.POST.get("fields", "[]")))
client = get_client_by_user(request.user.username)

topo_modules_id = set()

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

# get filter module id
if request.GET.get("topo", None):
topo = json.loads(request.GET.get("topo"))
if request.POST.get("topo", None):
topo = json.loads(request.POST.get("topo"))
topo_result = get_cmdb_topo_tree(request.user.username, bk_biz_id, bk_supplier_account)
if not topo_result["result"]:
return JsonResponse(topo_result)
Expand All @@ -113,12 +114,9 @@ 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)

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

# map cloud_area_id to cloud_area
cloud_area_dict = {}
Expand Down Expand Up @@ -207,7 +205,7 @@ def cmdb_search_host(request, bk_biz_id, bk_supplier_account="", bk_supplier_id=
host["agent"] = agent_info.get("bk_agent_alive", -1)

# search host lock status
if request.GET.get("search_host_lock", None):
if request.POST.get("search_host_lock", None):
bk_host_id_list = [host_detail["bk_host_id"] for host_detail in data]
host_lock_status_result = client.cc.search_host_lock({"id_list": bk_host_id_list})
if not host_lock_status_result["result"]:
Expand All @@ -219,7 +217,11 @@ def cmdb_search_host(request, bk_biz_id, bk_supplier_account="", bk_supplier_id=
host_lock_status = host_lock_status_data.get(host_detail["bk_host_id"])
if host_lock_status is not None:
host_detail["bk_host_lock_status"] = host_lock_status
result = {"result": True, "code": NO_ERROR, "data": data}

if count:
result = {"result": True, "code": NO_ERROR, "data": {"data": data, "count": count}}
Copy link
Member

Choose a reason for hiding this comment

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

这里涉及返回两套协议,前端需要自己判断如何处理?

else:
result = {"result": True, "code": NO_ERROR, "data": data}
return JsonResponse(result)


Expand Down
Loading