From 54aa6f3921d9e9f3d432cdc3dc12f67a30c83aa1 Mon Sep 17 00:00:00 2001 From: tknipper11 Date: Wed, 22 May 2019 17:47:44 -0500 Subject: [PATCH] ACI CDP Interface Policy Module Creation (#54818) * initial commit * Fix typo in integration test which caused an assertion to fail * Updated integration testing * Fix Ansible Sanity Errors * Changed version added to 2.8 from 2.9 * added from __future__ import absolute_import, division, print_function __metaclass__ = type --- .../network/aci/aci_interface_policy_cdp.py | 235 ++++++++++++++++++ .../targets/aci_interface_policy_cdp/aliases | 1 + .../aci_interface_policy_cdp/tasks/main.yml | 106 ++++++++ 3 files changed, 342 insertions(+) create mode 100644 lib/ansible/modules/network/aci/aci_interface_policy_cdp.py create mode 100644 test/integration/targets/aci_interface_policy_cdp/aliases create mode 100644 test/integration/targets/aci_interface_policy_cdp/tasks/main.yml diff --git a/lib/ansible/modules/network/aci/aci_interface_policy_cdp.py b/lib/ansible/modules/network/aci/aci_interface_policy_cdp.py new file mode 100644 index 00000000000..9bda55e2693 --- /dev/null +++ b/lib/ansible/modules/network/aci/aci_interface_policy_cdp.py @@ -0,0 +1,235 @@ +#!/usr/bin/python + +# Copyright: (c) 2019, Tim Knipper +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + +ANSIBLE_METADATA = { + 'metadata_version': '1.1', + 'status': ['preview'], + 'supported_by': 'community' +} + +DOCUMENTATION = r''' +--- +module: aci_interface_policy_cdp +short_description: Manage CDP interface policies (cdp:IfPol) +description: +- Manage CDP interface policies on Cisco ACI fabrics. +version_added: '2.8' +options: + cdp_policy: + description: + - The CDP interface policy name. + type: str + required: yes + aliases: [ cdp_interface, name ] + description: + description: + - The description for the CDP interface policy name. + type: str + aliases: [ descr ] + admin_state: + description: + - Enable or Disable CDP state. + - The APIC defaults to C(yes) when unset during creation. + type: bool + state: + description: + - Use C(present) or C(absent) for adding or removing. + - Use C(query) for listing an object or multiple objects. + type: str + choices: [ absent, present, query ] + default: present +extends_documentation_fragment: aci +seealso: +- name: APIC Management Information Model reference + description: More information about the internal APIC class B(cdp:IfPol). + link: https://developer.cisco.com/docs/apic-mim-ref/ +author: +- Tim Knipper (@tknipper11) +''' + +# FIXME: Add more, better examples +EXAMPLES = r''' +- aci_interface_policy_cdp: + host: '{{ hostname }}' + username: '{{ username }}' + password: '{{ password }}' + cdp_policy: '{{ cdp_policy }}' + description: '{{ description }}' + admin_state: '{{ admin_state }}' + delegate_to: localhost +''' + +RETURN = r''' +current: + description: The existing configuration from the APIC after the module has finished + returned: success + type: list + sample: + [ + { + "cdpIfPol": { + "attributes": { + "adminSt": "disabled", + "annotation": "", + "descr": "Ansible Created CDP Test Policy", + "dn": "uni/infra/cdpIfP-Ansible_CDP_Test_Policy", + "name": "Ansible_CDP_Test_Policy", + "nameAlias": "", + "ownerKey": "", + "ownerTag": "" + } + } + } + ] +error: + description: The error information as returned from the APIC + returned: failure + type: dict + sample: + { + "code": "122", + "text": "unknown managed object class foo" + } +raw: + description: The raw output returned by the APIC REST API (xml or json) + returned: parse error + type: str + sample: '' +sent: + description: The actual/minimal configuration pushed to the APIC + returned: info + type: list + sample: + { + "fvTenant": { + "attributes": { + "descr": "Production environment" + } + } + } +previous: + description: The original configuration from the APIC before the module has started + returned: info + type: list + sample: + [ + { + "fvTenant": { + "attributes": { + "descr": "Production", + "dn": "uni/tn-production", + "name": "production", + "nameAlias": "", + "ownerKey": "", + "ownerTag": "" + } + } + } + ] +proposed: + description: The assembled configuration from the user-provided parameters + returned: info + type: dict + sample: + { + "fvTenant": { + "attributes": { + "descr": "Production environment", + "name": "production" + } + } + } +filter_string: + description: The filter string used for the request + returned: failure or debug + type: str + sample: ?rsp-prop-include=config-only +method: + description: The HTTP method used for the request to the APIC + returned: failure or debug + type: str + sample: POST +response: + description: The HTTP response from the APIC + returned: failure or debug + type: str + sample: OK (30 bytes) +status: + description: The HTTP status from the APIC + returned: failure or debug + type: int + sample: 200 +url: + description: The HTTP url used for the request to the APIC + returned: failure or debug + type: str + sample: https://10.11.12.13/api/mo/uni/tn-production.json +''' +from ansible.module_utils.network.aci.aci import ACIModule, aci_argument_spec + +from ansible.module_utils.basic import AnsibleModule + + +def main(): + argument_spec = aci_argument_spec() + argument_spec.update( + cdp_policy=dict(type='str', required=False, aliases=['cdp_interface', 'name']), # Not required for querying all objects + description=dict(type='str', aliases=['descr']), + admin_state=dict(type='bool'), + state=dict(type='str', default='present', choices=['absent', 'present', 'query']), + ) + + module = AnsibleModule( + argument_spec=argument_spec, + supports_check_mode=True, + required_if=[ + ['state', 'absent', ['cdp_policy']], + ['state', 'present', ['cdp_policy']], + ], + ) + + aci = ACIModule(module) + + cdp_policy = module.params['cdp_policy'] + description = module.params['description'] + admin_state = aci.boolean(module.params['admin_state'], 'enabled', 'disabled') + state = module.params['state'] + + aci.construct_url( + root_class=dict( + aci_class='cdpIfPol', + aci_rn='infra/cdpIfP-{0}'.format(cdp_policy), + module_object=cdp_policy, + target_filter={'name': cdp_policy}, + ), + ) + + aci.get_existing() + + if state == 'present': + aci.payload( + aci_class='cdpIfPol', + class_config=dict( + name=cdp_policy, + descr=description, + adminSt=admin_state, + ), + ) + + aci.get_diff(aci_class='cdpIfPol') + + aci.post_config() + + elif state == 'absent': + aci.delete_config() + + aci.exit_json() + + +if __name__ == '__main__': + main() diff --git a/test/integration/targets/aci_interface_policy_cdp/aliases b/test/integration/targets/aci_interface_policy_cdp/aliases new file mode 100644 index 00000000000..ad7ccf7ada2 --- /dev/null +++ b/test/integration/targets/aci_interface_policy_cdp/aliases @@ -0,0 +1 @@ +unsupported diff --git a/test/integration/targets/aci_interface_policy_cdp/tasks/main.yml b/test/integration/targets/aci_interface_policy_cdp/tasks/main.yml new file mode 100644 index 00000000000..f82b3b78a02 --- /dev/null +++ b/test/integration/targets/aci_interface_policy_cdp/tasks/main.yml @@ -0,0 +1,106 @@ +# Test code for the ACI modules +# Copyright: (c) 2019, Tim Knipper (tknipper11) + +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +- name: Test that we have an ACI APIC host, ACI username and ACI password + fail: + msg: 'Please define the following variables: aci_hostname, aci_username and aci_password.' + when: aci_hostname is not defined or aci_username is not defined or aci_password is not defined + +# CLEAN ENVIRONMENT +- name: Remove CDP Test Policy + aci_interface_policy_cdp: + name: Ansible_CDP_Test_Policy + 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(false) }}' + output_level: debug + state: absent + register: cdp_delete + + + +- name: Create CDP Test Policy + aci_interface_policy_cdp: + name: Ansible_CDP_Test_Policy + 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(false) }}' +# output_level: debug + state: present + register: cdp_create +- debug: + var: cdp_create + +- assert: + that: + - cdp_create is changed + + +- name: test for idempotency + aci_interface_policy_cdp: + name: Ansible_CDP_Test_Policy + 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(false) }}' +# output_level: debug + state: present + register: cdp_idem + +- name: Assert that idempotency is not changed + assert: + that: + - cdp_idem is not changed + + + +- name: Create CDP Disable Test Policy + aci_interface_policy_cdp: + name: Ansible_CDP_Test_Policy + 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(false) }}' +# output_level: debug + state: present + admin_state: no + register: cdp_disable +- debug: + var: cdp_disable + +- name: Assert that CDP is Disabled + assert: + that: + - 'cdp_disable.current.0.cdpIfPol.attributes.adminSt == "disabled"' + + +- name: Query CDP Policy + aci_interface_policy_cdp: + 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(false) }}' +# output_level: debug + state: query + register: cdp_query +- debug: + var: cdp_query + +- name: CDP Query Assertion + assert: + that: + - cdp_query is not changed \ No newline at end of file