Ejemplo n.º 1
0
def cast_params(action_ref, params):
    """
    """
    action_db = action_db_util.get_action_by_ref(action_ref)
    action_parameters_schema = action_db.parameters
    runnertype_db = action_db_util.get_runnertype_by_name(action_db.runner_type['name'])
    runner_parameters_schema = runnertype_db.runner_parameters
    # combine into 1 list of parameter schemas
    parameters_schema = {}
    if runner_parameters_schema:
        parameters_schema.update(runner_parameters_schema)
    if action_parameters_schema:
        parameters_schema.update(action_parameters_schema)
    # cast each param individually
    for k, v in six.iteritems(params):
        parameter_schema = parameters_schema.get(k, None)
        if not parameter_schema:
            LOG.debug('Will skip cast of param[name: %s, value: %s]. No schema.', k, v)
            continue
        parameter_type = parameter_schema.get('type', None)
        if not parameter_type:
            LOG.debug('Will skip cast of param[name: %s, value: %s]. No type.', k, v)
            continue
        cast = get_cast(cast_type=parameter_type)
        if not cast:
            LOG.debug('Will skip cast of param[name: %s, value: %s]. No cast for %s.', k, v,
                      parameter_type)
            continue
        LOG.debug('Casting param: %s of type %s to type: %s', v, type(v), parameter_type)
        params[k] = cast(v)
    return params
Ejemplo n.º 2
0
def cast_params(action_ref, params, cast_overrides=None):
    """"""
    params = params or {}
    action_db = action_db_util.get_action_by_ref(action_ref)

    if not action_db:
        raise ValueError('Action with ref "%s" doesn\'t exist' % (action_ref))

    action_parameters_schema = action_db.parameters
    runnertype_db = action_db_util.get_runnertype_by_name(
        action_db.runner_type["name"])
    runner_parameters_schema = runnertype_db.runner_parameters
    # combine into 1 list of parameter schemas
    parameters_schema = {}
    if runner_parameters_schema:
        parameters_schema.update(runner_parameters_schema)
    if action_parameters_schema:
        parameters_schema.update(action_parameters_schema)
    # cast each param individually
    for k, v in six.iteritems(params):
        parameter_schema = parameters_schema.get(k, None)
        if not parameter_schema:
            LOG.debug(
                "Will skip cast of param[name: %s, value: %s]. No schema.", k,
                v)
            continue
        parameter_type = parameter_schema.get("type", None)
        if not parameter_type:
            LOG.debug("Will skip cast of param[name: %s, value: %s]. No type.",
                      k, v)
            continue
        # Pick up cast from teh override and then from the system suppied ones.
        cast = cast_overrides.get(parameter_type,
                                  None) if cast_overrides else None
        if not cast:
            cast = get_cast(cast_type=parameter_type)
        if not cast:
            LOG.debug(
                "Will skip cast of param[name: %s, value: %s]. No cast for %s.",
                k,
                v,
                parameter_type,
            )
            continue
        LOG.debug("Casting param: %s of type %s to type: %s", v, type(v),
                  parameter_type)

        try:
            params[k] = cast(v)
        except Exception as e:
            v_type = type(v).__name__
            msg = (
                'Failed to cast value "%s" (type: %s) for parameter "%s" of type "%s": %s. '
                "Perhaps the value is of an invalid type?" %
                (v, v_type, k, parameter_type, six.text_type(e)))
            raise ValueError(msg)

    return params
Ejemplo n.º 3
0
def _cast(v, parameter_schema):
    if v is None or not parameter_schema:
        return v

    parameter_type = parameter_schema.get('type', None)
    if not parameter_type:
        return v

    cast = get_cast(cast_type=parameter_type)
    if not cast:
        return v

    return cast(v)
Ejemplo n.º 4
0
def _cast(v, parameter_schema):
    if v is None or not parameter_schema:
        return v

    parameter_type = parameter_schema.get('type', None)
    if not parameter_type:
        return v

    cast = get_cast(cast_type=parameter_type)
    if not cast:
        return v

    return cast(v)
