HEX
Server: Apache
System: Linux server2.voipitup.com.au 4.18.0-553.104.1.lve.el8.x86_64 #1 SMP Tue Feb 10 20:07:30 UTC 2026 x86_64
User: posscale (1027)
PHP: 8.2.29
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/__pycache__/consul.cpython-310.pyc
o

�N�g��@s6dZddlZddlZddlZddlZddlZddlZddl	m
Z
e�e�Z
ddiZdZdd�Zd	d
�Z					dgd
d�Zdhdd�Zdidd�Zdjdd�Zdhdd�Zdkdd�Zdkdd�Zdkdd�Zdkdd�Zdkdd �Zdhd!d"�Zdhd#d$�Zdkd%d&�Zdhd'd(�Zdhd)d*�Z dhd+d,�Z!dhd-d.�Z"dkd/d0�Z#dhd1d2�Z$dhd3d4�Z%dkd5d6�Z&dld7d8�Z'dhd9d:�Z(dhd;d<�Z)dkd=d>�Z*dkd?d@�Z+dkdAdB�Z,dkdCdD�Z-dkdEdF�Z.dhdGdH�Z/dhdIdJ�Z0dhdKdL�Z1dhdMdN�Z2dhdOdP�Z3dhdQdR�Z4dkdSdT�Z5dmdUdV�Z6dkdWdX�Z7dkdYdZ�Z8dkd[d\�Z9dmd]d^�Z:dkd_d`�Z;dkdadb�Z<dhdcdd�Z=dkdedf�Z>dS)nz.
Interact with Consul

https://www.consul.io

�N)�SaltInvocationError�list_�listZconsulcC�tdd�ptdd�S)�'
    Retrieve Consul configuration
    �
config.getz
consul.urlz
consul:url�Z__salt__�r	r	�G/opt/saltstack/salt/lib/python3.10/site-packages/salt/modules/consul.py�_get_configsrcCr)rrzconsul.tokenzconsul:tokenrr	r	r	r
�
_get_token!s�r�GET�v1c
Csv|si}ddd�}|st�}|dd�}tj�||�d��}	tj�|	|d�}
|dkr,d	}n|d	ur=t|t�s<tjj�	|�}ntjj�	i�}tjj
j|
|||dd|td
�}|�
dd	�t
jjkrk|�
d|�|d
<d|d<|S|�
dd	�t
jjkrd|d
<d|d<|S|�
dd	�t
jjkr�d|d
<d|d<|S|�
dd	�r�d|d
<|d|d<d|d<|S|r�||d
<d|d<|Sd|d<|S)a�
    Consul object method function to construct and execute on the API URL.

    :param api_url:     The Consul api url.
    :param api_version  The Consul api version
    :param function:    The Consul api function to perform.
    :param method:      The HTTP method, e.g. GET or POST.
    :param data:        The data to be sent for POST method. This param is ignored for GET requests.
    :return:            The json response from the API call or False.
    �T)�data�reszapplication/json)zX-Consul-TokenzContent-Type�/Fr
