2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Copyright (c) 2017, Eike Frost <ei@kefro.st>
|
2022-08-05 12:28:29 +02:00
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: keycloak_clienttemplate
|
|
|
|
|
|
|
|
short_description: Allows administration of Keycloak client templates via Keycloak API
|
|
|
|
|
|
|
|
description:
|
|
|
|
- This module allows the administration of Keycloak client templates via the Keycloak REST API. It
|
|
|
|
requires access to the REST API via OpenID Connect; the user connecting and the client being
|
|
|
|
used must have the requisite access rights. In a default Keycloak installation, admin-cli
|
|
|
|
and an admin user would work, as would a separate client definition with the scope tailored
|
|
|
|
to your needs and a user having the expected roles.
|
|
|
|
|
|
|
|
- The names of module options are snake_cased versions of the camelCase ones found in the
|
|
|
|
Keycloak API and its documentation at U(https://www.keycloak.org/docs-api/8.0/rest-api/index.html)
|
|
|
|
|
|
|
|
- The Keycloak API does not always enforce for only sensible settings to be used -- you can set
|
|
|
|
SAML-specific settings on an OpenID Connect client for instance and vice versa. Be careful.
|
|
|
|
If you do not specify a setting, usually a sensible default is chosen.
|
|
|
|
|
2023-02-24 09:25:31 +01:00
|
|
|
attributes:
|
|
|
|
check_mode:
|
|
|
|
support: full
|
|
|
|
diff_mode:
|
|
|
|
support: full
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
options:
|
|
|
|
state:
|
|
|
|
description:
|
2021-10-22 07:57:18 +02:00
|
|
|
- State of the client template.
|
2023-06-15 15:47:13 +02:00
|
|
|
- On V(present), the client template will be created (or updated if it exists already).
|
|
|
|
- On V(absent), the client template will be removed if it exists
|
2020-03-09 10:11:07 +01:00
|
|
|
choices: ['present', 'absent']
|
|
|
|
default: 'present'
|
2020-12-01 21:28:40 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
id:
|
|
|
|
description:
|
|
|
|
- Id of client template to be worked on. This is usually a UUID.
|
2020-12-01 21:28:40 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
realm:
|
|
|
|
description:
|
|
|
|
- Realm this client template is found in.
|
2020-12-01 21:28:40 +01:00
|
|
|
type: str
|
|
|
|
default: master
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
name:
|
|
|
|
description:
|
2021-10-22 07:57:18 +02:00
|
|
|
- Name of the client template.
|
2020-12-01 21:28:40 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
description:
|
|
|
|
description:
|
2021-10-22 07:57:18 +02:00
|
|
|
- Description of the client template in Keycloak.
|
2020-12-01 21:28:40 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
protocol:
|
|
|
|
description:
|
2023-06-15 15:47:13 +02:00
|
|
|
- Type of client template.
|
2024-04-20 09:24:00 +02:00
|
|
|
- The V(docker-v2) value was added in community.general 8.6.0.
|
|
|
|
choices: ['openid-connect', 'saml', 'docker-v2']
|
2020-12-01 21:28:40 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
full_scope_allowed:
|
|
|
|
description:
|
|
|
|
- Is the "Full Scope Allowed" feature set for this client template or not.
|
|
|
|
This is 'fullScopeAllowed' in the Keycloak REST API.
|
|
|
|
type: bool
|
|
|
|
|
|
|
|
protocol_mappers:
|
|
|
|
description:
|
|
|
|
- a list of dicts defining protocol mappers for this client template.
|
|
|
|
This is 'protocolMappers' in the Keycloak REST API.
|
2020-11-27 08:01:02 +01:00
|
|
|
type: list
|
|
|
|
elements: dict
|
2020-03-09 10:11:07 +01:00
|
|
|
suboptions:
|
|
|
|
consentRequired:
|
|
|
|
description:
|
|
|
|
- Specifies whether a user needs to provide consent to a client for this mapper to be active.
|
2020-12-01 21:28:40 +01:00
|
|
|
type: bool
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
consentText:
|
|
|
|
description:
|
|
|
|
- The human-readable name of the consent the user is presented to accept.
|
2020-12-01 21:28:40 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
id:
|
|
|
|
description:
|
|
|
|
- Usually a UUID specifying the internal ID of this protocol mapper instance.
|
2020-12-01 21:28:40 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- The name of this protocol mapper.
|
2020-12-01 21:28:40 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
protocol:
|
|
|
|
description:
|
2023-06-15 15:47:13 +02:00
|
|
|
- This specifies for which protocol this protocol mapper is active.
|
2024-04-20 09:24:00 +02:00
|
|
|
choices: ['openid-connect', 'saml', 'docker-v2']
|
2020-12-01 21:28:40 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
protocolMapper:
|
|
|
|
description:
|
2023-06-15 15:47:13 +02:00
|
|
|
- "The Keycloak-internal name of the type of this protocol-mapper. While an exhaustive list is
|
2020-03-09 10:11:07 +01:00
|
|
|
impossible to provide since this may be extended through SPIs by the user of Keycloak,
|
2023-06-15 15:47:13 +02:00
|
|
|
by default Keycloak as of 3.4 ships with at least:"
|
|
|
|
- V(docker-v2-allow-all-mapper)
|
|
|
|
- V(oidc-address-mapper)
|
|
|
|
- V(oidc-full-name-mapper)
|
|
|
|
- V(oidc-group-membership-mapper)
|
|
|
|
- V(oidc-hardcoded-claim-mapper)
|
|
|
|
- V(oidc-hardcoded-role-mapper)
|
|
|
|
- V(oidc-role-name-mapper)
|
|
|
|
- V(oidc-script-based-protocol-mapper)
|
|
|
|
- V(oidc-sha256-pairwise-sub-mapper)
|
|
|
|
- V(oidc-usermodel-attribute-mapper)
|
|
|
|
- V(oidc-usermodel-client-role-mapper)
|
|
|
|
- V(oidc-usermodel-property-mapper)
|
|
|
|
- V(oidc-usermodel-realm-role-mapper)
|
|
|
|
- V(oidc-usersessionmodel-note-mapper)
|
|
|
|
- V(saml-group-membership-mapper)
|
|
|
|
- V(saml-hardcode-attribute-mapper)
|
|
|
|
- V(saml-hardcode-role-mapper)
|
|
|
|
- V(saml-role-list-mapper)
|
|
|
|
- V(saml-role-name-mapper)
|
|
|
|
- V(saml-user-attribute-mapper)
|
|
|
|
- V(saml-user-property-mapper)
|
|
|
|
- V(saml-user-session-note-mapper)
|
2020-03-09 10:11:07 +01:00
|
|
|
- An exhaustive list of available mappers on your installation can be obtained on
|
|
|
|
the admin console by going to Server Info -> Providers and looking under
|
|
|
|
'protocol-mapper'.
|
2020-12-01 21:28:40 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
config:
|
|
|
|
description:
|
|
|
|
- Dict specifying the configuration options for the protocol mapper; the
|
2023-06-15 15:47:13 +02:00
|
|
|
contents differ depending on the value of O(protocol_mappers[].protocolMapper) and are not documented
|
2020-03-09 10:11:07 +01:00
|
|
|
other than by the source of the mappers and its parent class(es). An example is given
|
|
|
|
below. It is easiest to obtain valid config values by dumping an already-existing
|
2023-06-15 15:47:13 +02:00
|
|
|
protocol mapper configuration through check-mode in the RV(existing) field.
|
2020-12-01 21:28:40 +01:00
|
|
|
type: dict
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
attributes:
|
|
|
|
description:
|
|
|
|
- A dict of further attributes for this client template. This can contain various
|
|
|
|
configuration settings, though in the default installation of Keycloak as of 3.4, none
|
|
|
|
are documented or known, so this is usually empty.
|
2020-12-01 21:28:40 +01:00
|
|
|
type: dict
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
notes:
|
2023-06-15 15:47:13 +02:00
|
|
|
- The Keycloak REST API defines further fields (namely C(bearerOnly), C(consentRequired), C(standardFlowEnabled),
|
|
|
|
C(implicitFlowEnabled), C(directAccessGrantsEnabled), C(serviceAccountsEnabled), C(publicClient), and
|
|
|
|
C(frontchannelLogout)) which, while available with keycloak_client, do not have any effect on
|
2023-02-24 09:25:31 +01:00
|
|
|
Keycloak client-templates and are discarded if supplied with an API request changing client-templates. As such,
|
|
|
|
they are not available through this module.
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
extends_documentation_fragment:
|
2023-02-24 09:25:31 +01:00
|
|
|
- community.general.keycloak
|
|
|
|
- community.general.attributes
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
author:
|
|
|
|
- Eike Frost (@eikef)
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2021-04-20 13:20:46 +02:00
|
|
|
- name: Create or update Keycloak client template (minimal), authentication with credentials
|
|
|
|
community.general.keycloak_client:
|
2020-03-09 10:11:07 +01:00
|
|
|
auth_client_id: admin-cli
|
|
|
|
auth_keycloak_url: https://auth.example.com/auth
|
|
|
|
auth_realm: master
|
|
|
|
auth_username: USERNAME
|
|
|
|
auth_password: PASSWORD
|
|
|
|
realm: master
|
|
|
|
name: this_is_a_test
|
2021-04-20 13:20:46 +02:00
|
|
|
delegate_to: localhost
|
|
|
|
|
|
|
|
- name: Create or update Keycloak client template (minimal), authentication with token
|
|
|
|
community.general.keycloak_clienttemplate:
|
|
|
|
auth_client_id: admin-cli
|
|
|
|
auth_keycloak_url: https://auth.example.com/auth
|
|
|
|
auth_realm: master
|
|
|
|
token: TOKEN
|
|
|
|
realm: master
|
|
|
|
name: this_is_a_test
|
|
|
|
delegate_to: localhost
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2020-05-16 15:07:51 +02:00
|
|
|
- name: Delete Keycloak client template
|
2021-04-20 13:20:46 +02:00
|
|
|
community.general.keycloak_client:
|
2020-03-09 10:11:07 +01:00
|
|
|
auth_client_id: admin-cli
|
|
|
|
auth_keycloak_url: https://auth.example.com/auth
|
|
|
|
auth_realm: master
|
|
|
|
auth_username: USERNAME
|
|
|
|
auth_password: PASSWORD
|
|
|
|
realm: master
|
|
|
|
state: absent
|
|
|
|
name: test01
|
2021-04-20 13:20:46 +02:00
|
|
|
delegate_to: localhost
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
- name: Create or update Keycloak client template (with a protocol mapper)
|
2021-04-20 13:20:46 +02:00
|
|
|
community.general.keycloak_client:
|
2020-03-09 10:11:07 +01:00
|
|
|
auth_client_id: admin-cli
|
|
|
|
auth_keycloak_url: https://auth.example.com/auth
|
|
|
|
auth_realm: master
|
|
|
|
auth_username: USERNAME
|
|
|
|
auth_password: PASSWORD
|
|
|
|
realm: master
|
|
|
|
name: this_is_a_test
|
|
|
|
protocol_mappers:
|
|
|
|
- config:
|
2022-09-06 20:42:17 +02:00
|
|
|
access.token.claim: true
|
2020-03-09 10:11:07 +01:00
|
|
|
claim.name: "family_name"
|
2022-09-06 20:42:17 +02:00
|
|
|
id.token.claim: true
|
2020-03-09 10:11:07 +01:00
|
|
|
jsonType.label: String
|
|
|
|
user.attribute: lastName
|
2022-09-06 20:42:17 +02:00
|
|
|
userinfo.token.claim: true
|
|
|
|
consentRequired: true
|
2020-03-09 10:11:07 +01:00
|
|
|
consentText: "${familyName}"
|
|
|
|
name: family name
|
|
|
|
protocol: openid-connect
|
|
|
|
protocolMapper: oidc-usermodel-property-mapper
|
|
|
|
full_scope_allowed: false
|
|
|
|
id: bce6f5e9-d7d3-4955-817e-c5b7f8d65b3f
|
2021-04-20 13:20:46 +02:00
|
|
|
delegate_to: localhost
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = '''
|
|
|
|
msg:
|
2021-10-22 07:57:18 +02:00
|
|
|
description: Message as to what action was taken.
|
|
|
|
returned: always
|
|
|
|
type: str
|
|
|
|
sample: "Client template testclient has been updated"
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
proposed:
|
2021-10-22 07:57:18 +02:00
|
|
|
description: Representation of proposed client template.
|
2020-03-09 10:11:07 +01:00
|
|
|
returned: always
|
|
|
|
type: dict
|
|
|
|
sample: {
|
|
|
|
name: "test01"
|
|
|
|
}
|
2021-10-22 07:57:18 +02:00
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
existing:
|
2021-10-22 07:57:18 +02:00
|
|
|
description: Representation of existing client template (sample is truncated).
|
2020-03-09 10:11:07 +01:00
|
|
|
returned: always
|
|
|
|
type: dict
|
|
|
|
sample: {
|
|
|
|
"description": "test01",
|
|
|
|
"fullScopeAllowed": false,
|
|
|
|
"id": "9c3712ab-decd-481e-954f-76da7b006e5f",
|
|
|
|
"name": "test01",
|
|
|
|
"protocol": "saml"
|
|
|
|
}
|
2021-10-22 07:57:18 +02:00
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
end_state:
|
2021-10-22 07:57:18 +02:00
|
|
|
description: Representation of client template after module execution (sample is truncated).
|
|
|
|
returned: on success
|
2020-03-09 10:11:07 +01:00
|
|
|
type: dict
|
|
|
|
sample: {
|
|
|
|
"description": "test01",
|
|
|
|
"fullScopeAllowed": false,
|
|
|
|
"id": "9c3712ab-decd-481e-954f-76da7b006e5f",
|
|
|
|
"name": "test01",
|
|
|
|
"protocol": "saml"
|
|
|
|
}
|
|
|
|
'''
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
"""
|
|
|
|
Module execution
|
|
|
|
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
argument_spec = keycloak_argument_spec()
|
|
|
|
|
|
|
|
protmapper_spec = dict(
|
|
|
|
consentRequired=dict(type='bool'),
|
|
|
|
consentText=dict(type='str'),
|
|
|
|
id=dict(type='str'),
|
|
|
|
name=dict(type='str'),
|
2024-04-20 09:24:00 +02:00
|
|
|
protocol=dict(type='str', choices=['openid-connect', 'saml', 'docker-v2']),
|
2020-03-09 10:11:07 +01:00
|
|
|
protocolMapper=dict(type='str'),
|
|
|
|
config=dict(type='dict'),
|
|
|
|
)
|
|
|
|
|
|
|
|
meta_args = dict(
|
|
|
|
realm=dict(type='str', default='master'),
|
|
|
|
state=dict(default='present', choices=['present', 'absent']),
|
|
|
|
|
|
|
|
id=dict(type='str'),
|
|
|
|
name=dict(type='str'),
|
|
|
|
description=dict(type='str'),
|
2024-04-20 09:24:00 +02:00
|
|
|
protocol=dict(type='str', choices=['openid-connect', 'saml', 'docker-v2']),
|
2020-03-09 10:11:07 +01:00
|
|
|
attributes=dict(type='dict'),
|
|
|
|
full_scope_allowed=dict(type='bool'),
|
|
|
|
protocol_mappers=dict(type='list', elements='dict', options=protmapper_spec),
|
|
|
|
)
|
2021-10-22 07:57:18 +02:00
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
argument_spec.update(meta_args)
|
|
|
|
|
|
|
|
module = AnsibleModule(argument_spec=argument_spec,
|
|
|
|
supports_check_mode=True,
|
2021-04-20 13:20:46 +02:00
|
|
|
required_one_of=([['id', 'name'],
|
|
|
|
['token', 'auth_realm', 'auth_username', 'auth_password']]),
|
|
|
|
required_together=([['auth_realm', 'auth_username', 'auth_password']]))
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
result = dict(changed=False, msg='', diff={}, proposed={}, existing={}, end_state={})
|
|
|
|
|
|
|
|
# Obtain access token, initialize API
|
|
|
|
try:
|
2021-04-20 13:20:46 +02:00
|
|
|
connection_header = get_token(module.params)
|
2020-03-09 10:11:07 +01:00
|
|
|
except KeycloakError as e:
|
|
|
|
module.fail_json(msg=str(e))
|
2021-10-22 07:57:18 +02:00
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
kc = KeycloakAPI(module, connection_header)
|
|
|
|
|
|
|
|
realm = module.params.get('realm')
|
|
|
|
state = module.params.get('state')
|
|
|
|
cid = module.params.get('id')
|
|
|
|
|
2021-10-22 07:57:18 +02:00
|
|
|
# Filter and map the parameters names that apply to the client template
|
2020-03-09 10:11:07 +01:00
|
|
|
clientt_params = [x for x in module.params
|
|
|
|
if x not in ['state', 'auth_keycloak_url', 'auth_client_id', 'auth_realm',
|
|
|
|
'auth_client_secret', 'auth_username', 'auth_password',
|
|
|
|
'validate_certs', 'realm'] and module.params.get(x) is not None]
|
|
|
|
|
2021-10-22 07:57:18 +02:00
|
|
|
# See if it already exists in Keycloak
|
2020-03-09 10:11:07 +01:00
|
|
|
if cid is None:
|
|
|
|
before_clientt = kc.get_client_template_by_name(module.params.get('name'), realm=realm)
|
|
|
|
if before_clientt is not None:
|
|
|
|
cid = before_clientt['id']
|
|
|
|
else:
|
|
|
|
before_clientt = kc.get_client_template_by_id(cid, realm=realm)
|
|
|
|
|
|
|
|
if before_clientt is None:
|
2021-10-22 07:57:18 +02:00
|
|
|
before_clientt = {}
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
result['existing'] = before_clientt
|
|
|
|
|
|
|
|
# Build a proposed changeset from parameters given to this module
|
2021-10-22 07:57:18 +02:00
|
|
|
changeset = {}
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
for clientt_param in clientt_params:
|
|
|
|
# lists in the Keycloak API are sorted
|
|
|
|
new_param_value = module.params.get(clientt_param)
|
|
|
|
if isinstance(new_param_value, list):
|
|
|
|
try:
|
|
|
|
new_param_value = sorted(new_param_value)
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
changeset[camel(clientt_param)] = new_param_value
|
|
|
|
|
2021-10-22 07:57:18 +02:00
|
|
|
# Prepare the desired values using the existing values (non-existence results in a dict that is save to use as a basis)
|
|
|
|
desired_clientt = before_clientt.copy()
|
|
|
|
desired_clientt.update(changeset)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
result['proposed'] = changeset
|
|
|
|
|
2021-10-22 07:57:18 +02:00
|
|
|
# Cater for when it doesn't exist (an empty dict)
|
|
|
|
if not before_clientt:
|
2020-03-09 10:11:07 +01:00
|
|
|
if state == 'absent':
|
2021-10-22 07:57:18 +02:00
|
|
|
# Do nothing and exit
|
2020-03-09 10:11:07 +01:00
|
|
|
if module._diff:
|
|
|
|
result['diff'] = dict(before='', after='')
|
2021-10-22 07:57:18 +02:00
|
|
|
result['changed'] = False
|
|
|
|
result['end_state'] = {}
|
2020-03-09 10:11:07 +01:00
|
|
|
result['msg'] = 'Client template does not exist, doing nothing.'
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
2021-10-22 07:57:18 +02:00
|
|
|
# Process a creation
|
2020-03-09 10:11:07 +01:00
|
|
|
result['changed'] = True
|
2021-10-22 07:57:18 +02:00
|
|
|
|
|
|
|
if 'name' not in desired_clientt:
|
2020-03-09 10:11:07 +01:00
|
|
|
module.fail_json(msg='name needs to be specified when creating a new client')
|
|
|
|
|
|
|
|
if module._diff:
|
2021-10-22 07:57:18 +02:00
|
|
|
result['diff'] = dict(before='', after=desired_clientt)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
2021-10-22 07:57:18 +02:00
|
|
|
# create it
|
|
|
|
kc.create_client_template(desired_clientt, realm=realm)
|
|
|
|
after_clientt = kc.get_client_template_by_name(desired_clientt['name'], realm=realm)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
result['end_state'] = after_clientt
|
|
|
|
|
2021-10-22 07:57:18 +02:00
|
|
|
result['msg'] = 'Client template %s has been created.' % desired_clientt['name']
|
2020-03-09 10:11:07 +01:00
|
|
|
module.exit_json(**result)
|
2021-10-22 07:57:18 +02:00
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
else:
|
|
|
|
if state == 'present':
|
2021-10-22 07:57:18 +02:00
|
|
|
# Process an update
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
result['changed'] = True
|
|
|
|
if module.check_mode:
|
|
|
|
# We can only compare the current client template with the proposed updates we have
|
|
|
|
if module._diff:
|
|
|
|
result['diff'] = dict(before=before_clientt,
|
2021-10-22 07:57:18 +02:00
|
|
|
after=desired_clientt)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
2021-10-22 07:57:18 +02:00
|
|
|
# do the update
|
|
|
|
kc.update_client_template(cid, desired_clientt, realm=realm)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
after_clientt = kc.get_client_template_by_id(cid, realm=realm)
|
|
|
|
if before_clientt == after_clientt:
|
|
|
|
result['changed'] = False
|
2021-10-22 07:57:18 +02:00
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
result['end_state'] = after_clientt
|
|
|
|
|
2021-10-22 07:57:18 +02:00
|
|
|
if module._diff:
|
|
|
|
result['diff'] = dict(before=before_clientt, after=after_clientt)
|
|
|
|
|
|
|
|
result['msg'] = 'Client template %s has been updated.' % desired_clientt['name']
|
2020-03-09 10:11:07 +01:00
|
|
|
module.exit_json(**result)
|
2021-10-22 07:57:18 +02:00
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
else:
|
2021-10-22 07:57:18 +02:00
|
|
|
# Process a deletion (because state was not 'present')
|
2020-03-09 10:11:07 +01:00
|
|
|
result['changed'] = True
|
2021-10-22 07:57:18 +02:00
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
if module._diff:
|
2021-10-22 07:57:18 +02:00
|
|
|
result['diff'] = dict(before=before_clientt, after='')
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
2021-10-22 07:57:18 +02:00
|
|
|
# delete it
|
2020-03-09 10:11:07 +01:00
|
|
|
kc.delete_client_template(cid, realm=realm)
|
2021-10-22 07:57:18 +02:00
|
|
|
result['proposed'] = {}
|
|
|
|
|
|
|
|
result['end_state'] = {}
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
result['msg'] = 'Client template %s has been deleted.' % before_clientt['name']
|
|
|
|
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|