# Test code for the ACI modules
# Copyright: (c) 2017, Jacob McGill (@jmcgill298)

# 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 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
    state: present
    tenant: anstest
  register: tenant_present

- name: ensure contracts exist for tests to kick off
  aci_contract:
    <<: *aci_tenant_present
    contract: "{{ item }}"
  with_items: ["anstest_http", "anstest_https", "anstest_db"]

- 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 contract to epg - check mode works
  aci_epg_to_contract: &aci_epg_provide_present
    <<: *aci_epg_present
    contract_type: provider
    contract: "anstest_http"
  check_mode: yes
  register: provide_present_check_mode

- name: bind contract to epg - provide works
  aci_epg_to_contract:
    <<: *aci_epg_provide_present
  register: provide_present

- name: bind contract to epg - consume works
  aci_epg_to_contract: &aci_epg_consume_present
    <<: *aci_epg_provide_present
    contract_type: consumer
    contract: anstest_db
  register: consume_present

- name: bind contract to epg - add additional contract
  aci_epg_to_contract: &aci_epg_provide_present2
    <<: *aci_epg_provide_present
    contract: anstest_https
    provider_match: at_most_one
  register: provide_present2

- name: bind contract to epg - idempotency works
  aci_epg_to_contract:
    <<: *aci_epg_provide_present
  register: idempotent_present

- name: missing param - failure message works
  aci_epg_to_contract:
    <<: *aci_tenant_present
    contract_type: provider
  ignore_errors: yes
  register: missing_param_present

- name: missing required param - failure message works
  aci_epg_to_contract:
    <<: *aci_tenant_present
  ignore_errors: yes
  register: missing_required_present

- name: incompatible param - failure message works
  aci_epg_to_contract:
    <<: *aci_epg_consume_present
    provider_match: all
  ignore_errors: yes
  register: incompatible_present

- name: present assertions
  assert:
    that:
      - provide_present_check_mode.changed == true
      - 'provide_present_check_mode.sent == {"fvRsProv": {"attributes": {"tnVzBrCPName": "anstest_http"}}}'
      - provide_present.changed == true
      - provide_present.sent == provide_present_check_mode.sent
      - provide_present.previous == []
      - consume_present.changed == true
      - consume_present.previous == []
      - 'consume_present.sent == {"fvRsCons": {"attributes": {"tnVzBrCPName": "anstest_db"}}}'
      - provide_present2.changed == true
      - provide_present2.previous == []
      - missing_param_present.failed == true
      - 'missing_param_present.msg == "state is present but all of the following are missing: ap, contract, epg"'
      - missing_required_present.failed == true
      - 'missing_required_present.msg == "missing required arguments: contract_type"'
      - incompatible_present.failed == true
      - incompatible_present.msg == "the 'provider_match' is only configurable for Provided Contracts"

- name: get binding
  aci_epg_to_contract:
    <<: *aci_epg_provide_present2
    state: query
  register: query_provide_contract

- name: get binding
  aci_epg_to_contract:
    <<: *aci_epg_consume_present
    state: query
  register: query_consume_contract

- name: get all bindings
  aci_epg_to_contract:
    <<: *aci_tenant_present
    state: query
    tenant: "{{ fakevar | default(omit) }}"
    contract_type: provider
  register: query_all

- name: missing required param - failure message works
  aci_epg_to_contract:
    <<: *aci_tenant_present
    state: query
  ignore_errors: yes
  register: missing_required_query

- name: query assertions
  assert:
    that:
      - query_provide_contract.changed == false
      - query_provide_contract.current != []
      - '"class/fvRsProv.json" in query_provide_contract.url'
      - query_consume_contract.changed == false
      - query_consume_contract.current != []
      - '"class/fvRsCons.json" in query_consume_contract.url'
      - query_all.changed == false
      - '"class/fvRsProv.json" in query_all.url'
      - missing_required_query.failed == true
      - 'missing_required_query.msg == "missing required arguments: contract_type"'

- name: delete consume binding - check mode works
  aci_epg_to_contract: &aci_epg_consume_absent
    <<: *aci_epg_consume_present
    state: absent
  check_mode: yes
  register: consume_absent_check_mode

- name: delete consume binding - deletion works
  aci_epg_to_contract:
    <<: *aci_epg_consume_absent
  register: consume_absent

- name: delete provide binding - deletion works
  aci_epg_to_contract:
    <<: *aci_epg_provide_present
    state: absent
  register: provide_absent

- name: delete provide binding - deletion works
  aci_epg_to_contract:
    <<: *aci_epg_provide_present2
    state: absent
  register: provide_absent2

- name: delete consume binding - idempotency works
  aci_epg_to_contract:
    <<: *aci_epg_consume_absent
  register: consume_absent_idempotent

- name: missing param - failure message works
  aci_epg_to_contract:
    <<: *aci_epg_consume_absent
    contract: "{{ fakevar | default(omit) }}"
  ignore_errors: yes
  register: missing_param_absent

- name: missing required param - failure message works
  aci_epg_to_contract:
    <<: *aci_epg_consume_absent
    contract_type: "{{ fakevar | default(omit) }}"
  ignore_errors: yes
  register: missing_required_absent

- name: absent assertions
  assert:
    that:
      - consume_absent_check_mode.changed == true
      - consume_absent_check_mode.previous.0.fvRsCons is defined
      - consume_absent.changed == true
      - consume_absent.previous == consume_absent_check_mode.previous
      - provide_absent.changed == true
      - provide_absent.previous.0.fvRsProv is defined
      - provide_absent2.changed == true
      - consume_absent_idempotent.changed == false
      - consume_absent_idempotent.previous == []
      - missing_param_absent.failed == true
      - 'missing_param_absent.msg == "state is absent but all of the following are missing: contract"'
      - missing_required_absent.failed == true
      - 'missing_required_absent.msg == "missing required arguments: contract_type"'

- name: cleanup contracts
  aci_contract:
    <<: *aci_tenant_present
    state: absent
    contract: "{{ item }}"
  with_items: ["anstest_http", "anstest_https", "anstest_db"]

- name: cleanup epg
  aci_epg:
    <<: *aci_epg_present
    state: absent
  when: epg_present.changed == true

- name: cleanup ap
  aci_ap:
    <<: *aci_ap_present
    state: absent
  when: ap_present.changed == true

- name: cleanup tenant
  aci_tenant:
    <<: *aci_tenant_present
    state: absent
  when: tenant_present.changed == true