Ejemplo n.º 5
0
def cast_params(action_ref, params, cast_overrides=None):
    """
    """
    params = params or {}
    action_db = action_db_util.get_action_by_ref(action_ref)

    if not action_db:
        raise ValueError('Action with ref "%s" doesn\'t exist' % (action_ref))

    action_parameters_schema = action_db.parameters
    runnertype_db = action_db_util.get_runnertype_by_name(action_db.runner_type['name'])
    runner_parameters_schema = runnertype_db.runner_parameters
    # combine into 1 list of parameter schemas
    parameters_schema = {}
    if runner_parameters_schema:
        parameters_schema.update(runner_parameters_schema)
    if action_parameters_schema:
        parameters_schema.update(action_parameters_schema)
    # cast each param individually
    for k, v in six.iteritems(params):
        parameter_schema = parameters_schema.get(k, None)
        if not parameter_schema:
            LOG.debug('Will skip cast of param[name: %s, value: %s]. No schema.', k, v)
            continue
        parameter_type = parameter_schema.get('type', None)
        if not parameter_type:
            LOG.debug('Will skip cast of param[name: %s, value: %s]. No type.', k, v)
            continue
        # Pick up cast from teh override and then from the system suppied ones.
        cast = cast_overrides.get(parameter_type, None) if cast_overrides else None
        if not cast:
            cast = get_cast(cast_type=parameter_type)
        if not cast:
            LOG.debug('Will skip cast of param[name: %s, value: %s]. No cast for %s.', k, v,
                      parameter_type)
            continue
        LOG.debug('Casting param: %s of type %s to type: %s', v, type(v), parameter_type)

        try:
            params[k] = cast(v)
        except Exception as e:
            v_type = type(v).__name__
            msg = ('Failed to cast value "%s" (type: %s) for parameter "%s" of type "%s": %s. '
                   'Perhaphs the value is of an invalid type?' %
                   (v, v_type, k, parameter_type, str(e)))
            raise ValueError(msg)

    return params
Ejemplo n.º 6
0
    def test_cast_array(self):
        cast_func = get_cast('array')

        # Python literal
        value = str([1, 2, 3])
        result = cast_func(value)
        self.assertEqual(result, [1, 2, 3])

        # JSON serialized
        value = json.dumps([4, 5, 6])
        result = cast_func(value)
        self.assertEqual(result, [4, 5, 6])

        # Can't cast, should throw
        value = "\\invalid"
        self.assertRaises(SyntaxError, cast_func, value)
Ejemplo n.º 7
0
def cast_params(action_ref, params, cast_overrides=None):
    """
    """
    params = params or {}
    action_db = action_db_util.get_action_by_ref(action_ref)

    if not action_db:
        raise ValueError('Action with ref "%s" doesn\'t exist' % (action_ref))

    action_parameters_schema = action_db.parameters
    runnertype_db = action_db_util.get_runnertype_by_name(
        action_db.runner_type['name'])
    runner_parameters_schema = runnertype_db.runner_parameters
    # combine into 1 list of parameter schemas
    parameters_schema = {}
    if runner_parameters_schema:
        parameters_schema.update(runner_parameters_schema)
    if action_parameters_schema:
        parameters_schema.update(action_parameters_schema)
    # cast each param individually
    for k, v in six.iteritems(params):
        parameter_schema = parameters_schema.get(k, None)
        if not parameter_schema:
            LOG.debug(
                'Will skip cast of param[name: %s, value: %s]. No schema.', k,
                v)
            continue
        parameter_type = parameter_schema.get('type', None)
        if not parameter_type:
            LOG.debug('Will skip cast of param[name: %s, value: %s]. No type.',
                      k, v)
            continue
        # Pick up cast from teh override and then from the system suppied ones.
        cast = cast_overrides.get(parameter_type,
                                  None) if cast_overrides else None
        if not cast:
            cast = get_cast(cast_type=parameter_type)
        if not cast:
            LOG.debug(
                'Will skip cast of param[name: %s, value: %s]. No cast for %s.',
                k, v, parameter_type)
            continue
        LOG.debug('Casting param: %s of type %s to type: %s', v, type(v),
                  parameter_type)
        params[k] = cast(v)
    return params
Ejemplo n.º 8
0
def _cast_params(rendered, parameter_schemas):
    casted_params = {}
    for k, v in six.iteritems(rendered):
        # Add uncasted first and then override with casted param. Not all params will end up
        # being cast.
        casted_params[k] = v
        # No casting if the value is None. It leads to weird cases like str(None) = 'None'
        # leading to downstream failures as well as int(None) leading to TypeError.
        if v is None:
            continue
        parameter_schema = parameter_schemas.get(k, None)
        if not parameter_schema:
            continue
        parameter_type = parameter_schema.get('type', None)
        if not parameter_type:
            continue
        cast = get_cast(cast_type=parameter_type)
        if not cast:
            continue
        casted_params[k] = cast(v)
    return casted_params
Ejemplo n.º 9
0
def _cast_params(rendered, parameter_schemas):
    casted_params = {}
    for k, v in six.iteritems(rendered):
        # Add uncasted first and then override with casted param. Not all params will end up
        # being cast.
        casted_params[k] = v
        # No casting if the value is None. It leads to weird cases like str(None) = 'None'
        # leading to downstream failures as well as int(None) leading to TypeError.
        if v is None:
            continue
        parameter_schema = parameter_schemas.get(k, None)
        if not parameter_schema:
            continue
        parameter_type = parameter_schema.get('type', None)
        if not parameter_type:
            continue
        cast = get_cast(cast_type=parameter_type)
        if not cast:
            continue
        casted_params[k] = cast(v)
    return casted_params
