
    VhP"                         d Z dZdZddlZ	 ddlZddlmZ ddlm	Z	 ddl
mZ dg d	Zd
 Zd Zd Zd Zd Zd Zd Zd Zd Zedk(  r e        yy# e$ r Y Gw xY w)aO  
---
module: mq_user
version_added: 6.0.0
short_description: Manage users in existing Amazon MQ broker
description:
  - Manage Amazon MQ users.
  - Pending changes are taking into account for idempotency.
author:
  - FCO (@fotto)
options:
  broker_id:
    description:
      - The ID of the MQ broker to work on.
    type: str
    required: true
  username:
    description:
      - The name of the user to create/update/delete.
    type: str
    required: true
  state:
    description:
      - Create/Update vs Delete of user.
    default: present
    choices: [ 'present', 'absent' ]
    type: str
  console_access:
    description:
      - Whether the user can access the MQ Console.
      - Defaults to C(false) on creation.
    type: bool
  groups:
    description:
      - Set group memberships for user.
      - Defaults to C([]) on creation.
    type: list
    elements: str
  password:
    description:
      - Set password for user.
      - Defaults to a random password on creation.
      - Ignored unless I(allow_pw_update=true).
    type: str
  allow_pw_update:
    description:
      - When I(allow_pw_update=true) and I(password) is set, the password
        will always be updated for the user.
    default: false
    type: bool
extends_documentation_fragment:
  - amazon.aws.boto3
  - amazon.aws.common.modules
  - amazon.aws.region.modules
a  
- name: create/update user - set provided password if user doesn't exist, yet
  community.aws.mq_user:
    state: present
    broker_id: "aws-mq-broker-id"
    username: "sample_user1"
    console_access: false
    groups: ["g1", "g2"]
    password: "plain-text-password"

- name: allow console access and update group list - relying on default state
  community.aws.mq_user:
    broker_id: "aws-mq-broker-id"
    username: "sample_user1"
    region: "{{ aws_region }}"
    console_access: true
    groups: ["g1", "g2", "g3"]

- name: remove user - setting all credentials explicitly
  community.aws.mq_user:
    state: absent
    broker_id: "aws-mq-broker-id"
    username: "other_user"
z
user:
    description:
      - just echos the username
      - "only present when state=present"
    type: str
    returned: success
    N)camel_dict_to_snake_dict)is_boto3_error_code)AnsibleAWSModuleF)console_accessgroupsc                     g }d| v r| d   }nd| v r| d   d   }t        |      t        |      k7  ryt        |      t        t        |      t        |      z        k7  ryy)NGroupsPendingTF)lenset)user_responserequested_groupscurrent_groupss      i/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/aws/plugins/modules/mq_user.py_group_change_requiredr   t   sn    N= &x0	m	#&y1(;
>c"233
>c#n"5<L8M"MNN    c                 R    t         d   }d| v r
| d   }||k7  S d| v r| d   d   }||k7  S )Nr   ConsoleAccessr
   )CREATE_DEFAULTS)r   requested_booleancurrent_booleans      r   _console_access_change_requiredr      sR    %&67O-''8
 ///	 
m	#'	2?C///r   c                  ,    t        j                  d      S )N   )secrets	token_hex r   r   generate_passwordr      s    R  r   c                    |j                   d   |j                   d   d}d|j                   v r"|j                   d   |j                   d   |d<   nt        d   |d<   d|j                   v r"|j                   d   r|j                   d   |d<   nt               |d<   d|j                   v r"|j                   d   |j                   d   |d	<   nt        d   |d	<   	  | j                  di |}|S # t        j
                  j                  t        j
                  j                  f$ r}|j                  |d
       Y d }~S d }~ww xY w)N	broker_idusernameBrokerIdUsernamer   r	   passwordPasswordr   r   zCouldn't create usermsgr   )	paramsr   r   create_userbotocore
