Skip to content

Commit

Permalink
Merge c47ff1b into d3264a6
Browse files Browse the repository at this point in the history
  • Loading branch information
akinross authored Sep 20, 2023
2 parents d3264a6 + c47ff1b commit 372d654
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 36 deletions.
59 changes: 37 additions & 22 deletions plugins/modules/aci_rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ def main():

module = AnsibleModule(
argument_spec=argument_spec,
supports_check_mode=True,
mutually_exclusive=[["content", "src"]],
)

Expand Down Expand Up @@ -410,34 +411,48 @@ def main():
aci.url = update_qsl(aci.url, {"rsp-subtree": "modified"})

method = aci.params.get("method").upper()

# Perform request
resp, info = aci.api_call(method, aci.url, data=payload, return_response=True)
if not aci.module.check_mode:
resp, info = aci.api_call(method, aci.url, data=payload, return_response=True)
# Report failure
if info.get("status") != 200:
try:
# APIC error
aci.response_type(info["body"], rest_type)
aci.fail_json(msg="APIC Error {code}: {text}".format_map(aci.error))
except KeyError:
# Connection error
aci.fail_json(msg="Connection failed for {url}. {msg}".format_map(info))

Check warning on line 425 in plugins/modules/aci_rest.py

View check run for this annotation

Codecov / codecov/patch

plugins/modules/aci_rest.py#L425

Added line #L425 was not covered by tests

# Report failure
if info.get("status") != 200:
try:
# APIC error
aci.response_type(info["body"], rest_type)
aci.fail_json(msg="APIC Error {code}: {text}".format_map(aci.error))
except KeyError:
# Connection error
aci.fail_json(msg="Connection failed for {url}. {msg}".format_map(info))

try:
aci.response_type(resp.read(), rest_type)
except AttributeError:
aci.response_type(info.get("body"), rest_type)

aci.result["status"] = aci.status
aci.result["imdata"] = aci.imdata
aci.result["totalCount"] = aci.totalCount

if aci.params.get("method") != "get":
aci.response_type(resp.read(), rest_type)
except AttributeError:
aci.response_type(info.get("body"), rest_type)

aci.result["status"] = aci.status
aci.result["imdata"] = aci.imdata
aci.result["totalCount"] = aci.totalCount

else:
aci.method = method
# Set changed to true so check_mode changed result is behaving similar to non aci_rest modules
aci.result["changed"] = True

# Only set proposed if we have a payload and thus also only allow output_path if we have a payload
# DELETE and GET do not have a payload
if payload and method == "POST":
if rest_type == "json":
payload = json.loads(payload)

aci.result["proposed"] = payload

output_path = aci.params.get("output_path")
if output_path is not None:
with open(output_path, "a") as output_file:
output_file.write(str(payload))
if rest_type == "json":
json.dump([payload], output_file)
else:
output_file.write(str(payload))

# Report success
aci.exit_json(**aci.result)
Expand Down
6 changes: 0 additions & 6 deletions tests/integration/targets/aci_rest/tasks/error_handling.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
- "'current' not in error_on_name_resolution"
- "'previous' not in error_on_name_resolution"
- "'sent' not in error_on_name_resolution"
- "'proposed' not in error_on_name_resolution"
- "'filter_string' not in error_on_name_resolution"

- name: Error when required parameter is missing
Expand Down Expand Up @@ -59,7 +58,6 @@
- "'current' not in error_on_missing_required_param"
- "'previous' not in error_on_missing_required_param"
- "'sent' not in error_on_missing_required_param"
- "'proposed' not in error_on_missing_required_param"
- "'filter_string' not in error_on_missing_required_param"

- name: Error when attributes are missing
Expand Down Expand Up @@ -90,7 +88,6 @@
- "'current' not in error_on_missing_attributes"
- "'previous' not in error_on_missing_attributes"
- "'sent' not in error_on_missing_attributes"
- "'proposed' not in error_on_missing_attributes"
- "'filter_string' not in error_on_missing_attributes"

- name: Error when input does not validate
Expand Down Expand Up @@ -123,7 +120,6 @@
- "'current' not in error_on_input_validation"
- "'previous' not in error_on_input_validation"
- "'sent' not in error_on_input_validation"
- "'proposed' not in error_on_input_validation"
- "'filter_string' not in error_on_input_validation"

- name: Error when invalid attributes are used
Expand Down Expand Up @@ -156,7 +152,6 @@
- "'current' not in error_on_invalid_attributes"
- "'previous' not in error_on_invalid_attributes"
- "'sent' not in error_on_invalid_attributes"
- "'proposed' not in error_on_invalid_attributes"
- "'filter_string' not in error_on_invalid_attributes"

- name: Error on invalid object
Expand Down Expand Up @@ -188,7 +183,6 @@
- "'current' not in error_on_invalid_object"
- "'previous' not in error_on_invalid_object"
- "'sent' not in error_on_invalid_object"
- "'proposed' not in error_on_invalid_object"

# Test case for certificate based error issue: https://github.com/CiscoDevNet/ansible-aci/issues/339
# Original error was with ospfCtxPol but same behaviour detected for tenant creation thus simplifying the test case
Expand Down
27 changes: 26 additions & 1 deletion tests/integration/targets/aci_rest/tasks/json_inline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
method: delete

# ADD TENANT
- name: Add tenant (normal mode)
- name: Add tenant (check mode)
cisco.aci.aci_rest: &tenant_present
host: '{{ aci_hostname }}'
username: '{{ aci_username }}'
Expand All @@ -37,6 +37,31 @@
}
}
}
register: cm_add_tenant
check_mode: true

