Skip to content
ansible_freeipa_module.py 38 KiB
Newer Older
            raise NotImplementedError
        def get_command_errors(self, command, result):
            """Look for erros into command results."""
            # Get all errors
            # All "already a member" and "not a member" failures in the
            # result are ignored. All others are reported.
            errors = []
            for item in result.get("failed", tuple()):
                failed_item = result["failed"][item]
                for member_type in failed_item:
                    for member, failure in failed_item[member_type]:
                        if (
                            "already a member" in failure
                            or "not a member" in failure
                        ):
                            continue
                        errors.append(
                            "%s: %s %s: %s"
                            % (command, member_type, member, failure)
                        )

            if len(errors) > 0:
                self.fail_json(", ".join("errors"))  # pylint: disable=E1121

        def add_ipa_command(self, command, name=None, args=None):
            """Add a command to the list of commands to be executed."""
            self.ipa_commands.append((name, command, args or {}))

        def _run_ipa_commands(self):
            """Execute commands in self.ipa_commands."""
            if self.check_mode:
                self.changed = len(self.ipa_commands) > 0
                return

            result = None

            for name, command, args in self.ipa_commands:
                try:
                    result = self.ipa_command(command, name, args)
                except Exception as excpt:
                    self.fail_json(msg="%s: %s: %s" % (command, name,
                                                       str(excpt)))
                else:
                    self.process_command_result(name, command, args, result)
                self.get_command_errors(command, result)

        def process_command_result(self, _name, _command, _args, result):
            """
            Process an API command result.

            This method can be overriden in subclasses, and
            change self.exit_values
            to return data in the result for the controller.
            """
            if "completed" in result:
                if result["completed"] > 0:
                    self.changed = True
            else:
        def require_ipa_attrs_change(self, command_args, ipa_attrs):
            """
            Compare given args with current object attributes.
            Returns True in case current IPA object attributes differ from
            args passed to the module.
            """
            equal = compare_args_ipa(self, command_args, ipa_attrs)
            return not equal

        def ipa_run(self):
            """Execute module actions."""
                self.check_ipa_params()
                self.define_ipa_commands()
                self._run_ipa_commands()
            self.exit_json(changed=self.changed, **self.exit_args)