exceptionsClientErrorBotoCoreErrorfail_json_awsconnmodulekwargsresponsees        r   _create_userr6      s=    --4&--PZB[\F6== V]]8%<%H!==2x*84xV]]"v}}Z'@#]]:6z.0z6==(V]];K-L-X"(--0@"A"12B"C<#4##-f- O ++X-@-@-N-NO <Q$:;;O<s   C* *7D?!D::D?c                     	  | j                   di |}|S # t        j                  j                  t        j                  j                  f$ r}|j                  |d       Y d }~S d }~ww xY w)NzCouldn't update userr'   r   )update_userr+   r,   r-   r.   r/   r0   s        r   _update_userr9      sn    <#4##-f- O ++X-@-@-N-NO <Q$:;;O<s    7A+A&&A+c                     	 | j                  ||      }|S # t        d      $ r Y y t        j                  j                  t        j                  j
                  f$ r}|j                  |d       Y d }~S d }~ww xY w)Nr"   NotFoundExceptionzCouldn't get user detailsr'   )describe_userr   r+   r,   r-   r.   r/   )r1   r2   r    r!   r4   r5   s         r   get_matching_userr=      s    A%%y8%L
 O	 23 ++X-@-@-N-NO AQ$?@@OAs    A;6A;A66A;c                 `   t        | ||j                  d   |j                  d         }d}||j                  st        | |      }d}n.i }d|j                  v r:|j                  d   +t	        ||j                  d         r|j                  d   |d<   d|j                  v r:|j                  d   +t        ||j                  d         r|j                  d   |d<   d	|j                  v r>|j                  d	   r/d
|j                  v r!|j                  d
   r|j                  d	   |d<   t        |      dk(  rd}n?|j                  s1|j                  d   |d<   |j                  d   |d<   t        | ||       d}t        | ||j                  d   |j                  d         }|t        |dg      dS )Nr    r!   FTr   r	   r   r   r%   allow_pw_updater&   r   r#   r$   Tags)ignore_list)changeduser)	r=   r)   
check_moder6   r   r   r   r9   r   )r1   r2   rC   rB   	_responser3   s         r   ensure_user_presentrF      s   T66==+Ev}}U_G`aDG|  $T62Iv}}$x)@)L%dFMM(,CD#)==#:x v}},?O1P1\.tV]]CS5TU*0--8H*I'&6==+D FMM1fmmDU6V%+]]:%>z"v;!G$$%+]];%?z"%+]]:%>z"T662GT66==+Ev}}U_G`aD(@TZS[(\]]r   c                    t        | ||j                  d   |j                  d         }ddi}||S d|v rd|d   v r|d   d   dk(  r|S ddi}|j                  r|S 	 | j                  |d	   |d
          |S # t        j
                  j                  t        j
                  j                  f$ r}|j                  |d       Y d }~|S d }~ww xY w)Nr    r!   rB   Fr
   PendingChangeDELETETr#   r$   r"   zCouldn't delete userr'   )	r=   r)   rD   delete_userr+   r,   r-   r.   r/   )r1   r2   rC   resultr5   s        r   ensure_user_absentrL      s    T66==+Ev}}U_G`aDF|D_Y?DOTcDdhpDpF<$z"2T*=MN M ++X-@-@-N-NO <Q$:;;M<s   A7 77C.CCc                      t        t        dd      t        dd      t        dd      t        ddd      t        ddd      t        ddd	      t        d
d
dg            } t        | d      }|j                  d      }|j                  j	                  d      }	 |d
k(  rt        ||      }n|dk(  rt        ||      } |j                  di  y # t        j                  j                  $ r}|j                  |       Y d }~Fd }~ww xY w)NTstr)requiredtypeFboollist)rO   rP   elements)rO   rP   no_log)defaultrO   rP   presentabsent)rU   choices)r    r!   r   r   r%   r?   state)argument_specsupports_check_modemqrY   r   )dictr   clientr)   getrF   rL   r+   r,   r-   r/   	exit_json)rZ   r2   
connectionrY   rK   r5   s         r   mainrb      s    51t%0U8U%@u5>UUH9y(.CDM MtTFt$JMMg&E I(V<Fh'
F;F Fv **  Q s   #C D2DD__main__)DOCUMENTATIONEXAMPLESRETURNr   r+   ImportError0ansible.module_utils.common.dict_transformationsr   <ansible_collections.amazon.aws.plugins.module_utils.botocorer   ;ansible_collections.amazon.aws.plugins.module_utils.modulesr   r   r   r   r   r6   r9   r=   rF   rL   rb   __name__r   r   r   <module>rl      s   6p2
 	
 V \ X 0!
,^D*8 zF S  		s   A AA