
    Vh&                         d dl mZmZmZ eZd dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlZd dlZd dlZdZ	 d dlZd dlZdZd	 Z G d
 d      Zy# e$ r  ej.                         ZdZY (w xY w)    )absolute_importdivisionprint_function)	to_native)missing_required_lib)parse)	MimeTypesNTFc                      t        t        dd      t        ddd      t        dd      t        dd      t        dddd	g
      t        ddg      t        ddg      t        ddg      t        dd      	      S )Nstrguest)typedefaultT)r   r   no_log	localhost15672httphttps)r   r   choicespathcacert)r   aliasescertkey/)	
login_userlogin_password
login_host
login_portlogin_protocolca_certclient_cert
client_keyvhostdict     t/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/rabbitmq/plugins/module_utils/rabbitmq.pyrabbitmq_argument_specr)      sx    UG4EUK8UG4@QR&8*5fvh7VeW5s+
 
r'   c                   d    e Zd Zd Zd Zd Zd Zed        Z	 d Z	ed        Z
d Zd	 Zd
 Zd Zy)RabbitClientc                 4   || _         |j                  | _        | j                          | j                          | 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                  | j%                          | j                  | j'                          | j)                          y )Nurlprotousernamepasswordhostportr#   queueexchangerouting_keyheaderscafilecertfilekeyfile)moduleparamscheck_required_librarycheck_host_paramsr-   r.   r/   r0   r1   r2   r#   r3   r4   r5   r6   r7   r8   r9   	build_urlappend_ssl_certsconnect_to_rabbitmq)selfr:   s     r(   __init__zRabbitClient.__init__.   sC   mm##% ;;u%[[)
J/J/KK'	KK'	[[)
[[)
J/;;}5{{9-kk(+J/{{9-99 NN;;"!!#  "r'   c                 f    t         s+| j                  j                  t        d      t               y y )Npika)msg	exception)HAS_PIKAr:   	fail_jsonr   PIKA_IMP_ERRrA   s    r(   r<   z#RabbitClient.check_required_libraryJ   s'    KK!!&:6&Bl![ r'   c                      j                   d   0t         fddD              r j                  j                  d        j                   d   2t         fddD              r j                  j                  d       y y y )Nr-   c              3   @   K   | ]  }j                   |   d u  y wNr;   .0krA   s     r(   	<genexpr>z1RabbitClient.check_host_params.<locals>.<genexpr>P   s%       2RQR$++a.2L  2R   )r.   r1   r2   r0   r/   r#   z\url and proto, host, port, vhost, username or password cannot be specified at the same time.rE   c              3   @   K   | ]  }j                   |   d u   y wrM   rN   rO   s     r(   rR   z1RabbitClient.check_host_params.<locals>.<genexpr>T   s$       .Jdkk!n.D  .JrS   zbConnection parameters must be passed via url, or,  proto, host, port, vhost, username or password.)r;   anyr:   rH   rJ   s   `r(   r=   zRabbitClient.check_host_paramsN   s    ;;u)c  2R  WQ  2R  /RKK!!  'E!  F ;;u%#  .J  OI  .J  +JKK!!  'K!  L +J%r'   c                 $   i }| j                   r| j                   |d<   | j                  r| j                  |d<   | j                  r| j                  |d<   | j                  dz   t	        j
                  t        j                  |            z   | _        y )Nr7   r8   r9   z?ssl_options=)r7   r8   r9   r-   urllib_parsequotejsondumps)rA   ssl_optionss     r(   r?   zRabbitClient.append_ssl_certsW   so    ;;$(KKK!==&*mmK
#<<%)\\K	"88o-0B0B4::kCZ0[[r'   c                      t        t        d      t        dddg      t        d      t        d      t        d      t        dd      t        d      t        d      	      S )
Nr   )r   amqpamqps)r   r   intT)r   r   )r-   r.   r1   r2   r/   r0   r#   r3   r$   r&   r'   r(   r)   z#RabbitClient.rabbitmq_argument_specb   sX    % EFG+<=5!5!u%uT2E"E"	
 		
r'   c           	          	 t        |d      5 }|j                         cd d d        S # 1 sw Y   y xY w# t        $ r5}| j                  j	                  d|dt        |             Y d }~y d }~ww xY w)NrbzUnable to open file z: rT   )openreadIOErrorr:   rH   r   )rA   r   file_handlees       r(   
_read_filezRabbitClient._read_filep   sh    	[dD! *["'')* * * 	[KK!!dIVWL&Y!ZZ	[s(   4 (	4 14 4 	A2+A--A2c                 8    t               }|j                  |       S rM   )r	   
guess_type)r   mimes     r(   _check_file_mime_typez"RabbitClient._check_file_mime_typew   s    {t$$r'   c                     dj                  | j                  | j                  | j                  | j                  | j
                  | j                        | _        y )Nz{0}://{1}:{2}@{3}:{4}/{5})formatr.   r/   r0   r1   r2   r#   r-   rJ   s    r(   r>   zRabbitClient.build_url|   s>    .55djj6:mm6:mm6:ii6:ii6:jjBr'   c                 v   	 t        j                  | j                        }	 t        j                        | _        	 | j                  j                         | _
        y# t        $ r2}| j                  j                  dt        |      z         Y d}~rd}~ww xY w# t        $ r2}| j                  j                  dt        |      z         Y d}~d}~ww xY w# t         j                  j                  $ rB}| j                          | j                  j                  dt        |      z         Y d}~yd}~ww xY w)zM
        Function to connect to rabbitmq using username and password
        zURL malformed: %srT   NzConnection issue: %szChannel issue: %s)rD   URLParametersr-   	Exceptionr:   rH   r   BlockingConnection
connectionchannelconn_channel
exceptionsAMQPChannelErrorclose_connection)rA   
parametersrg   s      r(   r@   z RabbitClient.connect_to_rabbitmq   s   	J++DHH5J	M"55jADO	J $ 7 7 9D  	JKK!!&9IaL&H!II	J
  	MKK!!&<y|&K!LL	M
 // 	J!!#KK!!&9IaL&H!II	JsE   A B C 	B&(BB	C$(CCD868D33D8c                     	 | j                   j                          y # t        j                  j                  $ r Y y w xY wrM   )rs   closerD   rv   AMQPConnectionErrorrJ   s    r(   rx   zRabbitClient.close_connection   s3    	OO!!#22 		s    ==c           	      	   | j                   j                  d      | _        | j                   j                  d      Pt        | j                   j                  d      t	        j
                  | j                  d| j                              }| j                   j                  d      | j                  dk(  rt        j                  | j                   j                  d            d   | _        | j                  j                  t        j                  j                  | j                   j                  d            	       t        | j                  | j                   j                  d            t	        j
                  | j                  d| j                              }nz| j                   j                  d      _t        | j                  | j                   j                  d            t	        j
                  | j                  d| j                              }	 | j                  | j                  | j                   j#                  d
