Exemplo n.º 1
0
    def _handle_aliases(self, spec=None, param=None, option_prefix=''):
        if spec is None:
            spec = self.argument_spec
        if param is None:
            param = self.params

        # this uses exceptions as it happens before we can safely call fail_json
        alias_warnings = []
        alias_results, self._legal_inputs = handle_aliases(
            spec, param, alias_warnings=alias_warnings)
        for option, alias in alias_warnings:
            self.warn('Both option %s and its alias %s are set.' %
                      (option_prefix + option, option_prefix + alias))

        deprecated_aliases = []
        for i in spec.keys():
            if 'deprecated_aliases' in spec[i].keys():
                for alias in spec[i]['deprecated_aliases']:
                    deprecated_aliases.append(alias)

        for deprecation in deprecated_aliases:
            if deprecation['name'] in param.keys():
                self.deprecate(
                    "Alias '%s' is deprecated. See the module docs for more information"
                    % deprecation['name'],
                    version=deprecation.get('version'),
                    date=deprecation.get('date'),
                    collection_name=deprecation.get('collection_name'))
        return alias_results
Exemplo n.º 2
0
def test_handle_aliases_basic():
    argument_spec = {
        'name': {
            'type': 'str',
            'aliases': ['surname', 'nick']
        },
    }

    params = {
        'name': 'foo',
        'path': 'bar',
        'surname': 'foo',
        'nick': 'foo',
    }

    expected = (
        {
            'surname': 'name',
            'nick': 'name'
        },
        DEFAULT_LEGAL_INPUTS + ['name', 'surname', 'nick'],
    )
    expected[1].sort()

    result = handle_aliases(argument_spec, params)
    result[1].sort()
    assert expected == result
Exemplo n.º 3
0
def test_handle_aliases_type_error():
    argument_spec = {
        'name': {
            'type': 'str',
            'aliases': 'surname'
        },
    }

    params = {
        'name': 'foo',
    }

    with pytest.raises(TypeError) as te:
        handle_aliases(argument_spec, params)
        assert 'internal error: required and default are mutually exclusive' in to_native(
            te.error)
Exemplo n.º 4
0
def test_handle_aliases_value_error():
    argument_spec = {
        'name': {
            'type': 'str',
            'aliases': ['surname', 'nick'],
            'default': 'bob',
            'required': True
        },
    }

    params = {
        'name': 'foo',
    }

    with pytest.raises(ValueError) as ve:
        handle_aliases(argument_spec, params)
        assert 'internal error: aliases must be a list or tuple' == to_native(
            ve.error)
Exemplo n.º 5
0
def test_handle_aliases_no_aliases():
    argument_spec = {
        'name': {
            'type': 'str'
        },
    }

    params = {'name': 'foo', 'path': 'bar'}

    expected = (
        {},
        DEFAULT_LEGAL_INPUTS + ['name'],
    )
    expected[1].sort()

    result = handle_aliases(argument_spec, params)
    result[1].sort()
    assert expected == result
Exemplo n.º 6
0
    def validate(self, *args, **kwargs):
        """Validate module parameters against argument spec.

        :Example:

        validator = ArgumentSpecValidator(argument_spec, parameters)
        passeded = validator.validate()

        :param argument_spec: Specification of parameters, type, and valid values
        :type argument_spec: dict

        :param parameters: Parameters provided to the role
        :type parameters: dict

        :returns: True if no errors were encountered, False if any errors were encountered.
        :rtype: bool
        """

        self._no_log_values.update(set_fallbacks(self.argument_spec, self._validated_parameters))

        alias_warnings = []
        alias_deprecations = []
        try:
            alias_results, legal_inputs = handle_aliases(self.argument_spec, self._validated_parameters, alias_warnings, alias_deprecations)
        except (TypeError, ValueError) as e:
            alias_results = {}
            legal_inputs = None
            self._add_error(to_native(e))

        for option, alias in alias_warnings:
            warn('Both option %s and its alias %s are set.' % (option, alias))

        for deprecation in alias_deprecations:
            deprecate("Alias '%s' is deprecated. See the module docs for more information" % deprecation['name'],
                      version=deprecation.get('version'), date=deprecation.get('date'),
                      collection_name=deprecation.get('collection_name'))

        self._no_log_values.update(list_no_log_values(self.argument_spec, self._validated_parameters))

        if legal_inputs is None:
            legal_inputs = list(alias_results.keys()) + list(self.argument_spec.keys())
        self._unsupported_parameters.update(get_unsupported_parameters(self.argument_spec, self._validated_parameters, legal_inputs))

        self._no_log_values.update(set_defaults(self.argument_spec, self._validated_parameters, False))

        try:
            check_required_arguments(self.argument_spec, self._validated_parameters)
        except TypeError as e:
            self._add_error(to_native(e))

        validate_argument_types(self.argument_spec, self._validated_parameters, errors=self._error_messages)
        validate_argument_values(self.argument_spec, self._validated_parameters, errors=self._error_messages)

        self._no_log_values.update(set_defaults(self.argument_spec, self._validated_parameters))

        validate_sub_spec(self.argument_spec, self._validated_parameters,
                          errors=self._error_messages,
                          no_log_values=self._no_log_values,
                          unsupported_parameters=self._unsupported_parameters)

        if self._unsupported_parameters:
            self._add_error('Unsupported parameters: %s' % ', '.join(sorted(list(self._unsupported_parameters))))

        self._sanitize_error_messages()

        if self.error_messages:
            return False
        else:
            return True