Example #1
0
def verify_host_property(executor, supplier_account, language,
                         cc_host_property, cc_host_prop_value):
    if cc_host_property == "bk_isp_name":
        bk_isp_name = cc_format_prop_data(executor, "host", "bk_isp_name",
                                          language, supplier_account)
        if not bk_isp_name["result"]:
            ex_data = bk_isp_name["message"]
            return False, ex_data

        host_prop_value = bk_isp_name["data"].get(cc_host_prop_value)
        if not host_prop_value:
            ex_data = _(
                "所属运营商【{}】校验失败,请重试并修改为正确的所属运营商".format(cc_host_prop_value))
            return False, ex_data

    elif cc_host_property == "bk_state_name":
        bk_state_name = cc_format_prop_data(executor, "host", "bk_state_name",
                                            language, supplier_account)
        if not bk_state_name["result"]:
            ex_data = bk_state_name["message"]
            return False, ex_data

        host_prop_value = bk_state_name["data"].get(cc_host_prop_value)
        if not host_prop_value:
            ex_data = _(
                "所在国家【{}】校验失败,请重试并修改为正确的所在国家".format(cc_host_prop_value))
            return False, ex_data
    elif cc_host_property == "bk_province_name":
        bk_province_name = cc_format_prop_data(executor, "host",
                                               "bk_province_name", language,
                                               supplier_account)
        if not bk_province_name["result"]:
            ex_data = bk_province_name["message"]
            return False, ex_data
        host_prop_value = bk_province_name["data"].get(cc_host_prop_value)
        if not host_prop_value:
            ex_data = _(
                "所在省份【{}】校验失败,请重试并修改为正确的所在省份".format(cc_host_prop_value))
            return False, ex_data
    return True, ""
Example #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)
        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
Example #3
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_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
Example #4
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_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
Example #5
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
Example #6
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
Example #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)
        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