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

fixes #67 #86

Merged
merged 6 commits into from
Sep 16, 2021
Merged
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
17 changes: 15 additions & 2 deletions plugins/action/query_graphql.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ def nautobot_action_graphql(args):
if not isinstance(ssl_verify, bool):
raise AnsibleError("validate_certs must be a boolean")

populate_root = args.get("populate_root", False)
Display().vv("Populate root is set to: %s" % populate_root)

# Verify SSL Verify is of boolean
if not isinstance(populate_root, bool):
raise AnsibleError("populate_root must be a boolean")

nautobot_api = NautobotApiBase(token=token, url=url, ssl_verify=ssl_verify)
query = args.get("query")
Display().v("Query String: %s" % query)
Expand Down Expand Up @@ -90,8 +97,14 @@ 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:
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we need the else here as we should be returning the data regardless. The flag should just set the facts or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I had that at first, it seemed like duplicate, what say everyone else?

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's go ahead and remove the else, remove the indent for the results. Then the data key will be populated regardless, whether it has data or it is None.

# Assign the data of a good result to the response to the data key
# otherwise, e.g. hostvars[inventory_hostname]['data']
results["data"] = nautobot_response.json.get("data")

return results

Expand Down
16 changes: 13 additions & 3 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 @@ -82,22 +90,23 @@
site_name: den
query_string: |
query ($site_name:String!) {
sites (name: $site_name) {
sites (name: $site_name) {
id
name
region {
name
}
}
}
}

# Get Response with variables
# Get Response with variables and set to root keys
- name: Obtain list of devices at site in variables from Nautobot
networktocode.nautobot.query_graphql:
url: http://nautobot.local
token: thisIsMyToken
query: "{{ query_string }}"
variables: "{{ variables }}"
populate_root: "yes"
Copy link
Contributor

Choose a reason for hiding this comment

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

Shouldn't that be boolean true ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In ansible they are equivalent

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes I know, unquoted yes is also evaluated as bool True.
This is more about documentation consistency, it's described as type bool in DOCUMENTATION section.
I was looking form a static-typing perspective, where bool is True or False without making assumptions about ansible internals, maybe unnecessarily. Anyway I don't have any issue with it, just a friendly suggestion.

"""

RETURN = """
Expand Down Expand Up @@ -145,6 +154,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),
itdependsnetworks marked this conversation as resolved.
Show resolved Hide resolved
),
# 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
1 change: 1 addition & 0 deletions tests/unit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,5 @@ def nautobot_valid_args(graphql_test_query):
"validate_certs": False,
"query": graphql_test_query,
"graph_variables": {},
"populate_root": False,
}