Skip to content

Commit

Permalink
Fix Exception handling for #51
Browse files Browse the repository at this point in the history
  • Loading branch information
lgetwan committed Jun 2, 2022
1 parent ba66555 commit 2481c88
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 5 deletions.
48 changes: 48 additions & 0 deletions changelogs/fragments/fix-51-exceptionhandling.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# https://docs.ansible.com/ansible/latest/community/development_process.html#changelogs-how-to
# minor_changes:
# - Add agent role. Currently supports the vanilla agent.

# known_issues:
# - This release is still in development and a heavy work in progress.
# - Discovery module is not feature complete yet.
# - Downtime module is not fully idempotent yet. This affects service downtimes and deletions.

## Line Format
# When writing a changelog entry, use the following format:

# - scope - description starting with a lowercase letter and ending with a period at the very end. Multiple sentences are allowed (https://github.com/reference/to/an/issue or, if there is no issue, reference to a pull request itself).

# The scope is usually a module or plugin name or group of modules or plugins, for example, lookup plugins. While module names can (and should) be mentioned directly (foo_module), plugin names should always be followed by the type (foo inventory plugin).

# For changes that are not really scoped (for example, which affect a whole collection), use the following format:

# - Description starting with an uppercase letter and ending with a dot at the very end. Multiple sentences are allowed (https://github.com/reference/to/an/issue or, if there is no issue, reference to a pull request itself).


## Possible keys:

# breaking_changes

# Changes that break existing playbooks or roles. This includes any change to existing behavior that forces users to update tasks. Displayed in both the changelogs and the Porting Guides.
# major_changes

# Major changes to Ansible itself. Generally does not include module or plugin changes. Displayed in both the changelogs and the Porting Guides.
# minor_changes

# Minor changes to Ansible, modules, or plugins. This includes new features, new parameters added to modules, or behavior changes to existing parameters.
# deprecated_features

# Features that have been deprecated and are scheduled for removal in a future release. Displayed in both the changelogs and the Porting Guides.
# removed_features

# Features that were previously deprecated and are now removed. Displayed in both the changelogs and the Porting Guides.
# security_fixes

# Fixes that address CVEs or resolve security concerns. Include links to CVE information.
bugfixes
- Improved the exception handling of the discovery module.

# Fixes that resolve issues.
# known_issues

# Known issues that are currently not fixed or will not be fixed.
13 changes: 8 additions & 5 deletions plugins/modules/discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def run_module():

api_endpoint = (
"/objects/host/"
+ module.params.get("host_name")
+ module.params.get("host_name")s
+ "/actions/discover_services/invoke"
)
url = base_url + api_endpoint
Expand All @@ -144,12 +144,15 @@ def run_module():
if http_code in http_code_mapping.keys():
changed, failed, msg = http_code_mapping[http_code]
else:
http_body = json.loads(info["body"])["detail"]
changed, failed, msg = (False, True, "Error calling API.")
changed, failed, msg = (
False,
True,
"Error calling API. HTTP Return Code is %d" % http_code,
)

if failed:
http_body = json.loads(info["body"])["detail"]
msg += " Details: %s" % http_body
body = info.get("body", "N/A")
msg += " Details: %s" % body

result["msg"] = msg
result["changed"] = changed
Expand Down

0 comments on commit 2481c88

Please sign in to comment.