N)�method�paramsr�decode�statusZheader_dict�optsr�dictrrzNo content available.zKey not found.�errorzAn error occurred.)r�urllib�parse�urljoin�
isinstance�str�salt�utils�json�dumps�http�queryZ__opts__�get�client�OK�
NO_CONTENT�	NOT_FOUND)�function�
consul_url�tokenr�api_versionr�query_params�ret�headersZbase_url�url�resultr	r	r
�_query*s`


�������r3cKs�i}|st�}|st�d�d|d<d|d<|Si}d|vr"d|d<|s+d|d<d}nd|��}d|d<d	|d
<t||||d�}|S)a 
    List keys in Consul

    :param consul_url: The Consul server URL.
    :param key: The key to use as the starting point for the list.
    :return: The list of keys.

    CLI Example:

    .. code-block:: bash

        salt '*' consul.list
        salt '*' consul.list key='web'

    �No Consul URL found.�messageFr�recurse�True�kv/�keysr�	separator�r+r*r,r.�r�logrr3)r+r,�key�kwargsr/r.r*r	r	r
rts*

�Fc
Cs�i}|st�}|st�d�d|d<d|d<|S|std��i}d|��}|r+d|d<|r1d	|d
<t||||d�}|drZ|rZ|dD]}	|	d
durUt�|	d
�|	d
<qCd|	d
<qC|S)a�
    Get key from Consul

    :param consul_url: The Consul server URL.
    :param key: The key to use as the starting point for the list.
    :param recurse: Return values recursively beginning at the value of key.
    :param decode: By default values are stored as Base64 encoded values,
                   decode will return the whole key with the value decoded.
    :param raw: Simply return the decoded value of the key.
    :return: The keys in Consul.

    CLI Example:

    .. code-block:: bash

        salt '*' consul.get key='web/key1'
        salt '*' consul.get key='web' recurse=True
        salt '*' consul.get key='web' recurse=True decode=True

    By default values stored in Consul are base64 encoded, passing the
    decode option will show them as the decoded values.

    .. code-block:: bash

        salt '*' consul.get key='web' recurse=True decode=True raw=True

    By default Consult will return other information about the key, the raw
    option will return only the raw value.

    r4r5Fr�#Required argument "key" is missing.r8r7r6T�rawr;rZValueNr)rr=rrr3�base64�	b64decode)
r+r>r,r6rrAr/r.r*�itemr	r	r
r%�s4

�
r%cKs�i}|st�}|st�d�d|d<d|d<|S|std��gd�}|D]}|D]}||vr>||vr>||kr>td�||���q(q$i}	t|dd	�}
t|||d
�}d|vre|ddkre|dd
kre|d|	d<d|vr�|dr�|ddkr�d|�d�|d<d|d<|S|d|ddkr�d|�d�|d<d|d<|S|d|	d<nd|�d�|d<d|d<|Sd|vr�|d|
vr�d�|d�|d<d|d<|S|d|	d<d|v�r|d�r	d|dvr�|dd|dkr�|d|	d<n |�d�|d<d|d<|Sd�|d�|d<d|d<nt�d�|}d|��}
d}t|||
|||	d�}|d�r7d|d<d|�d|�d �|d<|Sd|d<d!|�d|�d �|d<d"|v�rQ|d"|d"<|S)#a>
    Put values into Consul

    :param consul_url: The Consul server URL.
    :param key: The key to use as the starting point for the list.
    :param value: The value to set the key to.
    :param flags: This can be used to specify an unsigned value
                  between 0 and 2^64-1. Clients can choose to use
                  this however makes sense for their application.
    :param cas: This flag is used to turn the PUT into a
                Check-And-Set operation.
    :param acquire: This flag is used to turn the PUT into a
                    lock acquisition operation.
    :param release: This flag is used to turn the PUT into a
                    lock release operation.
    :return: Boolean & message of success or failure.

    CLI Example:

    .. code-block:: bash

        salt '*' consul.put key='web/key1' value="Hello there"

        salt '*' consul.put key='web/key1' value="Hello there" acquire='d5d371f4-c380-5280-12fd-8810be175592'

        salt '*' consul.put key='web/key1' value="Hello there" release='d5d371f4-c380-5280-12fd-8810be175592'

    r4r5Frr@)�cas�release�acquirez2Using arguments `{}` and `{}` together is invalid.T)r+�return_list)r+r,r>�flagsrlrEzKey z  exists, index must be non-zero.rZModifyIndexz" exists, but indexes do not match.z/ does not exists, CAS argument can not be used.rGz{} is not a valid session.rFZSessionz locked by another session.z)Key {0} does not exist. Skipping release.r8�PUT)r+r,r*rrr.z
Added key z with value �.zUnable to add key r)rr=rr�format�session_listr%r3)r+r,r>�valuer?r/Zconflicting_argsZ_l1Z_l2r.Zavailable_sessionsZ_currentrr*rrr	r	r
�put�s�
����




�
	�
rOcKs�i}|st�}|st�d�d|d<d|d<|S|std��i}d|vr(d|d<d|vrC|dd	kr9|d|d<n
d
|d<d|d<|Sd|��}t|||d|d
�}|drcd|d<d|�d�|d<|Sd|d<d|�d�|d<d|vry|d|d<|S)a
    Delete values from Consul

    :param consul_url: The Consul server URL.
    :param key: The key to use as the starting point for the list.
    :param recurse: Delete values recursively beginning at the value of key.
    :param cas: This flag is used to turn the DELETE into
                a Check-And-Set operation.
    :return: Boolean & message of success or failure.

    CLI Example:

    .. code-block:: bash

        salt '*' consul.delete key='web'
        salt '*' consul.delete key='web' recurse='True'

    r4r5Frr@r6TrEr)zCheck and Set Operation zvalue must be greater than 0.r8ZDELETE)r+r,r*rr.zDeleted key rKzUnable to delete key r�rr=rrr3)r+r,r>r?r/r.r*rr	r	r
�delete^sH

��rQcC�Hi}|st�}|st�d�d|d<d|d<|Sd}t|||dd�}|S)z�
    Returns the checks the local agent is managing

    :param consul_url: The Consul server URL.
    :return: Returns the checks the local agent is managing

    CLI Example:

    .. code-block:: bash

        salt '*' consul.agent_checks

    r4r5Frzagent/checksr
�r+r*r,rr<�r+r,r/r*r	r	r
�agent_checks��
rUcCrR)z�
    Returns the services the local agent is managing

    :param consul_url: The Consul server URL.
    :return: Returns the services the local agent is managing

    CLI Example:

    .. code-block:: bash

        salt '*' consul.agent_services

    r4r5Frzagent/servicesr
rSr<rTr	r	r
�agent_services�rVrWcKsbi}i}|st�}|st�d�d|d<d|d<|Sd|vr$|d|d<d}t|||d|d�}|S)	a
    Returns the members as seen by the local serf agent

    :param consul_url: The Consul server URL.
    :return: Returns the members as seen by the local serf agent

    CLI Example:

    .. code-block:: bash

        salt '*' consul.agent_members

    r4r5Fr�wanz
agent/membersr
�r+r*r,rr.r<�r+r,r?r/r.r*r	r	r
�
agent_members�s(
�r[cCsNi}i}|st�}|st�d�d|d<d|d<|Sd}t|||d|d�}|S)z�
    Returns the local node configuration

    :param consul_url: The Consul server URL.
    :return: Returns the local node configuration

    CLI Example:

    .. code-block:: bash

        salt '*' consul.agent_self

    r4r5Frz
agent/selfr
rYr<)r+r,r/r.r*r	r	r
�
agent_self�s$
�r\cKs�i}i}|st�}|st�d�d|d<d|d<|Sd|vr%|d|d<n
d|d<d|d<|Sd|vr9|d|d<d}t|||d	|d
�}|drWd|d<d�|d�|d<|Sd|d<d
|d<|S)a�
    Manages node maintenance mode

    :param consul_url: The Consul server URL.
    :param enable: The enable flag is required.
                   Acceptable values are either true
                   (to enter maintenance mode) or
                   false (to resume normal operation).
    :param reason: If provided, its value should be a
                   text string explaining the reason for
                   placing the node into maintenance mode.
    :return: Boolean and message indicating success or failure.

    CLI Example:

    .. code-block:: bash

        salt '*' consul.agent_maintenance enable='False' reason='Upgrade in progress'

    r4r5Fr�enable�'Required parameter "enable" is missing.�reasonzagent/maintenancerJrYTzAgent maintenance mode {}ed.z,Unable to change maintenance mode for agent.�rr=rr3rL)r+r,r?r/r.r*rr	r	r
�agent_maintenance"s>
��racKs�i}i}|st�}|st�d�d|d<d|d<|S|s td��d|vr*|d|d<d|��}t|||d|d	�}|drFd
|d<d|d<|Sd|d<d|d<|S)
a�
    Triggers the local agent to join a node

    :param consul_url: The Consul server URL.
    :param address: The address for the agent to connect to.
    :param wan: Causes the agent to attempt to join using the WAN pool.
    :return: Boolean and message indicating success or failure.

    CLI Example:

    .. code-block:: bash

        salt '*' consul.agent_join address='192.168.1.1'

    r4r5Frz'Required argument "address" is missing.rXzagent/join/r
rYTzAgent joined the clusterzUnable to join the cluster.rP)r+r,�addressr?r/r.r*rr	r	r
�
agent_join\s8

��rccC�i}i}|st�}|st�d�d|d<d|d<|S|s td��d|��}t|||d|d�}|dr@d	|d<d
|�d�|d<|Sd|d<d|�d
�|d<|S)aa
    Used to instruct the agent to force a node into the left state.

    :param consul_url: The Consul server URL.
    :param node: The node the agent will force into left state
    :return: Boolean and message indicating success or failure.

    CLI Example:

    .. code-block:: bash

        salt '*' consul.agent_leave node='web1.example.com'

    r4r5Fr�$Required argument "node" is missing.zagent/force-leave/r
rYTzNode z put in leave state.zUnable to change state for rKrP)r+r,�noder/r.r*rr	r	r
�agent_leave�s4

��rgcs�i}i}|st�}|st�d�d|d<d|d<|Sd�vr%�d|d<ntd��d�fd	d
�dD�vr>d|d<d|d<|Sd
�vrH�d
|d<d�vrR�d|d<d�vrpd�vrdd|d<d|d<|S�d|d<�d|d<d�vr�d�vr�d|d<d|d<|S�d|d<�d|d<d�vr��d|d<d}t|||d|d�}|dr�d|d<d��d�|d<|Sd|d<d|d<|S)ak
    The register endpoint is used to add a new check to the local agent.

    :param consul_url: The Consul server URL.
    :param name: The description of what the check is for.
    :param id: The unique name to use for the check, if not
               provided 'name' is used.
    :param notes: Human readable description of the check.
    :param script: If script is provided, the check type is
                   a script, and Consul will evaluate that script
                   based on the interval parameter.
    :param http: Check will perform an HTTP GET request against
                 the value of HTTP (expected to be a URL) based
                 on the interval parameter.
    :param ttl: If a TTL type is used, then the TTL update endpoint
                must be used periodically to update the state of the check.
    :param interval: Interval at which the check should run.
    :return: Boolean and message indicating success or failure.

    CLI Example:

    .. code-block:: bash

        salt '*' consul.agent_check_register name='Memory Utilization' script='/usr/local/bin/check_mem.py' interval='15s'

    r4r5Fr�name�Name�$Required argument "name" is missing.Tcsg|]}|�vrd�qS)Tr	)�.0rD�r?r	r
�
<listcomp>�sz(agent_check_register.<locals>.<listcomp>)�scriptr#�ttlz1Required parameter "script" or "http" is missing.�id�IDZnotes�Notesrn�interval�)Required parameter "interval" is missing.�Script�Intervalr#�HTTPro�TTLzagent/check/registerrJ�r+r*r,rrzCheck {} added to agent.zUnable to add check to agent.�rr=rrr3rL�r+r,r?r/rr*rr	rlr
�agent_check_register�s^

��r|cCs�i}|st�}|st�d�d|d<d|d<|S|std��d|��}t|||dd�}|dr=d	|d<d
|�d�|d<|Sd|d<d|d<|S)
a{
    The agent will take care of deregistering the check from the Catalog.

    :param consul_url: The Consul server URL.
    :param checkid: The ID of the check to deregister from Consul.
    :return: Boolean and message indicating success or failure.

    CLI Example:

    .. code-block:: bash

        salt '*' consul.agent_check_deregister checkid='Memory Utilization'

    r4r5Fr�'Required argument "checkid" is missing.zagent/check/deregister/r
rST�Check � removed from agent.z"Unable to remove check from agent.rP)r+r,�checkidr/r*rr	r	r
�agent_check_deregisters&

�r�cK�i}i}|st�}|st�d�d|d<d|d<|S|s td��d|vr*|d|d<d|��}t||||dd	�}|drJd
|d<d|�d�|d<|Sd|d<d
|�d�|d<|S)a2
    This endpoint is used with a check that is of the TTL type. When this
    is called, the status of the check is set to passing and the TTL
    clock is reset.

    :param consul_url: The Consul server URL.
    :param checkid: The ID of the check to mark as passing.
    :param note: A human-readable message with the status of the check.
    :return: Boolean and message indicating success or failure.

    CLI Example:

    .. code-block:: bash

        salt '*' consul.agent_check_pass checkid='redis_check1' note='Forcing check into passing state.'

    r4r5Frr}�notezagent/check/pass/r
�r+r*r,r.rTr~z marked as passing.�Unable to update check rKrP�r+r,r�r?r/r.r*rr	r	r
�agent_check_pass6�8

��r�cKr�)a9
    This endpoint is used with a check that is of the TTL type. When this
    is called, the status of the check is set to warning and the TTL
    clock is reset.

    :param consul_url: The Consul server URL.
    :param checkid: The ID of the check to deregister from Consul.
    :param note: A human-readable message with the status of the check.
    :return: Boolean and message indicating success or failure.

    CLI Example:

    .. code-block:: bash

        salt '*' consul.agent_check_warn checkid='redis_check1' note='Forcing check into warning state.'

    r4r5Frr}r�zagent/check/warn/r
r�Tr~z marked as warning.r�rKrPr�r	r	r
�agent_check_warnir�r�cKr�)a;
    This endpoint is used with a check that is of the TTL type. When this
    is called, the status of the check is set to critical and the
    TTL clock is reset.

    :param consul_url: The Consul server URL.
    :param checkid: The ID of the check to deregister from Consul.
    :param note: A human-readable message with the status of the check.
    :return: Boolean and message indicating success or failure.

    CLI Example:

    .. code-block:: bash

        salt '*' consul.agent_check_fail checkid='redis_check1' note='Forcing check into critical state.'

    r4r5Frr}r�zagent/check/fail/r
r�Tr~z marked as critical.r�rKrPr�r	r	r
�agent_check_fail�r�r�cKsi}i}|st�}|st�d�d|d<d|d<|St�}|��D]
\}}|||��<q!d|vr7|d|d<ntd��d|vrE|d|d	<d
|vrO|d
|d<d|vrY|d|d
<d|vrm|d}t|t�si|g}||d<d|vrw|d|d<d|vr�t�}	|d��D]
\}}||	|��<q�d}
t�}d|	vr�d}
|	d|d<d|	vr�d}
|	d|d<d|	vr�|	d|d<d|	vr�|	d|d<|
r�d|vr�d|d<d|d<|Snd|vr�|d=|r�||d<d}t	|||d|d �}
|
dr�d|d<d!�
|d�|d<|Sd|d<d"�
|d�|d<|S)#a�
    The used to add a new service, with an optional
    health check, to the local agent.

    :param consul_url: The Consul server URL.
    :param name: A name describing the service.
    :param address: The address used by the service, defaults
                    to the address of the agent.
    :param port: The port used by the service.
    :param id: Unique ID to identify the service, if not
               provided the value of the name parameter is used.
    :param tags: Identifying tags for service, string or list.
    :param script: If script is provided, the check type is
                   a script, and Consul will evaluate that script
                   based on the interval parameter.
    :param http: Check will perform an HTTP GET request against
                 the value of HTTP (expected to be a URL) based
                 on the interval parameter.
    :param check_ttl: If a TTL type is used, then the TTL update
                      endpoint must be used periodically to update
                      the state of the check.
    :param check_interval: Interval at which the check should run.
    :return: Boolean and message indicating success or failure.

    CLI Example:

    .. code-block:: bash

        salt '*' consul.agent_service_register name='redis' tags='["master", "v1"]' address="127.0.0.1" port="8080" check_script="/usr/local/bin/check_redis.py" interval="10s"

    r4r5Frrhrirjrb�Address�port�Portrprq�tags�TagsZenabletagoverrideZEnableTagOverride�checkrnTrur#rwrorxrsrvrt�Checkzagent/service/registerrJryzService {} registered on agent.zUnable to register service {}.)rr=rr�items�lowerrrrr3rL)r+r,r?r/rZ	lc_kwargs�k�v�_tags�ddZinterval_requiredZcheck_ddr*rr	r	r
�agent_service_register�s� 

�
��r�cCrd)a=
    Used to remove a service.

    :param consul_url: The Consul server URL.
    :param serviceid: A serviceid describing the service.
    :return: Boolean and message indicating success or failure.

    CLI Example:

    .. code-block:: bash

        salt '*' consul.agent_service_deregister serviceid='redis'

    r4r5Fr�)Required argument "serviceid" is missing.zagent/service/deregister/rJryT�Service rzUnable to remove service rKrP)r+r,�	serviceidr/rr*rr	r	r
�agent_service_deregister?s,


��r�cKs�i}i}|st�}|st�d�d|d<d|d<|S|s td��d|vr+|d|d<n
d|d<d|d<|Sd|vr?|d|d<d	|��}t||||d
�}|dr^d|d<d|�d
�|d<|Sd|d<d�|�|d<|S)a
    Used to place a service into maintenance mode.

    :param consul_url: The Consul server URL.
    :param serviceid: A name of the service.
    :param enable: Whether the service should be enabled or disabled.
    :param reason: A human readable message of why the service was
                   enabled or disabled.
    :return: Boolean and message indicating success or failure.

    CLI Example:

    .. code-block:: bash

        salt '*' consul.agent_service_deregister serviceid='redis' enable='True' reason='Down for upgrade'

    r4r5Frr�r]r^r_zagent/service/maintenance/)r+r,r*r.Tr�z set in maintenance mode.z-Unable to set service {} to maintenance mode.rz)r+r,r�r?r/r.r*rr	r	r
�agent_service_maintenancehs>

���r�cKs�i}|st�}|st�d�d|d<d|d<|Si}d|vr$|d|d<d|vr.|d|d<d	|vr9|d	|d
<ntd��d|vrG|d
|d<d|vra|ddvr[d|d<d|d<|S|d|d<d|vr�|d}t|��d�rv|dd�}t|�dks�t|�dkr�d|d<d|d<|S|�d�|d<d}t|||d|d�}|dr�d|d<d�|d	�|d<|Sd|d<d �|d	�|d<|S)!a:
    Used to create a session.

    :param consul_url: The Consul server URL.
    :param lockdelay: Duration string using a "s" suffix for seconds.
                      The default is 15s.
    :param node: Must refer to a node that is already registered,
                 if specified. By default, the agent's own node
                 name is used.
    :param name: A human-readable name for the session
    :param checks: A list of associated health checks. It is highly
                   recommended that, if you override this list, you
                   include the default "serfHealth".
    :param behavior: Can be set to either release or delete. This controls
                     the behavior when a session is invalidated. By default,
                     this is release, causing any locks that are held to be
                     released. Changing this to delete causes any locks that
                     are held to be deleted. delete is useful for creating
                     ephemeral key/value entries.
    :param ttl: Session is invalidated if it is not renewed before
                the TTL expires
    :return: Boolean and message indicating success or failure.

    CLI Example:

    .. code-block:: bash

        salt '*' consul.session_create node='node1' name='my-session' behavior='delete' ttl='3600s'

    r4r5FrZ	lockdelayZ	LockDelayrf�NoderhrirjZchecks�touchZTouchZbehavior)rQrF)zBehavior must be zeither delete or release.ZBehaviorro�sN���ri)zTTL must be zbetween 0 and 3600.rxzsession/createrJryTzCreated session {}.zUnable to create session {}.)	rr=rrr�endswith�intr3rL)r+r,r?r/rZ_ttlr*rr	r	r
�session_create�sX

��r�c	Ks�i}|st�}|st�d�d|d<d|d<|Si}d|vr$|d|d<d}t||||d�}|rBg}|dD]	}|�|d	�q6|S|S)
a$
    Used to list sessions.

    :param consul_url: The Consul server URL.
    :param dc: By default, the datacenter of the agent is queried;
               however, the dc can be provided using the "dc" parameter.
    :param return_list: By default, all information about the sessions is
                        returned, using the return_list parameter will return
                        a list of session IDs.
    :return: A list of all available sessions.

    CLI Example:

    .. code-block:: bash

        salt '*' consul.session_list

    r4r5Fr�dczsession/listr;rrq)rr=rr3�append)	r+r,rHr?r/r.r*Z_listrDr	r	r
rM�s*
�rMcKs�i}|st�}|st�d�d|d<d|d<|S|std��i}d|vr*|d|d<d|��}t|||d|d	�}|drJd
|d<d|�d�|d<|Sd|d<d
|�d�|d<|S)a�
    Destroy session

    :param consul_url: The Consul server URL.
    :param session: The ID of the session to destroy.
    :param dc: By default, the datacenter of the agent is queried;
               however, the dc can be provided using the "dc" parameter.
    :return: Boolean & message of success or failure.

    CLI Example:

    .. code-block:: bash

        salt '*' consul.session_destroy session='c1c4d223-91cb-3d1f-1ee8-f2af9e7b6716'

    r4r5Fr�'Required argument "session" is missing.r�zsession/destroy/rJrYTzDestroyed Session rKzUnable to destroy session rP)r+r,�sessionr?r/r.r*rr	r	r
�session_destroy&s8

��r�cKsri}|st�}|st�d�d|d<d|d<|S|std��i}d|vr*|d|d<d|��}t||||d�}|S)	a�
    Information about a session

    :param consul_url: The Consul server URL.
    :param session: The ID of the session to return information about.
    :param dc: By default, the datacenter of the agent is queried;
               however, the dc can be provided using the "dc" parameter.
    :return: Boolean & message of success or failure.

    CLI Example:

    .. code-block:: bash

        salt '*' consul.session_info session='c1c4d223-91cb-3d1f-1ee8-f2af9e7b6716'

    r4r5Frr�r�z
session/info/r;rP)r+r,r�r?r/r.r*r	r	r
�session_infoYs$

�r�c
Ks�i}i}i|d<|st�}|st�d�d|d<d|d<|Sd|vr(|d|d<d|vr3|d|d	<n
d
|d<d|d<|Sd|vrXt|dt�rO|dd}n|d}||d
<n
d|d<d|d<|Sd|vr�i|d<|dD]}|d�|�r�|d|d|d|<qnd|vr�i|d<|d|dd<d|vr�|d|dd
<d|vr�|d|dd<d|vr�|d|dd<d|vr�|d}t|t�s�|g}||dd<d|vr�|d|dd<d|vr�|d|dd<d|vr�|d|dd<d |vr�|d |dd!<d"|v�r|d"|dd#<d$|v�r|d$|dd%<d&|v�r%|d&|dd'<d(|v�r2|d(|dd(<d)|v�r?|d)|dd)<d*|v�rL|d*|dd*<d+|v�rY|d+|dd+<d,|v�rf|d,|dd,<d-|v�r�i|d.<|d-|d.d/<d0|v�r�|d0d1v�r�d2|d<d|d<|S|d0|d.d3<d4|v�r�|d4|d.d5<d6|v�r�|d6|d.d7<d8|v�r�|d8|d.d9<d:}t|||d;|d<�}	|	d�r�d=|d<d>�|d�|d<n
d|d<d?�|d�|d<||d@<|S)AaV
    Registers a new node, service, or check

    :param consul_url: The Consul server URL.
    :param dc: By default, the datacenter of the agent is queried;
               however, the dc can be provided using the "dc" parameter.
    :param node: The node to register.
    :param address: The address of the node.
    :param service: The service that will be registered.
    :param service_address: The address that the service listens on.
    :param service_port: The port for the service.
    :param service_id: A unique identifier for the service, if this is not
                       provided "name" will be used.
    :param service_tags: Any tags associated with the service.
    :param check: The name of the health check to register
    :param check_status: The initial status of the check,
                         must be one of unknown, passing, warning, or critical.
    :param check_service: The service that the check is performed against.
    :param check_id: Unique identifier for the service.
    :param check_notes: An opaque field that is meant to hold human-readable text.
    :return: Boolean & message of success or failure.

    CLI Example:

    .. code-block:: bash

        salt '*' consul.catalog_register node='node1' address='192.168.1.1' service='redis' service_address='127.0.0.1' service_port='8080' service_id='redis_server1'

    ZNodeMetar4r5Fr�
datacenter�
Datacenterrfr�z+Required argument node argument is missing.rbrr�z.Required argument address argument is missing.Z
ip_interfacesZTaggedAddresses�serviceZServiceZservice_addressZservice_portr�Z
service_idrqZservice_tagsr�ZcpuZCpuZnum_cpusZCpu_numZmemZMemoryZoscodeZOsZosarchZOsarchZkernelZKernelZ
kernelreleaseZ
KernelreleaseZ	localhost�nodenameZ	os_familyZlsb_distrib_descriptionZmasterr�r�riZcheck_status)�unknown�passing�warning�criticalz<Check status must be unknown, passing, warning, or critical.ZStatusZ
check_service�	ServiceIDZcheck_id�CheckIDZcheck_notesrrzcatalog/registerrJryTz'Catalog registration for {} successful.z#Catalog registration for {} failed.r)rr=rrrr%r3rL)
r+r,r?r/rZ_addressr�r�r*rr	r	r
�catalog_register�s�

�










�



�

�r�cKs�i}i}|st�}|st�d�d|d<d|d<|Sd|vr$|d|d<d|vr/|d|d<n
d	|d<d|d<|Sd
|vrC|d
|d<d|vrM|d|d
<d}t|||d|d�}|drkd|d<d�|d�|d<|Sd|d<d�|d�|d<|S)a_
    Deregisters a node, service, or check

    :param consul_url: The Consul server URL.
    :param node: The node to deregister.
    :param datacenter: By default, the datacenter of the agent is queried;
               however, the dc can be provided using the "dc" parameter.
    :param checkid: The ID of the health check to deregister.
    :param serviceid: The ID of the service to deregister.
    :return: Boolean & message of success or failure.

    CLI Example:

    .. code-block:: bash

        salt '*' consul.catalog_register node='node1' serviceid='redis_server1' checkid='redis_check1'

    r4r5Frr�r�rfr�zNode argument required.r�r�r�r�zcatalog/deregisterrJryTzCatalog item {} removed.� Removing Catalog item {} failed.r`r{r	r	r
�catalog_deregister(s>

��r�cC�Fi}|st�}|st�d�d|d<d|d<|Sd}t|||d�}|S)z�
    Return list of available datacenters from catalog.

    :param consul_url: The Consul server URL.
    :return: The list of available datacenters.

    CLI Example:

    .. code-block:: bash

        salt '*' consul.catalog_datacenters

    r4r5Frzcatalog/datacenters�r+r*r,r<rTr	r	r
