Skip to content

Commit

Permalink
consul: add support for session TTL (#4996)
Browse files Browse the repository at this point in the history
Signed-off-by: Wilfried Roset <[email protected]>
(cherry picked from commit d214f49)
  • Loading branch information
wilfriedroset authored and patchback[bot] committed Jul 31, 2022
1 parent 258471b commit 2c77073
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/4996-consul-session-ttl.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- consul - adds ``ttl`` parameter for session (https://github.com/ansible-collections/community.general/pull/4996).
14 changes: 14 additions & 0 deletions plugins/modules/clustering/consul/consul_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@
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
'''

EXAMPLES = '''
Expand All @@ -121,6 +126,11 @@
- 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
'''

try:
Expand Down Expand Up @@ -185,13 +195,15 @@ def update_session(module):
datacenter = module.params.get('datacenter')
node = module.params.get('node')
behavior = module.params.get('behavior')
ttl = module.params.get('ttl')

consul_client = get_consul_api(module)

try:
session = consul_client.session.create(
name=name,
behavior=behavior,
ttl=ttl,
node=node,
lock_delay=delay,
dc=datacenter,
Expand All @@ -201,6 +213,7 @@ def update_session(module):
session_id=session,
name=name,
behavior=behavior,
ttl=ttl,
delay=delay,
checks=checks,
node=node)
Expand Down Expand Up @@ -241,6 +254,7 @@ def main():
checks=dict(type='list', elements='str'),
delay=dict(type='int', default='15'),
behavior=dict(type='str', default='release', choices=['release', 'delete']),
ttl=dict(type='int'),
host=dict(type='str', default='localhost'),
port=dict(type='int', default=8500),
scheme=dict(type='str', default='http'),
Expand Down
12 changes: 12 additions & 0 deletions tests/integration/targets/consul/tasks/consul_session.yml
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,15 @@
that:
- search_deleted is skipped # each iteration is skipped
- search_deleted is not changed # and then unchanged

- name: ensure session can be created with a ttl
consul_session:
state: present
name: session-with-ttl
ttl: 180 # sec
register: result

- assert:
that:
- result is changed
- result['ttl'] == 180

0 comments on commit 2c77073

Please sign in to comment.