# Test code for the vmware_host 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)

- name: store the vcenter container ip
  set_fact:
    vcsim: "{{ lookup('env', 'vcenter_host') }}"

- debug: var=vcsim

- name: Wait for Flask controller to come up online
  wait_for:
    host: "{{ vcsim }}"
    port: 5000
    state: started

- name: kill vcsim
  uri:
    url: http://{{ vcsim }}:5000/killall

- name: start vcsim
  uri:
    url: http://{{ vcsim }}:5000/spawn?cluster=2
  register: vcsim_instance

- debug:
    var: vcsim_instance

- name: Wait for vcsim server to come up online
  wait_for:
    host: "{{ vcsim }}"
    port: 443
    state: started

- name: get a list of Datacenter from vcsim
  uri:
    url: http://{{ vcsim }}:5000/govc_find?filter=DC
  register: datacenters

- name: get a datacenter
  set_fact:
    dc1: "{{ datacenters.json[0] | basename }}"

- debug: var=dc1

- name: get a list of Cluster from vcsim
  uri:
    url: http://{{ vcsim }}:5000/govc_find?filter=CCR
  register: clusters

- name: get a cluster
  set_fact:
    ccr1: "{{ clusters.json[0] | basename }}"

- debug: var=ccr1

# Testcase: Add Host
- name: add host
  vmware_host:
    hostname: "{{ vcsim }}"
    username: "{{ vcsim_instance.json.username }}"
    password: "{{ vcsim_instance.json.password }}"
    validate_certs: no
    esxi_hostname: test_host_system_0001
    esxi_username: "{{ vcsim_instance.json.username }}"
    esxi_password: "{{ vcsim_instance.json.password }}"
    datacenter_name: "{{ dc1 }}"
    cluster_name: "{{ ccr1 }}"
    state: present
  register: add_host_result

- name: get a list of host system from vcsim after adding host system
  uri:
    url: http://{{ vcsim }}:5000/govc_find?filter=H
  register: host_list

- name: ensure host system is present
  assert:
    that:
        - add_host_result is changed
        - "{% for host in host_list.json if ((host | basename) == 'test_host_system_0001') -%} True {%- else -%} False {%- endfor %}"

# Testcase: Add Host again
- name: add host again
  vmware_host:
    hostname: "{{ vcsim }}"
    username: "{{ vcsim_instance.json.username }}"
    password: "{{ vcsim_instance.json.password }}"
    validate_certs: no
    esxi_hostname: test_host_system_0001
    esxi_username: "{{ vcsim_instance.json.username }}"
    esxi_password: "{{ vcsim_instance.json.password }}"
    datacenter_name: "{{ dc1 }}"
    cluster_name: "{{ ccr1 }}"
    state: present
  register: readd_host_result

- name: ensure precend task didn't changed anything
  assert:
    that:
        - not ( readd_host_result is changed)

# Testcase: Add Host via add_or_reconnect state
- name: add host via add_or_reconnect
  vmware_host:
    hostname: "{{ vcsim }}"
    username: "{{ vcsim_instance.json.username }}"
    password: "{{ vcsim_instance.json.password }}"
    validate_certs: no
    esxi_hostname: test_host_system_0002
    esxi_username: "{{ vcsim_instance.json.username }}"
    esxi_password: "{{ vcsim_instance.json.password }}"
    datacenter_name: "{{ dc1 }}"
    cluster_name: "{{ ccr1 }}"
    state: add_or_reconnect
  register: add_or_reconnect_host_result

- name: get a list of host system from vcsim after adding host system
  uri:
    url: http://{{ vcsim }}:5000/govc_find?filter=H
  register: host_list

- name: ensure host system is present
  assert:
    that:
        - add_or_reconnect_host_result is changed
        - "{% for host in host_list.json if ((host | basename) == 'test_host_system_0002') -%} True {%- else -%} False {%- endfor %}"

## Testcase: Reconnect Host
#
# ReconnectHost_Task need to be implemented in vcsim for this test to work
# https://github.com/vmware/govmomi/tree/master/vcsim#supported-methods
#
#- name: reconnect host
#  vmware_host:
#    hostname: "{{ vcsim }}"
#    username: "{{ vcsim_instance.json.username }}"
#    password: "{{ vcsim_instance.json.password }}"
#    validate_certs: no
#    esxi_hostname: test_host_system_0001
#    datacenter_name: "{{ dc1 }}"
#    cluster_name: "{{ ccr1 }}"
#    state: reconnect
#  register: reconnect_host_result
#
#- name: ensure host system has been reconnected
#  assert:
#    that:
#        - reconnect_host_result is changed
#        # it would be a good idea to check the events on the host to see the reconnect
#        # https://github.com/vmware/govmomi/blob/master/govc/USAGE.md#events
#        # "govc events ..." need to be callable from
#        # test/utils/docker/vcenter-simulator/flask_control.py

## Testcase: Remove Host
#
# EnterMaintenanceMode_Task need to be implemented in vcsim for this test to work
# https://github.com/vmware/govmomi/tree/master/vcsim#supported-methods
#
#- name: remove host
#  vmware_host:
#    hostname: "{{ vcsim }}"
#    username: "{{ vcsim_instance.json.username }}"
#    password: "{{ vcsim_instance.json.password }}"
#    validate_certs: no
#    esxi_hostname: test_host_system_0001
#    datacenter_name: "{{ dc1 }}"
#    cluster_name: "{{ ccr1 }}"
#    state: absent
#  register: remove_host_result
#
#- name: get a list of host system from vcsim after removing host system
#  uri:
#    url: http://{{ vcsim }}:5000/govc_find?filter=H
#  register: host_list
#
#- name: ensure host system is absent
#  assert:
#    that:
#        - remove_host_result is changed
#        - "{% for host in host_list.json if ((host | basename) == 'test_host_system_0001') -%} False {%- else -%} True {%- endfor %}"

## Testcase: Remove Host again
#
# EnterMaintenanceMode_Task need to be implemented in vcsim for this test to work
# https://github.com/vmware/govmomi/tree/master/vcsim#supported-methods
#
#- name: remove host again
#  vmware_host:
#    hostname: "{{ vcsim }}"
#    username: "{{ vcsim_instance.json.username }}"
#    password: "{{ vcsim_instance.json.password }}"
#    validate_certs: no
#    esxi_hostname: test_host_system_0001
#    datacenter_name: "{{ dc1 }}"
#    cluster_name: "{{ ccr1 }}"
#    state: absent
#  register: reremove_host_result
#
#- name: ensure precend task didn't changed anything
#  assert:
#    that:
#        - not ( reremove_host_result is changed)
