Skip to content

Commit

Permalink
Merge pull request #224 from jvanderaa/inventory_graceful_exit
Browse files Browse the repository at this point in the history
Raise error instead of returning empty.
  • Loading branch information
jvanderaa authored Aug 15, 2023
2 parents 6fd0583 + c5299e4 commit 9dcf0c6
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 49 deletions.
7 changes: 6 additions & 1 deletion changelogs/changelog.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ releases:
- (#172) Add description to manufacturer

4.3.0:
changes:
changes:
minor_changes:
- (#185) Updated Doc Fragments
- (#187) Updated Tag documentation
Expand All @@ -373,3 +373,8 @@ releases:
- (#211) Removes codecov from dev dependencies
- (#203) Adds plugin module
- (#209) Catches HTTPError for GraphQL query and enables Ansible retries

4.5.0:
changes:
bugfixes:
- (#223) Inventory Hosts Empty On Error
2 changes: 1 addition & 1 deletion galaxy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace: networktocode
name: nautobot

# The version of the collection. Must be compatible with semantic versioning
version: 4.4.0
version: 4.5.0

# The path to the Markdown (.md) readme file. This path is relative to the root of the collection
readme: README.md
Expand Down
35 changes: 5 additions & 30 deletions plugins/inventory/gql_inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,11 @@
from sys import version as python_version
from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable
from ansible.module_utils.ansible_release import __version__ as ansible_version
from ansible.errors import AnsibleError
from ansible.errors import AnsibleError, AnsibleParserError
from ansible.module_utils.urls import open_url

from ansible.module_utils.six.moves.urllib import error as urllib_error
from ansible.module_utils.common.text.converters import to_native

from ansible_collections.networktocode.nautobot.plugins.filter.graphql import convert_to_graphql_string

Expand Down Expand Up @@ -367,40 +368,14 @@ def main(self):
validate_certs=self.validate_certs,
follow_redirects=self.follow_redirects,
)
except urllib_error.HTTPError as e:
"""This will return the response body when we encounter an error.
This is to help determine what might be the issue when encountering an error.
Please check issue #294 for more info.
"""
# Prevent inventory from failing completely if the token does not have the proper permissions for specific URLs
if e.code == 403:
self.display.display(
"Permission denied: {0}. This may impair functionality of the inventory plugin.".format(self.api_endpoint + "/"),
color="red",
)
# Need to return mock response data that is empty to prevent any failures downstream
return {"results": [], "next": None}
else:
self.display.display(f"{e.code}", color="red")
self.display.display(
"Something went wrong while executing the query.\nReason: {reason}".format(
reason=json.loads(e.fp.read().decode())["errors"][0]["message"],
),
color="red",
)
# Need to return mock response data that is empty to prevent any failures downstream
return {"results": [], "next": None}
except urllib_error.HTTPError as err:
raise AnsibleParserError(to_native(err.fp.read()))
json_data = json.loads(response.read())
self.display.vvvv(f"JSON response: {json_data}")

# Error handling in case of a malformed query
if "errors" in json_data:
self.display.display(
"Query returned an error.\nReason: {0}".format(json_data["errors"][0]["message"]),
color="red",
)
# Need to return mock response data that is empty to prevent any failures downstream
return {"results": [], "next": None}
raise AnsibleParserError(to_native(json_data["errors"][0]["message"]))

for device in json_data["data"].get("devices", []) + json_data["data"].get("virtual_machines", []):
self.inventory.add_host(device["name"])
Expand Down
19 changes: 3 additions & 16 deletions plugins/inventory/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@

from ansible.plugins.inventory import BaseInventoryPlugin, Constructable, Cacheable
from ansible.module_utils.ansible_release import __version__ as ansible_version
from ansible.errors import AnsibleError
from ansible.errors import AnsibleError, AnsibleParserError
from ansible.module_utils._text import to_text, to_native
from ansible.module_utils.urls import open_url
from ansible.module_utils.six.moves.urllib import error as urllib_error
Expand Down Expand Up @@ -303,21 +303,8 @@ def _fetch_information(self, url):
validate_certs=self.validate_certs,
follow_redirects=self.follow_redirects,
)
except urllib_error.HTTPError as e:
"""This will return the response body when we encounter an error.
This is to help determine what might be the issue when encountering an error.
Please check issue #294 for more info.
"""
# Prevent inventory from failing completely if the token does not have the proper permissions for specific URLs
if e.code == 403:
self.display.display(
"Permission denied: {0}. This may impair functionality of the inventory plugin.".format(url),
color="red",
)
# Need to return mock response data that is empty to prevent any failures downstream
return {"results": [], "next": None}

raise AnsibleError(to_native(e.fp.read()))
except urllib_error.HTTPError as err:
raise AnsibleParserError(to_native(err.fp.read()))

try:
raw_data = to_text(response.read(), errors="surrogate_or_strict")
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "nautobot_ansible_modules"
version = "4.4.0"
version = "4.5.0"
description = "Ansible collection to interact with Nautobot's API"
authors = ["Network to Code <opensource@networktocode.com"]
license = "Apache 2.0"
Expand Down

0 comments on commit 9dcf0c6

Please sign in to comment.