mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Some adjustments/improvements (#1516)
* 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
This commit is contained in:
parent
e9dafb3467
commit
5ee5c004b4
2 changed files with 8 additions and 17 deletions
2
changelogs/fragments/1516-ldap_entry-improvements.yaml
Normal file
2
changelogs/fragments/1516-ldap_entry-improvements.yaml
Normal file
|
@ -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).
|
|
@ -38,11 +38,14 @@ options:
|
||||||
- If I(state=present), attributes necessary to create an entry. Existing
|
- If I(state=present), attributes necessary to create an entry. Existing
|
||||||
entries are never modified. To assert specific attribute values on an
|
entries are never modified. To assert specific attribute values on an
|
||||||
existing entry, use M(community.general.ldap_attr) module instead.
|
existing entry, use M(community.general.ldap_attr) module instead.
|
||||||
|
type: dict
|
||||||
objectClass:
|
objectClass:
|
||||||
description:
|
description:
|
||||||
- If I(state=present), value or list of values to use when creating
|
- 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
|
the entry. It can either be a string or an actual list of
|
||||||
strings.
|
strings.
|
||||||
|
type: list
|
||||||
|
elements: str
|
||||||
state:
|
state:
|
||||||
description:
|
description:
|
||||||
- The target state of the entry.
|
- The target state of the entry.
|
||||||
|
@ -103,7 +106,6 @@ RETURN = """
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
|
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.module_utils._text import to_native, to_bytes
|
||||||
from ansible_collections.community.general.plugins.module_utils.ldap import LdapGeneric, gen_specs
|
from ansible_collections.community.general.plugins.module_utils.ldap import LdapGeneric, gen_specs
|
||||||
|
|
||||||
|
@ -137,13 +139,10 @@ class LdapEntry(LdapGeneric):
|
||||||
attrs = {}
|
attrs = {}
|
||||||
|
|
||||||
for name, value in self.module.params['attributes'].items():
|
for name, value in self.module.params['attributes'].items():
|
||||||
if name not in attrs:
|
|
||||||
attrs[name] = []
|
|
||||||
|
|
||||||
if isinstance(value, list):
|
if isinstance(value, list):
|
||||||
attrs[name] = list(map(to_bytes, value))
|
attrs[name] = list(map(to_bytes, value))
|
||||||
else:
|
else:
|
||||||
attrs[name].append(to_bytes(value))
|
attrs[name] = [to_bytes(value)]
|
||||||
|
|
||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
|
@ -187,10 +186,11 @@ def main():
|
||||||
module = AnsibleModule(
|
module = AnsibleModule(
|
||||||
argument_spec=gen_specs(
|
argument_spec=gen_specs(
|
||||||
attributes=dict(default={}, type='dict'),
|
attributes=dict(default={}, type='dict'),
|
||||||
objectClass=dict(type='raw'),
|
objectClass=dict(type='list', elements='str'),
|
||||||
params=dict(type='dict'),
|
params=dict(type='dict'),
|
||||||
state=dict(default='present', choices=['present', 'absent']),
|
state=dict(default='present', choices=['present', 'absent']),
|
||||||
),
|
),
|
||||||
|
required_if=[('state', 'present', ['objectClass'])],
|
||||||
supports_check_mode=True,
|
supports_check_mode=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -203,17 +203,6 @@ def main():
|
||||||
|
|
||||||
state = module.params['state']
|
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
|
# Instantiate the LdapEntry object
|
||||||
ldap = LdapEntry(module)
|
ldap = LdapEntry(module)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue