
    Vh                         d dl mZmZmZ eZdZdZd dlZd dl	Z	d dl
mZ d dlmZ  G d de      Zd	 Zed
k(  r e        yy)    )absolute_importdivisionprint_functiona  
---
module: rabbitmq_policy
short_description: Manage the state of policies in RabbitMQ
description:
  - Manage the state of a policy in RabbitMQ.
author: John Dewey (@retr0h)
options:
  name:
    description:
      - The name of the policy to manage.
    type: str
    required: true
  vhost:
    description:
      - The name of the vhost to apply to.
    type: str
    default: /
  apply_to:
    description:
      - What the policy applies to. Requires RabbitMQ 3.2.0 or later. For classic_queues,
        quorum_queues and streams RabbitMQ 3.12 or later is required
    type: str
    default: all
    choices: [all, exchanges, queues, classic_queues, quorum_queues, streams]
  pattern:
    description:
      - A regex of queues to apply the policy to. Required when
        C(state=present). This option is no longer required as of Ansible 2.9.
    type: str
    required: false
    default: null
  tags:
    description:
      - A dict or string describing the policy. Required when
        C(state=present). This option is no longer required as of Ansible 2.9.
    type: dict
    required: false
    default: null
  priority:
    description:
      - The priority of the policy.
    type: str
    default: '0'
  node:
    description:
      - Erlang node name of the rabbit we wish to configure.
    type: str
    default: rabbit
  state:
    description:
      - The state of the policy.
    type: str
    default: present
    choices: [present, absent]
aE  
- name: ensure the default vhost contains the HA policy via a dict
  community.rabbitmq.rabbitmq_policy:
    name: HA
    pattern: .*
  args:
    tags:
      ha-mode: all

- name: ensure the default vhost contains the HA policy
  community.rabbitmq.rabbitmq_policy:
    name: HA
    pattern: .*
    tags:
      ha-mode: all
N)LooseVersion)AnsibleModulec                   \    e Zd Zd Z	 	 	 ddZd Zd Zd Zd Zd Z	d Z
	 	 	 	 	 dd	Zd
 Zy)RabbitMqPolicyc                 h   || _         || _        |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  d   | _        |j                  dd      | _
        | j                         | _        y )	Nvhostpatternapply_totagsprioritynoderabbitmqctlT)_module_nameparams_vhost_pattern	_apply_to_tags	_priority_nodeget_bin_path_rabbitmqctl_rabbit_version_version)selfmodulenames      v/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/rabbitmq/plugins/modules/rabbitmq_policy.py__init__zRabbitMqPolicy.__init__^   s    
mmG,i0z2]]6*
z2]]6*
"//tD,,.    c                 v   | j                   j                  r| j                   j                  r|r| j                  dd| j                  g}|r.|j	                  dd       |j	                  d| j
                         | j                   j                  ||z   d      \  }}}|r|j                         S |S t               S )Nz-qz-n   z-p   T)check_rc)	r   
check_moder   r   insertr   run_command
splitlineslist)	r   argsforce_exec_in_check_modesplit_lines	add_vhostcmdrcouterrs	            r"   _execzRabbitMqPolicy._execk   s    
 ''LL++0H$$dD$**=CAt$At{{+<<33C$J3NLBS~~''Jvr$   c                     | j                  dgddd      }t        j                  d|      }|rt        |j	                  d            S t        j                  d|      }|rt        |j	                  d            S y )NstatusTFz{rabbit,".*","(?P<version>.*)"}versionz!RabbitMQ version: (?P<version>.*))r6   researchVersiongroup)r   r8   version_matchs      r"   r   zRabbitMqPolicy._rabbit_version   ss    XJeU; 		"CVL=..y9:: 		"EvN=..y9::r$   c                     | j                   r.| j                   t        d      k\  r| j                  dgd      dd  S | j                  dgd      S )Nz3.7.9list_policiesTr&   )r   r<   r6   r   s    r"   _list_policieszRabbitMqPolicy._list_policies   sH    ==T]]gg.>>::/6qr::zz?+T22r$   c                 @     j                    j                   j                  j                  d        j                  r; j                  t        d      k\  r#t         fd j                         D               S t         fd j                         D               S )Nz/pattern and tags are required for state=present)msgz3.7.0c              3   F   K   | ]  }j                  |d d        yw)   r'   )apply_to_fnopattern_fnoN_policy_check.0policyr   s     r"   	<genexpr>z3RabbitMqPolicy.has_modifications.<locals>.<genexpr>   s+      5 ""6q"I5s   !c              3   @   K   | ]  }j                  |        y wNrI   rK   s     r"   rN   z3RabbitMqPolicy.has_modifications.<locals>.<genexpr>   s#      P/5""6*P   )r   r   r   	fail_jsonr   r<   anyrB   rA   s   `r"   has_modificationsz RabbitMqPolicy.has_modifications   s    == DJJ$6LL""% # ' ==T]]gg.>> 5"1135 5 5 5  P9=9L9L9NP P P Pr$   c                 H     t         fd j                         D              S )Nc              3   @   K   | ]  }j                  |        y wrP   )_policy_check_by_namerK   s     r"   rN   z3RabbitMqPolicy.should_be_deleted.<locals>.<genexpr>   s$      1 &&v.1rQ   )rS   rB   rA   s   `r"   should_be_deletedz RabbitMqPolicy.should_be_deleted   s&     1--/1 1 	1r$   c                    dg}|j                  | j                         |j                  | j                         |j                  t        j                  | j
                               |j                  d       |j                  | j                         | j                  dk7  r,|j                  d       |j                  | j                         | j                  |      S )N
