Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

k8s: handle fail_json API from inventory plugin #59

Merged
merged 1 commit into from
Apr 19, 2021
Merged
Show file tree
Hide file tree
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
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).
9 changes: 4 additions & 5 deletions plugins/inventory/k8s.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,11 @@
import json

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.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable
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

try:
from openshift.dynamic.exceptions import DynamicApiError
Expand All @@ -137,10 +140,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.community.kubernetes.plugins.module_utils.exceptions import K8sInventoryException
Akasurde marked this conversation as resolved.
Show resolved Hide resolved


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