Skip to content

Commit

Permalink
优化集群ip选择器筛选逻辑 (#6982)
Browse files Browse the repository at this point in the history
* optimization: 优化集群ip选择器筛选逻辑

* test: 修复单元测试

* minor: 优化代码结构
  • Loading branch information
hanshuaikang authored Aug 11, 2023
1 parent 33e459a commit f949d43
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -649,15 +649,7 @@ def test_ip_selector_select_method_no_inner_module_success_case(self, mock_get_c
[
{
"func": self.client.new().cc.find_module_with_relation,
"calls": [
call(
bk_biz_id=1,
bk_service_template_ids=[],
bk_set_ids=[31],
fields=["bk_module_id"],
page={"start": 0, "limit": 1},
)
],
"calls": [],
},
{"func": self.client.new().cc.list_biz_hosts, "calls": []},
]
Expand Down Expand Up @@ -821,15 +813,7 @@ def test_select_method_get_ip_fail_case(self, mock_get_client_by_user_return=Non
[
{
"func": self.client.new().cc.find_module_with_relation,
"calls": [
call(
bk_biz_id=1,
bk_service_template_ids=[],
bk_set_ids=[31],
fields=["bk_module_id"],
page={"start": 0, "limit": 1},
)
],
"calls": [],
},
{"func": self.client.new().cc.list_biz_hosts, "calls": []},
]
Expand Down Expand Up @@ -858,15 +842,7 @@ def test_manual_method_success_case(self, mock_get_client_by_user_return=None):
[
{
"func": self.client.new().cc.find_module_with_relation,
"calls": [
call(
bk_biz_id=1,
bk_service_template_ids=[],
bk_set_ids=[31],
fields=["bk_module_id"],
page={"start": 0, "limit": 1},
)
],
"calls": [],
},
{"func": self.client.new().cc.list_biz_hosts, "calls": []},
]
Expand Down Expand Up @@ -979,15 +955,7 @@ def test_manual_method_fail_case(self, mock_get_client_by_user_return=None):
[
{
"func": self.client.new().cc.find_module_with_relation,
"calls": [
call(
bk_biz_id=1,
bk_service_template_ids=[],
bk_set_ids=[31],
fields=["bk_module_id"],
page={"start": 0, "limit": 1},
)
],
"calls": [],
},
{"func": self.client.new().cc.list_biz_hosts, "calls": []},
]
Expand All @@ -1012,22 +980,7 @@ def test_custom_method_success_case(self, mock_get_client_by_user_return=None):
[
{
"func": self.client.new().cc.find_module_with_relation,
"calls": [
call(
bk_biz_id=1,
bk_service_template_ids=[61],
bk_set_ids=[31, 32],
fields=["bk_module_id"],
page={"start": 0, "limit": 1},
),
call(
bk_biz_id=1,
bk_service_template_ids=[61],
bk_set_ids=[31, 32],
fields=["bk_module_id"],
page={"limit": 500, "start": 0},
),
],
"calls": [],
},
{
"func": self.client.new().cc.list_biz_hosts,
Expand Down Expand Up @@ -1070,15 +1023,7 @@ def test_custom_method_fail_case(self, mock_get_client_by_user_return=None):
[
{
"func": self.client.new().cc.find_module_with_relation,
"calls": [
call(
bk_biz_id=1,
bk_service_template_ids=[],
bk_set_ids=[31, 32],
fields=["bk_module_id"],
page={"start": 0, "limit": 1},
)
],
"calls": [],
},
{"func": self.client.new().cc.list_biz_hosts, "calls": []},
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@
from typing import List

from django.utils.translation import ugettext_lazy as _
from pipeline.core.data.var import LazyVariable

from gcloud.conf import settings
from gcloud.constants import BIZ_INTERNAL_SET, Type
from gcloud.core.models import Project
from pipeline.core.data.var import LazyVariable
from pipeline_plugins.base.utils.inject import supplier_account_for_project
from pipeline_plugins.components.utils.sites.open.utils import cc_get_ips_info_by_str, cc_get_ips_info_by_str_ipv6
from pipeline_plugins.variables.base import FieldExplain, SelfExplainVariable
from pipeline_plugins.variables.utils import (
get_service_template_list_by_names,
get_list_by_selected_names,
filter_ip,
get_set_list,
get_service_template_list,
list_biz_hosts,
find_module_with_relation,
get_biz_internal_module,
get_list_by_selected_names,
get_service_template_list,
get_service_template_list_by_names,
get_set_list,
list_biz_hosts,
)
from pipeline_plugins.variables.base import SelfExplainVariable, FieldExplain

ALL_SELECTED_STR = "all"

Expand Down Expand Up @@ -87,6 +87,7 @@ def get_value(self):
)
logger.info("[SetModuleIpSelector.get_value] module_ids: %s" % module_ids)

# 如果没有过滤到任何模块id,此时返回空
# 自定义输入ip
custom_value = var_ip_selector["var_ip_custom_value"]
if settings.ENABLE_IPV6:
Expand Down Expand Up @@ -283,6 +284,10 @@ def get_module_id_list(
if biz_internal_module_item["name"] in internal_module_in_filter
]

# 当存在服务模板过滤,并一个也过滤不到时
if filter_service_template_names and not service_template_ids:
return []

# 调用find_module_with_relation接口根据set id list, service_template_id_list查询模块id
module_id_list = find_module_with_relation(bk_biz_id, username, set_ids, service_template_ids, ["bk_module_id"])
# 拼接空闲机、待回收等模块ID
Expand Down Expand Up @@ -418,6 +423,7 @@ def get_ip_result_by_input_method(
module_ids = get_module_id_list(
bk_biz_id, username, set_list, service_template_list, filter_set, filter_service_template, bk_supplier_account
)

# 根据模块 id 列表获取 ip 并返回
if settings.ENABLE_IPV6:
data = get_ip_list_by_module_id_with_ipv6(username, bk_biz_id, bk_supplier_account, module_ids)
Expand Down

0 comments on commit f949d43

Please sign in to comment.