From 937d1f6380256d8ca9af8e395326a36d4edd80bc Mon Sep 17 00:00:00 2001 From: Seena Fallah Date: Sat, 16 Mar 2024 16:22:08 +0100 Subject: [PATCH] radosgw_user: add support check mode Signed-off-by: Seena Fallah --- library/radosgw_user.py | 40 +++++++++++++++------------------------- 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/library/radosgw_user.py b/library/radosgw_user.py index fddbf893b9..2370a4ddd4 100644 --- a/library/radosgw_user.py +++ b/library/radosgw_user.py @@ -418,25 +418,14 @@ def run_module(): system = module.params.get('system') admin = module.params.get('admin') - if module.check_mode: - module.exit_json( - changed=False, - stdout='', - stderr='', - rc=0, - start='', - end='', - delta='', - ) - startd = datetime.datetime.now() changed = False # will return either the image name or None container_image = is_containerized() + rc, cmd, out, err = exec_commands(module, get_user(module, container_image=container_image)) # noqa: E501 if state == "present": - rc, cmd, out, err = exec_commands(module, get_user(module, container_image=container_image)) # noqa: E501 if rc == 0: user = json.loads(out) current = { @@ -452,32 +441,33 @@ def run_module(): if email: current['email'] = user['email'] asked['email'] = email - if access_key: - current['access_key'] = user['keys'][0]['access_key'] + + if access_key and secret_key: asked['access_key'] = access_key - if secret_key: - current['secret_key'] = user['keys'][0]['secret_key'] asked['secret_key'] = secret_key - - if current != asked: + for key in user['keys']: + if key['access_key'] == access_key and key['secret_key'] == secret_key: # noqa: E501 + del asked['access_key'] + del asked['secret_key'] + break + + changed = current != asked + if changed and not module.check_mode: rc, cmd, out, err = exec_commands(module, modify_user(module, container_image=container_image)) # noqa: E501 - changed = True else: - rc, cmd, out, err = exec_commands(module, create_user(module, container_image=container_image)) # noqa: E501 + if not module.check_mode: + rc, cmd, out, err = exec_commands(module, create_user(module, container_image=container_image)) # noqa: E501 changed = True elif state == "absent": - rc, cmd, out, err = exec_commands(module, get_user(module, container_image=container_image)) # noqa: E501 if rc == 0: - rc, cmd, out, err = exec_commands(module, remove_user(module, container_image=container_image)) # noqa: E501 + if not module.check_mode: + rc, cmd, out, err = exec_commands(module, remove_user(module, container_image=container_image)) # noqa: E501 changed = True else: rc = 0 out = "User {} doesn't exist".format(name) - elif state == "info": - rc, cmd, out, err = exec_commands(module, get_user(module, container_image=container_image)) # noqa: E501 - exit_module(module=module, out=out, rc=rc, cmd=cmd, err=err, startd=startd, changed=changed) # noqa: E501