# Test code for the MSO modules
# Copyright: (c) 2018, Dag Wieers (@dagwieers) <dag@wieers.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 MultiSite host, username and password
  fail:
    msg: 'Please define the following variables: mso_hostname, mso_username and mso_password.'
  when: mso_hostname is not defined or mso_username is not defined or mso_password is not defined


# CLEAN ENVIRONMENT
- name: Remove tenant ansible_test2
  mso_tenant: &tenant_absent
    host: '{{ mso_hostname }}'
    username: '{{ mso_username }}'
    password: '{{ mso_password }}'
    validate_certs: '{{ mso_validate_certs | default(false) }}'
    use_ssl: '{{ mso_use_ssl | default(true) }}'
    use_proxy: '{{ mso_use_proxy | default(true) }}'
    output_level: '{{ mso_output_level | default("info") }}'
    tenant: ansible_test2
    state: absent

- name: Remove tenant ansible_test
  mso_tenant:
    <<: *tenant_absent
    tenant: ansible_test
  register: cm_remove_tenant


# ADD TENANT
- name: Add tenant (check_mode)
  mso_tenant: &tenant_present
    host: '{{ mso_hostname }}'
    username: '{{ mso_username }}'
    password: '{{ mso_password }}'
    validate_certs: '{{ mso_validate_certs | default(false) }}'
    use_ssl: '{{ mso_use_ssl | default(true) }}'
    use_proxy: '{{ mso_use_proxy | default(true) }}'
    output_level: '{{ mso_output_level | default("info") }}'
    tenant: ansible_test
    display_name: Ansible test title
    description: Ansible test tenant
    state: present
  check_mode: yes
  register: cm_add_tenant

- name: Verify cm_add_tenant
  assert:
    that:
    - cm_add_tenant is changed
    - cm_add_tenant.previous == {}
    - cm_add_tenant.current.id is not defined
    - cm_add_tenant.current.name == 'ansible_test'
    - cm_add_tenant.current.description == 'Ansible test tenant'

- name: Add tenant (normal mode)
  mso_tenant: *tenant_present
  register: nm_add_tenant

- name: nm_Verify add_tenant
  assert:
    that:
    - nm_add_tenant is changed
    - nm_add_tenant.previous == {}
    - nm_add_tenant.current.id is defined
    - nm_add_tenant.current.name == 'ansible_test'
    - nm_add_tenant.current.description == 'Ansible test tenant'

- name: Add tenant again (check_mode)
  mso_tenant: *tenant_present
  check_mode: yes
  register: cm_add_tenant_again

- name: Verify cm_add_tenant_again
  assert:
    that:
    - cm_add_tenant_again is not changed
    - cm_add_tenant_again.previous.name == 'ansible_test'
    - cm_add_tenant_again.previous.description == 'Ansible test tenant'
    - cm_add_tenant_again.current.id == nm_add_tenant.current.id
    - cm_add_tenant_again.current.name == 'ansible_test'
    - cm_add_tenant_again.current.description == 'Ansible test tenant'

- name: Add tenant again (normal mode)
  mso_tenant: *tenant_present
  register: nm_add_tenant_again

- name: Verify nm_add_tenant_again
  assert:
    that:
    - nm_add_tenant_again is not changed
    - nm_add_tenant_again.previous.name == 'ansible_test'
    - nm_add_tenant_again.previous.description == 'Ansible test tenant'
    - nm_add_tenant_again.current.id == nm_add_tenant.current.id
    - nm_add_tenant_again.current.name == 'ansible_test'
    - nm_add_tenant_again.current.description == 'Ansible test tenant'


# CHANGE TENANT
- name: Change tenant (check_mode)
  mso_tenant:
    <<: *tenant_present
    tenant_id: '{{ nm_add_tenant.current.id }}'
    tenant: ansible_test2
    description: Ansible test tenant 2
  check_mode: yes
  register: cm_change_tenant

- name: Verify cm_change_tenant
  assert:
    that:
    - cm_change_tenant is changed
    - cm_change_tenant.current.id == nm_add_tenant.current.id
    - cm_change_tenant.current.name == 'ansible_test2'
    - cm_change_tenant.current.description == 'Ansible test tenant 2'

- name: Change tenant (normal mode)
  mso_tenant:
    <<: *tenant_present
    tenant_id: '{{ nm_add_tenant.current.id }}'
    tenant: ansible_test2
    description: Ansible test tenant 2
    output_level: debug
  register: nm_change_tenant

- name: Verify nm_change_tenant
  assert:
    that:
    - nm_change_tenant is changed
    - nm_change_tenant.current.id == nm_add_tenant.current.id
    - nm_change_tenant.current.name == 'ansible_test2'
    - nm_change_tenant.current.description == 'Ansible test tenant 2'

