updated plugsins based on feedback, fixed linting and documentation errors.

This commit is contained in:
Dave Costakos 2023-07-14 10:31:52 -07:00
parent 375b317692
commit 3ce29db3ee
No known key found for this signature in database
GPG key ID: C4DC31A1B32AC45C

View file

@ -116,11 +116,9 @@ RETURN = '''
# Imports # Imports
################################################################################ ################################################################################
import json
import os import os
import base64 import base64
from ansible.plugins.lookup import LookupBase from ansible.plugins.lookup import LookupBase
from ansible.errors import AnsibleError from ansible.errors import AnsibleError
from ansible.utils.display import Display from ansible.utils.display import Display
@ -139,12 +137,11 @@ try:
except ImportError: except ImportError:
HAS_GOOGLE_CLOUD_COLLECTION = False HAS_GOOGLE_CLOUD_COLLECTION = False
from ansible.errors import AnsibleError
from ansible.utils.display import Display
class GcpLookupException(Exception): class GcpLookupException(Exception):
pass pass
class GcpMockModule(object): class GcpMockModule(object):
def __init__(self, params): def __init__(self, params):
self.params = params self.params = params
@ -158,20 +155,23 @@ class GcpMockModule(object):
except getattr(requests.exceptions, "RequestException"): except getattr(requests.exceptions, "RequestException"):
self.fail_json(msg="GCP returned error: %s" % response.json()) self.fail_json(msg="GCP returned error: %s" % response.json())
class LookupModule(LookupBase): class LookupModule(LookupBase):
def run(self, terms=None, variables=None, **kwargs): def run(self, terms=None, variables=None, **kwargs):
self._display = Display() self._display = Display()
if not HAS_GOOGLE_CLOUD_COLLECTION: if not HAS_GOOGLE_CLOUD_COLLECTION:
raise AnsibleError( raise AnsibleError(
"gcp_secret lookup needs a supported version of the google.cloud collection installed. Use `ansible-galaxy collection install google.cloud` to install it" """gcp_secret lookup needs a supported version of the google.cloud
) collection installed. Use `ansible-galaxy collection install google.cloud`
to install it"""
)
self.set_options(var_options=variables, direct=kwargs) self.set_options(var_options=variables, direct=kwargs)
params = { params = {
"key": self.get_option("key"), "key": self.get_option("key"),
"version": self.get_option("version"), "version": self.get_option("version"),
"access_token": self.get_option("access_token"), "access_token": self.get_option("access_token"),
"scopes": self.get_option("scopes"), "scopes": self.get_option("scopes"),
"on_error": self.get_option("on_error") "on_error": self.get_option("on_error")
} }
params['name'] = params['key'] params['name'] = params['key']
@ -194,7 +194,6 @@ class LookupModule(LookupBase):
self.set_option(arg, os.environ[env_name]) self.set_option(arg, os.environ[env_name])
return self.get_option(arg) return self.get_option(arg)
# set version to the latest version because # set version to the latest version because
# we can't be sure that "latest" is always going # we can't be sure that "latest" is always going
# to be set if secret versions get disabled # to be set if secret versions get disabled
@ -213,7 +212,6 @@ class LookupModule(LookupBase):
else: else:
self.raise_error(module, f"Unable to list secret versions via {response.request.url}: {response.json()}") self.raise_error(module, f"Unable to list secret versions via {response.request.url}: {response.json()}")
def raise_error(self, module, msg): def raise_error(self, module, msg):
if module.params['on_error'] == 'strict': if module.params['on_error'] == 'strict':
raise GcpLookupException(msg) raise GcpLookupException(msg)
@ -243,10 +241,3 @@ class LookupModule(LookupBase):
return '' return ''
return response.json()['payload']['data'] return response.json()['payload']['data']