Skip to content

Commit

Permalink
k8s: handle fail_json API from inventory plugin
Browse files Browse the repository at this point in the history
* inventory plugin does not have module object so fail_json
  will not work. Use AnsibleError instead of ``fail_json``

Fixes: #57

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
  • Loading branch information
Akasurde committed Apr 16, 2021
1 parent f5eb062 commit b06a785
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/fail_json.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bugfixes:
- k8s - handle ``fail_json`` API from inventory plugin (https://github.com/ansible-collections/kubernetes.core/issues/57).
5 changes: 1 addition & 4 deletions plugins/inventory/k8s.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@

from ansible.errors import AnsibleError
from ansible_collections.community.kubernetes.plugins.module_utils.common import K8sAnsibleMixin, HAS_K8S_MODULE_HELPER, k8s_import_exception
from ansible_collections.community.kubernetes.plugins.module_utils.exceptions import K8sInventoryException
from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable

try:
Expand All @@ -137,10 +138,6 @@ def format_dynamic_api_exc(exc):
return '%s Reason: %s' % (exc.status, exc.reason)


class K8sInventoryException(Exception):
pass


class InventoryModule(BaseInventoryPlugin, Constructable, Cacheable, K8sAnsibleMixin):
NAME = 'community.kubernetes.k8s'

Expand Down
6 changes: 4 additions & 2 deletions plugins/module_utils/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
from datetime import datetime
from distutils.version import LooseVersion


from ansible.module_utils.basic import AnsibleModule, missing_required_lib
from ansible.module_utils.six import iteritems, string_types
from ansible.module_utils._text import to_native, to_bytes, to_text
from ansible.module_utils.common.dict_transformations import dict_merge
from ansible.module_utils.parsing.convert_bool import boolean
from ansible_collections.kubernetes.core.plugins.module_utils.exceptions import K8sInventoryException


K8S_IMP_ERR = None
Expand Down Expand Up @@ -425,7 +425,9 @@ def diff_objects(self, existing, new):
return True, result

def fail(self, msg=None):
self.fail_json(msg=msg)
if hasattr(self, 'fail_json'):
self.fail_json(msg=msg)
raise K8sInventoryException(msg)

def _wait_for(self, resource, name, namespace, predicate, sleep, timeout, state):
start = datetime.now()
Expand Down
11 changes: 11 additions & 0 deletions plugins/module_utils/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# -*- coding: utf-8 -*-
# Copyright: (c) 2021, Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function

__metaclass__ = type


class K8sInventoryException(Exception):
pass

0 comments on commit b06a785

Please sign in to comment.