Ejemplo n.º 10
0
def cast_params(action_ref, params, cast_overrides=None):
    """
    """
    params = params or {}
    action_db = action_db_util.get_action_by_ref(action_ref)

    if not action_db:
        raise ValueError('Action with ref "%s" doesn\'t exist' % (action_ref))

    action_parameters_schema = action_db.parameters
    runnertype_db = action_db_util.get_runnertype_by_name(action_db.runner_type["name"])
    runner_parameters_schema = runnertype_db.runner_parameters
    # combine into 1 list of parameter schemas
    parameters_schema = {}
    if runner_parameters_schema:
        parameters_schema.update(runner_parameters_schema)
    if action_parameters_schema:
        parameters_schema.update(action_parameters_schema)
    # cast each param individually
    for k, v in six.iteritems(params):
        parameter_schema = parameters_schema.get(k, None)
        if not parameter_schema:
            LOG.debug("Will skip cast of param[name: %s, value: %s]. No schema.", k, v)
            continue
        parameter_type = parameter_schema.get("type", None)
        if not parameter_type:
            LOG.debug("Will skip cast of param[name: %s, value: %s]. No type.", k, v)
            continue
        # Pick up cast from teh override and then from the system suppied ones.
        cast = cast_overrides.get(parameter_type, None) if cast_overrides else None
        if not cast:
            cast = get_cast(cast_type=parameter_type)
        if not cast:
            LOG.debug("Will skip cast of param[name: %s, value: %s]. No cast for %s.", k, v, parameter_type)
            continue
        LOG.debug("Casting param: %s of type %s to type: %s", v, type(v), parameter_type)
        params[k] = cast(v)
    return params
Ejemplo n.º 11
0
def cast_params(action_ref, params):
    """
    """
    action_db = action_db_util.get_action_by_ref(action_ref)
    action_parameters_schema = action_db.parameters
    runnertype_db = action_db_util.get_runnertype_by_name(
        action_db.runner_type['name'])
    runner_parameters_schema = runnertype_db.runner_parameters
    # combine into 1 list of parameter schemas
    parameters_schema = {}
    if runner_parameters_schema:
        parameters_schema.update(runner_parameters_schema)
    if action_parameters_schema:
        parameters_schema.update(action_parameters_schema)
    # cast each param individually
    for k, v in six.iteritems(params):
        parameter_schema = parameters_schema.get(k, None)
        if not parameter_schema:
            LOG.debug(
                'Will skip cast of param[name: %s, value: %s]. No schema.', k,
                v)
            continue
        parameter_type = parameter_schema.get('type', None)
        if not parameter_type:
            LOG.debug('Will skip cast of param[name: %s, value: %s]. No type.',
                      k, v)
            continue
        cast = get_cast(cast_type=parameter_type)
        if not cast:
            LOG.debug(
                'Will skip cast of param[name: %s, value: %s]. No cast for %s.',
                k, v, parameter_type)
            continue
        LOG.debug('Casting param: %s of type %s to type: %s', v, type(v),
                  parameter_type)
        params[k] = cast(v)
    return params
Ejemplo n.º 12
0
    def test_cast_string(self):
        cast_func = get_cast('string')

        value = 'test1'
        result = cast_func(value)
        self.assertEqual(result, 'test1')

        value = u'test2'
        result = cast_func(value)
        self.assertEqual(result, u'test2')

        value = ''
        result = cast_func(value)
        self.assertEqual(result, '')

        # None should be preserved
        value = None
        result = cast_func(value)
        self.assertEqual(result, None)

        # Non string or non, should throw a friendly exception
        value = []
        expected_msg = 'Value "\[\]" must either be a string or None. Got "list"'
        self.assertRaisesRegexp(ValueError, expected_msg, cast_func, value)
Ejemplo n.º 13
0
    def test_cast_string(self):
        cast_func = get_cast("string")

        value = "test1"
        result = cast_func(value)
        self.assertEqual(result, "test1")

        value = "test2"
        result = cast_func(value)
        self.assertEqual(result, "test2")

        value = ""
        result = cast_func(value)
        self.assertEqual(result, "")

        # None should be preserved
        value = None
        result = cast_func(value)
        self.assertEqual(result, None)

        # Non string or non, should throw a friendly exception
        value = []
        expected_msg = r'Value "\[\]" must either be a string or None. Got "list"'
        self.assertRaisesRegexp(ValueError, expected_msg, cast_func, value)