redis: PEP8 compliancy and doc fixes (#30920)

This PR includes:
- PEP8 compliancy fixes
- Documentation fixes
This commit is contained in:
Dag Wieers 2017-09-27 08:12:06 +02:00 committed by ansibot
parent 125c4b135d
commit 91bb211922
2 changed files with 57 additions and 100 deletions

View file

@ -1,90 +1,72 @@
#!/usr/bin/python #!/usr/bin/python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
#
# Copyright: Ansible Project # Copyright: Ansible Project
# 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: redis module: redis
short_description: Various redis commands, slave and flush short_description: Various redis commands, slave and flush
description: description:
- Unified utility to interact with redis instances. - Unified utility to interact with redis instances.
'slave' sets a redis instance in slave or master mode.
'flush' flushes all the instance or a specified db.
'config' (new in 1.6), ensures a configuration setting on an instance.
version_added: "1.3" version_added: "1.3"
options: options:
command: command:
description: description:
- The selected redis command - The selected redis command
- C(config) (new in 1.6), ensures a configuration setting on an instance.
- C(flush) flushes all the instance or a specified db.
- C(slave) sets a redis instance in slave or master mode.
required: true required: true
default: null choices: [ config, flush, slave ]
choices: [ "slave", "flush", "config" ]
login_password: login_password:
description: description:
- The password used to authenticate with (usually not used) - The password used to authenticate with (usually not used)
required: false
default: null
login_host: login_host:
description: description:
- The host running the database - The host running the database
required: false
default: localhost default: localhost
login_port: login_port:
description: description:
- The port to connect to - The port to connect to
required: false
default: 6379 default: 6379
master_host: master_host:
description: description:
- The host of the master instance [slave command] - The host of the master instance [slave command]
required: false
default: null default: null
master_port: master_port:
description: description:
- The port of the master instance [slave command] - The port of the master instance [slave command]
required: false
default: null
slave_mode: slave_mode:
description: description:
- the mode of the redis instance [slave command] - the mode of the redis instance [slave command]
required: false
default: slave default: slave
choices: [ "master", "slave" ] choices: [ master, slave ]
db: db:
description: description:
- The database to flush (used in db mode) [flush command] - The database to flush (used in db mode) [flush command]
required: false
default: null
flush_mode: flush_mode:
description: description:
- Type of flush (all the dbs in a redis instance or a specific one) - Type of flush (all the dbs in a redis instance or a specific one)
[flush command] [flush command]
required: false
default: all default: all
choices: [ "all", "db" ] choices: [ all, db ]
name: name:
version_added: 1.6
description: description:
- A redis config key. - A redis config key.
required: false
default: null
value:
version_added: 1.6 version_added: 1.6
value:
description: description:
- A redis config value. - A redis config value.
required: false version_added: 1.6
default: null
notes: notes:
@ -99,40 +81,41 @@ author: "Xabier Larrakoetxea (@slok)"
''' '''
EXAMPLES = ''' EXAMPLES = '''
# Set local redis instance to be slave of melee.island on port 6377 - name: Set local redis instance to be slave of melee.island on port 6377
- redis: redis:
command: slave command: slave
master_host: melee.island master_host: melee.island
master_port: 6377 master_port: 6377
# Deactivate slave mode - name: Deactivate slave mode
- redis: redis:
command: slave command: slave
slave_mode: master slave_mode: master
# Flush all the redis db - name: Flush all the redis db
- redis: redis:
command: flush command: flush
flush_mode: all flush_mode: all
# Flush only one db in a redis instance - name: Flush only one db in a redis instance
- redis: redis:
command: flush command: flush
db: 1 db: 1
flush_mode: db flush_mode: db
# Configure local redis to have 10000 max clients - name: Configure local redis to have 10000 max clients
- redis: redis:
command: config command: config
name: maxclients name: maxclients
value: 10000 value: 10000
# Configure local redis to have lua time limit of 100 ms - name: Configure local redis to have lua time limit of 100 ms
- redis: redis:
command: config command: config
name: lua-time-limit name: lua-time-limit
value: 100 value: 100
''' '''
import traceback import traceback
try: try:
@ -146,10 +129,7 @@ from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils._text import to_native from ansible.module_utils._text import to_native
# ===========================================
# Redis module specific support methods. # Redis module specific support methods.
#
def set_slave_mode(client, master_host, master_port): def set_slave_mode(client, master_host, master_port):
try: try:
return client.slaveof(master_host, master_port) return client.slaveof(master_host, master_port)
@ -175,26 +155,23 @@ def flush(client, db=None):
return False return False
# ===========================================
# Module execution. # Module execution.
#
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec = dict( argument_spec=dict(
command=dict(default=None, choices=['slave', 'flush', 'config']), command=dict(type='str', choices=['config', 'flush', 'slave']),
login_password=dict(default=None, no_log=True), login_password=dict(type='str', no_log=True),
login_host=dict(default='localhost'), login_host=dict(type='str', default='localhost'),
login_port=dict(default=6379, type='int'), login_port=dict(type='int', default=6379),
master_host=dict(default=None), master_host=dict(type='str'),
master_port=dict(default=None, type='int'), master_port=dict(type='int'),
slave_mode=dict(default='slave', choices=['master', 'slave']), slave_mode=dict(type='str', default='slave', choices=['master', 'slave']),
db=dict(default=None, type='int'), db=dict(type='int'),
flush_mode=dict(default='all', choices=['all', 'db']), flush_mode=dict(type='str', default='all', choices=['all', 'db']),
name=dict(default=None), name=dict(type='str'),
value=dict(default=None) value=dict(type='str')
), ),
supports_check_mode = True supports_check_mode=True,
) )
if not redis_found: if not redis_found:
@ -211,40 +188,32 @@ def main():
master_port = module.params['master_port'] master_port = module.params['master_port']
mode = module.params['slave_mode'] mode = module.params['slave_mode']
#Check if we have all the data # Check if we have all the data
if mode == "slave": # Only need data if we want to be slave if mode == "slave": # Only need data if we want to be slave
if not master_host: if not master_host:
module.fail_json( module.fail_json(msg='In slave mode master host must be provided')
msg='In slave mode master host must be provided')
if not master_port: if not master_port:
module.fail_json( module.fail_json(msg='In slave mode master port must be provided')
msg='In slave mode master port must be provided')
#Connect and check # Connect and check
r = redis.StrictRedis(host=login_host, r = redis.StrictRedis(host=login_host, port=login_port, password=login_password)
port=login_port,
password=login_password)
try: try:
r.ping() r.ping()
except Exception as e: except Exception as e:
module.fail_json(msg="unable to connect to database: %s" % to_native(e), module.fail_json(msg="unable to connect to database: %s" % to_native(e), exception=traceback.format_exc())
exception=traceback.format_exc())
#Check if we are already in the mode that we want # Check if we are already in the mode that we want
info = r.info() info = r.info()
if mode == "master" and info["role"] == "master": if mode == "master" and info["role"] == "master":
module.exit_json(changed=False, mode=mode) module.exit_json(changed=False, mode=mode)
elif mode == "slave" and\ elif mode == "slave" and info["role"] == "slave" and info["master_host"] == master_host and info["master_port"] == master_port:
info["role"] == "slave" and\ status = dict(
info["master_host"] == master_host and\ status=mode,
info["master_port"] == master_port: master_host=master_host,
status = { master_port=master_port,
'status': mode, )
'master_host': master_host,
'master_port': master_port,
}
module.exit_json(changed=False, mode=status) module.exit_json(changed=False, mode=status)
else: else:
# Do the stuff # Do the stuff
@ -274,22 +243,17 @@ def main():
db = module.params['db'] db = module.params['db']
mode = module.params['flush_mode'] mode = module.params['flush_mode']
#Check if we have all the data # Check if we have all the data
if mode == "db": if mode == "db":
if db is None: if db is None:
module.fail_json( module.fail_json(msg="In db mode the db number must be provided")
msg="In db mode the db number must be provided")
#Connect and check # Connect and check
r = redis.StrictRedis(host=login_host, r = redis.StrictRedis(host=login_host, port=login_port, password=login_password, db=db)
port=login_port,
password=login_password,
db=db)
try: try:
r.ping() r.ping()
except Exception as e: except Exception as e:
module.fail_json(msg="unable to connect to database: %s" % to_native(e), module.fail_json(msg="unable to connect to database: %s" % to_native(e), exception=traceback.format_exc())
exception=traceback.format_exc())
# Do the stuff # Do the stuff
# (Check Check_mode before commands so the commands aren't evaluated # (Check Check_mode before commands so the commands aren't evaluated
@ -309,22 +273,17 @@ def main():
name = module.params['name'] name = module.params['name']
value = module.params['value'] value = module.params['value']
r = redis.StrictRedis(host=login_host, r = redis.StrictRedis(host=login_host, port=login_port, password=login_password)
port=login_port,
password=login_password)
try: try:
r.ping() r.ping()
except Exception as e: except Exception as e:
module.fail_json(msg="unable to connect to database: %s" % to_native(e), module.fail_json(msg="unable to connect to database: %s" % to_native(e), exception=traceback.format_exc())
exception=traceback.format_exc())
try: try:
old_value = r.config_get(name)[name] old_value = r.config_get(name)[name]
except Exception as e: except Exception as e:
module.fail_json(msg="unable to read config: %s" % to_native(e), module.fail_json(msg="unable to read config: %s" % to_native(e), exception=traceback.format_exc())
exception=traceback.format_exc())
changed = old_value != value changed = old_value != value
if module.check_mode or not changed: if module.check_mode or not changed:
@ -333,8 +292,7 @@ def main():
try: try:
r.config_set(name, value) r.config_set(name, value)
except Exception as e: except Exception as e:
module.fail_json(msg="unable to write config: %s" % to_native(e), module.fail_json(msg="unable to write config: %s" % to_native(e), exception=traceback.format_exc())
exception=traceback.format_exc())
module.exit_json(changed=changed, name=name, value=value) module.exit_json(changed=changed, name=name, value=value)
else: else:
module.fail_json(msg='A valid command must be provided') module.fail_json(msg='A valid command must be provided')

View file

@ -180,7 +180,6 @@ lib/ansible/modules/clustering/consul_session.py
lib/ansible/modules/clustering/kubernetes.py lib/ansible/modules/clustering/kubernetes.py
lib/ansible/modules/clustering/pacemaker_cluster.py lib/ansible/modules/clustering/pacemaker_cluster.py
lib/ansible/modules/database/misc/kibana_plugin.py lib/ansible/modules/database/misc/kibana_plugin.py
lib/ansible/modules/database/misc/redis.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
lib/ansible/modules/database/mongodb/mongodb_user.py lib/ansible/modules/database/mongodb/mongodb_user.py