# Copyright: (c) 2019, Hetzner Cloud GmbH <info@hetzner-cloud.de>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
---
- name: setup ensure server is absent
  hcloud_server:
    name: "{{ hcloud_server_name }}"
    state: absent
  register: result

- name: create server
  hcloud_server:
    name: "{{ hcloud_server_name }}"
    server_type: cx11
    image: ubuntu-18.04
    state: started
    labels:
      key: value
  register: main_server
- name: verify create server
  assert:
    that:
      - main_server is changed
      - main_server.hcloud_server.name == "{{ hcloud_server_name }}"
      - main_server.hcloud_server.server_type == "cx11"
      - main_server.hcloud_server.status == "running"
      - main_server.root_password != ""


- name: test gather hcloud server infos in check mode
  hcloud_server_info:
  register: hcloud_server
  check_mode: yes

- name: verify test gather hcloud server infos in check mode
  assert:
    that:
      - hcloud_server.hcloud_server_info|selectattr('name','equalto','{{ hcloud_server_name }}') | list | count == 1


- name: test gather hcloud server infos with correct label selector
  hcloud_server_info:
    label_selector: "key=value"
  register: hcloud_server
- name: verify test gather hcloud server infos with correct label selector
  assert:
    that:
      - hcloud_server.hcloud_server_info|selectattr('name','equalto','{{ hcloud_server_name }}') | list | count == 1

- name: test gather hcloud server infos with wrong label selector
  hcloud_server_info:
    label_selector: "key!=value"
  register: hcloud_server
- name: verify test gather hcloud server infos with wrong label selector
  assert:
    that:
      - hcloud_server.hcloud_server_info | list | count == 0

- name: test gather hcloud server infos with correct name
  hcloud_server_info:
    name: "{{hcloud_server_name}}"
  register: hcloud_server
- name: verify test gather hcloud server infos with correct name
  assert:
    that:
      - hcloud_server.hcloud_server_info|selectattr('name','equalto','{{ hcloud_server_name }}') | list | count == 1

- name: test gather hcloud server infos with wrong name
  hcloud_server_info:
      name: "{{hcloud_server_name}}1"
  register: hcloud_server
- name: verify test gather hcloud server infos with wrong name
  assert:
    that:
      - hcloud_server.hcloud_server_info | list | count == 0

- name: test gather hcloud server infos with correct id
  hcloud_server_info:
    id: "{{main_server.hcloud_server.id}}"
  register: hcloud_server
- name: verify test gather hcloud server infos with correct id
  assert:
    that:
      - hcloud_server.hcloud_server_info|selectattr('name','equalto','{{ hcloud_server_name }}') | list | count == 1

- name: test gather hcloud server infos with wrong id
  hcloud_server_info:
      name: "4711"
  register: hcloud_server
- name: verify test gather hcloud server infos with wrong id
  assert:
    that:
      - hcloud_server.hcloud_server_info | list | count == 0

- name: cleanup
  hcloud_server:
    name: "{{ hcloud_server_name }}"
    state: absent
