
    VhQ9                     |    d dl mZmZmZ eZdZdZdZd dl	m
Z
mZmZmZmZmZ d dlmZ d dlZd Zed	k(  r e        yy)
    )absolute_importdivisionprint_functiona  
module: keycloak_role

short_description: Allows administration of Keycloak roles using Keycloak API

version_added: 3.4.0

description:
  - This module allows you to add, remove or modify Keycloak roles using the Keycloak REST API. It requires access to the
    REST API using 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).
  - Attributes are multi-valued in the Keycloak API. All attributes are lists of individual values and will be returned that
    way by this module. You may pass single values for attributes when calling the module, and this will be translated into
    a list suitable for the API.
attributes:
  check_mode:
    support: full
  diff_mode:
    support: full
  action_group:
    version_added: 10.2.0

options:
  state:
    description:
      - State of the role.
      - On V(present), the role will be created if it does not yet exist, or updated with the parameters you provide.
      - On V(absent), the role will be removed if it exists.
    default: 'present'
    type: str
    choices:
      - present
      - absent

  name:
    type: str
    required: true
    description:
      - Name of the role.
      - This parameter is required.
  description:
    type: str
    description:
      - The role description.
  realm:
    type: str
    description:
      - The Keycloak realm under which this role resides.
    default: 'master'

  client_id:
    type: str
    description:
      - If the role is a client role, the client ID under which it resides.
      - If this parameter is absent, the role is considered a realm role.
  attributes:
    type: dict
    description:
      - A dict of key/value pairs to set as custom attributes for the role.
      - Values may be single values (for example a string) or a list of strings.
  composite:
    description:
      - If V(true), the role is a composition of other realm and/or client role.
    default: false
    type: bool
    version_added: 7.1.0
  composites:
    description:
      - List of roles to include to the composite realm role.
      - If the composite role is a client role, the C(clientId) (not ID of the client) must be specified.
    default: []
    type: list
    elements: dict
    version_added: 7.1.0
    suboptions:
      name:
        description:
          - Name of the role. This can be the name of a REALM role or a client role.
        type: str
        required: true
      client_id:
        description:
          - Client ID if the role is a client role. Do not include this option for a REALM role.
          - Use the client ID you can see in the Keycloak console, not the technical ID of the client.
        type: str
        required: false
        aliases:
          - clientId
      state:
        description:
          - Create the composite if present, remove it if absent.
        type: str
        choices:
          - present
          - absent
        default: present

extends_documentation_fragment:
  - community.general.keycloak
  - community.general.keycloak.actiongroup_keycloak
  - community.general.attributes

author:
  - Laurent Paumier (@laurpaum)
a  
- name: Create a Keycloak realm role, authentication with credentials
  community.general.keycloak_role:
    name: my-new-kc-role
    realm: MyCustomRealm
    state: present
    auth_client_id: admin-cli
    auth_keycloak_url: https://auth.example.com/auth
    auth_realm: master
    auth_username: USERNAME
    auth_password: PASSWORD
  delegate_to: localhost

- name: Create a Keycloak realm role, authentication with token
  community.general.keycloak_role:
    name: my-new-kc-role
    realm: MyCustomRealm
    state: present
    auth_client_id: admin-cli
    auth_keycloak_url: https://auth.example.com/auth
    token: TOKEN
  delegate_to: localhost

- name: Create a Keycloak client role
  community.general.keycloak_role:
    name: my-new-kc-role
    realm: MyCustomRealm
    client_id: MyClient
    state: present
    auth_client_id: admin-cli
    auth_keycloak_url: https://auth.example.com/auth
    auth_realm: master
    auth_username: USERNAME
    auth_password: PASSWORD
  delegate_to: localhost

- name: Delete a Keycloak role
  community.general.keycloak_role:
    name: my-role-for-deletion
    state: absent
    auth_client_id: admin-cli
    auth_keycloak_url: https://auth.example.com/auth
    auth_realm: master
    auth_username: USERNAME
    auth_password: PASSWORD
  delegate_to: localhost

- name: Create a keycloak role with some custom attributes
  community.general.keycloak_role:
    auth_client_id: admin-cli
    auth_keycloak_url: https://auth.example.com/auth
    auth_realm: master
    auth_username: USERNAME
    auth_password: PASSWORD
    name: my-new-role
    attributes:
      attrib1: value1
      attrib2: value2
      attrib3:
        - with
        - numerous
        - individual
        - list
        - items
  delegate_to: localhost
a9  
msg:
  description: Message as to what action was taken.
  returned: always
  type: str
  sample: "Role myrole has been updated"

proposed:
  description: Representation of proposed role.
  returned: always
  type: dict
  sample:
    {
      "description": "My updated test description"
    }

existing:
  description: Representation of existing role.
  returned: always
  type: dict
  sample:
    {
      "attributes": {},
      "clientRole": true,
      "composite": false,
      "containerId": "9f03eb61-a826-4771-a9fd-930e06d2d36a",
      "description": "My client test role",
      "id": "561703dd-0f38-45ff-9a5a-0c978f794547",
      "name": "myrole"
    }

end_state:
  description: Representation of role after module execution (sample is truncated).
  returned: on success
  type: dict
  sample:
    {
      "attributes": {},
      "clientRole": true,
      "composite": false,
      "containerId": "9f03eb61-a826-4771-a9fd-930e06d2d36a",
      "description": "My updated client test role",
      "id": "561703dd-0f38-45ff-9a5a-0c978f794547",
      "name": "myrole"
    }
)KeycloakAPIcamelkeycloak_argument_spec	get_tokenKeycloakErroris_struct_included)AnsibleModuleNc                  F   t               } t        t        dd      t        ddgd      t        ddddg	      
      }t        t        ddddg	      t        dd      t        d      t        dd      t        d      t        d      t        dg |d      t        dd            }| j                  |       t        | dg dgg dgddi      }t        ddi i i i       }	 t	        |j
                        }t        |      }|j
                  j                  d      }|j
                  j                  d      }	|j
                  j                  d      }
