1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-05-06 01:55:51 +00:00

Reformat everything.

This commit is contained in:
Felix Fontein 2025-11-01 12:08:41 +01:00
parent 3f2213791a
commit 340ff8586d
1008 changed files with 61301 additions and 58309 deletions

View file

@ -163,7 +163,11 @@ import traceback
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
from ansible.module_utils.common.text.converters import to_native, to_bytes, to_text
from ansible_collections.community.general.plugins.module_utils.ldap import LdapGeneric, gen_specs, ldap_required_together
from ansible_collections.community.general.plugins.module_utils.ldap import (
LdapGeneric,
gen_specs,
ldap_required_together,
)
import re
@ -183,30 +187,28 @@ class LdapAttrs(LdapGeneric):
LdapGeneric.__init__(self, module)
# Shortcuts
self.attrs = self.module.params['attributes']
self.state = self.module.params['state']
self.ordered = self.module.params['ordered']
self.attrs = self.module.params["attributes"]
self.state = self.module.params["state"]
self.ordered = self.module.params["ordered"]
def _order_values(self, values):
""" Prepend X-ORDERED index numbers to attribute's values. """
"""Prepend X-ORDERED index numbers to attribute's values."""
ordered_values = []
if isinstance(values, list):
for index, value in enumerate(values):
cleaned_value = re.sub(r'^\{\d+\}', '', value)
cleaned_value = re.sub(r"^\{\d+\}", "", value)
ordered_values.append(f"{{{index!s}}}{cleaned_value}")
return ordered_values
def _normalize_values(self, values):
""" Normalize attribute's values. """
"""Normalize attribute's values."""
norm_values = []
if isinstance(values, list):
if self.ordered:
norm_values = list(map(to_bytes,
self._order_values(list(map(str,
values)))))
norm_values = list(map(to_bytes, self._order_values(list(map(str, values)))))
else:
norm_values = list(map(to_bytes, values))
else:
@ -217,7 +219,7 @@ class LdapAttrs(LdapGeneric):
def add(self):
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)
added_values = []
for value in norm_values:
@ -232,7 +234,7 @@ class LdapAttrs(LdapGeneric):
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)
removed_values = []
for value in norm_values:
@ -248,11 +250,10 @@ class LdapAttrs(LdapGeneric):
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)
try:
results = self.connection.search_s(
self.dn, ldap.SCOPE_BASE, attrlist=[name])
results = self.connection.search_s(self.dn, ldap.SCOPE_BASE, attrlist=[name])
except ldap.LDAPError as e:
self.fail(f"Cannot search for attribute {name}", e)
@ -274,7 +275,7 @@ class LdapAttrs(LdapGeneric):
return modlist, old_attrs, new_attrs
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."""
try:
escaped_value = ldap.filter.escape_filter_chars(to_text(value))
filterstr = f"({name}={escaped_value})"
@ -286,38 +287,37 @@ class LdapAttrs(LdapGeneric):
return is_present
def _is_value_absent(self, name, value):
""" True if the target attribute doesn't have the given value. """
"""True if the target attribute doesn't have the given value."""
return not self._is_value_present(name, value)
def main():
module = AnsibleModule(
argument_spec=gen_specs(
attributes=dict(type='dict', required=True),
ordered=dict(type='bool', default=False),
state=dict(type='str', default='present', choices=['absent', 'exact', 'present']),
attributes=dict(type="dict", required=True),
ordered=dict(type="bool", default=False),
state=dict(type="str", default="present", choices=["absent", "exact", "present"]),
),
supports_check_mode=True,
required_together=ldap_required_together(),
)
if not HAS_LDAP:
module.fail_json(msg=missing_required_lib('python-ldap'),
exception=LDAP_IMP_ERR)
module.fail_json(msg=missing_required_lib("python-ldap"), exception=LDAP_IMP_ERR)
# Instantiate the LdapAttr object
ldap = LdapAttrs(module)
old_attrs = None
new_attrs = None
state = module.params['state']
state = module.params["state"]
# Perform action
if state == 'present':
if state == "present":
modlist, old_attrs, new_attrs = ldap.add()
elif state == 'absent':
elif state == "absent":
modlist, old_attrs, new_attrs = ldap.delete()
elif state == 'exact':
elif state == "exact":
modlist, old_attrs, new_attrs = ldap.exact()
changed = False
@ -334,5 +334,5 @@ def main():
module.exit_json(changed=changed, modlist=modlist, diff={"before": old_attrs, "after": new_attrs})
if __name__ == '__main__':
if __name__ == "__main__":
main()