- name: Change tenant again (check_mode)
  mso_tenant:
    <<: *tenant_present
    tenant_id: '{{ nm_add_tenant.current.id }}'
    tenant: ansible_test2
    description: Ansible test tenant 2
  check_mode: yes
  register: cm_change_tenant_again

- name: Verify cm_change_tenant_again
  assert:
    that:
    - cm_change_tenant_again is not changed
    - cm_change_tenant_again.current.id == nm_add_tenant.current.id
    - cm_change_tenant_again.current.name == 'ansible_test2'
    - cm_change_tenant_again.current.description == 'Ansible test tenant 2'

- name: Change tenant again (normal mode)
  mso_tenant:
    <<: *tenant_present
    tenant_id: '{{ nm_add_tenant.current.id }}'
    tenant: ansible_test2
    description: Ansible test tenant 2
  register: nm_change_tenant_again

- name: Verify nm_change_tenant_again
  assert:
    that:
    - nm_change_tenant_again is not changed
    - nm_change_tenant_again.current.id == nm_add_tenant.current.id
    - nm_change_tenant_again.current.name == 'ansible_test2'
    - nm_change_tenant_again.current.description == 'Ansible test tenant 2'


# QUERY ALL TENANTS
- name: Query all tenants (check_mode)
  mso_tenant: &tenant_query
    host: '{{ mso_hostname }}'
    username: '{{ mso_username }}'
    password: '{{ mso_password }}'
    validate_certs: '{{ mso_validate_certs | default(false) }}'
    use_ssl: '{{ mso_use_ssl | default(true) }}'
    use_proxy: '{{ mso_use_proxy | default(true) }}'
    output_level: '{{ mso_output_level | default("info") }}'
    state: query
  check_mode: yes
  register: cm_query_all_tenants

- name: Query all tenants (normal mode)
  mso_tenant: *tenant_query
  register: nm_query_all_tenants

- name: Verify query_all_tenants
  assert:
    that:
    - cm_query_all_tenants is not changed
    - nm_query_all_tenants is not changed
    # NOTE: Order of tenants is not stable between calls
    #- cm_query_all_tenants == nm_query_all_tenants


# QUERY A TENANT
- name: Query our tenant
  mso_tenant:
    <<: *tenant_query
    tenant: ansible_test2
  check_mode: yes
  register: cm_query_tenant

- name: Query our tenant
  mso_tenant:
    <<: *tenant_query
    tenant: ansible_test2
  register: nm_query_tenant

- name: Verify query_tenant
  assert:
    that:
    - cm_query_tenant is not changed
    - cm_query_tenant.current.id == nm_add_tenant.current.id
    - cm_query_tenant.current.name == 'ansible_test2'
    - cm_query_tenant.current.description == 'Ansible test tenant 2'
    - nm_query_tenant is not changed
    - nm_query_tenant.current.id == nm_add_tenant.current.id
    - nm_query_tenant.current.name == 'ansible_test2'
    - nm_query_tenant.current.description == 'Ansible test tenant 2'
    - cm_query_tenant == nm_query_tenant


# REMOVE TENANT
- name: Remove tenant (check_mode)
  mso_tenant: *tenant_absent
  check_mode: yes
  register: cm_remove_tenant

- name: Verify cm_remove_tenant
  assert:
    that:
    - cm_remove_tenant is changed
    - cm_remove_tenant.current == {}

- name: Remove tenant (normal mode)
  mso_tenant: *tenant_absent
  register: nm_remove_tenant

- name: Verify nm_remove_tenant
  assert:
    that:
    - nm_remove_tenant is changed
    - nm_remove_tenant.current == {}

- name: Remove tenant again (check_mode)
  mso_tenant: *tenant_absent
  check_mode: yes
  register: cm_remove_tenant_again

- name: Verify cm_remove_tenant_again
  assert:
    that:
    - cm_remove_tenant_again is not changed
    - cm_remove_tenant_again.current == {}

- name: Remove tenant again (normal mode)
  mso_tenant: *tenant_absent
  register: nm_remove_tenant_again

- name: Verify nm_remove_tenant_again
  assert:
    that:
    - nm_remove_tenant_again is not changed
    - nm_remove_tenant_again.current == {}


# QUERY NON-EXISTING TENANT
- name: Query non-existing tenant (check_mode)
  mso_tenant:
    <<: *tenant_query
    tenant: ansible_test
  check_mode: yes
  register: cm_query_non_tenant

- name: Query non-existing tenant (normal mode)
  mso_tenant:
    <<: *tenant_query
    tenant: ansible_test
  register: nm_query_non_tenant

# TODO: Implement more tests
- name: Verify query_non_tenant
  assert:
    that:
    - cm_query_non_tenant is not changed
    - nm_query_non_tenant is not changed
    - cm_query_non_tenant == nm_query_non_tenant