
    Vh(                         d dl mZmZmZ eZdZdZd dlZd dl	m
Z
 d dlmZ dZ	 d dlZdZd dlmZ  G d	 d
e      Zd Zedk(  r e        yy# e$ r  ej"                         ZdZY <w xY w)    )absolute_importdivisionprint_functiona;  
---
module: rabbitmq_vhost
short_description: Manage the state of a virtual host in RabbitMQ
description:
  - Manage the state of a virtual host in RabbitMQ using rabbitmqctl or REST APIs.
author: Chris Hoffman (@chrishoffman)
options:
  name:
    description:
      - The name of the vhost to manage
    type: str
    required: true
    aliases: [vhost]
  node:
    description:
      - erlang node name of the rabbit we wish to configure
    type: str
    default: rabbit
  tracing:
    description:
      - Enable/disable tracing for a vhost
    type: bool
    default: false
    aliases: [trace]
  state:
    description:
      - The state of vhost
    type: str
    default: present
    choices: [present, absent]
  login_user:
      description:
          - RabbitMQ user for connection.
      type: str
      version_added: '1.5.0'
  login_password:
      description:
          - RabbitMQ password for connection.
      type: str
      version_added: '1.5.0'
  login_host:
      description:
          - RabbitMQ host for connection.
      type: str
      version_added: '1.5.0'
  login_port:
      description:
          - RabbitMQ management API port.
      type: str
      default: '15672'
      version_added: '1.5.0'
  login_protocol:
      description:
          - RabbitMQ management API protocol.
      type: str
      choices: [ http , https ]
      default: http
      version_added: '1.5.0'
  ca_cert:
      description:
          - CA certificate to verify SSL connection to management API.
      type: path
      version_added: '1.5.0'
  client_cert:
      description:
          - Client certificate to send on SSL connections to management API.
      type: path
      version_added: '1.5.0'
  client_key:
      description:
          - Private key matching the client certificate.
      type: path
      version_added: '1.5.0'
aJ  
- name: Ensure that the vhost /test exists.
  community.rabbitmq.rabbitmq_vhost:
    name: /test
    state: present

- name: Ensure that the vhost /test exists using REST APIs.
  community.rabbitmq.rabbitmq_vhost:
    name: /test
    state: present
    login_host: localhost
    login_user: admin
    login_password: changeadmin
N)parse)AnsibleModuleTFc                   F    e Zd Zd ZddZd Zd Zd Zd Zd Z	d Z
dd
Zy	)RabbitMqVhostc                     || _         || _        || _        || _        || _        || _        || _        || _        |	| _        |
| _	        || _
        || _        d| _        |j                  dd      | _        y )NFrabbitmqctlT)modulenametracingnode
login_userlogin_password
login_host
login_portlogin_protocolverifycertkey_tracingget_bin_path_rabbitmqctl)selfr   r   r   r   r   r   r   r   r   ca_certclient_cert
client_keys                u/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/rabbitmq/plugins/modules/rabbitmq_vhost.py__init__zRabbitMqVhost.__init__w   sv     		$,$$,	"//tD    c                    | j                   j                  r| j                   j                  rP|rN| j                  dd| j                  g}| j                   j	                  ||z   d      \  }}}|j                         S t               S )Nz-qz-nT)check_rc)r   
check_moder   r   run_command
splitlineslist)r   argsforce_exec_in_check_modecmdrcouterrs          r   _execzRabbitMqVhost._exec   sn    {{%%KK""'?$$dD$))<C;;223:2MLBS>>##vr!   c                    | j                   y| j                  d      }|j                  r|j                         d   | _        y|j
                  dk(  ry| j                  j                  d|j
                  |j                         y | j                  g dd      }|D ]L  }d	|vr|j                  d	      \  }}|| j                  k(  s,| j                  j                  |      | _         y y)
