1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Add --diff support for ldap_attrs module (#8073)

* Add --diff support for ldap_attrs module

* Change diff_mode support in docstring to full

* Use _attrs suffix for old and new

* Add version added to ldap_attrs diff mode

* Add fragment for ldap_attrs diff mode

* Update fragment to include link to PR and lowercase start

* Update changelogs/fragments/8073-ldap-attrs-diff.yml

Co-authored-by: Felix Fontein <felix@fontein.de>

---------

Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
StopMotionCuber 2024-03-14 23:23:05 +01:00 committed by GitHub
parent 67736d796a
commit 09cded05e7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 32 additions and 10 deletions

View file

@ -0,0 +1,2 @@
minor_changes:
- ldap_attrs - module now supports diff mode, showing which attributes are changed within an operation (https://github.com/ansible-collections/community.general/pull/8073).

View file

@ -44,7 +44,8 @@ attributes:
check_mode: check_mode:
support: full support: full
diff_mode: diff_mode:
support: none support: full
version_added: 8.5.0
options: options:
state: state:
required: false required: false
@ -235,26 +236,38 @@ class LdapAttrs(LdapGeneric):
def add(self): def add(self):
modlist = [] modlist = []
new_attrs = {}
for name, values in self.module.params['attributes'].items(): for name, values in self.module.params['attributes'].items():
norm_values = self._normalize_values(values) norm_values = self._normalize_values(values)
added_values = []
for value in norm_values: for value in norm_values:
if self._is_value_absent(name, value): if self._is_value_absent(name, value):
modlist.append((ldap.MOD_ADD, name, value)) modlist.append((ldap.MOD_ADD, name, value))
added_values.append(value)
return modlist if added_values:
new_attrs[name] = norm_values
return modlist, {}, new_attrs
def delete(self): def delete(self):
modlist = [] modlist = []
old_attrs = {}
new_attrs = {}
for name, values in self.module.params['attributes'].items(): for name, values in self.module.params['attributes'].items():
norm_values = self._normalize_values(values) norm_values = self._normalize_values(values)
removed_values = []
for value in norm_values: for value in norm_values:
if self._is_value_present(name, value): if self._is_value_present(name, value):
removed_values.append(value)
modlist.append((ldap.MOD_DELETE, name, value)) modlist.append((ldap.MOD_DELETE, name, value))
if removed_values:
return modlist old_attrs[name] = norm_values
new_attrs[name] = [value for value in norm_values if value not in removed_values]
return modlist, old_attrs, new_attrs
def exact(self): def exact(self):
modlist = [] modlist = []
old_attrs = {}
new_attrs = {}
for name, values in self.module.params['attributes'].items(): for name, values in self.module.params['attributes'].items():
norm_values = self._normalize_values(values) norm_values = self._normalize_values(values)
try: try:
@ -272,8 +285,13 @@ class LdapAttrs(LdapGeneric):
modlist.append((ldap.MOD_DELETE, name, None)) modlist.append((ldap.MOD_DELETE, name, None))
else: else:
modlist.append((ldap.MOD_REPLACE, name, norm_values)) modlist.append((ldap.MOD_REPLACE, name, norm_values))
old_attrs[name] = current
new_attrs[name] = norm_values
if len(current) == 1 and len(norm_values) == 1:
old_attrs[name] = current[0]
new_attrs[name] = norm_values[0]
return modlist return modlist, old_attrs, new_attrs
def _is_value_present(self, name, value): def _is_value_present(self, name, value):
""" True if the target attribute has the given value. """ """ True if the target attribute has the given value. """
@ -309,16 +327,18 @@ def main():
# Instantiate the LdapAttr object # Instantiate the LdapAttr object
ldap = LdapAttrs(module) ldap = LdapAttrs(module)
old_attrs = None
new_attrs = None
state = module.params['state'] state = module.params['state']
# Perform action # Perform action
if state == 'present': if state == 'present':
modlist = ldap.add() modlist, old_attrs, new_attrs = ldap.add()
elif state == 'absent': elif state == 'absent':
modlist = ldap.delete() modlist, old_attrs, new_attrs = ldap.delete()
elif state == 'exact': elif state == 'exact':
modlist = ldap.exact() modlist, old_attrs, new_attrs = ldap.exact()
changed = False changed = False
@ -331,7 +351,7 @@ def main():
except Exception as e: except Exception as e:
module.fail_json(msg="Attribute action failed.", details=to_native(e)) module.fail_json(msg="Attribute action failed.", details=to_native(e))
module.exit_json(changed=changed, modlist=modlist) module.exit_json(changed=changed, modlist=modlist, diff={"before": old_attrs, "after": new_attrs})
if __name__ == '__main__': if __name__ == '__main__':