|j
                  j                  d      }|j
                  j                  d       J|j
                  d    j                         D ]*  \  }}t        |t              s|gn||j
                  d    |<   , |j
                  D cg c]G  }|t        t               j                               g d!z   vr|j
                  j                  |      |I }}|	|j                  |
|      }n|j!                  |
|	|      }|i }i }|D ]O  }|j
                  j                  |      }||v r||   nd}||k7  s/t#        j$                  |      |t'        |      <   Q t#        j$                  |      }|j                  |       ||d"<   ||d#<   |s,|dk(  r=|j(                  rt        dd$      |d%<   d|d&<   i |d'<   d(|d)<    |j*                  d8i | d|d&<   |
|j                  d*       |j(                  rt        d|$      |d%<   |j,                  r |j*                  d8i | |	%|j/                  ||       |j                  |
|      }n&|j1                  ||	|       |j!                  |
|	|      }|d+   r|j3                  ||	|,      |d-<   ||d'<   d.j5                  |
/      |d)<    |j*                  d8i | nC|dk(  rg }d-|v rt        |d-   t              r~t7        |d-         d0kD  rm|j3                  ||	|,      }g |d-<   |D ]N  }i }|d1   r|j9                  |d2   |3      }|d   |d<   nd|d<   |d   |d<   d|d<   |d-   j;                  |       P n|j;                  d-       t=        |||4      r1d|d&<   ||d'<   d5j5                  |
/      |d)<    |j*                  d8i | d|d&<   |j(                  rt        ||$      |d%<   |j,                  r |j*                  d8i | |	%|j?                  ||       |j                  |
|      }n&|jA                  ||	|       |j!                  |
|	|      }|d+   r|j3                  ||	|,      |d-<   ||d'<   d6j5                  |
/      |d)<    |j*                  d8i | nd|d&<   |j(                  rt        |d$      |d%<   |j,                  r |j*                  d8i | |	|jC                  |
|       n|jE                  |
|	|       i |d'<   d7j5                  |
/      |d)<    |j*                  d8i | y# t        $ r&}|j                  t        |             Y d}~d}~ww xY wc c}w )9z(
    Module execution

    :return:
    strT)typerequiredclientIdF)r   aliasesr   presentabsent)r   defaultchoices)name	client_idstate)r   master)r   r   dictlist)r   r   optionselementsbool)r   r   descriptionrealmr   
attributes
composites	composite)token
auth_realmauth_usernameauth_password)r&   r'   r(   refresh_tokenr&   )argument_specsupports_check_moderequired_one_ofrequired_togetherrequired_by )changedmsgdiffproposedexisting	end_state)r1   Nr!   r   r   r   r"   )r   r!   r   r3   r4   )beforeafterr2   r0   r5   z#Role does not exist, doing nothing.r1   z/name must be specified when creating a new roler$   )rolerepclientidr!   r#   zRole {name} has been created)r   r   
clientRolecontainerId)idr!   )excludez#No changes required to role {name}.zRole {name} has been updatedzRole {name} has been deleted )#r   r   updater   r	   paramsr
   	fail_jsonr   r   getitems
isinstancer   keysget_realm_roleget_client_rolecopydeepcopyr   _diff	exit_json
check_modecreate_realm_rolecreate_client_roleget_role_compositesformatlenget_client_by_idappendr   update_realm_roleupdate_client_roledelete_realm_roledelete_client_role)r*   composites_spec	meta_argsmoduleresultconnection_headerekcr!   r9   r   r   keyvalxrole_paramsbefore_role	changesetparamnew_param_value	old_valuedesired_role
after_rolecompare_excluder#   r$   before_compositecomposite_clients                               s/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/keycloak_role.pymainrn      s2    +,Mut,EJ<%Hy9h:OPO y9h:OPut,e$x0E"V$VRSYZFE2	I #/3-f,g/_.`(7'F	F %Rb2VXYF%%fmm4 
V.	/BMMg&E}}  -H==V$DMMg&E
 }}&2l399; 	[HC<FsD<QuWZFMM,',	[ %mm 5t$:$<$A$A$CDGfff==$$Q'3  5K 5
 ''e4((x? I E --++E2*/;*>K&D	i'&*mmO&DIeEl#	E ==-L	""F:$F: H||!%Rr!:v %F9"$F;AF5MF&v& !y<!RS<<!<@F6NF&v&   u5**47J!!,%@++D(EBJk"')'='=j[ckp'='qJ|$({6==4=Hu"6" I O|+
<;UW[0\adeqr~e  bA  DE  bE33KRZbg3h
,.L)!+ 	GI')$ .+-+>+>)MBZbg+>+h(8H8T(58<(5/8/@$V,09$W--445EF	G  &&|4 ",_U$)y!&2{# E L LRV L Wu   *6* !%F9||!%[!Mv     *6* $$\59..tU;
%%lHeD//hF
+&+-+A+A*_got+A+u
<(",F;:AAtALF5MF&v& !%F9||!%[!Cv     *6* $$T51%%dHe<"$F;:AAtALF5MFvy  %SV$$%$5s   'Y, 'AZ,	Z5ZZ__main__)
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESRETURNUansible_collections.community.general.plugins.module_utils.identity.keycloak.keycloakr   r   r   r	   r
   r   ansible.module_utils.basicr   rH   rn   __name__r>       rm   <module>ry      sZ    A @kZAF-
^I I 4 CL zF rx   