mirror of
https://github.com/ansible-collections/community.general.git
synced 2026-04-14 07:55:05 +00:00
Reformat everything.
This commit is contained in:
parent
3f2213791a
commit
340ff8586d
1008 changed files with 61301 additions and 58309 deletions
|
|
@ -128,8 +128,13 @@ end_state:
|
|||
type: dict
|
||||
"""
|
||||
|
||||
from ansible_collections.community.general.plugins.module_utils.identity.keycloak.keycloak import KeycloakAPI, camel, \
|
||||
keycloak_argument_spec, get_token, KeycloakError
|
||||
from ansible_collections.community.general.plugins.module_utils.identity.keycloak.keycloak import (
|
||||
KeycloakAPI,
|
||||
camel,
|
||||
keycloak_argument_spec,
|
||||
get_token,
|
||||
KeycloakError,
|
||||
)
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from urllib.parse import urlencode
|
||||
from copy import deepcopy
|
||||
|
|
@ -139,31 +144,34 @@ def main():
|
|||
argument_spec = keycloak_argument_spec()
|
||||
|
||||
meta_args = dict(
|
||||
state=dict(type='str', default='present', choices=['present', 'absent']),
|
||||
name=dict(type='str', required=True),
|
||||
parent_id=dict(type='str', required=True),
|
||||
provider_id=dict(type='str', required=True),
|
||||
provider_type=dict(type='str', required=True),
|
||||
state=dict(type="str", default="present", choices=["present", "absent"]),
|
||||
name=dict(type="str", required=True),
|
||||
parent_id=dict(type="str", required=True),
|
||||
provider_id=dict(type="str", required=True),
|
||||
provider_type=dict(type="str", required=True),
|
||||
config=dict(
|
||||
type='dict',
|
||||
)
|
||||
type="dict",
|
||||
),
|
||||
)
|
||||
|
||||
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='', end_state={}, diff=dict(before={}, after={}))
|
||||
result = dict(changed=False, msg="", end_state={}, diff=dict(before={}, after={}))
|
||||
|
||||
# This will include the current state of the component if it is already
|
||||
# present. This is only used for diff-mode.
|
||||
before_component = {}
|
||||
before_component['config'] = {}
|
||||
before_component["config"] = {}
|
||||
|
||||
# Obtain access token, initialize API
|
||||
try:
|
||||
|
|
@ -176,15 +184,13 @@ def main():
|
|||
params_to_ignore = list(keycloak_argument_spec().keys()) + ["state", "parent_id"]
|
||||
|
||||
# Filter and map the parameters names that apply to the role
|
||||
component_params = [x for x in module.params
|
||||
if x not in params_to_ignore and
|
||||
module.params.get(x) is not None]
|
||||
component_params = [x for x in module.params if x not in params_to_ignore and module.params.get(x) is not None]
|
||||
|
||||
provider_type = module.params.get("provider_type")
|
||||
|
||||
# Build a proposed changeset from parameters given to this module
|
||||
changeset = {}
|
||||
changeset['config'] = {}
|
||||
changeset["config"] = {}
|
||||
|
||||
# Generate a JSON payload for Keycloak Admin API from the module
|
||||
# parameters. Parameters that do not belong to the JSON payload (e.g.
|
||||
|
|
@ -200,16 +206,16 @@ def main():
|
|||
# by Keycloak.
|
||||
#
|
||||
for component_param in component_params:
|
||||
if component_param == 'config':
|
||||
for config_param in module.params.get('config'):
|
||||
changeset['config'][camel(config_param)] = []
|
||||
raw_value = module.params.get('config')[config_param]
|
||||
if component_param == "config":
|
||||
for config_param in module.params.get("config"):
|
||||
changeset["config"][camel(config_param)] = []
|
||||
raw_value = module.params.get("config")[config_param]
|
||||
if isinstance(raw_value, bool):
|
||||
value = str(raw_value).lower()
|
||||
else:
|
||||
value = str(raw_value)
|
||||
|
||||
changeset['config'][camel(config_param)].append(value)
|
||||
changeset["config"][camel(config_param)].append(value)
|
||||
else:
|
||||
# No need for camelcase in here as these are one word parameters
|
||||
new_param_value = module.params.get(component_param)
|
||||
|
|
@ -220,13 +226,13 @@ def main():
|
|||
changeset_copy = deepcopy(changeset)
|
||||
|
||||
# Make it easier to refer to current module parameters
|
||||
name = module.params.get('name')
|
||||
force = module.params.get('force')
|
||||
state = module.params.get('state')
|
||||
enabled = module.params.get('enabled')
|
||||
provider_id = module.params.get('provider_id')
|
||||
provider_type = module.params.get('provider_type')
|
||||
parent_id = module.params.get('parent_id')
|
||||
name = module.params.get("name")
|
||||
force = module.params.get("force")
|
||||
state = module.params.get("state")
|
||||
enabled = module.params.get("enabled")
|
||||
provider_id = module.params.get("provider_id")
|
||||
provider_type = module.params.get("provider_type")
|
||||
parent_id = module.params.get("parent_id")
|
||||
|
||||
# Get a list of all Keycloak components that are of keyprovider type.
|
||||
current_components = kc.get_components(urlencode(dict(type=provider_type)), parent_id)
|
||||
|
|
@ -239,84 +245,84 @@ def main():
|
|||
changes = ""
|
||||
|
||||
# This tells Ansible whether the key was changed (added, removed, modified)
|
||||
result['changed'] = False
|
||||
result["changed"] = False
|
||||
|
||||
# Loop through the list of components. If we encounter a component whose
|
||||
# name matches the value of the name parameter then assume the key is
|
||||
# already present.
|
||||
for component in current_components:
|
||||
if component['name'] == name:
|
||||
component_id = component['id']
|
||||
changeset['id'] = component_id
|
||||
changeset_copy['id'] = component_id
|
||||
if component["name"] == name:
|
||||
component_id = component["id"]
|
||||
changeset["id"] = component_id
|
||||
changeset_copy["id"] = component_id
|
||||
|
||||
# Compare top-level parameters
|
||||
for param, value in changeset.items():
|
||||
before_component[param] = component[param]
|
||||
|
||||
if changeset_copy[param] != component[param] and param != 'config':
|
||||
if changeset_copy[param] != component[param] and param != "config":
|
||||
changes += f"{param}: {component[param]} -> {changeset_copy[param]}, "
|
||||
result['changed'] = True
|
||||
result["changed"] = True
|
||||
# Compare parameters under the "config" key
|
||||
for p, v in changeset_copy['config'].items():
|
||||
for p, v in changeset_copy["config"].items():
|
||||
try:
|
||||
before_component['config'][p] = component['config'][p] or []
|
||||
before_component["config"][p] = component["config"][p] or []
|
||||
except KeyError:
|
||||
before_component['config'][p] = []
|
||||
if changeset_copy['config'][p] != component['config'][p]:
|
||||
before_component["config"][p] = []
|
||||
if changeset_copy["config"][p] != component["config"][p]:
|
||||
changes += f"config.{p}: {component['config'][p]} -> {changeset_copy['config'][p]}, "
|
||||
result['changed'] = True
|
||||
result["changed"] = True
|
||||
|
||||
# Check all the possible states of the resource and do what is needed to
|
||||
# converge current state with desired state (create, update or delete
|
||||
# the key).
|
||||
if component_id and state == 'present':
|
||||
if result['changed']:
|
||||
if component_id and state == "present":
|
||||
if result["changed"]:
|
||||
if module._diff:
|
||||
result['diff'] = dict(before=before_component, after=changeset_copy)
|
||||
result["diff"] = dict(before=before_component, after=changeset_copy)
|
||||
|
||||
if module.check_mode:
|
||||
result['msg'] = f"Component {name} would be changed: {changes.strip(', ')}"
|
||||
result["msg"] = f"Component {name} would be changed: {changes.strip(', ')}"
|
||||
else:
|
||||
kc.update_component(changeset, parent_id)
|
||||
result['msg'] = f"Component {name} changed: {changes.strip(', ')}"
|
||||
result["msg"] = f"Component {name} changed: {changes.strip(', ')}"
|
||||
else:
|
||||
result['msg'] = f"Component {name} was in sync"
|
||||
result["msg"] = f"Component {name} was in sync"
|
||||
|
||||
result['end_state'] = changeset_copy
|
||||
elif component_id and state == 'absent':
|
||||
result["end_state"] = changeset_copy
|
||||
elif component_id and state == "absent":
|
||||
if module._diff:
|
||||
result['diff'] = dict(before=before_component, after={})
|
||||
result["diff"] = dict(before=before_component, after={})
|
||||
|
||||
if module.check_mode:
|
||||
result['changed'] = True
|
||||
result['msg'] = f"Component {name} would be deleted"
|
||||
result["changed"] = True
|
||||
result["msg"] = f"Component {name} would be deleted"
|
||||
else:
|
||||
kc.delete_component(component_id, parent_id)
|
||||
result['changed'] = True
|
||||
result['msg'] = f"Component {name} deleted"
|
||||
result["changed"] = True
|
||||
result["msg"] = f"Component {name} deleted"
|
||||
|
||||
result['end_state'] = {}
|
||||
elif not component_id and state == 'present':
|
||||
result["end_state"] = {}
|
||||
elif not component_id and state == "present":
|
||||
if module._diff:
|
||||
result['diff'] = dict(before={}, after=changeset_copy)
|
||||
result["diff"] = dict(before={}, after=changeset_copy)
|
||||
|
||||
if module.check_mode:
|
||||
result['changed'] = True
|
||||
result['msg'] = f"Component {name} would be created"
|
||||
result["changed"] = True
|
||||
result["msg"] = f"Component {name} would be created"
|
||||
else:
|
||||
kc.create_component(changeset, parent_id)
|
||||
result['changed'] = True
|
||||
result['msg'] = f"Component {name} created"
|
||||
result["changed"] = True
|
||||
result["msg"] = f"Component {name} created"
|
||||
|
||||
result['end_state'] = changeset_copy
|
||||
elif not component_id and state == 'absent':
|
||||
result['changed'] = False
|
||||
result['msg'] = f"Component {name} not present"
|
||||
result['end_state'] = {}
|
||||
result["end_state"] = changeset_copy
|
||||
elif not component_id and state == "absent":
|
||||
result["changed"] = False
|
||||
result["msg"] = f"Component {name} not present"
|
||||
result["end_state"] = {}
|
||||
|
||||
module.exit_json(**result)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue