示例#1
0
    def execute(self, data, parent_data):
        executor = parent_data.get_one_of_inputs('executor')

        client = get_client_by_user(executor)
        if parent_data.get_one_of_inputs('language'):
            setattr(client, 'language',
                    parent_data.get_one_of_inputs('language'))
            translation.activate(parent_data.get_one_of_inputs('language'))

        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)
        cc_set_select = cc_format_tree_mode_id(
            data.get_one_of_inputs('cc_set_select'))

        for set_id in cc_set_select:
            cc_kwargs = {
                "bk_biz_id": biz_cc_id,
                "bk_supplier_account": supplier_account,
                "bk_set_id": set_id,
                "data": {
                    "bk_service_status":
                    data.get_one_of_inputs('cc_set_status')
                }
            }
            cc_result = client.cc.update_set(cc_kwargs)
            if not cc_result['result']:
                message = cc_handle_api_error('cc.update_set', cc_kwargs,
                                              cc_result)
                self.logger.error(message)
                data.set_outputs('ex_data', message)
                return False
        return True
示例#2
0
    def execute(self, data, parent_data):
        executor = parent_data.get_one_of_inputs("executor")

        client = get_client_by_user(executor)
        if parent_data.get_one_of_inputs("language"):
            setattr(client, "language", parent_data.get_one_of_inputs("language"))
            translation.activate(parent_data.get_one_of_inputs("language"))

        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"))
        # 获取主机id列表
        host_result = cc_get_host_id_by_innerip(executor, biz_cc_id, ip_list, supplier_account)
        if not host_result["result"]:
            data.set_outputs("ex_data", host_result["message"])
            return False

        cc_is_increment = data.get_one_of_inputs("cc_is_increment")
        cc_module_select_method = data.get_one_of_inputs("cc_module_select_method")
        if cc_module_select_method == SelectMethod.TOPO.value:
            cc_module_select = data.get_one_of_inputs("cc_module_select_topo") or []
            filtered_modules = [module for module in cc_module_select if module.startswith("module")]
            cc_module_select = cc_format_tree_mode_id(filtered_modules)
        elif cc_module_select_method == SelectMethod.TEXT.value:
            cc_module_select_text = data.get_one_of_inputs("cc_module_select_text")
            cc_list_select_node_inst_id_return = cc_list_select_node_inst_id(
                executor, biz_cc_id, supplier_account, BkObjType.MODULE, cc_module_select_text
            )
            if not cc_list_select_node_inst_id_return["result"]:
                data.set_outputs("ex_data", cc_list_select_node_inst_id_return["message"])
                return False
            cc_module_select = cc_list_select_node_inst_id_return["data"]
        else:
            data.set_outputs("ex_data", _("请选择填参方式"))
            return False

        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_module_id": cc_module_select,
            "is_increment": True if cc_is_increment == "true" else False,
        }
        cc_result = client.cc.transfer_host_module(cc_kwargs)
        if cc_result["result"]:
            return True
        else:
            message = cc_handle_api_error("cc.transfer_host_module", cc_kwargs, cc_result)
            self.logger.error(message)
            data.set_outputs("ex_data", message)
            return False