set_policyz
--priorityallz
--apply-to)	appendr   r   jsondumpsr   r   r   r6   )r   r.   s     r"   setzRabbitMqPolicy.set   s    ~DJJDMM"DJJtzz*+L!DNN#>>U"KK%KK'zz$r$   c                 <    | j                  d| j                  g      S )Nclear_policy)r6   r   rA   s    r"   clearzRabbitMqPolicy.clear   s    zz>4::677r$   c                    |sy|j                  d      }||   }||   }	||   j                  dd      }
	 t        j                  ||         }||   }|| j                  k(  xrB |	| j                  k(  xr1 || j                  k(  xr  || j                  k(  xr |
| j                  k(  S # t        j                  j
                  $ r ||   }Y |w xY w)NF	z\\\)splitreplacer]   loadsdecoderJSONDecodeErrorr   r   r   r   r   )r   rM   name_fnorG   rH   tags_fnopriority_fnopolicy_datapolicy_namer   r   r   r   s                r"   rJ   zRabbitMqPolicy._policy_check   s     ll4(!(+|,k*2264@	)::k(34D |,tzz) -h$...H -DJJ&-+3t~~+E-t}},	. ||++ 	)x(D	)s   B% %"C
	C
c                 N    |sy|j                  d      d   }|| j                  k(  S )NFrd   r&   )rf   r   )r   rM   ro   s      r"   rW   z$RabbitMqPolicy._policy_check_by_name   s*    ll4(+djj((r$   N)FTT)r&   r'   rF         )__name__
__module____qualname__r#   r6   r   rB   rT   rX   r_   rb   rJ   rW    r$   r"   r	   r	   \   sQ    / (-	(3P1

 8
  !#$"# #$.6)r$   r	   c                  4   t        t        d      t        d      t        dd       t        dg d	      t        d
dd       t        d      t        d      t        dddg	            } t        | d      }|j                  d   }|j                  d   }t        ||      }t        d||      }|dk(  r&|j	                         r|j                          d|d<   n*|dk(  r%|j                         r|j                          d|d<    |j                  di | y )NT)required/)defaultF)rx   rz   r[   )r[   	exchangesqueuesclassic_queuesquorum_queuesstreams)rz   choicesdict)typerx   rz   0rabbitpresentabsent)r!   r   r   r   r   r   r   state)argument_specsupports_check_moder!   r   )changedr!   r   r   rv   )	r   r   r   r	   rT   r_   rX   rb   	exit_json)arg_specr    r!   r   rabbitmq_policyresults         r"   mainr      s   4 3eT2e-yzvt<c"(#9y(.CD	H  F
 == DMM'"E$VT2O%d%8F	o??A y	(	@@B yFvr$   __main__)
__future__r   r   r   r   __metaclass__DOCUMENTATIONEXAMPLESr]   r:   Cansible_collections.community.rabbitmq.plugins.module_utils.versionr   r<   ansible.module_utils.basicr   objectr	   r   rs   rv   r$   r"   <module>r      sY    A @7r"  	 g 4})V })@B zF r$   