
    Vh                     2   d dl mZmZmZ eZdZdZd dlm	c m
c mc mc mZ d dlZd dlZd dlmZ d dlZdZ	 d dlZdZd dlmZ d d	lmZ d
 Zd Zd Zd Z d Z!d Z"d Z# G d de$      Z%d Z&e'dk(  r e&        yy# e$ r  ej0                         ZdZY Ww xY w)    )absolute_importdivisionprint_functiona  
---
module: rabbitmq_user
short_description: Manage RabbitMQ users
description:
  - Add or remove users to RabbitMQ and assign permissions
author: Chris Hoffman (@chrishoffman)
options:
  user:
    description:
      - Name of user to add
    type: str
    required: true
    aliases: [username, name]
  password:
    description:
      - Password of user to add.
      - To change the password of an existing user, you must also specify
        C(update_password=always).
    type: str
  tags:
    description:
      - User tags specified as comma delimited.
      - The suggested tags to use are management, policymaker, monitoring and administrator.
    type: str
  permissions:
    description:
      - a list of dicts, each dict contains vhost, configure_priv, write_priv, and read_priv,
        and represents a permission rule for that vhost.
      - This option should be preferable when you care about all permissions of the user.
      - You should use vhost, configure_priv, write_priv, and read_priv options instead
        if you care about permissions for just some vhosts.
    type: list
    elements: dict
    default: []
  vhost:
    description:
      - vhost to apply access privileges.
      - This option will be ignored when permissions option is used.
    type: str
    default: /
  node:
    description:
      - erlang node name of the rabbit we wish to configure
    type: str
    default: rabbit
  configure_priv:
    description:
      - Regular expression to restrict configure actions on a resource
        for the specified vhost.
      - By default all actions are restricted.
      - This option will be ignored when permissions option is used.
    type: str
    default: '^$'
  write_priv:
    description:
      - Regular expression to restrict configure actions on a resource
        for the specified vhost.
      - By default all actions are restricted.
      - This option will be ignored when permissions option is used.
    type: str
    default: '^$'
  read_priv:
    description:
      - Regular expression to restrict configure actions on a resource
        for the specified vhost.
      - By default all actions are restricted.
      - This option will be ignored when permissions option is used.
    type: str
    default: '^$'
  topic_permissions:
    description:
      - A list of dicts, each dict contains vhost, exchange, read_priv and write_priv,
        and represents a topic permission rule for that vhost.
      - By default vhost is C(/) and exchange is C(amq.topic).
      - Supported since RabbitMQ 3.7.0. If RabbitMQ is older and topic_permissions are
        set, the module will fail.
    type: list
    elements: dict
    default: []
    version_added: '1.2.0'
  force:
    description:
      - Deletes and recreates the user.
    type: bool
    default: false
  state:
    description:
      - Specify if user is to be added or removed
    type: str
    default: present
    choices: ['present', 'absent']
  update_password:
    description:
      - C(on_create) will only set the password for newly created users.  C(always) will update passwords if they differ.
    type: str
    default: on_create
    choices: ['on_create', 'always']
  login_protocol:
    description:
      - Specify which TCP/IP protocol will be used.
    type: str
    default: http
    choices: ['http', 'https']
    version_added: '1.3.0'
  login_host:
    description:
      - Hostname of API.
    type: str
    version_added: '1.3.0'
  login_port:
    description:
      - login_port of access from API.
    type: str
    default: '15672'
    version_added: '1.3.0'
  login_user:
    description:
      - Administrator's username the management API.
    type: str
    version_added: '1.3.0'
  login_password:
    description:
      - Login password of the management API.
    type: str
    version_added: '1.3.0'
a5  
- name: |-
    Add user to server and assign full access control on / vhost.
    The user might have permission rules for other vhost but you don't care.
  community.rabbitmq.rabbitmq_user:
    user: joe
    password: changeme
    vhost: /
    configure_priv: .*
    read_priv: .*
    write_priv: .*
    state: present

- name: |-
    Add user to server and assign full access control on / vhost.
    The user doesn't have permission rules for other vhosts
  community.rabbitmq.rabbitmq_user:
    user: joe
    password: changeme
    permissions:
      - vhost: /
        configure_priv: .*
        read_priv: .*
        write_priv: .*
    state: present

- name: |-
    Add user to server and assign some topic permissions on / vhost.
    The user doesn't have topic permission rules for other vhosts
  community.rabbitmq.rabbitmq_user:
    user: joe
    password: changeme
    topic_permissions:
      - vhost: /
        exchange: amq.topic
        read_priv: .*
        write_priv: 'prod\\.logging\\..*'
    state: present

