# 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 network
  hcloud_network:
    name: "{{ hcloud_network_name }}"
    ip_range: "10.0.0.0/8"
    state: present
  register: network
- name: verify setup network
  assert:
    that:
    - network is success

- name: setup subnetwork
  hcloud_subnetwork:
    network: "{{ hcloud_network_name }}"
    ip_range: "10.0.0.0/16"
    type: "server"
    network_zone: "eu-central"
    state: present
  register: subnetwork
- name: verify subnetwork
  assert:
    that:
    - subnetwork is success

- name: setup server
  hcloud_server:
    name: "{{hcloud_server_name}}"
    server_type: cx11
    image: ubuntu-18.04
    state: started
    location: "fsn1"
  register: server
- name: verify setup server
  assert:
    that:
      - server is success

- name: test missing required parameters on create server network
  hcloud_server_network:
    state: present
  register: result
  ignore_errors: yes
- name: verify fail test missing required parameters on create server network
  assert:
    that:
      - result is failed
      - 'result.msg == "missing required arguments: network, server"'

- name: test create server network with checkmode
  hcloud_server_network:
    network: "{{ hcloud_network_name }}"
    server: "{{hcloud_server_name}}"
    state: present
  register: result
  check_mode: yes
- name: verify test create server network with checkmode
  assert:
    that:
    - result is changed

- name: test create server network
  hcloud_server_network:
    network: "{{ hcloud_network_name }}"
    server: "{{hcloud_server_name}}"
    state: present
  register: serverNetwork
- name: verify create server network
  assert:
    that:
    - serverNetwork is changed
    - serverNetwork.hcloud_server_network.network == hcloud_network_name
    - serverNetwork.hcloud_server_network.server == hcloud_server_name

- name: test create server network idempotency
  hcloud_server_network:
    network: "{{ hcloud_network_name }}"
    server: "{{hcloud_server_name}}"
    state: present
  register: serverNetwork
- name: verify create server network idempotency
  assert:
    that:
    - serverNetwork is not changed

- name: test absent server network
  hcloud_server_network:
    network: "{{ hcloud_network_name }}"
    server: "{{hcloud_server_name}}"
    state: absent
  register: result
- name: verify test absent server network
  assert:
    that:
    - result is changed

- name: test create server network with specified ip
  hcloud_server_network:
    network: "{{ hcloud_network_name }}"
    server: "{{hcloud_server_name}}"
    ip: "10.0.0.2"
    state: present
  register: serverNetwork
- name: verify create server network with specified ip
  assert:
    that:
    - serverNetwork is changed
    - serverNetwork.hcloud_server_network.network == hcloud_network_name
    - serverNetwork.hcloud_server_network.server == hcloud_server_name
    - serverNetwork.hcloud_server_network.ip == "10.0.0.2"

- name: cleanup create server network with specified ip
  hcloud_server_network:
    network: "{{ hcloud_network_name }}"
    server: "{{hcloud_server_name}}"
    state: absent
  register: result
- name: verify cleanup create server network with specified ip
  assert:
    that:
    - result is changed

- name: test create server network with alias ips
  hcloud_server_network:
    network: "{{ hcloud_network_name }}"
    server: "{{hcloud_server_name}}"
    ip: "10.0.0.2"
    alias_ips:
      - "10.0.1.2"
      - "10.0.2.3"
    state: present
  register: serverNetwork
- name: verify create server network with alias ips
  assert:
    that:
    - serverNetwork is changed
    - serverNetwork.hcloud_server_network.network == hcloud_network_name
    - serverNetwork.hcloud_server_network.server == hcloud_server_name
    - serverNetwork.hcloud_server_network.ip == "10.0.0.2"
    - 'serverNetwork.hcloud_server_network.alias_ips[0] == "10.0.2.3"'
    - 'serverNetwork.hcloud_server_network.alias_ips[1] == "10.0.1.2"'

- name: cleanup create server network with alias ips
  hcloud_server_network:
    network: "{{ hcloud_network_name }}"
    server: "{{hcloud_server_name}}"
    state: absent
  register: result
- name: verify cleanup create server network with alias ips
  assert:
    that:
    - result is changed

- name: cleanup server
  hcloud_server:
    name: "{{ hcloud_server_name }}"
    state: absent
  register: result
- name: verify cleanup server
  assert:
    that:
    - result is success

- name: cleanup subnetwork
  hcloud_subnetwork:
    network: "{{ hcloud_network_name }}"
    ip_range: "10.0.0.0/16"
    type: "server"
    network_zone: "eu-central"
    state: absent
  register: result
- name: verify cleanup subnetwork
  assert:
    that:
    - result is changed

- name: cleanup
  hcloud_network:
    name: "{{hcloud_network_name}}"
    state: absent
  register: result
- name: verify cleanup
  assert:
    that:
      - result is success
