Skip to content

Commit 961eb46

Browse files
committed
ipasudorule: Use common function to convert hostnames
This patch moves the function used to ensure that hostnames used in ipasurule are FQDN and unique to module_utils, so it can be used by other modules than require the same behavior for a hostaname parameter. Signed-off-by: Rafael Guterres Jeffman <rjeffman@redhat.com>
1 parent ede4b28 commit 961eb46

2 files changed

Lines changed: 18 additions & 16 deletions

File tree

plugins/module_utils/ansible_freeipa_module.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,20 @@ def ensure_fqdn(name, domain):
589589
return name
590590

591591

592+
def convert_param_value_to_unique_fqdn(value):
593+
if isinstance(value, (list, tuple)):
594+
value = list(
595+
set(
596+
ensure_fqdn(hostname.lower(), api_get_domain())
597+
for hostname in value
598+
)
599+
)
600+
else:
601+
value = ensure_fqdn(value.lower(), api_get_domain())
602+
603+
return value
604+
605+
592606
def convert_to_sid(items):
593607
"""Convert all items to SID, if possible."""
594608
def get_sid(data):

plugins/modules/ipasudorule.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,9 @@
369369

370370
from ansible.module_utils.ansible_freeipa_module import \
371371
IPAAnsibleModule, compare_args_ipa, gen_add_del_lists, gen_add_list, \
372-
gen_intersection_list, api_get_domain, ensure_fqdn, netaddr, to_text, \
373-
ipalib_errors, convert_param_value_to_lowercase, EntryFactory
372+
gen_intersection_list, netaddr, to_text, \
373+
ipalib_errors, convert_param_value_to_lowercase, EntryFactory, \
374+
convert_param_value_to_unique_fqdn
374375

375376

376377
def find_sudorule(module, name):
@@ -502,19 +503,6 @@ def convert_list_of_hostmask(hostmasks):
502503
]
503504

504505

505-
def convert_list_of_hostnames(hostnames):
506-
"""Ensure all hostnames in hostnames are lowercase FQDN."""
507-
return list(
508-
set(
509-
ensure_fqdn(value.lower(), api_get_domain())
510-
for value in (
511-
hostnames if isinstance(hostnames, (list, tuple))
512-
else [hostnames]
513-
)
514-
)
515-
)
516-
517-
518506
def validate_entry(module, entry, state, action):
519507
"""Ensure entry object is valid."""
520508
if state == "present" and action == "sudorule":
@@ -596,7 +584,7 @@ def main():
596584
"hostcategory": {},
597585
"runasusercategory": {},
598586
"runasgroupcategory": {},
599-
"host": {"convert": [convert_list_of_hostnames]},
587+
"host": {"convert": [convert_param_value_to_unique_fqdn]},
600588
"hostgroup": {"convert": [convert_param_value_to_lowercase]},
601589
"hostmask": {"convert": [convert_list_of_hostmask]},
602590
"user": {"convert": [convert_param_value_to_lowercase]},

0 commit comments

Comments
 (0)