mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
redis cache - better parsing of connection uri (#2579)
* better parsing of connection uri * added changelog fragment * fixed tests for ansible 2.9 * Update tests/unit/plugins/cache/test_redis.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/cache/test_redis.py Co-authored-by: Felix Fontein <felix@fontein.de> * Adjustments from PR * Update test_redis.py * Update test_redis.py * Update plugins/cache/redis.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update plugins/cache/redis.py Co-authored-by: Felix Fontein <felix@fontein.de> * Update tests/unit/plugins/cache/test_redis.py Co-authored-by: Felix Fontein <felix@fontein.de> Co-authored-by: Felix Fontein <felix@fontein.de>
This commit is contained in:
parent
aa74cf4d61
commit
4764a5deba
3 changed files with 28 additions and 3 deletions
2
changelogs/fragments/2579-redis-cache-ipv6.yml
Normal file
2
changelogs/fragments/2579-redis-cache-ipv6.yml
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
bugfixes:
|
||||||
|
- redis cache - improved connection string parsing (https://github.com/ansible-collections/community.general/issues/497).
|
14
plugins/cache/redis.py
vendored
14
plugins/cache/redis.py
vendored
|
@ -61,6 +61,7 @@ DOCUMENTATION = '''
|
||||||
type: integer
|
type: integer
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
import re
|
||||||
import time
|
import time
|
||||||
import json
|
import json
|
||||||
|
|
||||||
|
@ -91,6 +92,8 @@ class CacheModule(BaseCacheModule):
|
||||||
performance.
|
performance.
|
||||||
"""
|
"""
|
||||||
_sentinel_service_name = None
|
_sentinel_service_name = None
|
||||||
|
re_url_conn = re.compile(r'^([^:]+|\[[^]]+\]):(\d+):(\d+)(?::(.*))?$')
|
||||||
|
re_sent_conn = re.compile(r'^(.*):(\d+)$')
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
uri = ''
|
uri = ''
|
||||||
|
@ -130,11 +133,18 @@ class CacheModule(BaseCacheModule):
|
||||||
self._db = self._get_sentinel_connection(uri, kw)
|
self._db = self._get_sentinel_connection(uri, kw)
|
||||||
# normal connection
|
# normal connection
|
||||||
else:
|
else:
|
||||||
connection = uri.split(':')
|
connection = self._parse_connection(self.re_url_conn, uri)
|
||||||
self._db = StrictRedis(*connection, **kw)
|
self._db = StrictRedis(*connection, **kw)
|
||||||
|
|
||||||
display.vv('Redis connection: %s' % self._db)
|
display.vv('Redis connection: %s' % self._db)
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _parse_connection(re_patt, uri):
|
||||||
|
match = re_patt.match(uri)
|
||||||
|
if not match:
|
||||||
|
raise AnsibleError("Unable to parse connection string")
|
||||||
|
return match.groups()
|
||||||
|
|
||||||
def _get_sentinel_connection(self, uri, kw):
|
def _get_sentinel_connection(self, uri, kw):
|
||||||
"""
|
"""
|
||||||
get sentinel connection details from _uri
|
get sentinel connection details from _uri
|
||||||
|
@ -158,7 +168,7 @@ class CacheModule(BaseCacheModule):
|
||||||
except IndexError:
|
except IndexError:
|
||||||
pass # password is optional
|
pass # password is optional
|
||||||
|
|
||||||
sentinels = [tuple(shost.split(':')) for shost in connections]
|
sentinels = [self._parse_connection(self.re_sent_conn, shost) for shost in connections]
|
||||||
display.vv('\nUsing redis sentinels: %s' % sentinels)
|
display.vv('\nUsing redis sentinels: %s' % sentinels)
|
||||||
scon = Sentinel(sentinels, **kw)
|
scon = Sentinel(sentinels, **kw)
|
||||||
try:
|
try:
|
||||||
|
|
15
tests/unit/plugins/cache/test_redis.py
vendored
15
tests/unit/plugins/cache/test_redis.py
vendored
|
@ -23,10 +23,23 @@ import pytest
|
||||||
|
|
||||||
pytest.importorskip('redis')
|
pytest.importorskip('redis')
|
||||||
|
|
||||||
|
from ansible import constants as C
|
||||||
from ansible.plugins.loader import cache_loader
|
from ansible.plugins.loader import cache_loader
|
||||||
|
from ansible.release import __version__ as ansible_version
|
||||||
from ansible_collections.community.general.plugins.cache.redis import CacheModule as RedisCache
|
from ansible_collections.community.general.plugins.cache.redis import CacheModule as RedisCache
|
||||||
|
|
||||||
|
|
||||||
def test_redis_cachemodule():
|
def test_redis_cachemodule():
|
||||||
# The _uri option is required for the redis plugin
|
# The _uri option is required for the redis plugin
|
||||||
assert isinstance(cache_loader.get('community.general.redis', **{'_uri': '127.0.0.1:6379:1'}), RedisCache)
|
connection = '127.0.0.1:6379:1'
|
||||||
|
if ansible_version.startswith('2.9.'):
|
||||||
|
C.CACHE_PLUGIN_CONNECTION = connection
|
||||||
|
assert isinstance(cache_loader.get('community.general.redis', **{'_uri': connection}), RedisCache)
|
||||||
|
|
||||||
|
|
||||||
|
def test_redis_cachemodule():
|
||||||
|
# The _uri option is required for the redis plugin
|
||||||
|
connection = '[::1]:6379:1'
|
||||||
|
if ansible_version.startswith('2.9.'):
|
||||||
|
C.CACHE_PLUGIN_CONNECTION = connection
|
||||||
|
assert isinstance(cache_loader.get('community.general.redis', **{'_uri': connection}), RedisCache)
|
||||||
|
|
Loading…
Reference in a new issue