
    Vhm%                         d dl mZmZmZ eZdZdZd dlm	Z	 d dl
mZmZ d Zd Zd Zd	 Zd
 Zd Zd Zd Zd Zd Zedk(  r e        yy)    )absolute_importdivisionprint_functionu  
module: consul_session
short_description: Manipulate Consul sessions
description:
  - Allows the addition, modification and deletion of sessions in a Consul cluster. These sessions can then be used in conjunction
    with key value pairs to implement distributed locks. In depth documentation for working with sessions can be found at
    U(http://www.consul.io/docs/internals/sessions.html).
author:
  - Steve Gargan (@sgargan)
  - Håkon Lerring (@Hakon)
extends_documentation_fragment:
  - community.general.consul
  - community.general.consul.actiongroup_consul
  - community.general.consul.token
  - community.general.attributes
attributes:
  check_mode:
    support: none
  diff_mode:
    support: none
  action_group:
    version_added: 8.3.0
options:
  id:
    description:
      - ID of the session, required when O(state) is either V(info) or V(remove).
    type: str
  state:
    description:
      - Whether the session should be present, in other words it should be created if it does not exist, or absent, removed
        if present. If created, the O(id) for the session is returned in the output. If V(absent), O(id) is required to remove
        the session. Info for a single session, all the sessions for a node or all available sessions can be retrieved by
        specifying V(info), V(node) or V(list) for the O(state); for V(node) or V(info), the node O(name) or session O(id)
        is required as parameter.
    choices: [absent, info, list, node, present]
    type: str
    default: present
  name:
    description:
      - The name that should be associated with the session. Required when O(state=node) is used.
    type: str
  delay:
    description:
      - The optional lock delay that can be attached to the session when it is created. Locks for invalidated sessions ar
        blocked from being acquired until this delay has expired. Durations are in seconds.
    type: int
    default: 15
  node:
    description:
      - The name of the node that with which the session will be associated. By default this is the name of the agent.
    type: str
  datacenter:
    description:
      - The name of the datacenter in which the session exists or should be created.
    type: str
  checks:
    description:
      - Checks that will be used to verify the session health. If all the checks fail, the session will be invalidated and
        any locks associated with the session will be release and can be acquired once the associated lock delay has expired.
    type: list
    elements: str
  behavior:
    description:
      - The optional behavior that can be attached to the session when it is created. This controls the behavior when a session
        is invalidated.
    choices: [delete, release]
    type: str
    default: release
  ttl:
    description:
      - Specifies the duration of a session in seconds (between 10 and 86400).
    type: int
    version_added: 5.4.0
  token:
    version_added: 5.6.0
a  
- name: Register basic session with Consul
  community.general.consul_session:
    name: session1

- name: Register a session with an existing check
  community.general.consul_session:
    name: session_with_check
    checks:
      - existing_check_name

- name: Register a session with lock_delay
  community.general.consul_session:
    name: session_with_delay
    delay: 20s

- name: Retrieve info about session by id
  community.general.consul_session:
    id: session_id
    state: info

- name: Retrieve active sessions
  community.general.consul_session:
    state: list

- name: Register session with a ttl
  community.general.consul_session:
    name: session-with-ttl
    ttl: 600 # sec
)AnsibleModule)AUTH_ARGUMENTS_SPEC_ConsulModulec                     | j                   j                  d      }|dv rt        | |       y |dk(  rt        | |       y t	        | |       y )Nstate)infolistnodepresent)paramsgetlookup_sessionsupdate_sessionremove_session)moduleconsul_moduler
   s      t/home/dcms/DCMS/lib/python3.12/site-packages/ansible_collections/community/general/plugins/modules/consul_session.pyexecuter   }   sE    MMg&E((.	)	v}-v}-    c                 ,    | j                  dd|i      S )Nzsession/listdcr   r   )r   
