Exemple #1
0
def parse_int_param(name,
                    value,
                    allow_zero=True,
                    allow_negative=False,
                    lower_limit=None,
                    upper_limit=None):
    if value is None:
        return None

    if value in ('0', 0):
        if allow_zero:
            return int(value)
        raise exception.InvalidParameter(name=name, value=value)

    try:
        result = int(value)
    except (TypeError, ValueError):
        raise exception.InvalidParameter(name=name, value=value)
    else:
        if any([(allow_negative is False and result < 0),
                (lower_limit and result < lower_limit),
                (upper_limit and result > upper_limit)]):
            raise exception.InvalidParameter(name=name, value=value)

    return result
Exemple #2
0
    def _do_resize(self, req, cluster_id, this_action, body):
        data = body.get(this_action)
        adj_type = data.get(consts.ADJUSTMENT_TYPE)
        number = data.get(consts.ADJUSTMENT_NUMBER)
        min_size = data.get(consts.ADJUSTMENT_MIN_SIZE)
        max_size = data.get(consts.ADJUSTMENT_MAX_SIZE)
        min_step = data.get(consts.ADJUSTMENT_MIN_STEP)
        strict = data.get(consts.ADJUSTMENT_STRICT)
        if adj_type is not None:
            if adj_type not in consts.ADJUSTMENT_TYPES:
                raise senlin_exc.InvalidParameter(name='adjustment_type',
                                                  value=adj_type)
            if number is None:
                msg = _("Missing number value for resize operation.")
                raise exc.HTTPBadRequest(msg)

        if number is not None:
            if adj_type is None:
                msg = _("Missing adjustment_type value for resize "
                        "operation.")
                raise exc.HTTPBadRequest(msg)
            number = utils.parse_int_param(consts.ADJUSTMENT_NUMBER,
                                           number,
                                           allow_negative=True)

        if min_size is not None:
            min_size = utils.parse_int_param(consts.ADJUSTMENT_MIN_SIZE,
                                             min_size)
        if max_size is not None:
            max_size = utils.parse_int_param(consts.ADJUSTMENT_MAX_SIZE,
                                             max_size,
                                             allow_negative=True)
        if (min_size is not None and max_size is not None and max_size > 0
                and min_size > max_size):
            msg = _("The specified min_size (%(n)s) is greater than the "
                    "specified max_size (%(m)s).") % {
                        'm': max_size,
                        'n': min_size
                    }
            raise exc.HTTPBadRequest(msg)

        if min_step is not None:
            min_step = utils.parse_int_param(consts.ADJUSTMENT_MIN_STEP,
                                             min_step)
        if strict is not None:
            strict = utils.parse_bool_param(consts.ADJUSTMENT_STRICT, strict)

        result = self.rpc_client.cluster_resize(req.context, cluster_id,
                                                adj_type, number, min_size,
                                                max_size, min_step, strict)
        location = {'location': '/actions/%s' % result['action']}
        result.update(location)
        return result
Exemple #3
0
def validate_sort_param(value, whitelist):
    """Validate a string value and see if it is a valid sort param.

    :param value: A string as the input which should be one of the following
                  formats:
                  - 'key1,key2,key3'
                  - 'key1:asc,key2,key3:desc'
                  - 'key1:asc,key2:asc,key3:desc'
    :param whitelist: A list of permitted sorting keys.
    :return: None if validation succeeds or an exception of `InvalidParameter`
             otherwise.
    """

    if value is None:
        return None

    for s in value.split(','):
        s_key, _sep, s_dir = s.partition(':')
        if not s_key or s_key not in whitelist:
            raise exception.InvalidParameter(name='sort key', value=s_key)
        if s_dir and s_dir not in ('asc', 'desc'):
            raise exception.InvalidParameter(name='sort dir', value=s_dir)
Exemple #4
0
def parse_bool_param(name, value):
    if str(value).lower() not in ('true', 'false'):
        raise exception.InvalidParameter(name=name, value=str(value))

    return strutils.bool_from_string(value, strict=True)