Skip to content

Commit

Permalink
Move parse_kev_term into the hashi_vault lookup ansible-collections#351
Browse files Browse the repository at this point in the history
  • Loading branch information
MainakSil committed Sep 7, 2024
1 parent d34fac6 commit fd2ffc0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
23 changes: 23 additions & 0 deletions plugins/lookup/hashi_vault.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,29 @@


class LookupModule(HashiVaultLookupBase):
def parse_kev_term(self, term, plugin_name, first_unqualified=None):
'''parses a term string into a dictionary'''
param_dict = {}

for i, param in enumerate(term.split()):
try:
key, value = param.split('=', 1)
except ValueError:
if i == 0 and first_unqualified is not None:
# allow first item to be specified as value only and assign to assumed option name
key = first_unqualified
value = param
else:
raise AnsibleError("%s lookup plugin needs key=value pairs, but received %s" % (plugin_name, term))

if key in param_dict:
msg = "Duplicate key '%s' in the term string '%s'." % (key, term)
raise AnsibleOptionsError(msg)

param_dict[key] = value

return param_dict

def run(self, terms, variables=None, **kwargs):
if not HAS_HVAC:
raise AnsibleError("Please pip install hvac to use the hashi_vault lookup module.")
Expand Down
23 changes: 0 additions & 23 deletions plugins/plugin_utils/_hashi_vault_lookup_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,3 @@ class HashiVaultLookupBase(HashiVaultPlugin, LookupBase):
def __init__(self, loader=None, templar=None, **kwargs):
HashiVaultPlugin.__init__(self)
LookupBase.__init__(self, loader=loader, templar=templar, **kwargs)

def parse_kev_term(self, term, plugin_name, first_unqualified=None):
'''parses a term string into a dictionary'''
param_dict = {}

for i, param in enumerate(term.split()):
try:
key, value = param.split('=', 1)
except ValueError:
if i == 0 and first_unqualified is not None:
# allow first item to be specified as value only and assign to assumed option name
key = first_unqualified
value = param
else:
raise AnsibleError("%s lookup plugin needs key=value pairs, but received %s" % (plugin_name, term))

if key in param_dict:
msg = "Duplicate key '%s' in the term string '%s'." % (key, term)
raise AnsibleOptionsError(msg)

param_dict[key] = value

return param_dict

0 comments on commit fd2ffc0

Please sign in to comment.