datacenters     r   list_sessionsr      s%    j!  # #r   c                 2    | j                  dd|fd|i      S )Nsessionr   r   r   r   )r   r   r   s      r   list_sessions_for_noder!      s,    	FD!j!  # #r   c                 2    | j                  dd|fd|i      S )Nr    r   r   r   r   )r   
session_idr   s      r   get_session_infor$      s,    	FJ'j!  # #r   c                 D   | j                   j                  d      }| j                   j                  d      }	 |dk(  r5t        ||      }|rt        |      dk\  r|d   }| j	                  d|       y |dk(  r=| j                   j                  d      }t        |||      }| j	                  d||	       y |d
k(  r=| j                   j                  d      }t        |||      }| j	                  d||       y y # t        $ r}	| j                  d|	z         Y d }	~	y d }	~	ww xY w)Nr   r
   r         T)changedsessionsr   )r(   r   r)   r   id)r(   r#   r)   z"Could not retrieve session info %smsg)	r   r   r   len	exit_jsonr!   r$   	Exception	fail_json)
r   r   r   r
   sessions_listr   r)   r#   session_by_ides
             r   r   r      s1   ""<0JMMg&EGF?)-DM]!3q!8 -a 0T&3  5f_==$$V,D-mT:NHT"&&.  0 f_**40J,]J
SMT(2&3  5	   GAAEFFGs%   9C7 2AC7 4AC7 7	D DDc                 n    |||||d}|dt        |      z  |d<   | j                  dd|i|      }	|	d   S )N)	LockDelayNodeNameChecksBehaviorz%ssTTLzsession/creater   )r   dataID)strput)
r   namebehaviorttlr   
lock_delayr   checkscreate_datacreate_session_response_dicts
             r   create_sessionrF      sg      K "SX-E#0#4#4*	 $5 $ 
 (--r   c           
      .   | j                   j                  d      }| j                   j                  d      }| j                   j                  d      }| j                   j                  d      }| j                   j                  d      }| j                   j                  d      }| j                   j                  d      }	 t        ||||||||      }	| j                  d	|	||||||
       y # t        $ r}
| j                  d|
z         Y d }
~
y d }
~
ww xY w)Nr?   delayrC   r   r   r@   rA   )r?   r@   rA   r   rB   r   rC   T)r(   r#   r?   r@   rA   rH   rC   r   z"Could not create/update session %sr+   )r   r   rF   r.   r/   r0   )r   r   r?   rH   rC   r   r   r@   rA   r    r3   s              r   r   r      s   ==V$DMMg&E]]x(F""<0J==V$D}}  ,H
--

E
"CG &**2%(&*,1,6(.# 	$+""* $ &" 	 	$  GAAEFFGs   ?,C, ,	D5DDc                 *    | j                  dd|f      S )Nr    destroy)r>   )r   r#   s     r   destroy_sessionrK      s    iJ?@@r   c                     | j                   j                  d      }	 t        ||       | j                  d|       y # t        $ r"}| j                  d|d|       Y d }~y d }~ww xY w)Nr*   T)r(   r#   z"Could not remove session with id 'z' r+   )r   r   rK   r.   r/   r0   )r   r   r#   r3   s       r   r   r      sm    ""4(J)z2$. 	 	0 )#Q( 	) 	))s   = 	A(A##A(c                     t        dt        dd      t        dd      t        ddddg	      t        d
      t        d
      t        d
      t        d
      t        ddg d	      t        d
      d	t        } t        | dddgfdddgfdddgfgd      }t        |      }	 t	        ||       y # t
        $ r%}|j                  t        |             Y d }~y d }~ww xY w)Nr   r=   )typeelementsint15)rN   defaultreleasedelete)rN   rR   choices)rN   r   )absentr   r   r   r   )	rC   rH   r@   rA   r*   r?   r   r
   r   r
   r   r?   r   r*   removeF)argument_specrequired_ifsupports_check_moder+    )dictr   r   r   r   r/   r0   r=   )rX   r   r   r3   s       r   mainr]      s    %0t, eUuu U#-. /M4 #fvh'ftf%h'

 "F "&)M%& %SV$$%s   'B4 4	C"=CC"__main__N)
__future__r   r   r   rN   __metaclass__DOCUMENTATIONEXAMPLESansible.module_utils.basicr   Aansible_collections.community.general.plugins.module_utils.consulr   r   r   r   r!   r$   r   rF   r   rK   r   r]   __name__r[   r   r   <module>rf      sx    A @KZ> 5
	.###G>.&G@A
))%X zF r   