- name: Verify checkmode did not create tenant
cisco.aci.aci_tenant:
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") }}'
name: ansible_test
state: query
register: cm_verify_checkmode_tenant

- name: Verify checkmode POST operation
assert:
that:
- cm_add_tenant is changed
- cm_add_tenant.proposed.fvTenant.attributes.name == "ansible_test"
- cm_verify_checkmode_tenant.current == []

- name: Add tenant (normal mode)
cisco.aci.aci_rest: *tenant_present
register: nm_add_tenant

- name: Add tenant again (normal mode)
Expand Down
27 changes: 26 additions & 1 deletion tests/integration/targets/aci_rest/tasks/json_string.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
method: delete

# ADD TENANT
- name: Add tenant (normal mode)
- name: Add tenant (check mode)
cisco.aci.aci_rest: &tenant_present
host: '{{ aci_hostname }}'
username: '{{ aci_username }}'
Expand All @@ -38,6 +38,31 @@
}
}
output_path: "/tmp/ansible_output_file.log"
register: cm_add_tenant
check_mode: true

- name: Verify checkmode did not create tenant
cisco.aci.aci_tenant:
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") }}'
name: ansible_test
state: query
register: cm_verify_checkmode_tenant

- name: Verify checkmode POST operation
assert:
that:
- cm_add_tenant is changed
- cm_add_tenant.proposed.fvTenant.attributes.name == "ansible_test"
- cm_verify_checkmode_tenant.current == []

- name: Add tenant (normal mode)
cisco.aci.aci_rest: *tenant_present
register: nm_add_tenant

- name: Add tenant again (normal mode)
Expand Down
21 changes: 18 additions & 3 deletions tests/integration/targets/aci_rest/tasks/xml_file.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,25 @@
check_mode: true
register: cm_add_tenant

- name: Verify checkmode did not create tenant
cisco.aci.aci_tenant:
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") }}'
name: ans_test_create
state: query
register: cm_verify_checkmode_tenant

- name: Assertions check for add tenant using ans_test_create xml template file with check mode
assert:
that:
- cm_add_tenant is not changed
- cm_add_tenant is changed
- '"ans_test_create" in cm_add_tenant.proposed'
- cm_verify_checkmode_tenant.current == []

- name: Add tenant using ans_test_create xml template file with normal mode
cisco.aci.aci_rest:
Expand Down Expand Up @@ -95,7 +110,7 @@
- name: Assertions check for update tenant description using ans_test_update xml template file with check mode
assert:
that:
- cm_update_tenant is not changed
- cm_update_tenant is changed

- name: Update tenant description using ans_test_update xml template file with normal mode
cisco.aci.aci_rest:
Expand Down Expand Up @@ -168,7 +183,7 @@
- name: Assertions check for delete tenant using ans_test_delete xml template file with check mode
assert:
that:
- cm_delete_tenant is not changed
- cm_delete_tenant is changed

- name: Delete tenant using ans_test_delete xml template file with normal mode
cisco.aci.aci_rest:
Expand Down
60 changes: 60 additions & 0 deletions tests/integration/targets/aci_rest/tasks/xml_string.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,66 @@
method: delete

# ADD TENANT
- name: Add tenant (check mode)
cisco.aci.aci_rest:
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
method: post
content:
<fvTenant name="ansible_test"/>
register: cm_add_tenant
check_mode: true

- name: Add tenant 2 (check mode)
cisco.aci.aci_rest:
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
method: post
content:
{
"fvTenant": {
"attributes": {
"name": "ansible_test"
}
}
}
register: cm_add_tenant_2
check_mode: true

- name: Verify checkmode did not create tenant
cisco.aci.aci_tenant: &tenant_cm_query
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") }}'
name: ansible_test
state: query
register: cm_verify_checkmode_tenant

- name: Verify checkmode POST operation
assert:
that:
- cm_add_tenant is changed
- '"ansible_test" in cm_add_tenant.proposed'
- cm_add_tenant_2 is changed
- '"ansible_test" in cm_add_tenant.proposed'
- cm_verify_checkmode_tenant.current == []

- name: Add tenant (normal mode)
cisco.aci.aci_rest: &tenant_present
host: '{{ aci_hostname }}'
Expand Down
27 changes: 26 additions & 1 deletion tests/integration/targets/aci_rest/tasks/yaml_inline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
method: delete

# ADD TENANT
- name: Add tenant (normal mode)
- name: Add tenant (check mode)
cisco.aci.aci_rest: &tenant_present
host: '{{ aci_hostname }}'
username: '{{ aci_username }}'
Expand All @@ -33,6 +33,31 @@
fvTenant:
attributes:
name: ansible_test
register: cm_add_tenant
check_mode: true

- name: Verify checkmode did not create tenant
cisco.aci.aci_tenant:
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") }}'
name: ansible_test
state: query
register: cm_verify_checkmode_tenant

- name: Verify checkmode POST operation
assert:
that:
- cm_add_tenant is changed
- cm_add_tenant.proposed.fvTenant.attributes.name == "ansible_test"
- cm_verify_checkmode_tenant.current == []

- name: Add tenant (normal mode)
cisco.aci.aci_rest: *tenant_present
register: nm_add_tenant

- name: Add tenant again (normal mode)
Expand Down
Loading

0 comments on commit 372d654

Please sign in to comment.