示例#3
0
文件: v1_0.py 项目: Tencent/bk-sops
    def execute(self, data, parent_data):
        executor = parent_data.get_one_of_inputs("executor")

        client = get_client_by_user(executor)
        if parent_data.get_one_of_inputs("language"):
            setattr(client, "language",
                    parent_data.get_one_of_inputs("language"))
            translation.activate(parent_data.get_one_of_inputs("language"))

        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)
        cc_set_select_method = data.get_one_of_inputs("cc_set_select_method")
        if cc_set_select_method == SelectMethod.TOPO.value:
            cc_set_select = cc_format_tree_mode_id(
                data.get_one_of_inputs("cc_set_select_topo"))
        elif cc_set_select_method == SelectMethod.TEXT.value:
            cc_set_select_text = data.get_one_of_inputs("cc_set_select_text")
            cc_list_select_node_inst_id_return = cc_list_select_node_inst_id(
                executor, biz_cc_id, supplier_account, BkObjType.SET,
                cc_set_select_text)
            if not cc_list_select_node_inst_id_return["result"]:
                data.set_outputs("ex_data",
                                 cc_list_select_node_inst_id_return["message"])
                return False
            cc_set_select = cc_list_select_node_inst_id_return["data"]
        else:
            data.set_outputs("ex_data", _("请选择填参方式"))
            return True

        for set_id in cc_set_select:
            cc_kwargs = {
                "bk_biz_id": biz_cc_id,
                "bk_supplier_account": supplier_account,
                "bk_set_id": set_id,
            }
            cc_result = client.cc.transfer_sethost_to_idle_module(cc_kwargs)
            if not cc_result["result"]:
                message = cc_handle_api_error(
                    "cc.transfer_sethost_to_idle_module", cc_kwargs, cc_result)
                self.logger.error(message)
                data.set_outputs("ex_data", message)
                return False
        return True
示例#4
0
文件: v1_0.py 项目: Tencent/bk-sops
    def execute(self, data, parent_data):
        executor = parent_data.get_one_of_inputs("executor")

        client = get_client_by_user(executor)
        if parent_data.get_one_of_inputs("language"):
            setattr(client, "language",
                    parent_data.get_one_of_inputs("language"))
            translation.activate(parent_data.get_one_of_inputs("language"))

        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)
        cc_set_parent_select = cc_format_tree_mode_id(
            data.get_one_of_inputs("cc_set_parent_select"))
        cc_set_info = deepcopy(data.get_one_of_inputs("cc_set_info"))

        bk_set_env = cc_format_prop_data(
            executor, "set", "bk_set_env",
            parent_data.get_one_of_inputs("language"), supplier_account)
        if not bk_set_env["result"]:
            data.set_outputs("ex_data", bk_set_env["message"])
            return False

        bk_service_status = cc_format_prop_data(
            executor, "set", "bk_service_status",
            parent_data.get_one_of_inputs("language"), supplier_account)
        if not bk_service_status["result"]:
            data.set_outputs("ex_data", bk_service_status["message"])
            return False

        set_list = []
        for set_params in cc_set_info:
            chunk_result = chunk_table_data(set_params)
            if not chunk_result["result"]:
                data.set_outputs("ex_data", chunk_result["message"])
                return False

            for property_data in chunk_result["data"]:
                set_property = {}
                for key, value in property_data.items():
                    if value:
                        if key == "bk_set_env":
                            value = bk_set_env["data"].get(value)
                            if not value:
                                data.set_outputs("ex_data",
                                                 _("环境类型校验失败,请重试并修改为正确的环境类型"))
                                return False

                        elif key == "bk_service_status":
                            value = bk_service_status["data"].get(value)
                            if not value:
                                data.set_outputs("ex_data",
                                                 _("服务状态校验失败,请重试并修改为正确的服务状态"))
                                return False

                        elif key == "bk_capacity":
                            try:
                                value = int(value)
                            except ValueError:
                                self.logger.error(traceback.format_exc())
                                data.set_outputs("ex_data", _("集群容量必须为整数"))
                                return False

                        set_property[key] = value
                set_list.append(set_property)

        for parent_id in cc_set_parent_select:
            for set_data in set_list:
                cc_kwargs = {
                    "bk_biz_id": biz_cc_id,
                    "bk_supplier_account": supplier_account,
                    "data": {
                        "bk_parent_id": parent_id
                    },
                }
                cc_kwargs["data"].update(set_data)
                cc_result = client.cc.create_set(cc_kwargs)
                if not cc_result["result"]:
                    message = cc_handle_api_error("cc.create_set", cc_kwargs,
                                                  cc_result)
                    self.logger.error(message)
                    data.set_outputs("ex_data", message)
                    return False

        return True
