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

- name: test missing required parameters on create route
  hcloud_subnetwork:
    network: "{{ hcloud_network_name }}"
    state: present
  register: result
  ignore_errors: yes
- name: verify fail test missing required parameters on create route
  assert:
    that:
      - result is failed
      - 'result.msg == "missing required arguments: network_zone, type, ip_range"'

- name: test create subnetwork with checkmode
  hcloud_subnetwork:
    network: "{{ hcloud_network_name }}"
    ip_range: "10.0.0.0/16"
    type: "server"
    network_zone: "eu-central"
    state: present
  register: result
  check_mode: yes
- name: verify test create subnetwork with checkmode
  assert:
    that:
    - result is changed

- name: test create subnetwork
  hcloud_subnetwork:
    network: "{{ hcloud_network_name }}"
    ip_range: "10.0.0.0/16"
    type: "server"
    network_zone: "eu-central"
    state: present
  register: subnet
- name: verify create subnetwork
  assert:
    that:
    - subnet is changed
    - subnet.hcloud_subnetwork.network == "{{ hcloud_network_name }}"
    - subnet.hcloud_subnetwork.ip_range == "10.0.0.0/16"
    - subnet.hcloud_subnetwork.type == "server"
    - subnet.hcloud_subnetwork.network_zone == "eu-central"

- name: test create subnetwork idempotency
  hcloud_subnetwork:
    network: "{{ hcloud_network_name }}"
    ip_range: "10.0.0.0/16"
    type: "server"
    network_zone: "eu-central"
    state: present
  register: result
- name: verify create subnetwork idempotency
  assert:
    that:
    - result is not changed

- name: test absent 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 test absent 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