Ngetr   Ti  FzError getting the vhost)msgstatusdetails)list_vhostsr   r   	)r   _request_vhost_apiokjsonr   status_coder   	fail_jsontextr.   splitr   boolean)r   responsevhostsvhostr   r   s         r   r0   zRabbitMqVhost.get   s    ??&..u5H{{ (	 :%%,%%1#//$MM &  ZZ BDIF  u$ %D 1g499$$(KK$7$7$@DM  r!   c                 
   | j                   [| j                  d      }|G|j                  s:d| j                  d|j                  d}| j
                  j                  |       y y y | j                  d| j                  g      S )NputzError trying to create vhost  in rabbitmq. Status code ''.r1   	add_vhostr   r6   r7   r   r9   r   r:   r.   r   r>   r1   s      r   addzRabbitMqVhost.add   sy    ??&..u5H#HKK YY 4 46 %%#%.	 -8# ::{DII677r!   c                 
   | j                   [| j                  d      }|G|j                  s:d| j                  d|j                  d}| j
                  j                  |       y y y | j                  d| j                  g      S )NdeletezError trying to remove vhost rC   rD   rE   delete_vhostrG   rH   s      r   rK   zRabbitMqVhost.delete   sy    ??&..x8H#HKK YY 4 46 %%#%.	 -8# ::~tyy9::r!   c                     | j                   | j                  k7  r.| j                   r| j                          y| j                          yy)NTF)r   r   _enable_tracing_disable_tracing)r   s    r   set_tracingzRabbitMqVhost.set_tracing   s>    <<4==(||$$&  %%'r!   c                    | j                   _| j                  dddi      }|G|j                  s:d| j                  d|j                  d}| j
                  j                  |       y y y | j                  d	d
| j                  g      S )NrB   r   Tdataz(Error trying to enable tracing on vhost rC   rD   rE   trace_on-prG   rH   s      r   rN   zRabbitMqVhost._enable_tracing   s    ??&..uIt;L.MH#HKK YY 4 46 %%#%. -8# ::z4;<<r!   c                    | j                   _| j                  dddi      }|G|j                  s:d| j                  d|j                  d}| j
                  j                  |       y y y | j                  d	d
| j                  g      S )NrB   r   FrR   z)Error trying to disable tracing on vhost rC   rD   rE   	trace_offrU   rG   rH   s      r   rO   zRabbitMqVhost._disable_tracing   s    ??&..uIu;M.NH#HKK YY 4 46 %%#%. -8# ::{D$))<==r!   Nc           
      *   | j                   j                  r|dk7  ry 	 | j                  d| j                  d| j                  dt        j                  | j                  d      }t        j                  ||| j                  | j                  f| j                  | j                  | j                  f|      }|S # t        j                  j                   $ r;}d|d| j                  d	}| j                   j#                  ||
       Y d }~S d }~ww xY w)Nr0   z://:z/api/vhosts/ )methodurlauthr   r   r8   zError in HTTP request (method z) for user z in rabbitmq.)r1   	exception)r   r$   r   r   r   r   quoter   requestsrequestr   r   r   r   r   
exceptionsRequestExceptionr:   )r   r[   rS   r\   r>   r^   r1   s          r   r6   z RabbitMqVhost._request_vhost_api   s    ;;!!fo	@##DIIr*	C  ''oot':':;{{ii*H   ""33 	@C KK!!cY!??	@s   BB: :D0DD)F)N)__name__
__module____qualname__r    r.   r0   rI   rK   rP   rN   rO   r6    r!   r   r	   r	   v   s0    E>8
8
;=>r!   r	   c                  6   t        t        ddg      t        ddgd      t        ddd	g
      t        d      t        dd      t        dd      t        d      t        dd      t        ddddg      t        d      t        d      t        d            } t        | 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#   }t        s|j	                  d$       d }t        d%||&      }t        ||||||||	|
|||      }|j                         r1|d	k(  r|j                          d|d'<   n@|j                         r0d|d'<   n*|dk(  r%|j                          |j                          d|d'<    |j                  d(i | y ))NTr@   )requiredaliasesofftracebool)defaultrj   typepresentabsent)rn   choicesrabbit)rn   str)ro   no_log)ro   15672)ro   rn   httphttps)ro   rn   rr   path)r   r   stater   r   r   r   r   r   r   r   r   )argument_specsupports_check_moder   r   rz   r   r   r   r   r   r   r   r   r   z0requests module not present. Using RabbitMQ cli.F)changedr   rz   r}   rg   )dictr   paramsHAS_REQUESTSwarnr	   r0   rK   rP   rI   	exit_json)arg_specr   r   r   rz   r   r   r   r   r   r   r   r   r   resultrabbitmq_vhosts                   r   mainr     s   4'3UWIFC9y(.CD(#U40t4U#UG4@QR&!f%V$H tLF== DmmI&GMM'"E== D|,J]]#34N|,J|,J]]#34NmmI&G--.K|,JFG
%d%8F"N H!!# $F9))+$(y!	)	""$ yFvr!   __main__)
__future__r   r   r   ro   __metaclass__DOCUMENTATIONEXAMPLES	traceback%ansible.module_utils.six.moves.urllibr   ansible.module_utils.basicr   REQUESTS_IMP_ERRr`   r   ImportError
format_excobjectr	   r   rd   rg   r!   r   <module>r      s    A @JX  7 4 L
 5ZF Zz?D zF M  +y++-Ls   A A'&A'