- name: Add or Update a user using the API
  community.rabbitmq.rabbitmq_user:
    user: joe
    password: changeme
    tags: monitoring
    login_protocol: https
    login_host: localhost
    login_port: 15672
    login_user: admin
    login_password: changeadmin
    permissions:
          - vhost: /
            configure_priv: .*
            read_priv: .*
            write_priv: .*
    topic_permissions:
      - vhost: /
        exchange: amq.topic
        read_priv: .*
        write_priv: 'prod\\.logging\\..*'
    state: present


- name: Remove a user using the API
  community.rabbitmq.rabbitmq_user:
    user: joe
    password: changeme
    tags: monitoring
    login_protocol: https
    login_host: localhost
    login_port: 15672
    login_user: admin
    login_password: changeadmin
    state: absent

N)parseTF)AnsibleModule)countc              #   V   K   | D ]   }d|v r|d   |d   |d   |d   d | " yw)aX  Older versions of RabbitMQ output permissions with slightly different names.

    In older versions of RabbitMQ, the names of the permissions had the `_priv` suffix, which was removed in versions
    >= 3.7.6. For simplicity we only check the `configure` permission. If it's in the old format then all the other
    ones will be wrong too.
    configure_priv	read_priv
write_privvhost)	configurereadwriter   N vhost_permission_listvhost_permissions     t/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/rabbitmq/plugins/modules/rabbitmq_user.pynormalized_permissionsr      sT      2 	#//-.>?(5),7)'2	  #"	#s   ')c                 Z    t        t        |       D cg c]	  }|d   |f c}      S c c}w )Nr   )dictr   r   s     r   as_permission_dictr      s>    *+@ACCS"7+-=> C D D Cs   (c                 R    t        | D cg c]  }|d   |d   f|f c}      S c c}w )Nr   exchange)r   )topic_permission_listperms     r   as_topic_permission_dictr     s9    )+t4=$z"23T: + , , +s   $c                 *    | |j                  | i       iS Nget)r   vhost_permissionss     r   onlyr$   	  s    $((344    c                 *    t        t        |             S r    )nextiter)iterables    r   firstr*     s    Xr%   c                     | j                  d      r| j                  d      nd| j                  d      r| j                  d      nd| j                  d      r| j                  d      dS ddS )Nr   ^$r   r   )r   r   r   r!   permissionss    r   treat_permissions_for_apir/     sg    5@__[5Q[__[1W[-8__W-E)4+6??6+B'  IM r%   c                 h    | j                  d      | j                  d      | j                  d      dS )Nr   r   r   )r   r   r   r!   r-   s    r   treat_topic_permissions_for_apir1     s/    #
3koog>VOOF+- -r%   c                       e Zd Z	 	 	 ddZd ZddZddZddZd dZd Z	d	 Z
d
 Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd!dZd"dZd"dZy)#RabbitMqUserNc                 4   || _         || _        |xs d| _        || _        |s
t	               n |j                  dd      j                  d      | _        t        |      | _	        t        |      | _        || _        |	| _        |
| _        || _        || _        || _        d | _        t'               | _        t'               | _        | j                  |j-                  dd      | _        d | _        y |j-                  dd      | _        | j3                         | _        y )N  ,rabbitmqctlFT)moduleusernamepasswordnodelistreplacesplittagsr   r.   r   topic_permissionsbulk_permissionslogin_protocol
login_host
login_port
login_userlogin_passwordexisting_tagsr   existing_permissionsexisting_topic_permissionsget_bin_path_rabbitmqctl_version_check_version)selfr9   r:   r;   r@   r.   rA   r<   rB   rC   rD   rE   rF   rG   s                 r   __init__zRabbitMqUser.__init__  s       B	"&DFDLLb,A,G,G,L	-k:!9:K!L 0,$$$,!$(F!*.&'??& & 3 3M5 ID DM & 3 3M4 HD //1DMr%   c                     | j                  d      }|s| j                  d      }|s| j                  j                  d       |S )z'Get the version of the RabbitMQ server.F)fail_on_errorz7Could not determine the version of the RabbitMQ server.msg)_rabbitmq_version_post_3_7_rabbitmq_version_pre_3_7r9   	fail_json)rO   versions     r   rN   zRabbitMqUser._check_version;  sI    111F4454IGKK!!&_!`r%   c                 @    |r| j                   j                  |       y )NrS   )r9   rW   )rO   rT   stop_executions      r   _failzRabbitMqUser._failD  s    KK!!c!*r%   c                 f   d }| j                  g dd      \  }}}|dk7  r| j                  d|      S 	 t        j                  |      }d|v rt	        j
                  |d         S |j                  d	t                     D ]Z  }|d   d
