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

- &vapp_new_vm
  name: Create test VM with vAPP settings
  vmware_guest:
    validate_certs: False
    hostname: "{{ vcenter_hostname }}"
    username: "{{ vcenter_username }}"
    password: "{{ vcenter_password }}"
    folder: "vm"
    name: test_vm1
    datacenter: "{{ dc1 }}"
    cluster: "{{ ccr1 }}"
    resource_pool: Resources
    guest_id: centos64Guest
    hardware:
      memory_mb: 512
      num_cpus: 1
      scsi: paravirtual
    disk:
    - size_mb: 128
      type: thin
      datastore: "{{ ds2 }}"
    vapp_properties:
    - id: prop_id1
      category: category
      label: prop_label1
      type: string
      value: prop_value1
    - id: prop_id2
      category: category
      label: prop_label2
      type: string
      value: prop_value2
  register: vapp_vm

- debug: var=vapp_vm

- name: assert the vApp propeties were created
  assert:
    that:
      - "vapp_vm.failed == false"
      - "vapp_vm.changed == true"

- when: vcsim is not defined
  block:
    - <<: *vapp_new_vm
      name: Try to create same VM with same vAPP settings
      register: vapp_vm_no_change

    - debug: var=vapp_vm_no_change

    - name: Assert that vApp properties were not changed
      assert:
        that:
          - "vapp_vm_no_change.failed == false"
          - "not vapp_vm_no_change.changed"

- &vapp_edit_vm
  name: Edit one vApp property and removing another
  vmware_guest:
    validate_certs: False
    hostname: "{{ vcenter_hostname }}"
    username: "{{ vcenter_username }}"
    password: "{{ vcenter_password }}"
    folder: "vm"
    name: test_vm1
    datacenter: "{{ dc1 }}"
    vapp_properties:
    - id: prop_id1
      operation: remove
    - id: prop_id2
      value: prop_value3
    state: present
  register: vapp_vm

- debug: var=vapp_vm

- name: assert the VM was changed
  assert:
    that:
      - "vapp_vm.failed == false"
      - "vapp_vm.changed == true"

- when: vcsim is not defined
  block:
    - <<: *vapp_edit_vm
      name: Try to edit VM with vApp settings
      register: vapp_vm_no_change_edit

    - debug: var=vapp_vm_no_change_edit

    - name: assert the VM was changed
      assert:
        that:
          - "vapp_vm_no_change_edit.failed == false"
          - "vapp_vm_no_change_edit.changed == false"
