# Copyright (c) 2018, Yanis Guenane <yanis+ansible@guenane.org>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
---
- name: setup
  vultr_network:
    name: "{{ vultr_network_name }}"
    state: absent
  register: result
- name: verify setup
  assert:
    that:
    - result is success

- name: test fail if missing name
  vultr_network:
  register: result
  ignore_errors: yes
- name: verify test fail if missing name
  assert:
    that:
    - result is failed
    - 'result.msg == "missing required arguments: name"'

- name: test fail if missing params for state=present
  vultr_network:
    name: "{{ vultr_network_name }}"
  register: result
  ignore_errors: yes
- name: verify fail if missing params for state=present
  assert:
    that:
    - result is failed
    - 'result.msg == "state is present but all of the following are missing: cidr, region"'

- name: test create network in check mode
  vultr_network:
    name: "{{ vultr_network_name }}"
    cidr: "{{ vultr_network_cidr }}"
    region: "{{ vultr_network_region }}"
  register: result
  check_mode: yes
- name: verify test create server in check mode
  assert:
    that:
    - result is changed

- name: test create network
  vultr_network:
    name: "{{ vultr_network_name }}"
    cidr: "{{ vultr_network_cidr }}"
    region: "{{ vultr_network_region }}"
  register: result

- name: verify test create network
  assert:
    that:
    - result is changed
    - result.vultr_network.name == "{{ vultr_network_name }}"
    - result.vultr_network.region == "{{ vultr_network_region }}"
    - result.vultr_network.v4_subnet == "{{ vultr_network_cidr.split('/')[0] }}"
    - result.vultr_network.v4_subnet_mask == 24

- name: test create network idempotence
  vultr_network:
    name: "{{ vultr_network_name }}"
    cidr: "{{ vultr_network_cidr }}"
    region: "{{ vultr_network_region }}"
  register: result

- name: verify test network idempotence
  assert:
    that:
    - result is not changed
    - result.vultr_network.name == "{{ vultr_network_name }}"
    - result.vultr_network.region == "{{ vultr_network_region }}"
    - result.vultr_network.v4_subnet == "{{ vultr_network_cidr.split('/')[0] }}"
    - result.vultr_network.v4_subnet_mask == 24

- name: test destroy network in check mode
  vultr_network:
    name: "{{ vultr_network_name }}"
    state: absent
  register: result
  check_mode: yes

- name: verify test destroy network in check mode
  assert:
    that:
    - result is changed
    - result.vultr_network.name == "{{ vultr_network_name }}"

- name: test destroy network volume
  vultr_network:
    name: "{{ vultr_network_name }}"
    state: absent
  register: result

- name: verify test destroy an existing network
  assert:
    that:
    - result is changed
    - result.vultr_network.name == "{{ vultr_network_name }}"

- name: test destroy an existing network idempotence
  vultr_network:
    name: "{{ vultr_network_name }}"
    state: absent
  register: result

- name: verify test destroy an existing network idempotence
  assert:
    that:
    - result is not changed