k(  st        |d   d   t              r t	        j
                   ||d               c S t	        j
                  |d         c S  | j                  d|      S # t        $ r6}| j                  dj                  t        |            |      cY d}~S d}~ww xY w)a<  Use the JSON formatter to get a machine readable output of the version.

        At this point we do not know which RabbitMQ server version we are dealing with and which
        version of `rabbitmqctl` we are using, so we will try to use the JSON formatter and see
        what happens. In some versions of
        c                 \    dj                  | D cg c]  }t        |       c}      S c c}w )Nr5   )joinchr)intsis     r   int_list_to_strz@RabbitMqUser._rabbitmq_version_post_3_7.<locals>.int_list_to_strR  s#    77D1qCF1221s   ))status--formatterjsonFcheck_rcr   dCould not parse the version of the RabbitMQ server, because `rabbitmqctl status` returned no output.rT   rZ   rabbitmq_versionrunning_applicationsrabbit      z@Could not find RabbitMQ version of `rabbitmqctl status` command.z>Could not parse output of `rabbitmqctl status` as JSON: {exc}.excN)_execr[   re   loadsVersionStrictVersionr"   r=   
isinstanceint
ValueErrorformatrepr)	rO   rR   rb   rcoutputerrstatus_jsonapplicationes	            r   rU   z'RabbitMqUser._rabbitmq_version_post_3_7J  sH   	3 **%FQV*WFC7:: #U-:  < <	<**V,K![0,,[9K-LMM*/EtvN Eq>X-!+a."3S9&44_[QR^5TUU&44[^DDE ::"d-:  < < 	<::"b"i"inrstnu"i"v-:  < <	<s5   0C1 '%C1 5C1 C1 C1 1	D0:+D+%D0+D0c                    d}| j                  dgd      \  }}}|dk7  r |r| j                  j                  d       nyt        j                  ||t        j
                        }|s#| j                  d	j                  |
      |      S 	 t        j                  |j                  d            S # t        $ r6}| j                  dj                  t        |            |      cY d}~S d}~ww xY w)zGet the version of the RabbitMQ Server.

        Before version 3.7.6 the `rabbitmqctl` utility did not support the
        `--formatter` flag, so the output has to be parsed using regular expressions.
        z2{rabbit,\"RabbitMQ\",\"([0-9]+\.[0-9]+\.[0-9]+)\"}rc   Frf   r   rh   rS   NzmCould not parse the version of the RabbitMQ server from the output of `rabbitmqctl status` command: {output}.)r{   ri   rm   z:Could not parse the version of the RabbitMQ server: {exc}.ro   )rq   r9   rW   research
IGNORECASEr[   rx   rs   rt   grouprw   ry   )rO   rR   version_reg_exrz   r{   r|   
reg_ex_resr   s           r   rV   z&RabbitMqUser._rabbitmq_version_pre_3_7j  s     O**hZ%*@FC7%% +V% W YY~vr}}E
:: #LLRFZ`FLa-:  < <	<(()9)9!)<== 	<::"^"e"ejnopjq"e"r-:  < <	<s   #B/ /	C.8+C)#C.)C.c                    | j                   dg}| j                  r|j                  d| j                  g       | j                  j	                  ||z         \  }}}|r|dk7  rd}t        j                  ||      }|r<| j                  j                  dj                  |j                  d                   n.| j                  j                  d	j                  |      ||
       |r|S |||fS )aS  Execute a command using the `rabbitmqctl` utility.

        By default the _exec call will cause the module to fail, if the error code is non-zero. If the `check_rc`
        flag is set to False, then the exit_code, stdout and stderr will be returned to the calling function to
        perform whatever error handling it needs.

        :param args: the arguments to pass to the `rabbitmqctl` utility
        :param check_rc: when set to True, fail if the utility's exit code is non-zero
        :return: the output of the command or all the outputs plus the error code in case of error
        z-qz-nr   z$(Only root or .* .* run rabbitmqctl)z7Wrong user used to run the `rabbitmqctl` utility: {err}rm   )r|   rS   z,rabbitmqctl exited with non-zero code: {err})rT   rz   stdout)
