From f4311e08aabffb51b24bb1b611d2ea4b557211cd Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Mon, 21 Dec 2020 14:20:35 +0100 Subject: [PATCH] Some adjustments/improvements (#1516) (#1520) * Some adjustments/improvements - Added doc details for parameters ``description`` and ``objectClass`` - Added type details to argument_spec of parameters ``description`` and ``objectClass``. - Removed unused import - Simplified logic of ``LdapEntry._load_attrs()`` - Replaced parameter validation test with ``required_if``. * Added changelog frag (cherry picked from commit 5ee5c004b4ab122c930b322d261587f382322400) Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> --- .../1516-ldap_entry-improvements.yaml | 2 ++ plugins/modules/net_tools/ldap/ldap_entry.py | 23 +++++-------------- 2 files changed, 8 insertions(+), 17 deletions(-) create mode 100644 changelogs/fragments/1516-ldap_entry-improvements.yaml diff --git a/changelogs/fragments/1516-ldap_entry-improvements.yaml b/changelogs/fragments/1516-ldap_entry-improvements.yaml new file mode 100644 index 0000000000..3ea7e6c3ef --- /dev/null +++ b/changelogs/fragments/1516-ldap_entry-improvements.yaml @@ -0,0 +1,2 @@ +bugfixes: + - ldap_entry - improvements in documentation, simplifications and replaced code with better ``AnsibleModule`` arguments (https://github.com/ansible-collections/community.general/pull/1516). diff --git a/plugins/modules/net_tools/ldap/ldap_entry.py b/plugins/modules/net_tools/ldap/ldap_entry.py index 7303d7a3af..7ee0c3ddec 100644 --- a/plugins/modules/net_tools/ldap/ldap_entry.py +++ b/plugins/modules/net_tools/ldap/ldap_entry.py @@ -38,11 +38,14 @@ options: - If I(state=present), attributes necessary to create an entry. Existing entries are never modified. To assert specific attribute values on an existing entry, use M(community.general.ldap_attr) module instead. + type: dict objectClass: description: - If I(state=present), value or list of values to use when creating the entry. It can either be a string or an actual list of strings. + type: list + elements: str state: description: - The target state of the entry. @@ -103,7 +106,6 @@ RETURN = """ import traceback from ansible.module_utils.basic import AnsibleModule, missing_required_lib -from ansible.module_utils.six import string_types from ansible.module_utils._text import to_native, to_bytes from ansible_collections.community.general.plugins.module_utils.ldap import LdapGeneric, gen_specs @@ -137,13 +139,10 @@ class LdapEntry(LdapGeneric): attrs = {} for name, value in self.module.params['attributes'].items(): - if name not in attrs: - attrs[name] = [] - if isinstance(value, list): attrs[name] = list(map(to_bytes, value)) else: - attrs[name].append(to_bytes(value)) + attrs[name] = [to_bytes(value)] return attrs @@ -187,10 +186,11 @@ def main(): module = AnsibleModule( argument_spec=gen_specs( attributes=dict(default={}, type='dict'), - objectClass=dict(type='raw'), + objectClass=dict(type='list', elements='str'), params=dict(type='dict'), state=dict(default='present', choices=['present', 'absent']), ), + required_if=[('state', 'present', ['objectClass'])], supports_check_mode=True, ) @@ -203,17 +203,6 @@ def main(): state = module.params['state'] - # Check if objectClass is present when needed - if state == 'present' and module.params['objectClass'] is None: - module.fail_json(msg="At least one objectClass must be provided.") - - # Check if objectClass is of the correct type - if ( - module.params['objectClass'] is not None and not ( - isinstance(module.params['objectClass'], string_types) or - isinstance(module.params['objectClass'], list))): - module.fail_json(msg="objectClass must be either a string or a list.") - # Instantiate the LdapEntry object ldap = LdapEntry(module)