# Test code for the vmware_datacenter module.
# Copyright: (c) 2017, Abhijeet Kasurde <akasurde@redhat.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

- import_role:
    name: prepare_vmware_tests

- &add_dc
  name: Add datacenter
  vmware_datacenter:
    validate_certs: false
    hostname: "{{ vcenter_hostname }}"
    username: "{{ vcenter_username }}"
    password: "{{ vcenter_password }}"
    datacenter_name: datacenter_0001
    state: present
  register: dc_result

- debug:
    var: dc_result

- name: Ensure datacenter is present
  assert:
    that:
        - dc_result.changed

- <<: *add_dc
  name: add datacenter again to check idempotent behavior

- name: Ensure datacenter status is not changed
  assert:
    that:
        - not dc_result.changed

- when: vcsim is not defined
  block:
  - name: Delete datacenter
    vmware_datacenter:
      validate_certs: false
      hostname: "{{ vcenter_hostname }}"
      username: "{{ vcenter_username }}"
      password: "{{ vcenter_password }}"
      datacenter_name: datacenter_0001
      state: absent
    register: dc_result_delete

  - name: Ensure datacenter is absent
    assert:
      that:
        - dc_result_delete.changed

- name: Create Datacenter with special characters
  vmware_datacenter:
    hostname: "{{ vcenter_hostname }}"
    username: "{{ vcenter_username }}"
    password: "{{ vcenter_password }}"
    validate_certs: false
    datacenter_name: 'Datacenter\/%'
    state: present
  register: create_datacenter_with_special_characters_result

- assert:
    that:
      - create_datacenter_with_special_characters_result.changed is sameas true

- name: Create Datacenter with special characters(idempotency check)
  vmware_datacenter:
    hostname: "{{ vcenter_hostname }}"
    username: "{{ vcenter_username }}"
    password: "{{ vcenter_password }}"
    validate_certs: false
    datacenter_name: 'Datacenter\/%'
    state: present
  register: create_datacenter_with_special_characters_idempotency_check_result

- assert:
    that:
      - create_datacenter_with_special_characters_idempotency_check_result.changed is sameas false

- name: Delete Datacenter with special characters
  vmware_datacenter:
    hostname: "{{ vcenter_hostname }}"
    username: "{{ vcenter_username }}"
    password: "{{ vcenter_password }}"
    validate_certs: false
    datacenter_name: 'Datacenter\/%'
    state: absent
  register: delete_datacenter_with_special_characters_result

- assert:
    that:
      - delete_datacenter_with_special_characters_result.changed is sameas true

- name: Delete Datacenter with special characters(idempotency check)
  vmware_datacenter:
    hostname: "{{ vcenter_hostname }}"
    username: "{{ vcenter_username }}"
    password: "{{ vcenter_password }}"
    validate_certs: false
    datacenter_name: 'Datacenter\/%'
    state: absent
  register: delete_datacenter_with_special_characters_idempotency_check_result

- assert:
    that:
      - delete_datacenter_with_special_characters_idempotency_check_result.changed is sameas false