rL   r<   extendr9   run_commandr   r   rW   rx   r   )	rO   argsrg   cmdrz   outr|   user_error_msg_regexuser_error_msgs	            r   rq   zRabbitMqUser._exec  s       $'99JJdii(){{..sTz:Ca $K YY';SAN%%*c'-v.2F2Fq2Iv'J & L %%*X*_*_dg*_*h)+C & 9s2RcN2r%   c                    t               }| j                  | j                  d      }|j                  dk(  rwt	        |j                         t              r1t        |j                         D cg c]  }|d   |d   f c}      }nU|j                         d   |j                         d   i}n-|j                  dk(  ry| j                  j                  d|j                  |j                         n| j                  t        j                  d	      k\  rFt        t        j                  | j                  g d
            D cg c]  }|d   |d   f c}      }n| j                  dg      }d }|j                         j!                  d      D cg c]  }|j!                  d       }}t               }|D ]#  }t#        |      dkD  r ||d         ng ||d   <   % |j%                  | j&                  t                     | _        | j&                  |v r| j+                         n	t               | _        | j&                  |v r| j/                         n	t               | _        | j&                  |v S c c}w c c}w c c}w )a?  Retrieves the list of registered users from the node.

        If the user is already present, the node will also be queried for the user's permissions and topic
        permissions.
        If the version of the node is >= 3.7.6 the JSON formatter will be used, otherwise the plaintext will be
        parsed.
        NGET   namer@     Error getting the userrT   rc   details3.7.6)
list_usersrd   re   userr   c                     | s
t               S | j                  dd      j                  dd      j                  dd      j                  d      j                  d      S )N[r5   ]r6   	r7   )r=   r>   stripr?   )r@   s    r   process_tagsz&RabbitMqUser.get.<locals>.process_tags  sO    #v<<R088bAII#rRXXY]^ddehiir%   
r   rm   r   )r   rD   request_users_apistatus_coderu   re   r=   r9   rW   textrM   rs   rt   rr   rq   r   r?   lenr"   r:   rH   _get_permissionsrI   _get_topic_permissionsrJ   )rO   usersresponse
user_entryr   users_and_tags
user_partss          r   r"   zRabbitMqUser.get  s7    ??&--e4H##s*hmmot4 ]e]j]j]l!mz:f#5z&7I"J!mnE%]]_V4hmmof6MNE%%,%%0#//$MM &  }} 5 5g >>04

4::Fk;l0mo",  *&1:f3EF o p 

L>2j
 LQ;;=K^K^_cKd!eZ*"2"24"8!e!e"0 fJJMj/\]J]<
1+FceE*Q-(f #YYt}}df=?C}}PU?UD$9$9$;[_[a!KO==\aKa$*E*E*Ggkgm'}}%%A "no "fs   ,I, I1I6c           	         | j                   	 t        j                  | j                  | j                        | j
                  | j                  f      }j                  s|j                  dk(  rst               }|j                         D ]T  }|j                  |j                  d      |j                  d      |j                  d      |j                  d	      d
       V nM|j                  dk(  ry| j                  j                  d|j                  |j                          n	| j"                  t%        j&                  d      k\  r3t        j(                  | j+                  d| j                  ddg            }n| j+                  d| j                  g      j-                         j/                  d      }|D cg c]$  }|j-                         s|j/                  d      & }}|D cg c]  }|g d
k7  s| }}t               }|D ]%  \  }	}
}}|j                  t1        |	|
||
             ' | j2                  rt5              S t7        t9        | j:                  j=                               t5                    S # t        j                  j                  $ r7}d| j                  z  }| j                  j                  ||       Y d}~wd}~ww xY wc c}w c c}w )z*Get permissions of the user from RabbitMQ.NauthzJError trying to request topic permissions of the user %s info in rabbitmq.rT   	exception   r   r   r   r   )r   r   r   r   r   r   r   r   list_user_permissionsrd   re   r   r   )rD   requestsr"   get_permissions_api_url_builderr:   rF   rG   