示例#5
0
文件: legacy.py 项目: Tencent/bk-sops
    def execute(self, data, parent_data):
        executor = parent_data.get_one_of_inputs('executor')

        client = get_client_by_user(executor)
        if parent_data.get_one_of_inputs('language'):
            setattr(client, 'language',
                    parent_data.get_one_of_inputs('language'))
            translation.activate(parent_data.get_one_of_inputs('language'))

        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)
        cc_set_parent_select = cc_format_tree_mode_id(
            data.get_one_of_inputs('cc_set_parent_select'))
        cc_set_info = data.get_one_of_inputs('cc_set_info')

        bk_set_env = cc_format_prop_data(
            executor, 'set', 'bk_set_env',
            parent_data.get_one_of_inputs('language'), supplier_account)
        if not bk_set_env['result']:
            data.set_outputs('ex_data', bk_set_env['message'])
            return False

        bk_service_status = cc_format_prop_data(
            executor, 'set', 'bk_service_status',
            parent_data.get_one_of_inputs('language'), supplier_account)
        if not bk_service_status['result']:
            data.set_outputs('ex_data', bk_service_status['message'])
            return False

        set_list = []
        for set_params in cc_set_info:
            set_property = {}
            for key, value in list(set_params.items()):
                if value:
                    if key == "bk_set_env":
                        value = bk_set_env['data'].get(value)
                        if not value:
                            data.set_outputs('ex_data',
                                             _("环境类型校验失败,请重试并修改为正确的环境类型"))
                            return False

                    elif key == "bk_service_status":
                        value = bk_service_status['data'].get(value)
                        if not value:
                            data.set_outputs('ex_data',
                                             _("服务状态校验失败,请重试并修改为正确的服务状态"))
                            return False

                    elif key == "bk_capacity":
                        try:
                            value = int(value)
                        except Exception:
                            self.logger.error(traceback.format_exc())
                            data.set_outputs('ex_data', _("集群容量必须为整数"))
                            return False

                    set_property[key] = value
            set_list.append(set_property)

        for parent_id in cc_set_parent_select:
            for set_data in set_list:
                cc_kwargs = {
                    'bk_biz_id': biz_cc_id,
                    'bk_supplier_account': supplier_account,
                    'data': {
                        'bk_parent_id': parent_id
                    }
                }
                cc_kwargs['data'].update(set_data)
                cc_result = client.cc.create_set(cc_kwargs)
                if not cc_result['result']:
                    message = cc_handle_api_error('cc.create_set', cc_kwargs,
                                                  cc_result)
                    self.logger.error(message)
                    data.set_outputs('ex_data', message)
                    return False

        return True
