# Test code for the ACI modules
# Copyright: (c) 2017, Bruno Calogero <bcalogero@cisco.com>

# 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

- name: Ensure static path to epg is deleted for test kick off
  aci_static_binding_to_epg: &aci_static_binding_to_epg_absent
    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
    tenant: anstest
    ap: anstest
    epg: anstest
    interface_type: switch_port
    pod: 1
    leafs: 101
    interface: '1/7'
    state: absent

- name: Ensure tenant exists for tests to kick off
  aci_tenant: &aci_tenant_present
    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
    tenant: anstest
    state: present
  register: tenant_present

- name: Ensure ap exists
  aci_ap: &aci_ap_present
    <<: *aci_tenant_present
    ap: anstest
  register: ap_present

- name: Ensure epg exists
  aci_epg: &aci_epg_present
    <<: *aci_ap_present
    epg: anstest
  register: epg_present

- name: Bind static-binding to epg - check mode works
  aci_static_binding_to_epg: &aci_static_binding_to_epg_present
    <<: *aci_epg_present
    encap_id: 222
    deploy_immediacy: lazy
    interface_mode: trunk
    interface_type: switch_port
    pod: 1
    leafs: 101
    interface: '1/7'
  check_mode: yes
  register: provide_present_check_mode

- name: Bind static-binding to epg - provide works (creation w/o check-mode)
  aci_static_binding_to_epg:
    <<: *aci_static_binding_to_epg_present
  ignore_errors: yes
  register: provide_present

- name: Bind static-binding to epg - primary_encap_id works
  aci_static_binding_to_epg: &primary_encap_id_present
    <<: *aci_static_binding_to_epg_present
    primary_encap_id: 50
  register: primary_ecap_id_present

- name: Bind contract to epg - idempotency works again
  aci_static_binding_to_epg:
    <<: *primary_encap_id_present
  register: idempotent_present

- name: Bind contract to epg - update description (check mode)
  aci_static_binding_to_epg:
    <<: *primary_encap_id_present
    description: Binding description
  check_mode: yes
  register: description_cm

- name: Bind contract to epg - update description (run mode)
  aci_static_binding_to_epg:
    <<: *primary_encap_id_present
    description: Binding description
  register: description

- name: Bind contract to epg - update description (check mode)
  aci_static_binding_to_epg:
    <<: *primary_encap_id_present
    description: Binding description
  register: idempotent_description_cm

- name: Bind contract to epg - update description (run mode)
  aci_static_binding_to_epg:
    <<: *primary_encap_id_present
    description: Binding description
  register: idempotent_description

- name: Missing required param - failure message works
  aci_static_binding_to_epg:
    <<: *aci_tenant_present
  ignore_errors: yes
  register: missing_required_present

- name: Present assertions
  assert:
    that:
      - provide_present_check_mode is changed
      - 'provide_present_check_mode.sent == {"fvRsPathAtt": {"attributes": { "encap": "vlan-222", "instrImedcy": "lazy", "mode": "regular", "tDn": "topology/pod-1/paths-101/pathep-[eth1/7]"}}}'
      - provide_present is changed
      - provide_present.sent == provide_present_check_mode.sent
      - provide_present.previous == []
      - primary_ecap_id_present is changed
      - 'primary_ecap_id_present.sent == {"fvRsPathAtt": {"attributes": {"primaryEncap": "vlan-50"}}}'
      - description_cm is changed
      - description is changed
      - idempotent_description_cm is not changed
      - idempotent_description is not changed
      - missing_required_present is failed
      - 'missing_required_present.msg == "state is present but all of the following are missing: ap, encap_id, epg, interface, leafs, pod_id"'
      - missing_required_present is failed


- name: Query specific binding
  aci_static_binding_to_epg:
    <<: *primary_encap_id_present
    state: query
  register: query_static_binding

- name: Query all bindings
  aci_static_binding_to_epg:
    <<: *aci_tenant_present
    state: query
  register: query_all

- name: Query assertions
  assert:
    that:
      - query_static_binding is not changed
      - query_static_binding.current != []
      - '"uni/tn-anstest/ap-anstest/epg-anstest/rspathAtt-[topology/pod-1/paths-101/pathep-[eth1/7]]" in query_static_binding.url'
      - query_all is not changed
      - '"uni/tn-anstest.json" in query_all.url'


- name: Delete provide binding - deletion works
  aci_static_binding_to_epg:
    <<: *primary_encap_id_present
    state: absent
  register: provide_absent

- name: Delete provide binding - idempotency works
  aci_static_binding_to_epg:
    <<: *primary_encap_id_present
    state: absent
  register: provide_absent_idempotent

- name: Missing param - failure message works
  aci_static_binding_to_epg:
    <<: *aci_tenant_present
    state: absent
  ignore_errors: yes
  register: missing_param_absent

- name: Absent assertions
  assert:
    that:
      - provide_absent is changed
      - provide_absent.previous.0.fvRsPathAtt is defined
      - provide_absent_idempotent is not changed
      - provide_absent_idempotent.previous == []
      - missing_param_absent is failed
      - missing_param_absent is failed
      - 'missing_param_absent.msg == "state is absent but all of the following are missing: ap, epg, interface, leafs, pod_id"'

- name: Cleanup binding
  aci_static_binding_to_epg:
    <<: *aci_static_binding_to_epg_absent

- name: Cleanup epg
  aci_epg:
    <<: *aci_epg_present
    state: absent

- name: Cleanup ap
  aci_ap:
    <<: *aci_ap_present
    state: absent

- name: Cleanup tenant
  aci_tenant:
    <<: *aci_tenant_present
    state: absent