exceptionsRequestExceptionr9   rW   okr   r=   re   appendr   rM   rs   rt   rr   rq   r   r?   r   rB   r   r$   r*   r.   keys)rO   r   r   rT   r.   
permissionr{   r   	perms_outr   r   r   r   s                r   r   zRabbitMqUser._get_permissions  s{   ??&
#<<(L(LT]]([.2oo.2.A.A.CD {{x33s:"f"*--/ J&&!+!8%/^^K%@!+!8 *v 6	(  %%,%%0#//$MM &  }} 5 5g >>"jj5Ldmm]jlr4s)tu%<dmm$LMSSU[[\`a:@Q$DJJLTZZ-Q	Q.7kd4Cj;jTk	k"f5> g1E9eT&&t%9TY`d'efg   %k22d..33568J;8WXXS &&77 :>BmmM%%' &  < Rks0   AI= :KK)K6K=K,KKc           	      |   | j                   !	 t        j                  | j                  | j                        | j
                  | j                  f      }j                  s|j                  dk(  r|t               }|j                         D ]T  }|j                  |j                  d      |j                  d      |j                  d      |j                  d	      d
       V t!        |      S |j                  dk(  ry| j                  j                  d|j                  |j"                         y| j$                  t'        j(                  d      k  r
t+               S | j$                  t'        j(                  d      k\  r=t        j,                  | j/                  d| j                  ddg            }t!        |      S | j/                  d| j                  g      j1                         j3                  d      }|D cg c]$  }|j1                         s|j3                  d      & }}t               }|D ]%  \  }	}
}}|j                  t+        |	|
||
             ' t!        |      S # t        j                  j                  $ r7}d| j                  z  }| j                  j                  ||       Y d}~\d}~ww xY wc c}w )z0Get topic permissions of the user from RabbitMQ.Nr   DError trying to request permissions of the user %s info in rabbitmq.r   r   r   r   r   r   )r   r   r   r   r   r   r   z3.7.0r   list_user_topic_permissionsrd   re   r   r   )rD   r   r"   %get_topic_permissions_api_url_builderr:   rF   rG   r   r   r9   rW   r   r   r=   re   r   r   r   rM   rs   rt   r   rr   rq   r   r?   )rO   r   r   rT   r.   r   r{   r   r   r   r   r   r   s                r   r   z#RabbitMqUser._get_topic_permissions   sj   ??&
#<<(R(RSWS`S`(a.2oo.2.A.A.CD {{x33s:"f"*--/ J&&!+!8$.NN:$>!+!8 *v 6	(  0<<%%,%%0#//$MM &  }}w44W==v}} 5 5g >>"jjJJ =t}}m]cdeg ,K88 %BDMM$RSYY[aabfg:@Q$DJJLTZZ-Q	Q"f4= e0E8UD&&t%(RW^b'cde+K88M &&77 :>BmmM%%' &  D Rs$   AI" 8J9J9"J6?,J11J6c                 :   | j                   | j                  d| j                   d| j                  d}	 t        j                  || j
                  | j                  f      }j                  s"|j                         j	                  d      d	k(  ry
y| j                  d| j
                  | j                  gd      \  }}}|dk(  S # t        j                  j                  $ r6}d| j
                  z  }| j                  j                  ||       Y d}~d}~ww xY w)z8Return `True` if the user can authenticate successfully.N://:z/api/whoamir   r   r   reasonzNot management userTFauthenticate_userrf   r   )rD   rC   rE   r   r"   r:   r;   r   r   r9   rW   r   re   rq   )rO   urlr   r   rT   rz   r   r|   s           r   check_passwordzRabbitMqUser.check_password/  s    ??&##!C#<<4==$--2PQ {{hmmo11(;?TT::':DMM4==&Ydi:jLBS7N &&77 :>BmmM%%' &  s   -C D$,DDc                    | j                   | j                  | j                         xs dd}| j                  d|      }|j                  r|j
                  dk(  r:d| j                  d|j
                  d}| j                  j                  |       y y | j                  d	| j                  | j                  xs dg       | j                  s| j                  d
| j                  g       y y )Nr5   r;   r@   PUTr   zError trying to create user  in rabbitmq. Status code ''.rS   add_userclear_password)
rD   r;   treat_tags_for_apir   r   r   r:   r9   rW   rq   rO   datar   rT   s       r   addzRabbitMqUser.addH  s    ??& $t7N7N7P7VTVWD--eT:H;;8#7#73#>/3}}h>R>RT%%#%. $?
 JJ
DMM4==3FBGH==