示例#6
0
文件: v1_0.py 项目: Tencent/bk-sops
    def execute(self, data, parent_data):
        executor = parent_data.get_one_of_inputs("executor")

        client = get_client_by_user(executor)
        if parent_data.get_one_of_inputs("language"):
            setattr(client, "language", parent_data.get_one_of_inputs("language"))
            translation.activate(parent_data.get_one_of_inputs("language"))

        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)
        cc_set_select_method = data.get_one_of_inputs("cc_set_select_method")
        if cc_set_select_method == SelectMethod.TOPO.value:
            cc_set_select = cc_format_tree_mode_id(data.get_one_of_inputs("cc_set_select_topo"))
        elif cc_set_select_method == SelectMethod.TEXT.value:
            cc_set_select_text = data.get_one_of_inputs("cc_set_select_text")
            cc_list_select_node_inst_id_return = cc_list_select_node_inst_id(
                executor, biz_cc_id, supplier_account, BkObjType.SET, cc_set_select_text
            )
            if not cc_list_select_node_inst_id_return["result"]:
                data.set_outputs("ex_data", cc_list_select_node_inst_id_return["message"])
                return False
            cc_set_select = cc_list_select_node_inst_id_return["data"]
        else:
            data.set_outputs("ex_data", _("请选择填参方式"))
            return False

        cc_set_property = data.get_one_of_inputs("cc_set_property")
        if cc_set_property == "bk_service_status":
            bk_service_status = cc_format_prop_data(
                executor, "set", "bk_service_status", parent_data.get_one_of_inputs("language"), supplier_account
            )
            if not bk_service_status["result"]:
                data.set_outputs("ex_data", bk_service_status["message"])
                return False

            cc_set_prop_value = bk_service_status["data"].get(data.get_one_of_inputs("cc_set_prop_value"))
            if not cc_set_prop_value:
                data.set_outputs("ex_data", _("服务状态校验失败,请重试并修改为正确的服务状态"))
                return False

        elif cc_set_property == "bk_set_env":
            bk_set_env = cc_format_prop_data(
                executor, "set", "bk_set_env", parent_data.get_one_of_inputs("language"), supplier_account
            )
            if not bk_set_env["result"]:
                data.set_outputs("ex_data", bk_set_env["message"])
                return False

            cc_set_prop_value = bk_set_env["data"].get(data.get_one_of_inputs("cc_set_prop_value"))
            if not cc_set_prop_value:
                data.set_outputs("ex_data", _("环境类型校验失败,请重试并修改为正确的环境类型"))
                return False

        elif cc_set_property == "bk_capacity":
            try:
                cc_set_prop_value = int(data.get_one_of_inputs("cc_set_prop_value"))
            except Exception:
                self.logger.error(traceback.format_exc())
                data.set_outputs("ex_data", _("集群容量必须为整数"))
                return False

        else:
            cc_set_prop_value = data.get_one_of_inputs("cc_set_prop_value")

        for set_id in cc_set_select:
            cc_kwargs = {
                "bk_biz_id": biz_cc_id,
                "bk_supplier_account": supplier_account,
                "bk_set_id": set_id,
                "data": {cc_set_property: cc_set_prop_value},
            }
            cc_result = client.cc.update_set(cc_kwargs)
            if not cc_result["result"]:
                message = cc_handle_api_error("cc.update_set", cc_kwargs, cc_result)
                self.logger.error(message)
                data.set_outputs("ex_data", message)
                return False
        return True
示例#7
0
    def execute(self, data, parent_data):
        executor = parent_data.get_one_of_inputs("executor")

        client = get_client_by_user(executor)
        if parent_data.get_one_of_inputs("language"):
            setattr(client, "language",
                    parent_data.get_one_of_inputs("language"))
            translation.activate(parent_data.get_one_of_inputs("language"))

        biz_cc_id = data.get_one_of_inputs("biz_cc_id",
                                           parent_data.inputs.biz_cc_id)
        cc_module_select_method = data.get_one_of_inputs(
            "cc_module_select_method")
        supplier_account = supplier_account_for_business(biz_cc_id)
        kwargs = {
            "bk_biz_id": biz_cc_id,
            "bk_supplier_account": supplier_account
        }
        tree_data = client.cc.search_biz_inst_topo(kwargs)
        if not tree_data["result"]:
            message = cc_handle_api_error("cc.search_biz_inst_topo", kwargs,
                                          tree_data)
            self.logger.error(message)
            data.set_outputs("ex_data", message)
            return False

        if cc_module_select_method == SelectMethod.TOPO.value:
            module_list = data.get_one_of_inputs("cc_module_select_topo")
            # 过滤掉set的情况
            module_list = list(
                filter(lambda x: x.startswith("module"), module_list))
            cc_module_select = cc_format_tree_mode_id(module_list)
        elif cc_module_select_method == SelectMethod.TEXT.value:
            cc_module_select_text = data.get_one_of_inputs(
                "cc_module_select_text")
            cc_list_select_node_inst_id_return = cc_list_select_node_inst_id(
                executor, biz_cc_id, supplier_account, BkObjType.MODULE,
                cc_module_select_text)
            if not cc_list_select_node_inst_id_return["result"]:
                data.set_outputs("ex_data",
                                 cc_list_select_node_inst_id_return["message"])
                return False
            cc_module_select = cc_list_select_node_inst_id_return["data"]
        else:
            data.set_outputs("ex_data", _("请选择模块填参方式"))
            return False

        cc_module_property = data.get_one_of_inputs("cc_module_property")
        if cc_module_property == "bk_module_type":
            bk_module_type = cc_format_prop_data(
                executor, "module", "bk_module_type",
                parent_data.get_one_of_inputs("language"), supplier_account)
            if not bk_module_type["result"]:
                data.set_outputs("ex_data", bk_module_type["message"])
                return False

            cc_module_prop_value = bk_module_type["data"].get(
                data.get_one_of_inputs("cc_module_prop_value"))
            if not cc_module_prop_value:
                data.set_outputs("ex_data", _("模块类型校验失败,请重试并填写正确的模块类型"))
                return False
        else:
            cc_module_prop_value = data.get_one_of_inputs(
                "cc_module_prop_value")

        for module_id in cc_module_select:
            cc_kwargs = {
                "bk_biz_id": biz_cc_id,
                "bk_supplier_account": supplier_account,
                "bk_set_id": get_module_set_id(tree_data["data"], module_id),
                "bk_module_id": module_id,
                "data": {
                    cc_module_property: cc_module_prop_value
                },
            }
            cc_result = client.cc.update_module(cc_kwargs)
            if not cc_result["result"]:
                message = cc_handle_api_error("cc.update_module", cc_kwargs,
                                              cc_result)
                self.logger.error(message)
                data.set_outputs("ex_data", message)
                return False
        return True
