Skip to content

Commit

Permalink
fixes nautobot#67
Browse files Browse the repository at this point in the history
  • Loading branch information
itdependsnetworks committed Sep 11, 2021
1 parent 9150130 commit 24a58c2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
9 changes: 7 additions & 2 deletions plugins/action/query_graphql.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,13 @@ def nautobot_action_graphql(args):

# Good result, return it
if isinstance(nautobot_response, pynautobot.core.graphql.GraphQLRecord):
# Assign the data of a good result to the response
results["data"] = nautobot_response.json.get("data")
# If populate_root is set, add to ansible_facts which will set to the root of
# the data structure, e.g. hostvars[inventory_hostname]
if args.get("populate_root"):
results["ansible_facts"] = nautobot_response.json.get("data")
else:
# Assign the data of a good result to the response to data otherwise
results["data"] = nautobot_response.json.get("data")

return results

Expand Down
9 changes: 9 additions & 0 deletions plugins/modules/query_graphql.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@
required: False
default: True
type: bool
populate_root:
description:
- Whether or not to populate data in the in the root (e.g. hostvars[inventory_hostname]) or within the
data key (e.g. hostvars[inventory_hostname]['data']). Beware, that the root keys provided by the query
will overwrite any root keys already present, leverage the GraphQL alias feature to avoid issues.
required: False
default: False
type: bool
"""

EXAMPLES = """
Expand Down Expand Up @@ -145,6 +153,7 @@ def main():
token=dict(required=False, type="str", no_log=True, default=None),
url=dict(required=False, type="str", default=None),
validate_certs=dict(required=False, type="bool", default=True),
populate_root=dict(required=False, type="bool", default=False),
),
# Set to true as this is a read only API, this may need to change or have significant changes when Mutations are
# added to the GraphQL endpoint of Nautobot
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/action/test_graphql_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ def test_setup_api_error_incorrect_validate_certs(nautobot_valid_args):
assert str(exc.value) == "validate_certs must be a boolean"


def test_setup_api_error_incorrect_populate_root(nautobot_valid_args):
nautobot_valid_args["populate_root"] = "Hi"
with pytest.raises(AnsibleError) as exc:
test_class = nautobot_action_graphql(args=nautobot_valid_args)

assert str(exc.value) == "populate_root must be a boolean"


def test_query_api_query_error_none(nautobot_valid_args):
nautobot_valid_args["query"] = None
with pytest.raises(AnsibleError) as exc:
Expand Down

0 comments on commit 24a58c2

Please sign in to comment.