,dmm<= !r%   c                    | j                   [| j                  d      }|j                  dk7  r:d| j                  d|j                  d}| j                  j                  |       y y | j                  d| j                  g       y )NDELETEr   zError trying to remove user r   r   rS   delete_user)rD   r   r   r:   r9   rW   rq   )rO   r   rT   s      r   deletezRabbitMqUser.deleteV  sr    ??&--h7H##s*/3}}h>R>RT%%#%. +
 JJt}}56r%   c                 6   | j                   | j                  xs d| j                  xs dd}| j                  d|      }|j                  r|j
                  dk(  r:d| j                  d|j
                  d}| j                  j                  |       y | j                  j                  d	|j
                  |j                  
       y | j                  r)| j                  d| j                  | j                  g       y | j                  d| j                  g       y )Nr5   r   r   r   &Error trying to set tags for the user r   r   rS   zError setting tags for the userr   change_passwordr   )rD   r;   r@   r   r   r   r:   r9   rW   r   rq   r   s       r   r   zRabbitMqUser.change_password`  s    ??& $ 3TYY_"MD--eT:H;;8#7#73#>/3}}h>R>RT%%#%.%%9#//$MM &  }}

-t}}dmmLM

,dmm<=r%   c                 j   | j                   }| j                  | j                         xs dd}| j                  d|      }|j                  dk(  s:d| j
                  d|j                  d}| j                  j                  |       y y | j                  d	| j
                  g| j                  z          y )
Nr5   r   r   r   r   r   r   rS   set_user_tags)
rD   r;   r   r   r   r:   r9   rW   rq   r@   r   s       r   set_tagszRabbitMqUser.set_tagsu  s    ??& $t7N7N7P7VTVWD--eT:H''3./3}}h>R>RT%%#%. / JJ7$))CDr%   c                 ^   t               }| j                  j                         D ]6  \  }}|| j                  j	                  |i       k7  s&|j                  |       8 t               }| j                  j                         D ]"  }|| j                  vs|j                  |       $ |D ]  }| j                  \| j                  d|      }|j                  dk7  s1d| j                  d|j                  d}| j                  j                  |       kdj                  | j                  |      }| j                  |j                  d	              |D ]  }| j                  u| j                  d
|j	                  d      t!        |            }|j                  dvsJd| j                  d|j                  d}| j                  j                  |        dj                  dd| j                  i|}| j                  |j                  d	              | j#                         | _        y )Nr   r   z,Error trying to remove permission from user r   r   rS   z'clear_permissions -p {vhost} {username})r:   r   r6   r   r   r      r   z'Error trying to add permission to user z@set_permissions -p {vhost} {username} {configure} {write} {read}r:   r   )r=   r.   itemsrI   r"   r   r   rD   request_permissions_apir   r:   r9   rW   rx   rq   r?   r/   r   )	rO   permissions_to_addr   permission_dictpermissions_to_clearr   rT   r   r.   s	            r   set_permissionszRabbitMqUser.set_permissions  s   !V&*&6&6&<&<&> 	;"E?$";";"?"?r"JJ"))/:	;  $v..335 	3ED,,,$++E2	3 * 		+E*77%H''3.37==(BVBVXCKK))c)2?FFPTP]P]ejFk

399S>*		+ . 	+K*77{w?W=VWb=c 8 e''z937==(BVBVXCKK))c)2YF(,F9DF

399S>*	+ %)$9$9$;!r%   c                 j   t               }| j                  j                         D ]6  \  }}|| j                  j	                  |i       k7  s&|j                  |       8 t               }| j                  j                         D ]"  }|| j                  vs|j                  |       $ |D ]  }|\  }}| j                  \| j                  d|      }|j                  dk7  s6d| j                  d|j                  d}| j                  j                  |       pdj                  | j                  ||      }	| j                  |	j                  d	              |D ]  }
| j                  u| j                  d
|
j	                  d      t!        |
            }|j                  dvsJd| j                  d|j                  d}| j                  j                  |        dj                  dd| j                  i|
}	| j                  |	j                  d	              | j#                         | _        y )Nr   r   z2Error trying to remove topic permission from user r   r   rS   z8clear_topic_permissions -p {vhost} {username} {exchange})r:   r   r   r6   r   r   r   r   z-Error trying to add topic permission to user zEset_topic_permissions -p {vhost} {username} {exchange} {write} {read}r:   r   )r=   rA   r   rJ   r"   r   r   rD   request_topic_permissions_apir   r:   r9   rW   rx   rq   r?   r1   r   )rO   r   vhost_exchanger   r   r   r   r   rT   r   r.   s              r   set_topic_permissionsz"RabbitMqUser.set_topic_permissions  s   !V/3/E/E/K/K/M 	;+NO$"A"A"E"EnVX"YY"))/:	;  $v"==BBD 	<NT%;%;;$++N;	< 3 	+N,OE8*==hN''3.37==(BVBVXCKK))c)2QUXV 

399S>*	+ . 	+K*==e[__U\E]CbcnCo > q''z937==(BVBVXCKK))c)2^F(,F9DF

399S>*	+ +/*E*E*G'r%   c                 X    t        | j                        t        | j                        k7  S r    )setr@   rH   rO   s    r   has_tags_modificationsz#RabbitMqUser.has_tags_modifications  s     499~T%7%7!888r%   c                 4    | j                   | j                  k7  S r    )rI   r.   r   s    r   has_permissions_modificationsz*RabbitMqUser.has_permissions_modifications  s    ((D,<,<<<r%   c                 4    | j                   | j                  k7  S r    )rJ   rA   r   s    r   #has_topic_permissions_modificationsz0RabbitMqUser.has_topic_permissions_modifications  s    ..$2H2HHHr%   c           
          | j                   d| j                  d| j                  dt        j                  |d      S )Nr   r   /api/users/r5   )rC   rD   rE   r   quoterO   r:   s     r   users_api_url_builderz"RabbitMqUser.users_api_url_builder  s1    OOOOKK"%	
 	
r%   c                 Z    | j                   d| j                  d| j                  d|dS )Nr   r   r   z/permissionsrC   rD   rE   r   s     r   r   z,RabbitMqUser.get_permissions_api_url_builder  &    OOOO	
 	
r%   c                 Z    | j                   d| j                  d| j                  d|dS )Nr   r   r   z/topic-permissionsr   r   s     r   r   z2RabbitMqUser.get_topic_permissions_api_url_builder  r   r%   c           	      p    ||dk(  rd}| j                   d| j                  d| j                  d|d|	S )N/%2Fr   r   z/api/permissions/r   rO   r:   r   s      r   permissions_api_url_builderz(RabbitMqUser.permissions_api_url_builder  9    =ESLEOOOO
 	
r%   c           	      p    ||dk(  rd}| j                   d| j                  d| j                  d|d|	S )Nr   r   r   r   z/api/topic-permissions/r   r   s      r   !topic_permissions_api_url_builderz.RabbitMqUser.topic_permissions_api_url_builder  r  r%   c                 F    dj                  d | j                  D              S )Nr7   c              3       K   | ]  }|  y wr    r   ).0tags     r   	<genexpr>z2RabbitMqUser.treat_tags_for_api.<locals>.<genexpr>  s     11s   )r^   r@   r   s    r   r   zRabbitMqUser.treat_tags_for_api   s    xx1tyy111r%   c                 j   	 t        j                  || j                  | j                        | j                  | j
                  f|      }|S # t         j                  j                  $ rI}d|j                         d| j                  d}| j                  j                  ||       Y d }~S d }~ww xY w)Nr   re   Error in HTTP request (method z) for user  in rabbitmq.r   )r   requestr   r:   rF   rG   r   r   lowerr9   rW   )rO   methodr   r   r   rT   s         r   r   zRabbitMqUser.request_users_api  s    	@''0J0J4==0Y.2oot?R?R-SZ^`H  ""33 	@C KK!!cY!??	@s   AA B2)>B--B2c                 l   	 t        j                  || j                  | j                  |      | j                  | j
                  f|      }|S # t         j                  j                  $ rI}d|j                         d| j                  d}| j                  j                  ||       Y d }~S d }~ww xY w)Nr
  r  z) for user's permission r  r   )r   r  r   r:   rF   rG   r   r   r  r9   rW   rO   r  r   r   r   r   rT   s          r   r   z$RabbitMqUser.request_permissions_api  s    	@''0P0PQUQ^Q^`e0f.2oot?R?R-SZ^`H  ""33 	@C KK!!cY!??	@   A	A B3*>B..B3c                 l   	 t        j                  || j                  | j                  |      | j                  | j
                  f|      }|S # t         j                  j                  $ rI}d|j                         d| j                  d}| j                  j                  ||       Y d }~S d }~ww xY w)Nr
  r  z ) for topic permission for user r  r   )r   r  r  r:   rF   rG   r   r   r  r9   rW   r  s          r   r   z*RabbitMqUser.request_topic_permissions_api  s    	@''0V0VW[WdWdfk0l.2oot?R?R-SZ^`H  ""33 	@C KK!!cY!??	@r  )FNNNNN)F)Tr    )NN)__name__
__module____qualname__rP   rN   r[   rU   rV   rq   r"   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r   r   r   r   r   r%   r   r3   r3     s    ;@BF1528<@<238-&^0Yd-9^2>7>*E <D#HJ9=I


	
	
2


r%   r3   c                  F
   t        dAi dt        dddg      dt        d d      dt        d 	      d
t        t               dd      dt        d	      dt        d	      dt        d	      dt        d	      dt        t               dd      dt        dd      dt        dddg      dt        d	      dt        d d d!gd"#      d$t        d%d&d&d'g(      d)t        d%*      d+t        d%d,-      d.t        d%d/      d0t        d%d/      } t        | d"1      }|j                  d   }|j                  d   }|j                  d   }|j                  d
   }|j                  d   }|j                  d   }|j                  d   }|j                  d   }	|j                  d   }
|j                  d   }|j                  d   }|j                  d   }|j                  d   }|j                  d$   }|j                  d)   }|j                  d+   }|j                  d.   }|j                  d0   }|r]|D cg c]  }|j	                  dd       }}t        d2 t        |      j                         D              r|j                  d34       d}n||||	d5}|j                  |       d"}|
rk|
D cg c]%  }|j	                  dd      |j	                  d6      f' }}t        d7 t        |      j                         D              r|j                  d84       |D ]  }|d   r	|j                  d94        |
D ]X  }|j                  dd       |j                  d6d:       d;D ]-  }d<j                  |=      }||v s|j                  |      ||<   / Z t        ||||||
|||||||>      }t        d"||?      }|j	                         r|dk(  r|j                          d|d@<   n|r6|j                          |j                          |j	                          d|d@<   n*|d!k(  r%|j!                         s|j#                          d|d@<   |j%                         r|j'                          d|d@<   |j)                         r|j+                          d|d@<   |j-                         r`|j/                          d|d@<   nJ|dk(  rE|j                          |j'                          |j+                          |j/                          d|d@<    |j0                  dAi | y c c}w c c}w )BNr   Tr:   r   )requiredaliasesr;   )defaultno_logr@   )r  r.   r=   r   )r  typeelementsr   r   r
   r,   r   r   rA   forcenobool)r  r  statepresentabsent)r  choicesr<   rl   update_password	on_createalwaysF)r  r$  r  rC   strhttphttps)r  r  r$  rD   )r  rE   15672)r  r  rF   )r  r  rG   )argument_specsupports_check_modec              3   &   K   | ]	  }|d kD    ywrm   Nr   )r  vhost_counts     r   r  zmain.<locals>.<genexpr>W  s     I;{QI   zWError parsing vhost permissions: You can't have two permission dicts for the same vhostrS   )r   r
   r   r   r   c              3   &   K   | ]	  }|d kD    ywr/  r   )r  ve_counts     r   r  zmain.<locals>.<genexpr>j  s     Lx!|Lr1  zyError parsing vhost topic_permissions: You can't have two topic permission dicts for the same vhost and the same exchangezWError parsing vhost permissions: You can't have an empty vhost when setting permissionsz	amq.topic)r   r   z{perm_name}_priv)	perm_name)rB   rC   rD   rE   rF   rG   )changedr   r!  r5  r   )r   r=   r   paramsr"   anyr   valuesrW   r   
