1
0
Fork 0
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:
fkuep 2021-06-03 12:08:14 +02:00
parent 1d57374c3e
commit 748be8e366
2 changed files with 41 additions and 18 deletions

View file

@ -1,5 +1,5 @@
--- ---
bugfixes: bugfixes:
- consul_kv lookup - use ``self.get_option`` and - 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). (https://github.com/ansible-collections/community.general/issues/2124).

View file

@ -2,6 +2,11 @@
# (c) 2017 Ansible Project # (c) 2017 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)
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 __metaclass__ = type
@ -102,11 +107,6 @@ RETURN = """
type: dict 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: try:
import consul import consul
@ -126,6 +126,7 @@ class LookupModule(LookupBase):
# get options # get options
self.set_options(direct=kwargs) self.set_options(direct=kwargs)
scheme = self.get_option('scheme') scheme = self.get_option('scheme')
host = self.get_option('host') host = self.get_option('host')
port = self.get_option('port') port = self.get_option('port')
@ -137,10 +138,6 @@ class LookupModule(LookupBase):
host = u.hostname host = u.hostname
if u.port is not None: if u.port is not None:
port = u.port 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') validate_certs = self.get_option('validate_certs')
client_cert = self.get_option('client_cert') client_cert = self.get_option('client_cert')
@ -148,14 +145,15 @@ class LookupModule(LookupBase):
values = [] values = []
try: try:
for term in terms: for term in terms:
key = term.split(' ')[0] params = self.parse_params(term)
consul_api = consul.Consul(host=host, port=port, scheme=scheme, token=token, consul_api = consul.Consul(
dc=datacenter, verify=validate_certs, cert=client_cert) host=host, port=port, scheme=scheme, verify=validate_certs, cert=client_cert)
results = consul_api.kv.get(key, results = consul_api.kv.get(params['key'],
index=index, token=params['token'],
recurse=recurse, index=params['index'],
) recurse=params['recurse'],
dc=params['datacenter'])
if results[1]: if results[1]:
# responds with a single or list of result maps # responds with a single or list of result maps
if isinstance(results[1], list): if isinstance(results[1], list):
@ -165,6 +163,31 @@ class LookupModule(LookupBase):
values.append(to_text(results[1]['Value'])) values.append(to_text(results[1]['Value']))
except Exception as e: except Exception as e:
raise AnsibleError( 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 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