�catalog_datacentersc�
r�cK�`i}i}|st�}|st�d�d|d<d|d<|Sd|vr$|d|d<d}t||||d�}|S)ao
    Return list of available nodes from catalog.

    :param consul_url: The Consul server URL.
    :param dc: By default, the datacenter of the agent is queried;
               however, the dc can be provided using the "dc" parameter.
    :return: The list of available nodes.

    CLI Example:

    .. code-block:: bash

        salt '*' consul.catalog_nodes

    r4r5Frr�z
catalog/nodesr;r<rZr	r	r
�
catalog_nodes� 
�r�cKr�)aw
    Return list of available services rom catalog.

    :param consul_url: The Consul server URL.
    :param dc: By default, the datacenter of the agent is queried;
               however, the dc can be provided using the "dc" parameter.
    :return: The list of available services.

    CLI Example:

    .. code-block:: bash

        salt '*' consul.catalog_services

    r4r5Frr�zcatalog/servicesr;r<rZr	r	r
�catalog_services�r�r�cKs�i}i}|st�}|st�d�d|d<d|d<|S|s td��d|vr*|d|d<d|vr4|d|d<d|��}t||||d	�}|S)
a�
    Information about the registered service.

    :param consul_url: The Consul server URL.
    :param dc: By default, the datacenter of the agent is queried;
               however, the dc can be provided using the "dc" parameter.
    :param tag: Filter returned services with tag parameter.
    :return: Information about the requested service.

    CLI Example:

    .. code-block:: bash

        salt '*' consul.catalog_service service='redis'

    r4r5Fr�'Required argument "service" is missing.r��tagzcatalog/service/r;rP�r+r,r�r?r/r.r*r	r	r
�catalog_service�s(

�r�cK�ri}i}|st�}|st�d�d|d<d|d<|S|s td��d|vr*|d|d<d|��}t||||d�}|S)	a�
    Information about the registered node.

    :param consul_url: The Consul server URL.
    :param node: The node to request information about.
    :param dc: By default, the datacenter of the agent is queried;
               however, the dc can be provided using the "dc" parameter.
    :return: Information about the requested node.

    CLI Example:

    .. code-block:: bash

        salt '*' consul.catalog_service service='redis'

    r4r5Frrer�z
catalog/node/r;rP�r+r,rfr?r/r.r*r	r	r
�catalog_node��$

�r�cKr�)	a�
    Health information about the registered node.

    :param consul_url: The Consul server URL.
    :param node: The node to request health information about.
    :param dc: By default, the datacenter of the agent is queried;
               however, the dc can be provided using the "dc" parameter.
    :return: Health information about the requested node.

    CLI Example:

    .. code-block:: bash

        salt '*' consul.health_node node='node1'

    r4r5Frrer�zhealth/node/r;rPr�r	r	r
�health_noder�r�cKr�)	a�
    Health information about the registered service.

    :param consul_url: The Consul server URL.
    :param service: The service to request health information about.
    :param dc: By default, the datacenter of the agent is queried;
               however, the dc can be provided using the "dc" parameter.
    :return: Health information about the requested node.

    CLI Example:

    .. code-block:: bash

        salt '*' consul.health_checks service='redis1'

    r4r5Frr�r�zhealth/checks/r;rPr�r	r	r
�
health_checksBr�r�cKs�i}i}|st�}|st�d�d|d<d|d<|S|s td��d|vr*|d|d<d|vr4|d|d<d|vr>|d|d<d	|��}t||||d
�}|S)a�
    Health information about the registered service.

    :param consul_url: The Consul server URL.
    :param service: The service to request health information about.
    :param dc: By default, the datacenter of the agent is queried;
               however, the dc can be provided using the "dc" parameter.
    :param tag: Filter returned services with tag parameter.
    :param passing: Filter results to only nodes with all
                    checks in the passing state.
    :return: Health information about the requested node.

    CLI Example:

    .. code-block:: bash

        salt '*' consul.health_service service='redis1'

        salt '*' consul.health_service service='redis1' passing='True'

    r4r5Frr�r�r�r�zhealth/service/r;rPr�r	r	r
�health_servicejs,

�r�cKs�i}i}|st�}|st�d�d|d<d|d<|S|s td��d|vr*|d|d<|dvr8d|d<d|d<|Sd	|��}t||||d
�}|S)a�
    Returns the checks in the state provided on the path.

    :param consul_url: The Consul server URL.
    :param state: The state to show checks for. The supported states
                  are any, unknown, passing, warning, or critical.
                  The any state is a wildcard that can be used to
                  return all checks.
    :param dc: By default, the datacenter of the agent is queried;
               however, the dc can be provided using the "dc" parameter.
    :return: The checks in the provided state.

    CLI Example:

    .. code-block:: bash

        salt '*' consul.health_state state='redis1'

        salt '*' consul.health_state service='redis1' passing='True'

    r4r5Frz%Required argument "state" is missing.r�)�anyr�r�r�r�z:State must be any, unknown, passing, warning, or critical.z
health/state/r;rP)r+r,�stater?r/r.r*r	r	r
�health_state�s,

�r�cCr�)z�
    Returns the current Raft leader

    :param consul_url: The Consul server URL.
    :return: The address of the Raft leader.

    CLI Example:

    .. code-block:: bash

        salt '*' consul.status_leader

    r4r5Frz
status/leaderr�r<rTr	r	r
�
status_leader�r�r�cCr�)a
    Returns the current Raft peer set

    :param consul_url: The Consul server URL.
    :return: Retrieves the Raft peers for the
             datacenter in which the agent is running.

    CLI Example:

    .. code-block:: bash

        salt '*' consul.status_peers

    r4r5Frzstatus/peersr�r<rTr	r	r
�status_peers�s
r�cKs�i}i}|st�}|st�d�d|d<d|d<|Sd|vr$|d|d<d|vr/|d|d<ntd��d	|vr=|d	|d
<d|vrG|d|d<d
}t|||d|d�}|dred|d<d�|d�|d<|Sd|d<d�|d�|d<|S)a1
    Create a new ACL token.

    :param consul_url: The Consul server URL.
    :param name: Meaningful indicator of the ACL's purpose.
    :param type: Type is either client or management. A management
                 token is comparable to a root user and has the
                 ability to perform any action including creating,
                 modifying, and deleting ACLs.
    :param rules: The Consul server URL.
    :return: Boolean & message of success or failure.

    CLI Example:

    .. code-block:: bash

        salt '*' consul.acl_create

    r4r5Frrprhrirj�type�Type�rules�Rulesz
acl/createrJ�r+r,rrr*T�ACL {} created.r�rzr{r	r	r
�
acl_creates:

��r�cKs�i}i}|st�}|st�d�d|d<d|d<|Sd|vr%|d|d<n
d|d<d|d<|Sd|vr:|d|d	<ntd
��d|vrH|d|d<d
|vrR|d
|d<d}t|||d|d�}|drpd|d<d�|d�|d<|Sd|d<d�|d�|d<|S)af
    Update an ACL token.

    :param consul_url: The Consul server URL.
    :param name: Meaningful indicator of the ACL's purpose.
    :param id: Unique identifier for the ACL to update.
    :param type: Type is either client or management. A management
                 token is comparable to a root user and has the
                 ability to perform any action including creating,
                 modifying, and deleting ACLs.
    :param rules: The Consul server URL.
    :return: Boolean & message of success or failure.

    CLI Example:

    .. code-block:: bash

        salt '*' consul.acl_update

    r4r5Frrprq�#Required parameter "id" is missing.rhrirjr�r�r�r�z
acl/updaterJr�Tr�zUpdating ACL {} failed.rzr{r	r	r
�
acl_updateBs@

��r�cKs�i}i}|st�}|st�d�d|d<d|d<|Sd|vr(d|d<d|d<|Sd�|d�}t|||d|d	�}|drKd
|d<d�|d�|d<|Sd|d<d�|d�|d<|S)
a6
    Delete an ACL token.

    :param consul_url: The Consul server URL.
    :param id: Unique identifier for the ACL to update.
    :return: Boolean & message of success or failure.

    CLI Example:

    .. code-block:: bash

        salt '*' consul.acl_delete id='c1c4d223-91cb-3d1f-1ee8-f2af9e7b6716'

    r4r5Frrpr�zacl/destroy/{}rJr�TzACL {} deleted.zRemoving ACL {} failed.�rr=rrLr3r{r	r	r
�
acl_delete�s0

��r�cKsri}i}|st�}|st�d�d|d<d|d<|Sd|vr(d|d<d|d<|Sd�|d�}t||d|d	�}|S)
a;
    Information about an ACL token.

    :param consul_url: The Consul server URL.
    :param id: Unique identifier for the ACL to update.
    :return: Information about the ACL requested.

    CLI Example:

    .. code-block:: bash

        salt '*' consul.acl_info id='c1c4d223-91cb-3d1f-1ee8-f2af9e7b6716'

    r4r5Frrpr�zacl/info/{}r
)r+rrr*r�)r+r?r/rr*r	r	r
�acl_info�s 
r�cKs�i}i}|st�}|st�d�d|d<d|d<|Sd|vr(d|d<d|d<|Sd�|d�}t|||d|d	�}|drQd
|d<d�|d�|d<|d
|d<|Sd|d<d�|d�|d<|S)ae
    Information about an ACL token.

    :param consul_url: The Consul server URL.
    :param id: Unique identifier for the ACL to update.
    :return: Boolean, message of success or
             failure, and new ID of cloned ACL.

    CLI Example:

    .. code-block:: bash

        salt '*' consul.acl_info id='c1c4d223-91cb-3d1f-1ee8-f2af9e7b6716'

    r4r5Frrpr�zacl/clone/{}rJr�TzACL {} cloned.rhrrq�Cloning ACL item {} failed.r�r{r	r	r
�	acl_clone�s2

��r�cKsNi}i}|st�}|st�d�d|d<d|d<|Sd}t|||d|d�}|S)z�
    List the ACL tokens.

    :param consul_url: The Consul server URL.
    :return: List of ACLs

    CLI Example:

    .. code-block:: bash

        salt '*' consul.acl_list

    r4r5Frzacl/listr
r�r<)r+r,r?r/rr*r	r	r
�acl_list�s

�r�cKs�i}i}|st�}|st�d�d|d<d|d<|S|s td��d|vr(|d}d|vr0|d}d|vr8|d}d	|vr@|d	}d
|��}t|||d|d�}|drfd
|d<d|�d�|d<|d|d<|Sd|d<d�|d�|d<|S)a�
    List the ACL tokens.

    :param consul_url: The Consul server URL.
    :param name: The name of the event to fire.
    :param dc: By default, the datacenter of the agent is queried;
               however, the dc can be provided using the "dc" parameter.
    :param node: Filter by node name.
    :param service: Filter by service name.
    :param tag: Filter by tag name.
    :return: List of ACLs

    CLI Example:

    .. code-block:: bash

        salt '*' consul.event_fire name='deploy'

    r4r5Frrjr�rfr�r�zevent/fire/rJ)r+r,r.rr*TzEvent z fired.rr�rhrz)r+r,rhr?r/r.r*rr	r	r
�
event_fire	sF

��r�cKsfi}i}|st�}|st�d�d|d<d|d<|Sd|vr#|d}ntd��d}t||||d�}|S)	z�
    List the recent events.

    :param consul_url: The Consul server URL.
    :param name: The name of the event to fire.
    :return: List of ACLs

    CLI Example:

    .. code-block:: bash

        salt '*' consul.event_list

    r4r5Frrhrjzevent/list/)r+r,r.r*rPrZr	r	r
�
event_list^	s"

�r�)Nr
rNN)NNN)NNNFFF)NNNN)NN)NNF)N)?�__doc__rB�http.clientr#�loggingrZsalt.utils.httprZsalt.utils.jsonZsalt.exceptionsr�	getLogger�__name__r=Z__func_alias__Z__virtualname__rrr3rr%rOrQrUrWr[r\rarcrgr|r�r�r�r�r�r�r�r�rMr�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	r
�<module>s�

�
J
.
?
}
C


&
#
:
1
-
V
&
3
3
3
p
)
9
W
.
3
)
'
;

$
$
+
(
(
(
3
2


:
@
-
#
-
@