Skip to content

Commit

Permalink
Add rest support for aci_rest module
Browse files Browse the repository at this point in the history
  • Loading branch information
edudppaz committed Dec 15, 2024
1 parent a3c9bd8 commit 8f0db6a
Show file tree
Hide file tree
Showing 4 changed files with 253 additions and 0 deletions.
21 changes: 21 additions & 0 deletions plugins/modules/aci_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@
- Preserve the response for the provided path.
type: bool
default: false
page_size:
description:
- The number of items to return in a single page.
type: int
required: false
page:
description:
- The page number to return.
type: int
required: false
default: 0
extends_documentation_fragment:
- cisco.aci.aci
- cisco.aci.annotation
Expand Down Expand Up @@ -377,6 +388,8 @@ def main():
src=dict(type="path", aliases=["config_file"]),
content=dict(type="raw"),
rsp_subtree_preserve=dict(type="bool", default=False),
page_size=dict(type="int", required=False),
page=dict(type="int", required=False, default=0),
)

module = AnsibleModule(
Expand All @@ -390,6 +403,10 @@ def main():
src = module.params.get("src")
rsp_subtree_preserve = module.params.get("rsp_subtree_preserve")
annotation = module.params.get("annotation")
page_size = module.params.get("page_size")
page = module.params.get("page")
if module.params.get("method") != "get" and page_size:
module.fail_json(msg="Pagination parameters (page and page_size) are only valid for GET method")

# Report missing file
file_exists = False
Expand Down Expand Up @@ -453,6 +470,10 @@ def main():
# NOTE By setting aci.path we ensure that Ansible displays accurate URL info when the plugin and the aci_rest module are used.
aci.path = path.lstrip("/")
aci.url = "{0}/{1}".format(aci.base_url, aci.path)

if aci.params.get("method") == "get" and page_size:
aci.path = "{0}?page={1}&page-size={2}".format(aci.path, page, page_size)
aci.url = update_qsl(aci.url, {"page": page, "page-size": page_size})
if aci.params.get("method") != "get" and not rsp_subtree_preserve:
aci.path = "{0}?rsp-subtree=modified".format(aci.path)
aci.url = update_qsl(aci.url, {"rsp-subtree": "modified"})
Expand Down
80 changes: 80 additions & 0 deletions tests/integration/targets/aci_rest/tasks/json_inline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
path: /api/mo/uni/tn-[ansible_test].json
method: delete

- name: Remove tenant 2
cisco.aci.aci_rest:
<<: *tenant_absent
path: /api/mo/uni/tn-[ansible_test_2].json

# ADD TENANT
- name: Add tenant (check mode)
cisco.aci.aci_rest: &tenant_present
Expand Down Expand Up @@ -138,6 +143,75 @@
that:
- nm_query_all_tenants is not changed

# ADD TENANT 2
- name: Add tenant 2 (normal mode)
cisco.aci.aci_rest:
<<: *tenant_present
content:
{
"fvTenant": {
"attributes": {
"descr": "Ansible test tenant",
"name": "ansible_test_2"
}
}
}

# QUERY ALL TENANTS WITH PAGINATION
- name: Query all tenants with pagination (normal mode)
cisco.aci.aci_rest: &tenant_query_all_paginated
host: '{{ aci_hostname }}'
username: '{{ aci_username }}'
password: '{{ aci_password }}'
validate_certs: '{{ aci_validate_certs | default(false) }}'
use_ssl: '{{ aci_use_ssl | default(true) }}'
use_proxy: '{{ aci_use_proxy | default(true) }}'
output_level: '{{ aci_output_level | default("info") }}'
path: /api/class/uni/fvTenant.json
page_size: 10
page: 0
method: get
register: nm_query_all_tenants_paginated

- name: Query all tenant with pagination - Size 1 / Page 0 (normal mode)
cisco.aci.aci_rest:
<<: *tenant_query_all_paginated
page_size: 1
page: 0
register: nm_query_all_tenants_paginated_1_0

- name: Query all tenant with pagination - Size 1 / Page 1 (normal mode)
cisco.aci.aci_rest:
<<: *tenant_query_all_paginated
page_size: 1
page: 1
register: nm_query_all_tenants_paginated_1_1

- name: Query all tenant with pagination - Size 2 / Page 0 (normal mode)
cisco.aci.aci_rest:
<<: *tenant_query_all_paginated
page_size: 2
page: 0
register: nm_query_all_tenants_paginated_2_0

- name: Verify query_all_tenants_paginated
ansible.builtin.assert:
that:
- nm_query_all_tenants_paginated is not changed
- nm_query_all_tenants_paginated_1_0 is not changed
- nm_query_all_tenants_paginated_1_1 is not changed
- nm_query_all_tenants_paginated_2_0 is not changed

- name: Verify pagination works as expected
ansible.builtin.assert:
that:
- nm_query_all_tenants_paginated is not changed
- nm_query_all_tenants_paginated_1_0.imdata | length == 1
- nm_query_all_tenants_paginated_1_1.imdata | length == 1
- nm_query_all_tenants_paginated_2_0.imdata | length == 2
- nm_query_all_tenants_paginated_1_0.imdata.0.fvTenant.attributes.name == nm_query_all_tenants_paginated_2_0.imdata.0.fvTenant.attributes.name
- nm_query_all_tenants_paginated_1_1.imdata.0.fvTenant.attributes.name == nm_query_all_tenants_paginated_2_0.imdata.1.fvTenant.attributes.name

# QUERY A TENANT
- name: Query our tenant
cisco.aci.aci_rest: &tenant_query
Expand All @@ -162,6 +236,12 @@
cisco.aci.aci_rest: *tenant_absent
register: nm_remove_tenant

- name: Remove tenant_2 (normal mode)
cisco.aci.aci_rest:
<<: *tenant_absent
path: /api/mo/uni/tn-[ansible_test_2].json
register: nm_remove_tenant_2

- name: Remove tenant again (normal mode)
cisco.aci.aci_rest: *tenant_absent
register: nm_remove_tenant_again
Expand Down
75 changes: 75 additions & 0 deletions tests/integration/targets/aci_rest/tasks/json_string.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
path: /api/mo/uni/tn-[ansible_test].json
method: delete

- name: Remove tenant 2
cisco.aci.aci_rest:
<<: *tenant_absent
path: /api/mo/uni/tn-[ansible_test_2].json

# ADD TENANT
- name: Add tenant (check mode)
cisco.aci.aci_rest: &tenant_present
Expand Down Expand Up @@ -139,6 +144,76 @@
that:
- nm_query_all_tenants is not changed


# ADD TENANT 2
- name: Add tenant 2 (normal mode)
cisco.aci.aci_rest:
<<: *tenant_present
content: |
{
"fvTenant": {
"attributes": {
"descr": "Ansible test tenant",
"name": "ansible_test_2"
}
}
}
# QUERY ALL TENANTS WITH PAGINATION
- name: Query all tenants with pagination (normal mode)
cisco.aci.aci_rest: &tenant_query_all_paginated
host: '{{ aci_hostname }}'
username: '{{ aci_username }}'
password: '{{ aci_password }}'
validate_certs: '{{ aci_validate_certs | default(false) }}'
use_ssl: '{{ aci_use_ssl | default(true) }}'
use_proxy: '{{ aci_use_proxy | default(true) }}'
output_level: '{{ aci_output_level | default("info") }}'
path: /api/class/uni/fvTenant.json
page_size: 10
page: 0
method: get
register: nm_query_all_tenants_paginated

- name: Query all tenant with pagination - Size 1 / Page 0 (normal mode)
cisco.aci.aci_rest:
<<: *tenant_query_all_paginated
page_size: 1
page: 0
register: nm_query_all_tenants_paginated_1_0

- name: Query all tenant with pagination - Size 1 / Page 1 (normal mode)
cisco.aci.aci_rest:
<<: *tenant_query_all_paginated
page_size: 1
page: 1
register: nm_query_all_tenants_paginated_1_1

- name: Query all tenant with pagination - Size 2 / Page 0 (normal mode)
cisco.aci.aci_rest:
<<: *tenant_query_all_paginated
page_size: 2
page: 0
register: nm_query_all_tenants_paginated_2_0

- name: Verify query_all_tenants_paginated
ansible.builtin.assert:
that:
- nm_query_all_tenants_paginated is not changed
- nm_query_all_tenants_paginated_1_0 is not changed
- nm_query_all_tenants_paginated_1_1 is not changed
- nm_query_all_tenants_paginated_2_0 is not changed

- name: Verify pagination works as expected
ansible.builtin.assert:
that:
- nm_query_all_tenants_paginated is not changed
- nm_query_all_tenants_paginated_1_0.imdata | length == 1
- nm_query_all_tenants_paginated_1_1.imdata | length == 1
- nm_query_all_tenants_paginated_2_0.imdata | length == 2
- nm_query_all_tenants_paginated_1_0.imdata.0.fvTenant.attributes.name == nm_query_all_tenants_paginated_2_0.imdata.0.fvTenant.attributes.name
- nm_query_all_tenants_paginated_1_1.imdata.0.fvTenant.attributes.name == nm_query_all_tenants_paginated_2_0.imdata.1.fvTenant.attributes.name

# QUERY A TENANT
- name: Query our tenant
cisco.aci.aci_rest: &tenant_query
Expand Down
77 changes: 77 additions & 0 deletions tests/integration/targets/aci_rest/tasks/xml_string.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@
path: /api/mo/uni/tn-[ansible_test].xml
method: delete

- name: Remove tenant2
cisco.aci.aci_rest: &tenant_absent_2
host: '{{ aci_hostname }}'
username: '{{ aci_username }}'
password: '{{ aci_password }}'
validate_certs: '{{ aci_validate_certs | default(false) }}'
use_ssl: '{{ aci_use_ssl | default(true) }}'
use_proxy: '{{ aci_use_proxy | default(true) }}'
output_level: '{{ aci_output_level | default("info") }}'
path: /api/mo/uni/tn-[ansible_test_2].xml
method: delete

# ADD TENANT
- name: Add tenant (check mode)
cisco.aci.aci_rest:
Expand Down Expand Up @@ -184,6 +196,71 @@
that:
- nm_query_all_tenants is not changed

# ADD TENANT 2
- name: Add tenant 2 (normal mode)
cisco.aci.aci_rest:
<<: *tenant_present
path: /api/mo/uni.xml
content:
<fvTenant name="ansible_test_2"/>
register: nm_add_tenant_2

# QUERY ALL TENANTS WITH PAGINATION
- name: Query all tenants with pagination (normal mode)
cisco.aci.aci_rest: &tenant_query_all_paginated
host: '{{ aci_hostname }}'
username: '{{ aci_username }}'
password: '{{ aci_password }}'
validate_certs: '{{ aci_validate_certs | default(false) }}'
use_ssl: '{{ aci_use_ssl | default(true) }}'
use_proxy: '{{ aci_use_proxy | default(true) }}'
output_level: '{{ aci_output_level | default("info") }}'
path: /api/mo/uni.xml
page_size: 10
page: 0
method: get
register: nm_query_all_tenants_paginated


- name: Query all tenant with pagination - Size 1 / Page 0 (normal mode)
cisco.aci.aci_rest:
<<: *tenant_query_all_paginated
page_size: 1
page: 0
register: nm_query_all_tenants_paginated_1_0

- name: Query all tenant with pagination - Size 1 / Page 1 (normal mode)
cisco.aci.aci_rest:
<<: *tenant_query_all_paginated
page_size: 1
page: 1
register: nm_query_all_tenants_paginated_1_1

- name: Query all tenant with pagination - Size 2 / Page 0 (normal mode)
cisco.aci.aci_rest:
<<: *tenant_query_all_paginated
page_size: 2
page: 0
register: nm_query_all_tenants_paginated_2_0

- name: Verify query_all_tenants_paginated
ansible.builtin.assert:
that:
- nm_query_all_tenants_paginated is not changed
- nm_query_all_tenants_paginated_1_0 is not changed
- nm_query_all_tenants_paginated_1_1 is not changed
- nm_query_all_tenants_paginated_2_0 is not changed

- name: Verify pagination works as expected
ansible.builtin.assert:
that:
- nm_query_all_tenants_paginated is not changed
- nm_query_all_tenants_paginated_1_0.imdata | length == 1
- nm_query_all_tenants_paginated_1_1.imdata | length == 1
- nm_query_all_tenants_paginated_2_0.imdata | length == 2
- nm_query_all_tenants_paginated_1_0.imdata.0.fvTenant.attributes.name == nm_query_all_tenants_paginated_2_0.imdata.0.fvTenant.attributes.name
- nm_query_all_tenants_paginated_1_1.imdata.0.fvTenant.attributes.name == nm_query_all_tenants_paginated_2_0.imdata.1.fvTenant.attributes.name

# QUERY A TENANT
- name: Query our tenant
cisco.aci.aci_rest: &tenant_query
Expand Down

0 comments on commit 8f0db6a

Please sign in to comment.