From afb16dc0a2a1455c7501b0eae53a3e22a1149216 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Wed, 8 Mar 2023 13:58:36 +0100 Subject: [PATCH] [PR #6114/6cf67448 backport][stable-5] memset*.py: Fixed URLError handling (#6156) 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/cloud/memset/memset_dns_reload.py | 5 ++++- plugins/modules/cloud/memset/memset_memstore_info.py | 5 ++++- plugins/modules/cloud/memset/memset_server_info.py | 5 ++++- plugins/modules/cloud/memset/memset_zone.py | 3 +++ plugins/modules/cloud/memset/memset_zone_domain.py | 5 ++++- plugins/modules/cloud/memset/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/cloud/memset/memset_dns_reload.py b/plugins/modules/cloud/memset/memset_dns_reload.py index 580740d1a3..d4f8ce08e9 100644 --- a/plugins/modules/cloud/memset/memset_dns_reload.py +++ b/plugins/modules/cloud/memset/memset_dns_reload.py @@ -132,7 +132,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/cloud/memset/memset_memstore_info.py b/plugins/modules/cloud/memset/memset_memstore_info.py index 95cacf9f01..c6dd4059e3 100644 --- a/plugins/modules/cloud/memset/memset_memstore_info.py +++ b/plugins/modules/cloud/memset/memset_memstore_info.py @@ -127,7 +127,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/cloud/memset/memset_server_info.py b/plugins/modules/cloud/memset/memset_server_info.py index 3b41bdddb9..41ea4bcf3e 100644 --- a/plugins/modules/cloud/memset/memset_server_info.py +++ b/plugins/modules/cloud/memset/memset_server_info.py @@ -252,7 +252,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/cloud/memset/memset_zone.py b/plugins/modules/cloud/memset/memset_zone.py index 02b5fd28f0..4c35c7a2c2 100644 --- a/plugins/modules/cloud/memset/memset_zone.py +++ b/plugins/modules/cloud/memset/memset_zone.py @@ -257,6 +257,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/cloud/memset/memset_zone_domain.py b/plugins/modules/cloud/memset/memset_zone_domain.py index 1e18a984b6..67a6125b6b 100644 --- a/plugins/modules/cloud/memset/memset_zone_domain.py +++ b/plugins/modules/cloud/memset/memset_zone_domain.py @@ -188,7 +188,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/cloud/memset/memset_zone_record.py b/plugins/modules/cloud/memset/memset_zone_record.py index 16a1c3a742..431483ccd1 100644 --- a/plugins/modules/cloud/memset/memset_zone_record.py +++ b/plugins/modules/cloud/memset/memset_zone_record.py @@ -306,7 +306,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())