consul_session: PEP8 compliancy and documentation changes (#33000)
THis PR includes: - PEP8 compliancy changes - Documentation changes
This commit is contained in:
parent
c94d57311c
commit
b3e4a88b90
2 changed files with 49 additions and 60 deletions
|
@ -1,107 +1,92 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# (c) 2015, Steve Gargan <steve.gargan@gmail.com>
|
||||
|
||||
# Copyright: (c) 2015, Steve Gargan <steve.gargan@gmail.com>
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
__metaclass__ = type
|
||||
|
||||
|
||||
ANSIBLE_METADATA = {'metadata_version': '1.1',
|
||||
'status': ['preview'],
|
||||
'supported_by': 'community'}
|
||||
|
||||
|
||||
DOCUMENTATION = """
|
||||
module: consul_session
|
||||
short_description: "manipulate consul sessions"
|
||||
short_description: Manipulate consul sessions
|
||||
description:
|
||||
- allows the addition, modification and deletion of sessions in a consul
|
||||
- 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 here http://www.consul.io/docs/internals/sessions.html
|
||||
sessions can be found at http://www.consul.io/docs/internals/sessions.html
|
||||
requirements:
|
||||
- "python >= 2.6"
|
||||
- python >= 2.6
|
||||
- python-consul
|
||||
- requests
|
||||
version_added: "2.0"
|
||||
author: "Steve Gargan @sgargan"
|
||||
author:
|
||||
- Steve Gargan (@sgargan)
|
||||
options:
|
||||
state:
|
||||
description:
|
||||
- whether the session should be present i.e. created if it doesn't
|
||||
- Whether the session should be present i.e. created if it doesn't
|
||||
exist, or absent, removed if present. If created, the ID for the
|
||||
session is returned in the output. If absent, the name or 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 info, node or list for the state; for node or info, the
|
||||
node name or session id is required as parameter.
|
||||
required: false
|
||||
choices: ['present', 'absent', 'info', 'node', 'list']
|
||||
choices: [ absent, info, list, node, present ]
|
||||
default: present
|
||||
name:
|
||||
description:
|
||||
- the name that should be associated with the session. This is opaque
|
||||
- The name that should be associated with the session. This is opaque
|
||||
to Consul and not required.
|
||||
required: false
|
||||
default: None
|
||||
delay:
|
||||
description:
|
||||
- the optional lock delay that can be attached to the session when it
|
||||
- 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
|
||||
acquired until this delay has expired. Durations are in seconds.
|
||||
default: 15
|
||||
required: false
|
||||
node:
|
||||
description:
|
||||
- the name of the node that with which the session will be associated.
|
||||
- The name of the node that with which the session will be associated.
|
||||
by default this is the name of the agent.
|
||||
required: false
|
||||
default: None
|
||||
datacenter:
|
||||
description:
|
||||
- name of the datacenter in which the session exists or should be
|
||||
- The name of the datacenter in which the session exists or should be
|
||||
created.
|
||||
required: false
|
||||
default: None
|
||||
checks:
|
||||
description:
|
||||
- a list of checks that will be used to verify the session health. If
|
||||
- A list of 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.
|
||||
required: false
|
||||
default: None
|
||||
host:
|
||||
description:
|
||||
- host of the consul agent defaults to localhost
|
||||
required: false
|
||||
- The host of the consul agent defaults to localhost.
|
||||
default: localhost
|
||||
port:
|
||||
description:
|
||||
- the port on which the consul agent is running
|
||||
required: false
|
||||
- The port on which the consul agent is running.
|
||||
default: 8500
|
||||
scheme:
|
||||
description:
|
||||
- the protocol scheme on which the consul agent is running
|
||||
required: false
|
||||
- The protocol scheme on which the consul agent is running.
|
||||
default: http
|
||||
version_added: "2.1"
|
||||
validate_certs:
|
||||
description:
|
||||
- whether to verify the tls certificate of the consul agent
|
||||
required: false
|
||||
- Whether to verify the tls certificate of the consul agent.
|
||||
type: bool
|
||||
default: True
|
||||
version_added: "2.1"
|
||||
behavior:
|
||||
description:
|
||||
- the optional behavior that can be attached to the session when it
|
||||
is created. This can be set to either 'release' or 'delete'. This
|
||||
controls the behavior when a session is invalidated.
|
||||
- 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 ]
|
||||
default: release
|
||||
required: false
|
||||
version_added: "2.2"
|
||||
"""
|
||||
|
||||
|
@ -122,10 +107,13 @@ EXAMPLES = '''
|
|||
delay: 20s
|
||||
|
||||
- name: retrieve info about session by id
|
||||
consul_session: id=session_id state=info
|
||||
consul_session:
|
||||
id: session_id
|
||||
state: info
|
||||
|
||||
- name: retrieve active sessions
|
||||
consul_session: state=list
|
||||
consul_session:
|
||||
state: list
|
||||
'''
|
||||
|
||||
try:
|
||||
|
@ -149,6 +137,7 @@ def execute(module):
|
|||
else:
|
||||
remove_session(module)
|
||||
|
||||
|
||||
def lookup_sessions(module):
|
||||
|
||||
datacenter = module.params.get('datacenter')
|
||||
|
@ -158,7 +147,7 @@ def lookup_sessions(module):
|
|||
try:
|
||||
if state == 'list':
|
||||
sessions_list = consul_client.session.list(dc=datacenter)
|
||||
#ditch the index, this can be grabbed from the results
|
||||
# Ditch the index, this can be grabbed from the results
|
||||
if sessions_list and sessions_list[1]:
|
||||
sessions_list = sessions_list[1]
|
||||
module.exit_json(changed=True,
|
||||
|
@ -235,31 +224,32 @@ def remove_session(module):
|
|||
module.fail_json(msg="Could not remove session with id '%s' %s" % (
|
||||
session_id, e))
|
||||
|
||||
|
||||
def get_consul_api(module):
|
||||
return consul.Consul(host=module.params.get('host'),
|
||||
port=module.params.get('port'))
|
||||
|
||||
|
||||
def test_dependencies(module):
|
||||
if not python_consul_installed:
|
||||
module.fail_json(msg="python-consul required for this module. "\
|
||||
module.fail_json(msg="python-consul required for this module. "
|
||||
"see http://python-consul.readthedocs.org/en/latest/#installation")
|
||||
|
||||
|
||||
def main():
|
||||
argument_spec = dict(
|
||||
checks=dict(default=None, required=False, type='list'),
|
||||
delay=dict(required=False,type='int', default='15'),
|
||||
behavior=dict(required=False,type='str', default='release',
|
||||
choices=['release', 'delete']),
|
||||
host=dict(default='localhost'),
|
||||
port=dict(default=8500, type='int'),
|
||||
scheme=dict(required=False, default='http'),
|
||||
validate_certs=dict(required=False, default=True),
|
||||
id=dict(required=False),
|
||||
name=dict(required=False),
|
||||
node=dict(required=False),
|
||||
state=dict(default='present',
|
||||
choices=['present', 'absent', 'info', 'node', 'list']),
|
||||
datacenter=dict(required=False)
|
||||
checks=dict(type='list'),
|
||||
delay=dict(type='int', default='15'),
|
||||
behavior=dict(type='str', default='release', choices=['release', 'delete']),
|
||||
host=dict(type='str', default='localhost'),
|
||||
port=dict(type='int', default=8500),
|
||||
scheme=dict(type='str', default='http'),
|
||||
validate_certs=dict(type='bool', default=True),
|
||||
id=dict(type='str'),
|
||||
name=dict(type='str'),
|
||||
node=dict(type='str'),
|
||||
state=dict(type='str', default='present', choices=['absent', 'info', 'list', 'node', 'present']),
|
||||
datacenter=dict(type='str'),
|
||||
)
|
||||
|
||||
module = AnsibleModule(argument_spec, supports_check_mode=False)
|
||||
|
@ -270,7 +260,7 @@ def main():
|
|||
execute(module)
|
||||
except ConnectionError as e:
|
||||
module.fail_json(msg='Could not connect to consul agent at %s:%s, error was %s' % (
|
||||
module.params.get('host'), module.params.get('port'), str(e)))
|
||||
module.params.get('host'), module.params.get('port'), e))
|
||||
except Exception as e:
|
||||
module.fail_json(msg=str(e))
|
||||
|
||||
|
|
|
@ -109,7 +109,6 @@ lib/ansible/modules/cloud/webfaction/webfaction_app.py
|
|||
lib/ansible/modules/cloud/webfaction/webfaction_db.py
|
||||
lib/ansible/modules/cloud/webfaction/webfaction_domain.py
|
||||
lib/ansible/modules/cloud/webfaction/webfaction_site.py
|
||||
lib/ansible/modules/clustering/consul_session.py
|
||||
lib/ansible/modules/database/misc/kibana_plugin.py
|
||||
lib/ansible/modules/database/misc/riak.py
|
||||
lib/ansible/modules/database/mongodb/mongodb_parameter.py
|
||||
|
|
Loading…
Reference in a new issue