setdefaultrx   popr3   r   r   r   r   r   r   r   r   r   r   	exit_json)arg_specr9   r:   r;   r@   r.   r   r
   r   r   rA   r  r!  r<   r%  rC   rD   rE   rF   rG   r   vhostsrB   r   vhost_exchangesr4  suffixed_perm_namerabbitmq_userresults                                r   mainrB  (  s    4*f)=>d40 $ fvF	
 3 D) % t$ tvFVL 4f- 9y(.CD (# [;:QZ_` @QR U#  UG4!" U40#$ t4%H( !F
 }}V$H}}Z(H== D--.KMM'"E]]#34N|,Jk*I&9:MM'"EMM'"E== Dmm$56O]]#34N|,J|,J|,J]]#34NALM:*..#.MMI%-2F2F2HII "P Q ,$"	
 	4   0
 ^^GS):>>*+EF
 
 LE/,B,I,I,KLL "9 : " Q
'" "P QQ
 ( K
gs+j+6* 	KI!3!:!:Y!:!O!Z/(27I(J
9%	K	K !8T;!2D2BSa,6:Zd0>	@M %he<FH  " $F9$$&!!#!!#$(y! H,$335!113(,F9%335&&($(y!::<--/$(y!@@B335$(y!	)	 %%'++- yFv_ N 
s   <T5*T__main__)(
__future__r   r   r   r  __metaclass__DOCUMENTATIONEXAMPLESCansible_collections.community.rabbitmq.plugins.module_utils.version	communityrabbitmqpluginsmodule_utilsrX   rs   re   r   %ansible.module_utils.six.moves.urllibr   	tracebackREQUESTS_IMP_ERRr   HAS_REQUESTSImportError
format_excansible.module_utils.basicr   'ansible.module_utils.common.collectionsr   r   r   r   r$   r*   r/   r1   objectr3   rB  r  r   r%   r   <module>rV     s    A @~@JX V U U  	 7  L
 5 9#&D
,
5 -
G6 GT}@ zF I  +y++-Ls   A; ;BB