Skip to content

Commit

Permalink
[ignore] Remove pagination as the URL query param pageSize is not wor…
Browse files Browse the repository at this point in the history
…king as intended and add test task and example in Documentation for URL query string using orderBy.
  • Loading branch information
gmicol committed Feb 27, 2025
1 parent 1af1af1 commit 2e77a8b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 40 deletions.
50 changes: 10 additions & 40 deletions plugins/modules/nd_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,6 @@
- The file path containing the body of the HTTP request.
type: path
aliases: [ config_file ]
page_size:
description:
- The number of items to return in a single page.
type: int
page:
description:
- The page number to return.
type: int
extends_documentation_fragment:
- cisco.nd.modules
- cisco.nd.check_mode
Expand Down Expand Up @@ -132,6 +124,15 @@
method: get
register: query_all
- name: Query all Security Domains ordered by name using GET method
cisco.nd.nd_rest:
host: nd
username: admin
password: SomeSecretPassword
path: /nexus/infra/api/aaa/v4/securitydomains?orderBy=spec.name
method: get
register: query_all_ordered_by_name
- name: Remove Security Domain using DELETE method
cisco.nd.nd_rest:
host: nd
Expand Down Expand Up @@ -184,12 +185,7 @@
import json
import os

# Optional, only used for YAML validation and update a URL
try:
from urllib.parse import parse_qsl, urlencode, urlparse, urlunparse
HAS_URLPARSE = True
except Exception:
HAS_URLPARSE = False
# Optional, only used for YAML validation
try:
import yaml

Expand All @@ -203,21 +199,6 @@
from ansible.module_utils._text import to_text


def update_qsl(url, params):
"""Add or update a URL query string"""

if HAS_URLPARSE:
url_parts = list(urlparse(url))
query = dict(parse_qsl(url_parts[4]))
query.update(params)
url_parts[4] = urlencode(query)
return urlunparse(url_parts)
elif "?" in url:
return url + "&" + "&".join(["%s=%s" % (k, v) for k, v in params.items()])
else:
return url + "?" + "&".join(["%s=%s" % (k, v) for k, v in params.items()])


def main():
argument_spec = nd_argument_spec()
argument_spec.update(
Expand All @@ -230,8 +211,6 @@ def main():
),
content=dict(type="raw", aliases=["payload"]),
file_path=dict(type="path", aliases=["config_file"]),
page=dict(type="int"),
page_size=dict(type="int"),
)

module = AnsibleModule(
Expand All @@ -242,8 +221,6 @@ def main():
content = module.params.get("content")
path = module.params.get("path")
file_path = module.params.get("config_file")
page = module.params.get("page")
page_size = module.params.get("page_size")

nd = NDModule(module)

Expand Down Expand Up @@ -276,13 +253,6 @@ def main():
nd.existing = nd.previous = sanitize(nd.query_obj(path, ignore_not_found_error=True), ND_REST_KEYS_TO_SANITIZE)
nd.result["previous"] = nd.previous

# Check paginition
if method == "GET":
if page:
path = update_qsl(path, {"page": page})
if page_size:
path = update_qsl(path, {"page-size": page_size})

# Perform request
if module.check_mode:
nd.result["jsondata"] = content
Expand Down
12 changes: 12 additions & 0 deletions tests/integration/targets/nd_rest/tasks/json_inline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,13 @@
method: get
register: quey_all_security_domains

- name: Query all Sites ordered by name with GET method (url query string test)
cisco.nd.nd_rest:
<<: *nd_info
path: /nexus/api/sitemanagement/v4/sites?orderBy=spec.name
method: get
register: query_all_sites_ordered

- name: Assert query tasks with GET method for nd_rest module
ansible.builtin.assert:
that:
Expand All @@ -132,6 +139,11 @@
- quey_all_security_domains.current["items"][1].spec.name == "ansible_security_domain_test"
- quey_all_security_domains.jsondata["items"][0].spec.name == "all"
- quey_all_security_domains.jsondata["items"][1].spec.name == "ansible_security_domain_test"
- query_all_sites_ordered is not changed
- query_all_sites_ordered.current["items"][0].spec.name == "ansible_test"
- query_all_sites_ordered.current["items"][1].spec.name == "ansible_test_2"
- query_all_sites_ordered.jsondata["items"][0].spec.name == "ansible_test"
- query_all_sites_ordered.jsondata["items"][1].spec.name == "ansible_test_2"

# DELETE tasks

Expand Down

0 comments on commit 2e77a8b

Please sign in to comment.