diff --git a/pipeline_plugins/components/collections/sites/open/cc/transfer_host_module/legacy.py b/pipeline_plugins/components/collections/sites/open/cc/transfer_host_module/legacy.py index 9a59fa0836..f46dd2bd81 100644 --- a/pipeline_plugins/components/collections/sites/open/cc/transfer_host_module/legacy.py +++ b/pipeline_plugins/components/collections/sites/open/cc/transfer_host_module/legacy.py @@ -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 @@ -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( @@ -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 @@ -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, } diff --git a/pipeline_plugins/components/collections/sites/open/cc/transfer_host_module/v1_0.py b/pipeline_plugins/components/collections/sites/open/cc/transfer_host_module/v1_0.py index 8097a12b3d..84f4a36243 100644 --- a/pipeline_plugins/components/collections/sites/open/cc/transfer_host_module/v1_0.py +++ b/pipeline_plugins/components/collections/sites/open/cc/transfer_host_module/v1_0.py @@ -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 @@ -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 @@ -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, } diff --git a/pipeline_plugins/tests/components/collections/sites/open/cc_test/transfer_host_module/test_v1_0.py b/pipeline_plugins/tests/components/collections/sites/open/cc_test/transfer_host_module/test_v1_0.py index 592eab1bef..a2392b0f19 100644 --- a/pipeline_plugins/tests/components/collections/sites/open/cc_test/transfer_host_module/test_v1_0.py +++ b/pipeline_plugins/tests/components/collections/sites/open/cc_test/transfer_host_module/test_v1_0.py @@ -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, ) @@ -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" ) @@ -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=[ @@ -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}]} + ), ], ) @@ -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=[ @@ -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}]} + ), ], ) @@ -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}]} + ), ], ) @@ -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}]} + ), ], )