From 72fe304b8f4f4987a5c142200df0e85b5fb2f0d5 Mon Sep 17 00:00:00 2001 From: Felix Fontein Date: Tue, 19 May 2020 23:06:40 +0200 Subject: [PATCH] gcp_storage_file: error gracefully when google.cloud is not there (#372) * Error gracefully when google.cloud is not there. * Add changelog fragment. --- changelogs/fragments/372-gcp_storage_file-gracefully.yml | 2 ++ plugins/lookup/gcp_storage_file.py | 9 ++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/372-gcp_storage_file-gracefully.yml diff --git a/changelogs/fragments/372-gcp_storage_file-gracefully.yml b/changelogs/fragments/372-gcp_storage_file-gracefully.yml new file mode 100644 index 0000000000..0ad49cd79f --- /dev/null +++ b/changelogs/fragments/372-gcp_storage_file-gracefully.yml @@ -0,0 +1,2 @@ +bugfixes: +- "gcp_storage_file lookup - die gracefully when the ``google.cloud`` collection is not installed, or changed in an incompatible way." diff --git a/plugins/lookup/gcp_storage_file.py b/plugins/lookup/gcp_storage_file.py index d73ebd2cc9..1ab4a60c1f 100644 --- a/plugins/lookup/gcp_storage_file.py +++ b/plugins/lookup/gcp_storage_file.py @@ -48,7 +48,12 @@ import requests from ansible.errors import AnsibleError from ansible.plugins.lookup import LookupBase from ansible.utils.display import Display -from ansible_collections.google.cloud.plugins.module_utils.gcp_utils import navigate_hash, GcpSession + +try: + from ansible_collections.google.cloud.plugins.module_utils.gcp_utils import navigate_hash, GcpSession + HAS_GOOGLE_CLOUD_COLLECTION = True +except ImportError: + HAS_GOOGLE_CLOUD_COLLECTION = False display = Display() @@ -135,4 +140,6 @@ class GcpFileLookup(): class LookupModule(LookupBase): def run(self, terms, variables=None, **kwargs): + if not HAS_GOOGLE_CLOUD_COLLECTION: + raise AnsibleError("community.general.gcp_storage_files needs a supported version of the google.cloud collection installed") return GcpFileLookup().run(terms, variables=variables, **kwargs)