Skip to content

Commit

Permalink
optimization: 优化cc插件转移主机模块不支持云区域的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
hanshuaikang authored and ZhuoZhuoCrayon committed Oct 19, 2023
1 parent 257362d commit f9994ad
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,14 @@

from django.utils import translation
from django.utils.translation import ugettext_lazy as _

from pipeline.core.flow.activity import Service
from pipeline.core.flow.io import StringItemSchema, ArrayItemSchema, IntItemSchema
from pipeline.component_framework.component import Component

from pipeline_plugins.base.utils.inject import supplier_account_for_business
from pipeline_plugins.components.collections.sites.open.cc.base import cc_get_host_id_by_innerip, cc_format_tree_mode_id
from pipeline.core.flow.activity import Service
from pipeline.core.flow.io import ArrayItemSchema, IntItemSchema, StringItemSchema

from gcloud.conf import settings
from gcloud.utils.ip import get_ip_by_regex
from gcloud.utils.handlers import handle_api_error
from pipeline_plugins.base.utils.inject import supplier_account_for_business
from pipeline_plugins.components.collections.sites.open.cc.base import CCPluginIPMixin, cc_format_tree_mode_id

logger = logging.getLogger("celery")
get_client_by_user = settings.ESB_GET_CLIENT_BY_USER
Expand All @@ -36,7 +33,7 @@
cc_handle_api_error = partial(handle_api_error, __group_name__)


