1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2026-04-20 10:48:59 +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

@ -226,8 +226,13 @@ end_state:
}
"""
from ansible_collections.community.general.plugins.module_utils.identity.keycloak.keycloak \
import KeycloakAPI, keycloak_argument_spec, get_token, KeycloakError, is_struct_included
from ansible_collections.community.general.plugins.module_utils.identity.keycloak.keycloak import (
KeycloakAPI,
keycloak_argument_spec,
get_token,
KeycloakError,
is_struct_included,
)
from ansible.module_utils.basic import AnsibleModule
@ -239,15 +244,19 @@ def find_exec_in_executions(searched_exec, executions):
:return: Index of the execution, -1 if not found..
"""
for i, existing_exec in enumerate(executions, start=0):
if ("providerId" in existing_exec and "providerId" in searched_exec and
existing_exec["providerId"] == searched_exec["providerId"] or
"displayName" in existing_exec and "displayName" in searched_exec and
existing_exec["displayName"] == searched_exec["displayName"]):
if (
"providerId" in existing_exec
and "providerId" in searched_exec
and existing_exec["providerId"] == searched_exec["providerId"]
or "displayName" in existing_exec
and "displayName" in searched_exec
and existing_exec["displayName"] == searched_exec["displayName"]
):
return i
return -1
def create_or_update_executions(kc, config, realm='master'):
def create_or_update_executions(kc, config, realm="master"):
"""
Create or update executions for an authentication flow.
:param kc: Keycloak API access.
@ -284,9 +293,12 @@ def create_or_update_executions(kc, config, realm='master'):
if new_exec[key] is None:
exclude_key.append(key)
# Compare the executions to see if it need changes
if not is_struct_included(new_exec, existing_executions[exec_index], exclude_key) or exec_index != new_exec_index:
if (
not is_struct_included(new_exec, existing_executions[exec_index], exclude_key)
or exec_index != new_exec_index
):
exec_found = True
if new_exec['index'] is None:
if new_exec["index"] is None:
new_exec_index = exec_index
before += f"{existing_executions[exec_index]}\n"
execution = existing_executions[exec_index].copy()
@ -299,7 +311,9 @@ def create_or_update_executions(kc, config, realm='master'):
exec_index = new_exec_index
after += f"{new_exec}\n"
elif new_exec["displayName"] is not None:
kc.create_subflow(new_exec["displayName"], flow_alias_parent, realm=realm, flowType=new_exec["subFlowType"])
kc.create_subflow(
new_exec["displayName"], flow_alias_parent, realm=realm, flowType=new_exec["subFlowType"]
)
execution = kc.get_executions_representation(config, realm=realm)[exec_index]
exec_found = True
exec_index = new_exec_index
@ -308,14 +322,14 @@ def create_or_update_executions(kc, config, realm='master'):
changed = True
if exec_index != -1:
# Update the existing execution
updated_exec = {
"id": execution["id"]
}
updated_exec = {"id": execution["id"]}
# add the execution configuration
if new_exec["authenticationConfig"] is not None:
if "authenticationConfig" in execution and "id" in execution["authenticationConfig"]:
kc.delete_authentication_config(execution["authenticationConfig"]["id"], realm=realm)
kc.add_authenticationConfig_to_execution(updated_exec["id"], new_exec["authenticationConfig"], realm=realm)
kc.add_authenticationConfig_to_execution(
updated_exec["id"], new_exec["authenticationConfig"], realm=realm
)
for key in new_exec:
# remove unwanted key for the next API call
if key not in ("flowAlias", "authenticationConfig", "subFlowType"):
@ -329,7 +343,9 @@ def create_or_update_executions(kc, config, realm='master'):
after += f"{kc.get_executions_representation(config, realm=realm)[new_exec_index]}\n"
return changed, dict(before=before, after=after)
except Exception as e:
kc.module.fail_json(msg=f"Could not create or update executions for authentication flow {config['alias']} in realm {realm}: {e}")
kc.module.fail_json(
msg=f"Could not create or update executions for authentication flow {config['alias']} in realm {realm}: {e}"
)
def main():
@ -341,35 +357,41 @@ def main():
argument_spec = keycloak_argument_spec()
meta_args = dict(
realm=dict(type='str', required=True),
alias=dict(type='str', required=True),
providerId=dict(type='str', choices=["basic-flow", "client-flow"]),
description=dict(type='str'),
copyFrom=dict(type='str'),
authenticationExecutions=dict(type='list', elements='dict',
options=dict(
providerId=dict(type='str'),
displayName=dict(type='str'),
requirement=dict(choices=["REQUIRED", "ALTERNATIVE", "DISABLED", "CONDITIONAL"], type='str'),
flowAlias=dict(type='str'),
authenticationConfig=dict(type='dict'),
index=dict(type='int'),
subFlowType=dict(choices=["basic-flow", "form-flow"], default='basic-flow', type='str'),
)),
state=dict(choices=["absent", "present"], default='present'),
force=dict(type='bool', default=False),
realm=dict(type="str", required=True),
alias=dict(type="str", required=True),
providerId=dict(type="str", choices=["basic-flow", "client-flow"]),
description=dict(type="str"),
copyFrom=dict(type="str"),
authenticationExecutions=dict(
type="list",
elements="dict",
options=dict(
providerId=dict(type="str"),
displayName=dict(type="str"),
requirement=dict(choices=["REQUIRED", "ALTERNATIVE", "DISABLED", "CONDITIONAL"], type="str"),
flowAlias=dict(type="str"),
authenticationConfig=dict(type="dict"),
index=dict(type="int"),
subFlowType=dict(choices=["basic-flow", "form-flow"], default="basic-flow", type="str"),
),
),
state=dict(choices=["absent", "present"], default="present"),
force=dict(type="bool", default=False),
)
argument_spec.update(meta_args)
module = AnsibleModule(argument_spec=argument_spec,
supports_check_mode=True,
required_one_of=([['token', 'auth_realm', 'auth_username', 'auth_password', 'auth_client_id', 'auth_client_secret']]),
required_together=([['auth_username', 'auth_password']]),
required_by={'refresh_token': 'auth_realm'},
)
module = AnsibleModule(
argument_spec=argument_spec,
supports_check_mode=True,
required_one_of=(
[["token", "auth_realm", "auth_username", "auth_password", "auth_client_id", "auth_client_secret"]]
),
required_together=([["auth_username", "auth_password"]]),
required_by={"refresh_token": "auth_realm"},
)
result = dict(changed=False, msg='', flow={})
result = dict(changed=False, msg="", flow={})
# Obtain access token, initialize API
try:
@ -379,9 +401,9 @@ def main():
kc = KeycloakAPI(module, connection_header)
realm = module.params.get('realm')
state = module.params.get('state')
force = module.params.get('force')
realm = module.params.get("realm")
state = module.params.get("state")
force = module.params.get("force")
new_auth_repr = {
"alias": module.params.get("alias"),
@ -397,21 +419,21 @@ def main():
# Cater for when it doesn't exist (an empty dict)
if not auth_repr:
if state == 'absent':
if state == "absent":
# Do nothing and exit
if module._diff:
result['diff'] = dict(before='', after='')
result['changed'] = False
result['end_state'] = {}
result['msg'] = f"{new_auth_repr['alias']} absent"
result["diff"] = dict(before="", after="")
result["changed"] = False
result["end_state"] = {}
result["msg"] = f"{new_auth_repr['alias']} absent"
module.exit_json(**result)
elif state == 'present':
elif state == "present":
# Process a creation
result['changed'] = True
result["changed"] = True
if module._diff:
result['diff'] = dict(before='', after=new_auth_repr)
result["diff"] = dict(before="", after=new_auth_repr)
if module.check_mode:
module.exit_json(**result)
@ -424,7 +446,7 @@ def main():
# If the authentication still not exist on the server, raise an exception.
if auth_repr is None:
result['msg'] = f"Authentication just created not found: {new_auth_repr}"
result["msg"] = f"Authentication just created not found: {new_auth_repr}"
module.fail_json(**result)
# Configure the executions for the flow
@ -434,17 +456,17 @@ def main():
exec_repr = kc.get_executions_representation(config=new_auth_repr, realm=realm)
if exec_repr is not None:
auth_repr["authenticationExecutions"] = exec_repr
result['end_state'] = auth_repr
result["end_state"] = auth_repr
else:
if state == 'present':
if state == "present":
# Process an update
if force: # If force option is true
# Delete the actual authentication flow
result['changed'] = True
result["changed"] = True
if module._diff:
result['diff'] = dict(before=auth_repr, after=new_auth_repr)
result["diff"] = dict(before=auth_repr, after=new_auth_repr)
if module.check_mode:
module.exit_json(**result)
kc.delete_authentication_flow_by_id(id=auth_repr["id"], realm=realm)
@ -455,30 +477,30 @@ def main():
auth_repr = kc.create_empty_auth_flow(config=new_auth_repr, realm=realm)
# If the authentication still not exist on the server, raise an exception.
if auth_repr is None:
result['msg'] = f"Authentication just created not found: {new_auth_repr}"
result["msg"] = f"Authentication just created not found: {new_auth_repr}"
module.fail_json(**result)
# Configure the executions for the flow
if module.check_mode:
module.exit_json(**result)
changed, diff = create_or_update_executions(kc=kc, config=new_auth_repr, realm=realm)
result['changed'] |= changed
result["changed"] |= changed
if module._diff:
result['diff'] = diff
result["diff"] = diff
# Get executions created
exec_repr = kc.get_executions_representation(config=new_auth_repr, realm=realm)
if exec_repr is not None:
auth_repr["authenticationExecutions"] = exec_repr
result['end_state'] = auth_repr
result["end_state"] = auth_repr
else:
# Process a deletion (because state was not 'present')
result['changed'] = True
result["changed"] = True
if module._diff:
result['diff'] = dict(before=auth_repr, after='')
result["diff"] = dict(before=auth_repr, after="")
if module.check_mode:
module.exit_json(**result)
@ -486,10 +508,10 @@ def main():
# delete it
kc.delete_authentication_flow_by_id(id=auth_repr["id"], realm=realm)
result['msg'] = f"Authentication flow: {new_auth_repr['alias']} id: {auth_repr['id']} is deleted"
result["msg"] = f"Authentication flow: {new_auth_repr['alias']} id: {auth_repr['id']} is deleted"
module.exit_json(**result)
if __name__ == '__main__':
if __name__ == "__main__":
main()