Exemplo n.º 1
0
    def __str__(self):
        queries = []
        for k, v in self.variables_dict.iteritems():
            value = "''"
            if (isinstance(v, basestring) and len(v.strip()) > 0) or (not isinstance(v, basestring) and v is not None):
                value = v
            
            if isinstance(v, basestring):
                value = patch_utils.convert_type(v, convertStr=True)
#                 if re.search('\d+', v) and not re.search('\D+', v) and not re.search('\.', v):
#                     value = int(v)
#                 if re.search('\D+', v):
#                     value = "'%s'" % v
#                 if re.search('\.', v):
#                     value = float(v)
                
            queries.append("%s = %s" % (k, value))
        return "SET GLOBAL " + ", ".join(queries) + ";"
Exemplo n.º 2
0
    def validate_configuration(context, values, datastore_manager="mysql", instances=[], dynamic_param=False):
        rules = configurations.get_validation_rules(datastore_manager=datastore_manager)

        LOG.info(_("Validating configuration values"))

        result = {}
        for k, v in values.iteritems():
            # get the validation rule dictionary, which will ensure there is a
            # rule for the given key name. An exception will be thrown if no
            # valid rule is located.
            rule = ConfigurationsController._get_item(k, rules["configuration-parameters"])

            exception_msg = []
            if rule.get("deleted_at") and not dynamic_param:
                output = {"parameter_name": rule.get("name"), "parameter_deleted_at": rule.get("deleted_at")}
                message = (
                    _("%(parameter_name)s parameter can no longer be " " set as of %(parameter_deleted_at)s") % output
                )
                exception_msg.append(message)

            # type checking
            valueType = rule.get("type")
            if isinstance(v, basestring):
                v = patch_utils.convert_type(v)

            """ignore express type's validation when validate configuration"""
            if valueType != "expression" and not dynamic_param:
                value_type = KSC_ConfigurationsController._find_type(valueType)
                if isinstance(value_type, tuple):
                    if not isinstance(v, value_type[0]) and not isinstance(v, value_type[1]):
                        output = {"key": k, "type": str(value_type)}
                        msg = (
                            _("The value provided for the configuration " "parameter %(key)s is not in type %(type)s.")
                            % output
                        )
                        exception_msg.append(msg)
                else:
                    """to handle the type is float and input is integer"""
                    if valueType == "float" and isinstance(v, int):
                        pass
                    elif not isinstance(v, KSC_ConfigurationsController._find_type(valueType)):
                        output = {"key": k, "type": valueType}
                        msg = (
                            _("The value provided for the configuration " "parameter %(key)s is not of type %(type)s.")
                            % output
                        )
                        exception_msg.append(msg)
            if valueType == "expression" and not dynamic_param:
                if not (isinstance(v, int) or isinstance(v, long)):
                    msg = (
                        _(
                            "The value provided for the configuration "
                            "parameter %s is neither integer nor long integer"
                        )
                        % k
                    )
                    exception_msg.append(msg)

            # integer min/max checking
            if (
                valueType != "expression"
                and (isinstance(v, int) or isinstance(v, float) or isinstance(v, long))
                and not isinstance(v, bool)
                and not dynamic_param
            ):

                min_value = rule.get("min")
                second_min_value = rule.get("second_min")

                if not second_min_value:
                    if valueType != "expression" and v < min_value:
                        output = {"key": k, "min": min_value}
                        message = (
                            _(
                                "The value for the configuration parameter "
                                "%(key)s is less than the minimum allowed: "
                                "%(min)s"
                            )
                            % output
                        )
                        exception_msg.append(message)
                else:
                    if valueType != "expression" and min_value < v < second_min_value:
                        output = {"key": k, "min": min_value, "second_min": second_min_value}
                        message = (
                            _(
                                "The value for the configuration parameter "
                                "%(key)s is greater than the minimun allowed: "
                                "%(min)s and less than the second minimum allowed: "
                                "%(second_min)s"
                            )
                            % output
                        )
                        exception_msg.append(message)

                try:
                    max_value = rule.get("max")
                except ValueError:
                    message = _(
                        "Invalid or unsupported max value defined in the "
                        "configuration-parameters configuration file. "
                        "Expected integer."
                    )
                    exception_msg.append(message)
                if valueType != "expression" and v > max_value:
                    output = {"key": k, "max": max_value}
                    message = (
                        _(
                            "The value for the configuration parameter "
                            "%(key)s is greater than the maximum "
                            "allowed: %(max)s"
                        )
                        % output
                    )
                    exception_msg.append(message)

            if isinstance(v, basestring) and not dynamic_param:
                enum_itmes = rule.get("enums", None)
                if enum_itmes != None:
                    valid_value = filter(lambda x: x.lower() == v.lower(), enum_itmes)
                    if valid_value == None or len(valid_value) == 0:
                        message = _("The string value %s is not a valid enum value. VALID ENUMS: %s" % (v, enum_itmes))
                        exception_msg.append(message)

            # step checking
            step = rule.get("step")
            if valueType != "expression" and step:
                min_value = rule.get("min")
                recommended_value = ((v - min_value) / step) * step + min_value
                if (v - min_value) % rule.get("step"):
                    output = {"key": k, "step": rule.get("step"), "recommended": recommended_value}
                    message = (
                        _(
                            "The value for the configuration parameter "
                            "%(key)s cannot be divisible by %(step)s "
                            "%(recommended)s is recommended"
                        )
                        % output
                    )
                    exception_msg.append(message)

            """validate expression value when the configuration attached"""
            if valueType == "expression" and dynamic_param:
                try:
                    max_value = rule.get("max")
                except ValueError:
                    message = _(
                        "Invalid or unsupported max value defined in the "
                        "configuration-parameters configuration file. "
                        "Expected integer."
                    )
                    exception_msg.append(message)
                inst_msg = []
                for instance in instances:
                    value = KSC_ConfigurationsController._get_dynamic_value(context, max_value, instance)
                    if value and float(v) > value:
                        output = {"key": k, "max": value, "instance_name": instance.name}
                        message = (
                            _(
                                "The value for the configuration parameter "
                                "%(key)s is greater than the maximum "
                                "allowed: %(max)s on %(instance_name)s"
                            )
                            % output
                        )
                        inst_msg.append(message)

                try:
                    min_value = rule.get("min")
                except ValueError:
                    message = _(
                        "Invalid or unsupported min value defined in the "
                        "configuration-parameters configuration file. "
                        "Expected integer."
                    )
                    exception_msg.append(message)
                for instance in instances:
                    value = KSC_ConfigurationsController._get_dynamic_value(context, min_value, instance)
                    if value and float(v) < value:
                        output = {"key": k, "min": value, "instance_name": instance.name}
                        message = (
                            _(
                                "The value for the configuration parameter "
                                "%(key)s is less than the minimum allowed: "
                                "%(min)s on %(instance_name)s"
                            )
                            % output
                        )
                        inst_msg.append(message)

                if inst_msg:
                    exception_msg.append("".join(inst_msg))
                else:
                    # step checking
                    step = rule.get("step")
                    if step and v % step:
                        output = {"key": k, "step": step}
                        message = (
                            _("The value for the configuration parameter " "%(key)s cannot be divisible by %(step)s")
                            % output
                        )
                        exception_msg.append(message)

            if exception_msg:
                result[rule.get("name")] = "".join(exception_msg)

        return result