| j                   j                  d      | j                   j                  d      | j                   j                  d      | j                   j                  d            }| j                   j%                          |j&                  j                  | _        n| j                  | j                  | j                   j#                  | j                  | j                   j                  d      | j                   j                  d      | j                   j                  d      | j                   j                  d             | j                   j%                          | j0                  | j0                  d<   nU| j0                  | j                  | j                  d<   n-| j0                  | j                  | j                  d<   nd
d<   | j                  d
|d<   n*| j                  |d<   | j0                  | j                  |d<   	  | j                   j2                  di | y# t(        $ r3}| j*                  j-                  dt/        |      z         Y d }~d }~ww xY w# t        j4                  j6                  $ r Y yw xY w)Ncontent_typebody   )r~   delivery_moder6   )r   
propertiessrcz
text/plainr   )filename durable	exclusiveauto_deleter6   )r3   r   r   r   	argumentszQueue declare issue: %srT   r5   r4   TFr&   )r;   getr~   r%   rD   BasicPropertiesr6   r+   rl   updateosr   basenamerh   r3   r4   ru   queue_declareconfirm_deliverymethodrq   r:   rH   r   r5   basic_publishrv   UnroutableError)rA   argsresultrg   s       r(   r   zRabbitClient.basic_publish   s    KKOON;;;??6".[[__V,//T=N=N^_imiuiuvxD
 ;;??5!-$2C2C|2S , B B4;;??SXCY Z[\ ]DLL))$++//%*@A    __T[[__U%;<//T=N=N>?8<23D [[__U#/__T[[__U%;<//T=N=N>?8<23D	Pzz!dmm&;**88rAEQZA[CG;;??S^C_EI[[__UbEcCG;;??S\C]	 9 _
 !!224#]]00
'DMM,A!!//djj8<	8R:>++//+:V<@KKOOM<Z:>++//):T	 0 V
 !!224 '"&"2"2D%$***@"&**D%$--*C"&--D"$D == !D#}}D'&*mm]#	+D++3d33  	PKK!!&?)A,&N!OO	P4 .. 		s+   FQ6 R5 6	R2?(R--R25SSN)__name__
__module____qualname__rB   r<   r=   r?   staticmethodr)   rh   rl   r>   r@   rx   r   r&   r'   r(   r+   r+   -   sc    #8\L	\ 

 

 0[ % %BJ(Ir'   r+   )
__future__r   r   r   r   __metaclass__ansible.module_utils._textr   ansible.module_utils.basicr   %ansible.module_utils.six.moves.urllibr   rX   	mimetypesr	   r   rZ   	tracebackrI   rD   pika.exceptionsrG   ImportError
format_excr)   r+   r&   r'   r(   <module>r      st    A @ 0 ; G  	  Hz z)  '9'')LHs   
A A'&A'