Beispiel #1
0
    def _lookup(self, settings_files, settings_dict, extra_vars=None):
        """
        Replaces a setting values with !lookup
        in the setting files by values from
        other settings files.
        """
        all_settings = utils.load_settings_files(settings_files)
        utils.dict_merge(all_settings, settings_dict)
        utils.merge_extra_vars(
            all_settings,
            extra_vars)
        yamls.replace_lookup(all_settings)

        return all_settings
Beispiel #2
0
    def from_files(cls, settings_folders, app_name, app_subfolder,
                   user_dict, subparser, *spec_files):
        """
        Reads specs files and constructs the parser instance
        """
        if user_dict is None:
            user_dict = {}
        result = user_dict
        for spec_file in spec_files:
            with open(spec_file) as stream:
                spec = yaml.load(stream) or {}
                utils.dict_merge(
                    result,
                    spec,
                    utils.ConflictResolver.unique_append_list_resolver)

        return SpecParser(
            result, settings_folders, app_name, app_subfolder, subparser)
Beispiel #3
0
def test_dict_merge():
    from infrared.core.utils.utils import dict_merge

    first_dict = {
        'a': 1,
        'b': 2,
        'c': {
            'd': 'foo1',
            'e': 'bar',
            'list1': ['a', 'b', 'c']
        },
        'list2': [1, 2, 3]
    }
    second_dict = {
        'a': 2,
        'c': {
            'd': 'foo2',
            'f': 5,
            'list1': [3, 4, 5]
        },
        'g': 'bla',
        5: 'yy',
        'list3': ['a', 2]
    }
    expected_result = {
        'a': 2,
        'b': 2,
        'c': {
            'd': 'foo2',
            'e': 'bar',
            'f': 5,
            'list1': [3, 4, 5]
        },
        'g': 'bla',
        5: 'yy',
        'list2': [1, 2, 3],
        'list3': ['a', 2]
    }

    dict_merge(first_dict, second_dict)

    assert not cmp(first_dict, expected_result)
Beispiel #4
0
    def get_config_file_args(self, cli_args):
        """
        Gets the args's from the configuration file
        """

        file_result = {}
        for (parser_name, parser_dict, arg_name, arg_value,
             option_spec) in self._iterate_received_arguments(cli_args):
            file_result[parser_name] = file_result.get(parser_name, {})
            if option_spec and option_spec.get(
                    'action', '') == 'read-config':
                # we have config option. saving it.
                self._convert_non_cli_args(
                    parser_name, parser_dict[arg_name])
                utils.dict_merge(
                    file_result[parser_name],
                    parser_dict[arg_name])
                # remove from cli args
                parser_dict.pop(arg_name)

        return file_result
Beispiel #5
0
def test_placeholder_validator(our_cwd_setup):

    injector = 'placeholder_injector.yml'
    overwriter = 'placeholder_overwriter.yml'

    # Checks that 'IRPlaceholderException' is raised if value isn't been
    # overwritten
    settings = yamls.load(os.path.join(test_utils.TESTS_CWD, injector))
    assert isinstance(settings['place']['holder']['validator'],
                      yamls.Placeholder)
    with pytest.raises(exceptions.IRPlaceholderException) as exc:
        yaml.safe_dump(settings, default_flow_style=False)
    assert "Mandatory value is missing." in str(exc.value.message)

    # Checks that exceptions haven't been raised after overwriting the
    # placeholder
    overwriter_dict = yamls.load(os.path.join(test_utils.TESTS_CWD,
                                              overwriter))
    core_utils.dict_merge(settings, overwriter_dict)
    assert settings['place']['holder']['validator'] == \
        "'!placeholder' has been overwritten"
    yaml.safe_dump(settings, default_flow_style=False)
Beispiel #6
0
    def validate_requires_args(self, args):
        """
        Check if all the required arguments have been provided.
        """

        silent_args = self.get_silent_args(args)

        def validate_parser(parser_name, expected_options, parser_args):
            result = collections.defaultdict(list)
            condition_req_args = self._get_conditionally_required_args(
                parser_name, expected_options, args)

            for option in expected_options:
                name = option['name']

                # check required options.
                if (option.get('required', False) and
                        name not in parser_args or
                        option['name'] in condition_req_args) and \
                        name not in silent_args:
                    result[parser_name].append(name)

            return result

        res = {}
        for command_data in self.spec_helper.iterate_parsers():
            cmd_name = command_data['name']
            if cmd_name in args:
                utils.dict_merge(
                    res,
                    validate_parser(
                        cmd_name,
                        self.spec_helper.get_parser_option_specs(cmd_name),
                        args[cmd_name]))

        missing_args = {cmd_name: args
                        for cmd_name, args in res.items() if len(args) > 0}
        if missing_args:
            raise exceptions.IRRequiredArgsMissingException(missing_args)
Beispiel #7
0
    def parse_args(self, arg_parser):
        """
        Parses all the arguments (cli, file, env) and returns two dicts:
            * command arguments dict (arguments to control the IR logic)
            * nested arguments dict (arguments to pass to the playbooks)
        """

        spec_defaults = self.get_spec_defaults()
        env_defaults = self.get_env_defaults()
        cli_args, unknown_args = CliParser.parse_args(
            self, arg_parser=arg_parser)

        file_args = self.get_config_file_args(cli_args)

        # generate config file and exit
        if self.generate_config_file(cli_args, spec_defaults):
            LOG.warning("Config file has been generated. Exiting.")
            return None

        # print warnings when something was overridden from non-cli source.
        self.validate_arg_sources(
            cli_args,
            env_defaults,
            file_args,
            spec_defaults)

        # todo(obaranov) Pass all the unknown arguments to the ansible
        # For now just raise exception
        if unknown_args:
            raise exceptions.IRUnrecognizedOptionsException(unknown_args)

        # merge defaults into one
        utils.dict_merge(spec_defaults, env_defaults)
        # now filter defaults to have only parser defined in cli
        defaults = {key: spec_defaults[key] for key in cli_args.keys() if
                    key in spec_defaults}

        # copy cli args with the same name to all parser groups
        self._merge_duplicated_cli_args(cli_args)
        self._merge_duplicated_cli_args(file_args)

        utils.dict_merge(defaults, file_args)
        utils.dict_merge(defaults, cli_args)
        self.validate_requires_args(defaults)

        # now resolve complex types.
        self.resolve_custom_types(defaults)
        nested, control = self.get_nested_and_control_args(defaults)
        return nested, control, unknown_args
Beispiel #8
0
def test_dict_merge_none_resolver(first, second, expected):
    from infrared.core.utils.utils import dict_merge, ConflictResolver

    dict_merge(first, second, conflict_resolver=ConflictResolver.none_resolver)
    assert not cmp(first, expected)