Skip to content
This repository has been archived by the owner on Jun 13, 2024. It is now read-only.

raw: Handle exception raised by underlying APIs #180

Merged
merged 1 commit into from
Aug 11, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion plugins/module_utils/raw.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
from ansible.module_utils.basic import missing_required_lib
from ansible_collections.community.kubernetes.plugins.module_utils.common import AUTH_ARG_SPEC, COMMON_ARG_SPEC
from ansible.module_utils.six import string_types
from ansible.module_utils._text import to_native
from ansible_collections.community.kubernetes.plugins.module_utils.common import KubernetesAnsibleModule
from ansible.module_utils.common.dict_transformations import dict_merge

Expand Down Expand Up @@ -175,7 +176,7 @@ def execute_module(self):
continue
kind = definition.get('kind', self.kind)
api_version = definition.get('apiVersion', self.api_version)
if kind.endswith('List'):
if kind and kind.endswith('List'):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if kind isn't defined here we're broken anyway, this should have been validated earlier. We could add an additional failout here if either kind or api_version aren't defined

resource = self.find_resource(kind, api_version, fail=False)
flattened_definitions.extend(self.flatten_list_kind(resource, definition))
else:
Expand Down Expand Up @@ -269,6 +270,9 @@ def perform_action(self, resource, definition):
except DynamicApiError as exc:
self.fail_json(msg='Failed to retrieve requested object: {0}'.format(exc.body),
error=exc.status, status=exc.status, reason=exc.reason)
except Exception as exc:
self.fail_json(msg='Failed to retrieve requested object: {0}'.format(to_native(exc)),
error='', status='', reason='')

if state == 'absent':
result['method'] = "delete"
Expand Down Expand Up @@ -422,6 +426,12 @@ def patch_resource(self, resource, definition, existing, name, namespace, merge_
msg += "\n" + "\n ".join(self.warnings)
error = dict(msg=msg, error=exc.status, status=exc.status, reason=exc.reason, warnings=self.warnings)
return None, error
except Exception as exc:
msg = "Failed to patch object: {0}".format(exc)
if self.warnings:
msg += "\n" + "\n ".join(self.warnings)
error = dict(msg=msg, error=to_native(exc), status='', reason='', warnings=self.warnings)
return None, error

def create_project_request(self, definition):
definition['kind'] = 'ProjectRequest'
Expand Down