consul_session: PEP8 compliancy and documentation changes (#33000)

THis PR includes:
- PEP8 compliancy changes
- Documentation changes
This commit is contained in:
Dag Wieers 2017-11-17 21:48:36 +01:00 committed by René Moser
parent c94d57311c
commit b3e4a88b90
2 changed files with 49 additions and 60 deletions

View file

@ -1,107 +1,92 @@
#!/usr/bin/python #!/usr/bin/python
# -*- coding: utf-8 -*- # -*- 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) # 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 from __future__ import absolute_import, division, print_function
__metaclass__ = type __metaclass__ = type
ANSIBLE_METADATA = {'metadata_version': '1.1', ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'], 'status': ['preview'],
'supported_by': 'community'} 'supported_by': 'community'}
DOCUMENTATION = """ DOCUMENTATION = """
module: consul_session module: consul_session
short_description: "manipulate consul sessions" short_description: Manipulate consul sessions
description: 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 cluster. These sessions can then be used in conjunction with key value pairs
to implement distributed locks. In depth documentation for working with 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: requirements:
- "python >= 2.6" - python >= 2.6
- python-consul - python-consul
- requests - requests
version_added: "2.0" version_added: "2.0"
author: "Steve Gargan @sgargan" author:
- Steve Gargan (@sgargan)
options: options:
state: state:
description: 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 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 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 required to remove the session. Info for a single session, all the
sessions for a node or all available sessions can be retrieved by 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 specifying info, node or list for the state; for node or info, the
node name or session id is required as parameter. node name or session id is required as parameter.
required: false choices: [ absent, info, list, node, present ]
choices: ['present', 'absent', 'info', 'node', 'list']
default: present default: present
name: name:
description: 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. to Consul and not required.
required: false
default: None
delay: delay:
description: 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 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 default: 15
required: false
node: node:
description: 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. by default this is the name of the agent.
required: false
default: None
datacenter: datacenter:
description: 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. created.
required: false
default: None
checks: checks:
description: 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 all the checks fail, the session will be invalidated and any locks
associated with the session will be release and can be acquired once associated with the session will be release and can be acquired once
the associated lock delay has expired. the associated lock delay has expired.
required: false
default: None
host: host:
description: description:
- host of the consul agent defaults to localhost - The host of the consul agent defaults to localhost.
required: false
default: localhost default: localhost
port: port:
description: description:
- the port on which the consul agent is running - The port on which the consul agent is running.
required: false
default: 8500 default: 8500
scheme: scheme:
description: description:
- the protocol scheme on which the consul agent is running - The protocol scheme on which the consul agent is running.
required: false
default: http default: http
version_added: "2.1" version_added: "2.1"
validate_certs: validate_certs:
description: description:
- whether to verify the tls certificate of the consul agent - Whether to verify the tls certificate of the consul agent.
required: false type: bool
default: True default: True
version_added: "2.1" version_added: "2.1"
behavior: behavior:
description: description:
- the optional behavior that can be attached to the session when it - The optional behavior that can be attached to the session when it
is created. This can be set to either 'release' or 'delete'. This is created. This controls the behavior when a session is invalidated.
controls the behavior when a session is invalidated. choices: [ delete, release ]
default: release default: release
required: false
version_added: "2.2" version_added: "2.2"
""" """
@ -122,10 +107,13 @@ EXAMPLES = '''
delay: 20s delay: 20s
- name: retrieve info about session by id - 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 - name: retrieve active sessions
consul_session: state=list consul_session:
state: list
''' '''
try: try:
@ -149,6 +137,7 @@ def execute(module):
else: else:
remove_session(module) remove_session(module)
def lookup_sessions(module): def lookup_sessions(module):
datacenter = module.params.get('datacenter') datacenter = module.params.get('datacenter')
@ -158,7 +147,7 @@ def lookup_sessions(module):
try: try:
if state == 'list': if state == 'list':
sessions_list = consul_client.session.list(dc=datacenter) 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]: if sessions_list and sessions_list[1]:
sessions_list = sessions_list[1] sessions_list = sessions_list[1]
module.exit_json(changed=True, 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" % ( module.fail_json(msg="Could not remove session with id '%s' %s" % (
session_id, e)) session_id, e))
def get_consul_api(module): def get_consul_api(module):
return consul.Consul(host=module.params.get('host'), return consul.Consul(host=module.params.get('host'),
port=module.params.get('port')) port=module.params.get('port'))
def test_dependencies(module): def test_dependencies(module):
if not python_consul_installed: 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") "see http://python-consul.readthedocs.org/en/latest/#installation")
def main(): def main():
argument_spec = dict( argument_spec = dict(
checks=dict(default=None, required=False, type='list'), checks=dict(type='list'),
delay=dict(required=False,type='int', default='15'), delay=dict(type='int', default='15'),
behavior=dict(required=False,type='str', default='release', behavior=dict(type='str', default='release', choices=['release', 'delete']),
choices=['release', 'delete']), host=dict(type='str', default='localhost'),
host=dict(default='localhost'), port=dict(type='int', default=8500),
port=dict(default=8500, type='int'), scheme=dict(type='str', default='http'),
scheme=dict(required=False, default='http'), validate_certs=dict(type='bool', default=True),
validate_certs=dict(required=False, default=True), id=dict(type='str'),
id=dict(required=False), name=dict(type='str'),
name=dict(required=False), node=dict(type='str'),
node=dict(required=False), state=dict(type='str', default='present', choices=['absent', 'info', 'list', 'node', 'present']),
state=dict(default='present', datacenter=dict(type='str'),
choices=['present', 'absent', 'info', 'node', 'list']),
datacenter=dict(required=False)
) )
module = AnsibleModule(argument_spec, supports_check_mode=False) module = AnsibleModule(argument_spec, supports_check_mode=False)
@ -270,7 +260,7 @@ def main():
execute(module) execute(module)
except ConnectionError as e: except ConnectionError as e:
module.fail_json(msg='Could not connect to consul agent at %s:%s, error was %s' % ( 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: except Exception as e:
module.fail_json(msg=str(e)) module.fail_json(msg=str(e))

View file

@ -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_db.py
lib/ansible/modules/cloud/webfaction/webfaction_domain.py lib/ansible/modules/cloud/webfaction/webfaction_domain.py
lib/ansible/modules/cloud/webfaction/webfaction_site.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/kibana_plugin.py
lib/ansible/modules/database/misc/riak.py lib/ansible/modules/database/misc/riak.py
lib/ansible/modules/database/mongodb/mongodb_parameter.py lib/ansible/modules/database/mongodb/mongodb_parameter.py