From 36eff2fd995fe4fef8830edda8dc7087fc69d015 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Wed, 8 Mar 2023 13:58:39 +0100 Subject: [PATCH] [PR #6114/6cf67448 backport][stable-6] memset*.py: Fixed URLError handling (#6157) memset*.py: Fixed URLError handling (#6114) * memset.py: Added URLError exception and stderr to Response() object * memset_*.py: Check response.status_code and response.stderr * Added changelog fragment * memset.py: Fixed pep8 * Renamed changelog fragment with .yml file extension (cherry picked from commit 6cf674485ff85c46ce7fd1464ac4f71a22e93aa0) Co-authored-by: Daniel Patrick --- changelogs/fragments/6114-memset-add-url-error-handling.yml | 2 ++ plugins/module_utils/memset.py | 5 +++++ plugins/modules/memset_dns_reload.py | 5 ++++- plugins/modules/memset_memstore_info.py | 5 ++++- plugins/modules/memset_server_info.py | 5 ++++- plugins/modules/memset_zone.py | 3 +++ plugins/modules/memset_zone_domain.py | 5 ++++- plugins/modules/memset_zone_record.py | 5 ++++- 8 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 changelogs/fragments/6114-memset-add-url-error-handling.yml diff --git a/changelogs/fragments/6114-memset-add-url-error-handling.yml b/changelogs/fragments/6114-memset-add-url-error-handling.yml new file mode 100644 index 0000000000..25dd869d19 --- /dev/null +++ b/changelogs/fragments/6114-memset-add-url-error-handling.yml @@ -0,0 +1,2 @@ +bugfixes: + - "memset - fix memset urlerror handling (https://github.com/ansible-collections/community.general/pull/6114)." diff --git a/plugins/module_utils/memset.py b/plugins/module_utils/memset.py index 671a8de308..374b40ff44 100644 --- a/plugins/module_utils/memset.py +++ b/plugins/module_utils/memset.py @@ -26,6 +26,7 @@ class Response(object): def __init__(self): self.content = None self.status_code = None + self.stderr = None def json(self): return json.loads(self.content) @@ -75,6 +76,10 @@ def memset_api_call(api_key, api_method, payload=None): msg = "Memset API returned a {0} response ({1}, {2})." . format(response.status_code, response.json()['error_type'], response.json()['error']) else: msg = "Memset API returned an error ({0}, {1})." . format(response.json()['error_type'], response.json()['error']) + except urllib_error.URLError as e: + has_failed = True + msg = "An URLError occured ({0})." . format(type(e)) + response.stderr = "{0}" . format(e) if msg is None: msg = response.json() diff --git a/plugins/modules/memset_dns_reload.py b/plugins/modules/memset_dns_reload.py index c69df6d783..a1168724f5 100644 --- a/plugins/modules/memset_dns_reload.py +++ b/plugins/modules/memset_dns_reload.py @@ -139,7 +139,10 @@ def reload_dns(args=None): # manifest themselves at this point so we need to ensure the user is # informed of the reason. retvals['failed'] = has_failed - retvals['memset_api'] = response.json() + if response.status_code is not None: + retvals['memset_api'] = response.json() + else: + retvals['stderr'] = response.stderr retvals['msg'] = msg return retvals diff --git a/plugins/modules/memset_memstore_info.py b/plugins/modules/memset_memstore_info.py index 4ee7cfc2a5..5fc9d79e1f 100644 --- a/plugins/modules/memset_memstore_info.py +++ b/plugins/modules/memset_memstore_info.py @@ -134,7 +134,10 @@ def get_facts(args=None): # informed of the reason. retvals['failed'] = has_failed retvals['msg'] = msg - retvals['stderr'] = "API returned an error: {0}" . format(response.status_code) + if response.status_code is not None: + retvals['stderr'] = "API returned an error: {0}" . format(response.status_code) + else: + retvals['stderr'] = "{0}" . format(response.stderr) return retvals # we don't want to return the same thing twice diff --git a/plugins/modules/memset_server_info.py b/plugins/modules/memset_server_info.py index 9023c0f8f0..ecc0375eb1 100644 --- a/plugins/modules/memset_server_info.py +++ b/plugins/modules/memset_server_info.py @@ -259,7 +259,10 @@ def get_facts(args=None): # informed of the reason. retvals['failed'] = has_failed retvals['msg'] = msg - retvals['stderr'] = "API returned an error: {0}" . format(response.status_code) + if response.status_code is not None: + retvals['stderr'] = "API returned an error: {0}" . format(response.status_code) + else: + retvals['stderr'] = "{0}" . format(response.stderr) return retvals # we don't want to return the same thing twice diff --git a/plugins/modules/memset_zone.py b/plugins/modules/memset_zone.py index 738d9e16ba..e17472e395 100644 --- a/plugins/modules/memset_zone.py +++ b/plugins/modules/memset_zone.py @@ -264,6 +264,9 @@ def create_or_delete(args=None): retvals['failed'] = _has_failed retvals['msg'] = _msg + if response.stderr is not None: + retvals['stderr'] = response.stderr + return retvals zone_exists, _msg, counter, _zone_id = get_zone_id(zone_name=args['name'], current_zones=response.json()) diff --git a/plugins/modules/memset_zone_domain.py b/plugins/modules/memset_zone_domain.py index 26276f68a5..172a48be25 100644 --- a/plugins/modules/memset_zone_domain.py +++ b/plugins/modules/memset_zone_domain.py @@ -195,7 +195,10 @@ def create_or_delete_domain(args=None): # informed of the reason. retvals['failed'] = has_failed retvals['msg'] = msg - retvals['stderr'] = "API returned an error: {0}" . format(response.status_code) + if response.status_code is not None: + retvals['stderr'] = "API returned an error: {0}" . format(response.status_code) + else: + retvals['stderr'] = response.stderr return retvals zone_exists, msg, counter, zone_id = get_zone_id(zone_name=args['zone'], current_zones=response.json()) diff --git a/plugins/modules/memset_zone_record.py b/plugins/modules/memset_zone_record.py index 021ad461d2..4e56a11caf 100644 --- a/plugins/modules/memset_zone_record.py +++ b/plugins/modules/memset_zone_record.py @@ -313,7 +313,10 @@ def create_or_delete(args=None): # informed of the reason. retvals['failed'] = _has_failed retvals['msg'] = msg - retvals['stderr'] = "API returned an error: {0}" . format(response.status_code) + if response.status_code is not None: + retvals['stderr'] = "API returned an error: {0}" . format(response.status_code) + else: + retvals['stderr'] = response.stderr return retvals zone_exists, _msg, counter, zone_id = get_zone_id(zone_name=args['zone'], current_zones=response.json())