示例#8
0
    def execute(self, data, parent_data):
        executor = parent_data.get_one_of_inputs('executor')

        client = get_client_by_user(executor)
        if parent_data.get_one_of_inputs('language'):
            setattr(client, 'language',
                    parent_data.get_one_of_inputs('language'))
            translation.activate(parent_data.get_one_of_inputs('language'))

        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)
        cc_set_select = cc_format_tree_mode_id(
            data.get_one_of_inputs('cc_set_select'))

        cc_set_property = data.get_one_of_inputs('cc_set_property')
        if cc_set_property == "bk_service_status":
            bk_service_status = cc_format_prop_data(
                executor, 'set', 'bk_service_status',
                parent_data.get_one_of_inputs('language'), supplier_account)
            if not bk_service_status['result']:
                data.set_outputs('ex_data', bk_service_status['message'])
                return False

            cc_set_prop_value = bk_service_status['data'].get(
                data.get_one_of_inputs('cc_set_prop_value'))
            if not cc_set_prop_value:
                data.set_outputs('ex_data', _("服务状态校验失败,请重试并修改为正确的服务状态"))
                return False

        elif cc_set_property == "bk_set_env":
            bk_set_env = cc_format_prop_data(
                executor, 'set', 'bk_set_env',
                parent_data.get_one_of_inputs('language'), supplier_account)
            if not bk_set_env['result']:
                data.set_outputs('ex_data', bk_set_env['message'])
                return False

            cc_set_prop_value = bk_set_env['data'].get(
                data.get_one_of_inputs('cc_set_prop_value'))
            if not cc_set_prop_value:
                data.set_outputs('ex_data', _("环境类型校验失败,请重试并修改为正确的环境类型"))
                return False

        elif cc_set_property == "bk_capacity":
            try:
                cc_set_prop_value = int(
                    data.get_one_of_inputs('cc_set_prop_value'))
            except Exception:
                self.logger.error(traceback.format_exc())
                data.set_outputs('ex_data', _("集群容量必须为整数"))
                return False

        else:
            cc_set_prop_value = data.get_one_of_inputs('cc_set_prop_value')

        for set_id in cc_set_select:
            cc_kwargs = {
                "bk_biz_id": biz_cc_id,
                "bk_supplier_account": supplier_account,
                "bk_set_id": set_id,
                "data": {
                    cc_set_property: cc_set_prop_value
                }
            }
            cc_result = client.cc.update_set(cc_kwargs)
            if not cc_result['result']:
                message = cc_handle_api_error('cc.update_set', cc_kwargs,
                                              cc_result)
                self.logger.error(message)
                data.set_outputs('ex_data', message)
                return False
        return True
