diff --git a/changelogs/changelog.yaml b/changelogs/changelog.yaml index 082e5faa..a30f071a 100644 --- a/changelogs/changelog.yaml +++ b/changelogs/changelog.yaml @@ -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 @@ -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 diff --git a/galaxy.yml b/galaxy.yml index 68bdf9a4..82a4d5b9 100644 --- a/galaxy.yml +++ b/galaxy.yml @@ -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 diff --git a/plugins/inventory/gql_inventory.py b/plugins/inventory/gql_inventory.py index 3886bc16..3fa289e5 100644 --- a/plugins/inventory/gql_inventory.py +++ b/plugins/inventory/gql_inventory.py @@ -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 @@ -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"]) diff --git a/plugins/inventory/inventory.py b/plugins/inventory/inventory.py index d0d2df9b..dc921b10 100644 --- a/plugins/inventory/inventory.py +++ b/plugins/inventory/inventory.py @@ -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 @@ -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") diff --git a/pyproject.toml b/pyproject.toml index fd126ca6..cc28a1b4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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