
    Vh                         d dl mZmZmZ eZdZdZdZd dl	Z	d dl
mZ d dlmZ d dlmZmZ d d	lmZ 	 d dlZd
Z e       Z G d de      Zy# e$ r dZY w xY w)    )absolute_importdivisionprint_functiona  
    name: rabbitmq
    author: John Imison (@Im0)
    short_description: Retrieve messages from an AMQP/AMQPS RabbitMQ queue.
    description:
        - This lookup uses a basic get to retrieve all, or a limited number C(count), messages from a RabbitMQ queue.
    options:
      url:
        description:
          - An URI connection string to connect to the AMQP/AMQPS RabbitMQ server.
          - For more information refer to the URI spec U(https://www.rabbitmq.com/uri-spec.html).
        required: True
      queue:
        description:
          - The queue to get messages from.
        required: True
      count:
        description:
          - How many messages to collect from the queue.
          - If not set, defaults to retrieving all the messages from the queue.
    requirements:
        - The python pika package U(https://pypi.org/project/pika/).
    notes:
        - This lookup implements BlockingChannel.basic_get to get messages from a RabbitMQ server.
        - After retrieving a message from the server, receipt of the message is acknowledged and the message on the server is deleted.
        - Pika is a pure-Python implementation of the AMQP 0-9-1 protocol that tries to stay fairly independent of the underlying network support library.
        - More information about pika can be found at U(https://pika.readthedocs.io/en/stable/).
        - This plugin is tested against RabbitMQ.  Other AMQP 0.9.1 protocol based servers may work but not tested/guaranteed.
        - Assigning the return messages to a variable under C(vars) may result in unexpected results as the lookup is evaluated every time the
          variable is referenced.
        - Currently this plugin only handles text based messages from a queue. Unexpected results may occur when retrieving binary data.
aO  
- name: Get all messages off a queue
  debug:
    msg: "{{ lookup('community.rabbitmq.rabbitmq', url='amqp://guest:guest@192.168.0.10:5672/%2F', queue='hello') }}"


# If you are intending on using the returned messages as a variable in more than
# one task (eg. debug, template), it is recommended to set_fact.

- name: Get 2 messages off a queue and set a fact for re-use
  set_fact:
    messages: "{{ lookup('community.rabbitmq.rabbiotmq', url='amqp://guest:guest@192.168.0.10:5672/%2F', queue='hello', count=2) }}"

- name: Dump out contents of the messages
  debug:
    var: messages

a  
  _list:
    description:
      - A list of dictionaries with keys and value from the queue.
    type: list
    contains:
      content_type:
        description: The content_type on the message in the queue.
        type: str
      delivery_mode:
        description: The delivery_mode on the message in the queue.
        type: str
      delivery_tag:
        description: The delivery_tag on the message in the queue.
        type: str
      exchange:
        description: The exchange the message came from.
        type: str
      message_count:
        description: The message_count for the message on the queue.
        type: str
      msg:
        description: The content of the message.
        type: str
      redelivered:
        description: The redelivered flag.  True if the message has been delivered before.
        type: bool
      routing_key:
        description: The routing_key on the message in the queue.
        type: str
      headers:
        description: The headers for the message returned from the queue.
        type: dict
      json:
        description: If application/json is specified in content_type, json will be loaded into variables.
        type: dict

N)AnsibleError)
LookupBase)	to_nativeto_text)DisplayTFc                       e Zd ZddZy)LookupModuleNc                 *   t         st        d      |st        d      |st        d      t        j                  d|d|d|d|d|
       	 t	        j
                  |      }	 t	        j                  |      }	 |j                         }	g }d}	 |	j                  |      \  }}}|rt        j                  |d|dt        |      d       t!        t        |      |j"                  |j$                  |j&                  |j(                  |j*                  |j,                  |j.                  |j0                  d	      }|j.                  dk(  r	 t3        j4                  |d         |d<   |j9                  |       |	j;                  |j&                         |dz  }|j"                  dk(  s||k(  rnn"|j<                  r|gS 	 |j                          |gS # t        $ r}t        d	t        |      z        d }~ww xY w# t        $ r}t        d
t        |      z        d }~ww xY w# t        j                  j                  $ rt}	 |j                          nF# t        j                  j                  $ r&}
t        dt        |      z  t        |
            d }
~
ww xY wt        dt        |      z        d }~ww xY w# t6        $ r)}t        d|j&                  dt        |            d }~ww xY w# t        j                  j                  $ r Y |gS w xY w)Nz4pika python package is required for rabbitmq lookup.z$URL is required for rabbitmq lookup.z&Queue is required for rabbitmq lookup.zterms:z : variables:z url:z queue:z count:zURL malformed: %szConnection issue: %sz.Channel and connection closing issues: %s / %szChannel issue: %sr   )queuez,  )	msgmessage_countrouting_keydelivery_tagredeliveredexchangedelivery_modecontent_typeheaderszapplication/jsonr   jsonz"Unable to decode JSON for message z:    )HAS_PIKAr   displayvvvpikaURLParameters	Exceptionr   BlockingConnectionchannel
exceptionsAMQPChannelErrorcloseAMQPConnectionError	basic_getr	   dictr   r   r   r   r   r   r   r   r   loads
ValueErrorappend	basic_ack	is_closed)selfterms	variablesurlr   count
parameterse
connectionconn_channelieretidxmethod_frame
propertiesbodymsg_detailss                    n/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/rabbitmq/plugins/lookup/rabbitmq.pyrunzLookupModule.runu   s   UVVEFFGHH5R[]`bginop	C++C0J	F00<J	C%--/L -9-C-C%-C-P*L*dlJPTVW #*1$-4@4N4N2>2J2J3?3L3L2>2J2J/;/D/D4>4L4L3=3J3J.8.@.@
$% 
& **.@@C.2jjU9K.LF+ 

;'&&|'@'@Aq--2cUl = @ 5L  " 5Lw  	C2Yq\ABB	C
  	F5	!DEE	F
 // 	Cs  "??66 s"#SV_`aVb#bdmnpdqrrs2Yq\ABB	C< & C*YeYrYrt}~  uA  ,B  C  CC" ??66 5Ls   G 'G? =H' J; K0 	G< G77G<?	H$HH$'J8IJ3J3!JJJ33J8;	K-$K((K-0LL)NNNN)__name__
__module____qualname__r?        r>   r   r   s   s    GrD   r   )
__future__r   r   r   type__metaclass__DOCUMENTATIONEXAMPLESRETURNr   ansible.errorsr   ansible.plugins.lookupr   ansible.module_utils._textr   r	   ansible.utils.displayr
   r   r   ImportErrorr   r   rC   rD   r>   <module>rP      su    C BD&%
N  ' - 9 )H )I: I  Hs   A AA