示例#9
0
文件: legacy.py 项目: Tencent/bk-sops
    def execute(self, data, parent_data):
        executor = parent_data.get_one_of_inputs("executor")
        client = get_client_by_user(executor)
        if parent_data.get_one_of_inputs("language"):
            setattr(client, "language", parent_data.get_one_of_inputs("language"))
            translation.activate(parent_data.get_one_of_inputs("language"))
        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)
        cc_set_select_method = data.get_one_of_inputs("cc_set_select_method")
        cc_create_method = data.get_one_of_inputs("cc_create_method")
        # 校验创建及填参方式,避免中途校验失败时已进行多余的逻辑判断
        if cc_set_select_method not in [SelectMethod.TOPO.value, SelectMethod.TEXT.value]:
            data.set_outputs("ex_data", _("请选择填参方式"))
            return False
        if cc_create_method not in [ModuleCreateMethod.CATEGORY.value, ModuleCreateMethod.TEMPLATE.value]:
            data.set_outputs("ex_data", _("请选择创建方式"))
            return False
        if cc_set_select_method == SelectMethod.TOPO.value:
            cc_set_select = cc_format_tree_mode_id(data.get_one_of_inputs("cc_set_select_topo"))
        else:
            cc_set_select_text = data.get_one_of_inputs("cc_set_select_text")
            cc_list_select_node_inst_id_return = cc_list_select_node_inst_id(
                executor, biz_cc_id, supplier_account, BkObjType.SET, cc_set_select_text
            )
            if not cc_list_select_node_inst_id_return["result"]:
                data.set_outputs("ex_data", cc_list_select_node_inst_id_return["message"])
                return False
            cc_set_select = cc_list_select_node_inst_id_return["data"]

        # 对象深拷贝保证inputs对象执行前后不变
        if cc_create_method == ModuleCreateMethod.TEMPLATE.value:
            cc_module_infos_untreated = copy.deepcopy(data.get_one_of_inputs("cc_module_infos_template"))
        else:
            cc_module_infos_untreated = copy.deepcopy(data.get_one_of_inputs("cc_module_infos_category"))
        if not cc_module_infos_untreated:
            data.set_outputs("ex_data", _("模块信息不能为空"))
            return False
        cc_module_infos = []
        for cc_module_info_untreated in cc_module_infos_untreated:
            if cc_create_method == ModuleCreateMethod.TEMPLATE.value:
                service_template_name, service_template_id = cc_get_name_id_from_combine_value(
                    cc_module_info_untreated.pop("cc_service_template")
                )
                if service_template_id is None:
                    data.set_outputs("ex_data", _("请选择正确的服务模板"))
                    return False
                cc_module_info_untreated["service_template_id"] = service_template_id
                # 按模板创建时,模块名称与模板名称保持一致
                cc_module_info_untreated["bk_module_name"] = service_template_name
            else:
                if len(cc_module_info_untreated["cc_service_category"]) != 2:
                    data.set_outputs("ex_data", _("请选择正确的服务类型"))
                    return False
                service_category_id = cc_module_info_untreated.pop("cc_service_category")[-1]
                cc_module_info_untreated["service_category_id"] = service_category_id

            # 过滤空值
            cc_module_info = {
                module_property: module_prop_value
                for module_property, module_prop_value in cc_module_info_untreated.items()
                if module_prop_value
            }

            if cc_create_method == ModuleCreateMethod.CATEGORY.value and "bk_module_name" not in cc_module_info:
                data.set_outputs("ex_data", _("模块名称不能为空"))
                return False
            # 校验模块类型
            if "bk_module_type" in cc_module_info:
                format_prop_data_return = cc_format_prop_data(
                    executor, "module", "bk_module_type", parent_data.get_one_of_inputs("language"), supplier_account
                )
                if not format_prop_data_return["result"]:
                    data.set_outputs("ex_data", format_prop_data_return["message"])
                    return False
                bk_module_type = format_prop_data_return["data"].get(cc_module_info["bk_module_type"])
                if not bk_module_type:
                    data.set_outputs("ex_data", _("模块类型校验失败,请重试并填写正确的模块类型"))
                    return False
                cc_module_info["bk_module_type"] = bk_module_type
            cc_module_infos.append(cc_module_info)
        for parent_id in cc_set_select:
            for cc_module_info in cc_module_infos:
                cc_kwargs = {
                    "bk_biz_id": biz_cc_id,
                    "bk_set_id": parent_id,
                    "data": {"bk_parent_id": parent_id},
                }
                cc_kwargs["data"].update(cc_module_info)
                cc_create_module_return = client.cc.create_module(cc_kwargs)
                if not cc_create_module_return["result"]:
                    message = cc_handle_api_error("cc.create_module", cc_kwargs, cc_create_module_return)
                    self.logger.error(message)
                    data.set_outputs("ex_data", message)
                    return False
        return True
