Skip to content
Snippets Groups Projects
Commit 13d7d714 authored by Rafael Guterres Jeffman's avatar Rafael Guterres Jeffman
Browse files

iparole: Remove custom code in favor of commom functions.

Removed custom code used to create add/del lists in iparole in favor
of ansible_freeipa_module functions, and custom result_handler, to
reduce code duplication, as these methods have equivalent shared
versions.
parent 8a936270
Branches
Tags
No related merge requests found
...@@ -103,10 +103,10 @@ EXAMPLES = """ ...@@ -103,10 +103,10 @@ EXAMPLES = """
# pylint: disable=no-name-in-module # pylint: disable=no-name-in-module
from ansible.module_utils._text import to_text from ansible.module_utils._text import to_text
from ansible.module_utils.ansible_freeipa_module import \ from ansible.module_utils.ansible_freeipa_module import \
IPAAnsibleModule, gen_add_del_lists, compare_args_ipa IPAAnsibleModule, gen_add_del_lists, compare_args_ipa, \
gen_intersection_list, ensure_fqdn
from ansible.module_utils import six from ansible.module_utils import six
if six.PY3: if six.PY3:
unicode = str unicode = str
...@@ -170,30 +170,6 @@ def check_parameters(module): ...@@ -170,30 +170,6 @@ def check_parameters(module):
module.params_fail_used_invalid(invalid, state, action) module.params_fail_used_invalid(invalid, state, action)
def member_intersect(module, attr, memberof, res_find):
"""Filter member arguments from role found by intersection."""
params = module.params_get(attr)
if not res_find:
return params
filtered = []
if params:
existing = res_find.get(memberof, [])
filtered = list(set(params) & set(existing))
return filtered
def member_difference(module, attr, memberof, res_find):
"""Filter member arguments from role found by difference."""
params = module.params_get(attr)
if not res_find:
return params
filtered = []
if params:
existing = res_find.get(memberof, [])
filtered = list(set(params) - set(existing))
return filtered
def ensure_absent_state(module, name, action, res_find): def ensure_absent_state(module, name, action, res_find):
"""Define commands to ensure absent state.""" """Define commands to ensure absent state."""
commands = [] commands = []
...@@ -203,16 +179,20 @@ def ensure_absent_state(module, name, action, res_find): ...@@ -203,16 +179,20 @@ def ensure_absent_state(module, name, action, res_find):
if action == "member": if action == "member":
members = member_intersect( members = gen_intersection_list(
module, 'privilege', 'memberof_privilege', res_find) module.params_get("privilege"),
res_find.get("memberof_privilege")
)
if members: if members:
commands.append([name, "role_remove_privilege", commands.append([name, "role_remove_privilege",
{"privilege": members}]) {"privilege": members}])
member_args = {} member_args = {}
for key in ['user', 'group', 'host', 'hostgroup']: for key in ['user', 'group', 'host', 'hostgroup']:
items = member_intersect( items = gen_intersection_list(
module, key, 'member_%s' % key, res_find) module.params_get(key),
res_find.get("member_%s" % key)
)
if items: if items:
member_args[key] = items member_args[key] = items
...@@ -298,24 +278,6 @@ def ensure_role_with_members_is_present(module, name, res_find, action): ...@@ -298,24 +278,6 @@ def ensure_role_with_members_is_present(module, name, res_find, action):
return commands return commands
# pylint: disable=unused-argument
def result_handler(module, result, command, name, args, errors):
"""Process the result of a command, looking for errors."""
# Get all errors
# All "already a member" and "not a member" failures in the
# result are ignored. All others are reported.
if "failed" in result and len(result["failed"]) > 0:
for item in result["failed"]:
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))
def role_commands_for_name(module, state, action, name): def role_commands_for_name(module, state, action, name):
"""Define commands for the Role module.""" """Define commands for the Role module."""
commands = [] commands = []
...@@ -414,7 +376,8 @@ def main(): ...@@ -414,7 +376,8 @@ def main():
# Execute commands # Execute commands
changed = ansible_module.execute_ipa_commands(commands, result_handler) changed = ansible_module.execute_ipa_commands(
commands, fail_on_member_errors=True)
# Done # Done
ansible_module.exit_json(changed=changed, **exit_args) ansible_module.exit_json(changed=changed, **exit_args)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment