1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-04-25 05:02:46 +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

@ -119,9 +119,13 @@ rfc4525:
from ansible.module_utils.basic import AnsibleModule, missing_required_lib
from ansible.module_utils.common.text.converters import to_native, to_bytes
from ansible_collections.community.general.plugins.module_utils import deps
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,
)
with deps.declare("ldap", reason=missing_required_lib('python-ldap')):
with deps.declare("ldap", reason=missing_required_lib("python-ldap")):
import ldap
import ldap.controls.readentry
@ -130,16 +134,15 @@ class LdapInc(LdapGeneric):
def __init__(self, module):
LdapGeneric.__init__(self, module)
# Shortcuts
self.attr = self.module.params['attribute']
self.increment = self.module.params['increment']
self.method = self.module.params['method']
self.attr = self.module.params["attribute"]
self.increment = self.module.params["increment"]
self.method = self.module.params["method"]
def inc_rfc4525(self):
return [(ldap.MOD_INCREMENT, self.attr, [to_bytes(str(self.increment))])]
def inc_legacy(self, curr_val, new_val):
return [(ldap.MOD_DELETE, self.attr, [to_bytes(curr_val)]),
(ldap.MOD_ADD, self.attr, [to_bytes(new_val)])]
return [(ldap.MOD_DELETE, self.attr, [to_bytes(curr_val)]), (ldap.MOD_ADD, self.attr, [to_bytes(new_val)])]
def serverControls(self):
return [ldap.controls.readentry.PostReadControl(attrList=[self.attr])]
@ -150,9 +153,9 @@ class LdapInc(LdapGeneric):
def main():
module = AnsibleModule(
argument_spec=gen_specs(
attribute=dict(type='str', required=True),
increment=dict(type='int', default=1),
method=dict(type='str', default='auto', choices=['auto', 'rfc4525', 'legacy']),
attribute=dict(type="str", required=True),
increment=dict(type="int", default=1),
method=dict(type="str", default="auto", choices=["auto", "rfc4525", "legacy"]),
),
supports_check_mode=True,
required_together=ldap_required_together(),
@ -174,23 +177,18 @@ def main():
if mod.method != "auto":
rfc4525 = mod.method == "rfc425"
else:
rootDSE = mod.connection.search_ext_s(
base="",
scope=ldap.SCOPE_BASE,
attrlist=["*", "+"])
rootDSE = mod.connection.search_ext_s(base="", scope=ldap.SCOPE_BASE, attrlist=["*", "+"])
if len(rootDSE) == 1:
if to_bytes(ldap.CONTROL_POST_READ) in rootDSE[0][1]["supportedControl"] and (
mod.LDAP_MOD_INCREMENT in rootDSE[0][1]["supportedFeatures"] or
mod.LDAP_MOD_INCREMENT in rootDSE[0][1]["supportedExtension"]
mod.LDAP_MOD_INCREMENT in rootDSE[0][1]["supportedFeatures"]
or mod.LDAP_MOD_INCREMENT in rootDSE[0][1]["supportedExtension"]
):
rfc4525 = True
if rfc4525:
dummy, dummy, dummy, resp_ctrls = mod.connection.modify_ext_s(
dn=mod.dn,
modlist=mod.inc_rfc4525(),
serverctrls=mod.serverControls(),
clientctrls=None)
dn=mod.dn, modlist=mod.inc_rfc4525(), serverctrls=mod.serverControls(), clientctrls=None
)
if len(resp_ctrls) == 1:
ret = resp_ctrls[0].entry[mod.attr][0]
@ -200,19 +198,15 @@ def main():
while tries < max_tries:
tries = tries + 1
result = mod.connection.search_ext_s(
base=mod.dn,
scope=ldap.SCOPE_BASE,
filterstr=f"({mod.attr}=*)",
attrlist=[mod.attr])
base=mod.dn, scope=ldap.SCOPE_BASE, filterstr=f"({mod.attr}=*)", attrlist=[mod.attr]
)
if len(result) != 1:
module.fail_json(msg="The entry does not exist or does not contain the specified attribute.")
return
try:
ret = str(int(result[0][1][mod.attr][0]) + mod.increment)
# if the current value first arg in inc_legacy has changed then the modify will fail
mod.connection.modify_s(
dn=mod.dn,
modlist=mod.inc_legacy(result[0][1][mod.attr][0], ret))
mod.connection.modify_s(dn=mod.dn, modlist=mod.inc_legacy(result[0][1][mod.attr][0], ret))
break
except ldap.NO_SUCH_ATTRIBUTE:
if tries == max_tries:
@ -221,10 +215,8 @@ def main():
else:
result = mod.connection.search_ext_s(
base=mod.dn,
scope=ldap.SCOPE_BASE,
filterstr=f"({mod.attr}=*)",
attrlist=[mod.attr])
base=mod.dn, scope=ldap.SCOPE_BASE, filterstr=f"({mod.attr}=*)", attrlist=[mod.attr]
)
if len(result) == 1:
ret = str(int(result[0][1][mod.attr][0]) + mod.increment)
changed = mod.increment != 0
@ -237,5 +229,5 @@ def main():
module.exit_json(changed=changed, incremented=changed, attribute=mod.attr, value=ret, rfc4525=rfc4525)
if __name__ == '__main__':
if __name__ == "__main__":
main()