# Test code for the vmware_vm_vm_drs_rule module.
# Copyright: (c) 2017, Karsten Kaj Jakobsen <kj@patientsky.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

- when: vcsim is not defined
  block:
    - import_role:
        name: prepare_vmware_tests
      vars:
        setup_attach_host: true
        setup_datastore: true
        setup_virtualmachines: true

    - name: Create DRS VM group
      vmware_drs_group:
        hostname: "{{ vcenter_hostname }}"
        username: "{{ vcenter_username }}"
        password: "{{ vcenter_password }}"
        validate_certs: False
        # Options
        cluster_name: '{{ ccr1 }}'
        datacenter_name: '{{ dc1 }}'
        group_name: DC0_C0_VM_GR1
        vms: "{{ virtual_machines_in_cluster | map(attribute='name') | list }}"
        state: present
      register: drs_vm_group_01_results
    - debug: var=drs_vm_group_01_results

    - name: Create DRS Host group
      vmware_drs_group:
        hostname: "{{ vcenter_hostname }}"
        username: "{{ vcenter_username }}"
        password: "{{ vcenter_password }}"
        validate_certs: False
        # Options
        cluster_name: '{{ ccr1 }}'
        datacenter_name: '{{ dc1 }}'
        group_name: DC0_C0_HOST_GR1
        hosts: '{{ esxi_hosts }}'
        state: present
      register: drs_host_group_01_results
    - debug: var=drs_host_group_01_results

    - name: Create mandatory DRS Affinity rule for VM/Host
      vmware_vm_host_drs_rule:
        validate_certs: False
        hostname: "{{ vcenter_hostname }}"
        username: "{{ vcenter_username }}"
        password: "{{ vcenter_password }}"
        drs_rule_name: drs_rule_host_aff_0001
        vm_group_name: DC0_C0_VM_GR1
        host_group_name: DC0_C0_HOST_GR1
        cluster_name: "{{ ccr1 }}"
        enabled: True
        affinity_rule: True
        mandatory: True
      register: drs_rule_host_0001_results
    - debug: var=drs_rule_host_0001_results
    - assert:
        that:
            - "{{ drs_rule_host_0001_results.changed }}"

    # TODO: create host/vm group when vcsim supports this
    - name: Create non-mandatory DRS Anti-Affinity rule for VM/Host
      vmware_vm_host_drs_rule:
        validate_certs: False
        hostname: "{{ vcenter_hostname }}"
        username: "{{ vcenter_username }}"
        password: "{{ vcenter_password }}"
        drs_rule_name: drs_rule_host_aff_0001
        vm_group_name: DC0_C0_VM_GR1
        host_group_name: DC0_C0_HOST_GR1
        cluster_name: "{{ ccr1 }}"
        enabled: True
        affinity_rule: False
        mandatory: False
      register: drs_rule_host_0002_results
    - debug: var=drs_rule_host_0002_results
    - assert:
        that:
            - "{{ drs_rule_host_0001_results.changed }}"
