mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
try overwriting defaults in parse_params with get_option vals, instead of removing that function completely.
This commit is contained in:
parent
1d57374c3e
commit
748be8e366
2 changed files with 41 additions and 18 deletions
|
@ -1,5 +1,5 @@
|
|||
---
|
||||
bugfixes:
|
||||
- consul_kv lookup - use ``self.get_option`` and
|
||||
remove the function parse_params get key form self.terms
|
||||
remove the function ``parse_params()`` get key form ``self.terms``
|
||||
(https://github.com/ansible-collections/community.general/issues/2124).
|
||||
|
|
|
@ -2,6 +2,11 @@
|
|||
# (c) 2017 Ansible Project
|
||||
# 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 ansible.module_utils._text import to_text
|
||||
from ansible.plugins.lookup import LookupBase
|
||||
from ansible.errors import AnsibleError, AnsibleAssertionError
|
||||
from ansible.module_utils.six.moves.urllib.parse import urlparse
|
||||
import os
|
||||
|
||||
__metaclass__ = type
|
||||
|
||||
|
@ -102,11 +107,6 @@ RETURN = """
|
|||
type: dict
|
||||
"""
|
||||
|
||||
import os
|
||||
from ansible.module_utils.six.moves.urllib.parse import urlparse
|
||||
from ansible.errors import AnsibleError, AnsibleAssertionError
|
||||
from ansible.plugins.lookup import LookupBase
|
||||
from ansible.module_utils._text import to_text
|
||||
|
||||
try:
|
||||
import consul
|
||||
|
@ -126,6 +126,7 @@ class LookupModule(LookupBase):
|
|||
|
||||
# get options
|
||||
self.set_options(direct=kwargs)
|
||||
|
||||
scheme = self.get_option('scheme')
|
||||
host = self.get_option('host')
|
||||
port = self.get_option('port')
|
||||
|
@ -137,10 +138,6 @@ class LookupModule(LookupBase):
|
|||
host = u.hostname
|
||||
if u.port is not None:
|
||||
port = u.port
|
||||
token = self.get_option('token')
|
||||
datacenter = self.get_option('datacenter')
|
||||
recurse = self.get_option('recurse')
|
||||
index = self.get_option('index')
|
||||
|
||||
validate_certs = self.get_option('validate_certs')
|
||||
client_cert = self.get_option('client_cert')
|
||||
|
@ -148,14 +145,15 @@ class LookupModule(LookupBase):
|
|||
values = []
|
||||
try:
|
||||
for term in terms:
|
||||
key = term.split(' ')[0]
|
||||
consul_api = consul.Consul(host=host, port=port, scheme=scheme, token=token,
|
||||
dc=datacenter, verify=validate_certs, cert=client_cert)
|
||||
params = self.parse_params(term)
|
||||
consul_api = consul.Consul(
|
||||
host=host, port=port, scheme=scheme, verify=validate_certs, cert=client_cert)
|
||||
|
||||
results = consul_api.kv.get(key,
|
||||
index=index,
|
||||
recurse=recurse,
|
||||
)
|
||||
results = consul_api.kv.get(params['key'],
|
||||
token=params['token'],
|
||||
index=params['index'],
|
||||
recurse=params['recurse'],
|
||||
dc=params['datacenter'])
|
||||
if results[1]:
|
||||
# responds with a single or list of result maps
|
||||
if isinstance(results[1], list):
|
||||
|
@ -165,6 +163,31 @@ class LookupModule(LookupBase):
|
|||
values.append(to_text(results[1]['Value']))
|
||||
except Exception as e:
|
||||
raise AnsibleError(
|
||||
"Error locating '%s' in kv store. Error was %s" % (key, e))
|
||||
"Error locating '%s' in kv store. Error was %s" % (term, e))
|
||||
|
||||
return values
|
||||
|
||||
def parse_params(self, term):
|
||||
params = term.split(' ')
|
||||
|
||||
paramvals = {
|
||||
'key': params[0],
|
||||
'token': self.get_option('token'),
|
||||
'recurse': self.get_option('recurse'),
|
||||
'index': self.get_option('index'),
|
||||
'datacenter': self.get_option('datacenter')
|
||||
}
|
||||
|
||||
# parameters specified?
|
||||
try:
|
||||
for param in params[1:]:
|
||||
if param and len(param) > 0:
|
||||
name, value = param.split('=')
|
||||
if name not in paramvals:
|
||||
raise AnsibleAssertionError(
|
||||
"%s not a valid consul lookup parameter" % name)
|
||||
paramvals[name] = value
|
||||
except (ValueError, AssertionError) as e:
|
||||
raise AnsibleError(e)
|
||||
|
||||
return paramvals
|
||||
|
|
Loading…
Reference in a new issue