示例#10
0
    def execute(self, data, parent_data):
        executor = parent_data.get_one_of_inputs("executor")

        client = get_client_by_user(executor)
        if parent_data.get_one_of_inputs("language"):
            setattr(client, "language",
                    parent_data.get_one_of_inputs("language"))
            translation.activate(parent_data.get_one_of_inputs("language"))

        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)
        cc_select_set_parent_method = data.get_one_of_inputs(
            "cc_select_set_parent_method")

        if cc_select_set_parent_method == SelectMethod.TOPO.value:
            # topo类型直接通过cc_format_tree_mode_id解析父节点bz_inst_id
            cc_set_parent_select = cc_format_tree_mode_id(
                data.get_one_of_inputs("cc_set_parent_select_topo"))
        elif cc_select_set_parent_method == SelectMethod.TEXT.value:
            cc_set_parent_select_text = data.get_one_of_inputs(
                "cc_set_parent_select_text")
            cc_list_select_node_inst_id_return = cc_list_select_node_inst_id(
                executor, biz_cc_id, supplier_account, BkObjType.LAST_CUSTOM,
                cc_set_parent_select_text)
            if not cc_list_select_node_inst_id_return["result"]:
                data.set_outputs("ex_data",
                                 cc_list_select_node_inst_id_return["message"])
                return False
            cc_set_parent_select = cc_list_select_node_inst_id_return["data"]
        else:
            data.set_outputs("ex_data", _("请选择填参方式"))
            return False

        cc_set_names = data.get_one_of_inputs("cc_set_name")
        cc_set_template = data.get_one_of_inputs("cc_set_template")

        result = {"fail": [], "success": []}

        for parent_id in cc_set_parent_select:
            cc_kwargs = {
                "bk_biz_id": biz_cc_id,
                "bk_supplier_account": supplier_account,
                "data": {
                    "bk_parent_id": parent_id
                },
            }
            for cc_set_name in cc_set_names.split(","):
                try:
                    attr_data_list = data.get_one_of_inputs("cc_set_attr")
                except Exception:
                    attr_data_list = []

                cc_kwargs["data"].update({
                    "bk_parent_id": parent_id,
                    "bk_set_name": cc_set_name,
                    "set_template_id": cc_set_template
                })

                if attr_data_list:
                    for attr_data in attr_data_list:
                        cc_kwargs["data"].update(
                            {attr_data["attr_id"]: attr_data["attr_value"]})

                cc_result = client.cc.create_set(cc_kwargs)
                if not cc_result["result"]:
                    message = cc_handle_api_error("cc.create_set", cc_kwargs,
                                                  cc_result)
                    self.logger.error(message)
                    result["fail"].append(message)
                else:
                    result["success"].append(cc_result["data"])
        if result["fail"]:
            data.set_outputs("ex_data", result["fail"])
            return False
        return True