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

Added Error message for dynamic inventory if wrong credentials are passed #512

Open
wants to merge 3 commits into
base: release/1.9.3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
27 changes: 27 additions & 0 deletions plugins/inventory/ntnx_prism_vm_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@
from ansible.plugins.inventory import BaseInventoryPlugin, Constructable # noqa: E402

from ..module_utils.prism import vms # noqa: E402
import sys
import traceback
from ansible.module_utils.six import (
PY2,
)


class Mock_Module:
Expand All @@ -91,6 +96,28 @@ def __init__(self, host, port, username, password, validate_certs=False):
def jsonify(self, data):
return json.dumps(data)

def fail_json(self, msg, **kwargs):
"""Invoke fail_json using the AnsibleModule's fail_json method."""
kwargs["failed"] = True
kwargs["msg"] = msg
# Add traceback if debug or high verbosity and it is missing
# NOTE: Badly named as exception, it really always has been a traceback
if (
"exception" not in kwargs
and sys.exc_info()[2]
and (self._debug or self._verbosity >= 3)
):
if PY2:
# On Python 2 this is the last (stack frame) exception and as such may be unrelated to the failure
kwargs["exception"] = (
"WARNING: The below traceback may *not* be related to the actual failure.\n"
+ "".join(traceback.format_tb(sys.exc_info()[2]))
)
else:
kwargs["exception"] = "".join(traceback.format_tb(sys.exc_info()[2]))
print("\n%s" % self.jsonify(kwargs))
sys.exit(1)


class InventoryModule(BaseInventoryPlugin, Constructable):
"""Nutanix VM dynamic inventory module for ansible"""
Expand Down
9 changes: 9 additions & 0 deletions plugins/module_utils/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,15 @@ def list(
no_response=no_response,
timeout=timeout,
)
if resp and resp.get("state") == "ERROR":
if raise_error:
self.module.fail_json(
msg="Failed fetching URL: {0}".format(url),
error=resp["message_list"],
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
error=resp["message_list"],
error=resp.get("message_list"),

response=resp,
)
else:
return resp
if resp:
custom_filters = self.module.params.get("custom_filter")

Expand Down
Loading