# Test code for the ACI modules
# Copyright: (c) 2020, Shreyas Srish (@shrsr)

# GNU General Public License v3.0+ (see LICENSE 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

- name: Set vars
  set_fact: 
   aci_info: &aci_info
    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: debug

# CLEAN ENVIRONMENT
- name: Remove the ansible_tenant
  aci_tenant:
    <<: *aci_info 
    tenant: ansible_tenant
    state: absent

- name: Add a new tenant
  aci_tenant:
    <<: *aci_info 
    tenant: ansible_tenant
    description: Ansible tenant
    state: present

- name: Add a new L2Out
  aci_l2out:
    <<: *aci_info
    tenant: ansible_tenant
    l2out: ansible_l2out
    description: Test deployment 
    bd: ansible_bd
    domain: l2Dom
    vlan: 3200
    state: present
  register: add_l2out

- name: Verify add_l2out
  assert:
    that:
    - add_l2out.current.0.l2extOut.attributes.dn == "uni/tn-ansible_tenant/l2out-ansible_l2out"
    - add_l2out.current.0.l2extOut.attributes.name == "ansible_l2out"

- name: Add the L2Out again
  aci_l2out:
    <<: *aci_info
    tenant: ansible_tenant
    l2out: ansible_l2out
    description: Test deployment 
    bd: ansible_bd
    domain: l2Dom
    vlan: 3200
    state: present
  register: add_l2out_again

- name: Verify add_l2out_again
  assert:
    that:
    - add_l2out_again is not changed

- name: Add a new L2Out 
  aci_l2out:
    <<: *aci_info
    tenant: ansible_tenant
    l2out: ansible_l2out_2
    description: Test deployment 
    bd: ansible_bd
    domain: l2Dom
    vlan: 3200
    state: present
  register: add_l2out_2

- name: Verify add_l2out_2
  assert:
    that:
    - add_l2out_2.current.0.l2extOut.attributes.dn == "uni/tn-ansible_tenant/l2out-ansible_l2out_2"
    - add_l2out_2.current.0.l2extOut.attributes.name == "ansible_l2out_2"

- name: Query the L2Out  
  aci_l2out:
    <<: *aci_info
    tenant: ansible_tenant
    l2out: ansible_l2out
    state: query
  register: query_l2out

- name: Verify query_l2out
  assert:
    that:
    - query_l2out is not changed
    - query_l2out.current.0.l2extOut.attributes.dn == "uni/tn-ansible_tenant/l2out-ansible_l2out"
    - query_l2out.current.0.l2extOut.attributes.name == "ansible_l2out"

- name: Query all l2outs under a specific tenant
  aci_l2out:
    <<: *aci_info
    tenant: ansible_tenant
    state: query
  register: query_l2out_all

- name: Verify query_l2out_all
  assert:
    that:
    - query_l2out_all is not changed

- name: Remove the L2Out 
  aci_l2out:
    <<: *aci_info
    tenant: ansible_tenant
    l2out: ansible_l2out
    state: absent
  register: remove_l2out

- name: Verify remove_l2out
  assert:
    that:
    - remove_l2out is changed
    - remove_l2out.previous.0.l2extOut.attributes.dn == "uni/tn-ansible_tenant/l2out-ansible_l2out"
    - remove_l2out.previous.0.l2extOut.attributes.name == "ansible_l2out"