class CCTransferHostModuleService(Service):
class CCTransferHostModuleService(Service, CCPluginIPMixin):
def inputs_format(self):
return [
self.InputItem(
Expand Down Expand Up @@ -81,9 +78,8 @@ def execute(self, data, parent_data):
biz_cc_id = data.get_one_of_inputs("biz_cc_id", parent_data.inputs.biz_cc_id)
supplier_account = supplier_account_for_business(biz_cc_id)

# 查询主机id
ip_list = get_ip_by_regex(data.get_one_of_inputs("cc_host_ip"))
host_result = cc_get_host_id_by_innerip(executor, biz_cc_id, ip_list, supplier_account)
host_result = self.get_ip_info_list(executor, biz_cc_id, data.get_one_of_inputs("cc_host_ip"), supplier_account)

if not host_result["result"]:
data.set_outputs("ex_data", host_result["message"])
return False
Expand All @@ -96,7 +92,7 @@ def execute(self, data, parent_data):
cc_kwargs = {
"bk_biz_id": biz_cc_id,
"bk_supplier_account": supplier_account,
"bk_host_id": [int(host_id) for host_id in host_result["data"]],
"bk_host_id": [int(host["HostID"]) for host in host_result["ip_result"]],
"bk_module_id": cc_module_select,
"is_increment": True if cc_is_increment == "true" else False,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,20 @@

from django.utils import translation
from django.utils.translation import ugettext_lazy as _

from pipeline.core.flow.activity import Service
from pipeline.core.flow.io import StringItemSchema, ArrayItemSchema, IntItemSchema
from pipeline.component_framework.component import Component
from pipeline.core.flow.activity import Service
from pipeline.core.flow.io import ArrayItemSchema, IntItemSchema, StringItemSchema

from gcloud.conf import settings
from gcloud.utils.handlers import handle_api_error
from pipeline_plugins.base.utils.inject import supplier_account_for_business
from pipeline_plugins.components.collections.sites.open.cc.base import (
BkObjType,
CCPluginIPMixin,
SelectMethod,
cc_format_tree_mode_id,
cc_list_select_node_inst_id,
CCPluginIPMixin,
)
from pipeline_plugins.base.utils.inject import supplier_account_for_business

from gcloud.conf import settings
from gcloud.utils.handlers import handle_api_error

logger = logging.getLogger("celery")
get_client_by_user = settings.ESB_GET_CLIENT_BY_USER
Expand Down Expand Up @@ -101,7 +99,7 @@ def execute(self, data, parent_data):
# 查询主机id
ip_str = data.get_one_of_inputs("cc_host_ip")
# 获取主机id列表
host_result = self.get_host_list(executor, biz_cc_id, ip_str, supplier_account)
host_result = self.get_ip_info_list(executor, biz_cc_id, ip_str, supplier_account)
if not host_result["result"]:
data.set_outputs("ex_data", host_result["message"])
return False
Expand All @@ -128,7 +126,7 @@ def execute(self, data, parent_data):
cc_kwargs = {
"bk_biz_id": biz_cc_id,
"bk_supplier_account": supplier_account,
"bk_host_id": [int(host_id) for host_id in host_result["data"]],
"bk_host_id": [int(host["HostID"]) for host in host_result["ip_result"]],
"bk_module_id": cc_module_select,
"is_increment": True if cc_is_increment == "true" else False,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@

from django.test import TestCase
from mock import MagicMock

from pipeline.component_framework.test import (
ComponentTestMixin,
ComponentTestCase,
Call,
CallAssertion,
ComponentTestCase,
ComponentTestMixin,
ExecuteAssertion,
Call,
Patcher,
)

from pipeline_plugins.components.collections.sites.open.cc.transfer_host_module.v1_0 import (
CCTransferHostModuleComponent,
)
Expand Down Expand Up @@ -53,7 +53,7 @@ def __init__(
GET_CLIENT_BY_USER = (
"pipeline_plugins.components.collections.sites.open.cc.transfer_host_module.v1_0" ".get_client_by_user"
)
CC_GET_HOST_ID_BY_INNERIP = "pipeline_plugins.components.collections.sites.open.cc.base.cc_get_host_id_by_innerip"
CC_GET_IPS_INFO_BY_STR = "pipeline_plugins.components.collections.sites.open.cc.base.cc_get_ips_info_by_str"
CC_LIST_MATCH_NODE_INST_ID = (
"pipeline_plugins.components.collections.sites.open.cc.transfer_host_module.v1_0" ".cc_list_match_node_inst_id"
)
Expand Down Expand Up @@ -184,7 +184,7 @@ def __init__(
execute_assertion=ExecuteAssertion(success=True, outputs={}),
schedule_assertion=None,
execute_call_assertion=[
CallAssertion(func=CC_GET_HOST_ID_BY_INNERIP, calls=[Call("admin", 2, ["1.1.1.1", "2.2.2.2"], 0)]),
CallAssertion(func=CC_GET_IPS_INFO_BY_STR, calls=[Call("admin", 2, "1.1.1.1;2.2.2.2", 0)]),
CallAssertion(
func=SELECT_BY_TEXT_SUCCESS_CLIENT.cc.transfer_host_module,
calls=[
Expand All @@ -203,7 +203,9 @@ def __init__(
patchers=[
Patcher(target=GET_CLIENT_BY_USER, return_value=SELECT_BY_TEXT_SUCCESS_CLIENT),
Patcher(target=CC_GET_CLIENT_BY_USER, return_value=SELECT_BY_TEXT_SUCCESS_CLIENT),
Patcher(target=CC_GET_HOST_ID_BY_INNERIP, return_value={"result": True, "data": ["2", "3"]}),
Patcher(
target=CC_GET_IPS_INFO_BY_STR, return_value={"result": True, "ip_result": [{"HostID": 2}, {"HostID": 3}]}
),
],
)

Expand Down Expand Up @@ -231,7 +233,7 @@ def __init__(
execute_assertion=ExecuteAssertion(success=True, outputs={}),
schedule_assertion=None,
execute_call_assertion=[
CallAssertion(func=CC_GET_HOST_ID_BY_INNERIP, calls=[Call("admin", 2, ["1.1.1.1", "2.2.2.2"], 0)]),
CallAssertion(func=CC_GET_IPS_INFO_BY_STR, calls=[Call("admin", 2, "1.1.1.1;2.2.2.2", 0)]),
CallAssertion(
func=SELECT_BY_TOPO_SUCCESS_CLIENT.cc.transfer_host_module,
calls=[
Expand All @@ -250,7 +252,9 @@ def __init__(
patchers=[
Patcher(target=GET_CLIENT_BY_USER, return_value=SELECT_BY_TOPO_SUCCESS_CLIENT),
Patcher(target=CC_GET_CLIENT_BY_USER, return_value=SELECT_BY_TOPO_SUCCESS_CLIENT),
Patcher(target=CC_GET_HOST_ID_BY_INNERIP, return_value={"result": True, "data": ["2", "3"]}),
Patcher(
target=CC_GET_IPS_INFO_BY_STR, return_value={"result": True, "ip_result": [{"HostID": 2}, {"HostID": 3}]}
),
],
)

Expand Down Expand Up @@ -280,12 +284,14 @@ def __init__(
),
schedule_assertion=None,
execute_call_assertion=[
CallAssertion(func=CC_GET_HOST_ID_BY_INNERIP, calls=[Call("admin", 2, ["1.1.1.1", "2.2.2.2"], 0)])
CallAssertion(func=CC_GET_IPS_INFO_BY_STR, calls=[Call("admin", 2, "1.1.1.1;2.2.2.2", 0)]),
],
patchers=[
Patcher(target=GET_CLIENT_BY_USER, return_value=SELECT_BY_TEXT_ERROR_PATH_FAIL_CLIENT),
Patcher(target=CC_GET_CLIENT_BY_USER, return_value=SELECT_BY_TEXT_ERROR_PATH_FAIL_CLIENT),
Patcher(target=CC_GET_HOST_ID_BY_INNERIP, return_value={"result": True, "data": ["2", "3"]}),
Patcher(
target=CC_GET_IPS_INFO_BY_STR, return_value={"result": True, "ip_result": [{"HostID": 2}, {"HostID": 3}]}
),
],
)

Expand All @@ -312,11 +318,13 @@ def __init__(
execute_assertion=ExecuteAssertion(success=False, outputs={"ex_data": "输入文本路径[蓝鲸>Yun>module]与业务拓扑层级不匹配"}),
schedule_assertion=None,
execute_call_assertion=[
CallAssertion(func=CC_GET_HOST_ID_BY_INNERIP, calls=[Call("admin", 2, ["1.1.1.1", "2.2.2.2"], 0)])
CallAssertion(func=CC_GET_IPS_INFO_BY_STR, calls=[Call("admin", 2, "1.1.1.1;2.2.2.2", 0)]),
],
patchers=[
Patcher(target=GET_CLIENT_BY_USER, return_value=SELECT_BY_TEXT_ERROR_LEVEL_FAIL_CLIENT),
Patcher(target=CC_GET_CLIENT_BY_USER, return_value=SELECT_BY_TEXT_ERROR_LEVEL_FAIL_CLIENT),
Patcher(target=CC_GET_HOST_ID_BY_INNERIP, return_value={"result": True, "data": ["2", "3"]}),
Patcher(
target=CC_GET_IPS_INFO_BY_STR, return_value={"result": True, "ip_result": [{"HostID": 2}, {"HostID": 3}]}
),
],
)

0 comments on commit f9994ad

Please sign in to comment.