- name: Delete virtual network, if it exists
  azure_rm_virtualnetwork:
    name: my_test_network
    resource_group: "{{ resource_group }}"
    state: absent

- name: Create virtual network
  azure_rm_virtualnetwork:
    name: my_test_network
    address_prefixes_cidr:
      - 10.1.0.0/16
      - 172.100.0.0/16
    dns_servers:
      - 127.0.0.1
      - 127.0.0.3
    tags:
      testing: testing
      delete: on-exit
    resource_group: "{{ resource_group }}"
  register: output

- assert:
    that:
      - "output.state.address_prefixes | length == 2"
      - "output.state.dns_servers | length == 2"
      - "output.state.tags.delete == 'on-exit'"
      - "output.state.tags | length == 2"

- name: Gather facts by name, tags
  azure_rm_virtualnetwork_facts:
    resource_group: "{{ resource_group }}"
    name: my_test_network
    tags:
      - testing

- assert:
    that: "azure_virtualnetworks | length == 1"

- name: Gather facts by resource group, tags
  azure_rm_virtualnetwork_facts:
    resource_group: "{{ resource_group }}"
    tags:
      - testing

- assert:
    that: "azure_virtualnetworks | length == 1"

- name: Gather facts by tags
  azure_rm_virtualnetwork_facts:
    tags:
      - testing

- assert:
    that: "azure_virtualnetworks | length >= 1"

- name: Should be idempotent
  azure_rm_virtualnetwork:
    name: my_test_network
    address_prefixes_cidr:
      - 10.1.0.0/16
      - 172.100.0.0/16
    dns_servers:
      - 127.0.0.1
      - 127.0.0.3
    tags:
      testing: testing
      delete: on-exit
    resource_group: "{{ resource_group }}"
  register: output

- assert:
    that: not output.changed

- name: Update tags
  azure_rm_virtualnetwork:
    name: my_test_network
    tags:
      testing: 'no'
      delete: never
      foo: bar
    resource_group: "{{ resource_group }}"
  register: output

- assert:
     that: output.state.tags | length == 3

- name: Purge tags
  azure_rm_virtualnetwork:
    name: my_test_network
    tags:
      testing: 'always'
    resource_group: "{{ resource_group }}"
  register: output

- assert:
     that:
       - output.state.tags | length == 1
       - output.state.tags.testing == 'always'

- name: Should require address_prefixes_cidr when purge_address_prefixes
  azure_rm_virtualnetwork:
    name: my_test_network
    purge_address_prefixes: true
    resource_group: "{{ resource_group }}"
  register: output
  ignore_errors: yes

- assert:
      that: output.failed

- name: Purge address prefixes
  azure_rm_virtualnetwork:
    name: my_test_network
    address_prefixes_cidr: 10.1.0.0/16
    purge_address_prefixes: true
    resource_group: "{{ resource_group }}"
  register: output

- assert:
    that:
        - output.state.address_prefixes | length == 1
        - output.state.address_prefixes[0] == '10.1.0.0/16'
        - output.state.dns_servers | length == 2
        - output.state.dns_servers[0] == '127.0.0.1'

- name: Purge DNS servers
  azure_rm_virtualnetwork:
    name: my_test_network
    purge_dns_servers: true
    resource_group: "{{ resource_group }}"
  register: output

- assert:
    that: output.state['dns_servers'] is undefined

- name: Gather facts
  azure_rm_virtualnetwork_facts:
    resource_group: "{{ resource_group }}"
    name: my_test_network

- assert:
    that: "azure_virtualnetworks | length == 1"

- name: Delete virtual network
  azure_rm_virtualnetwork:
    name: my_test_network
    resource_group: "{{ resource_group }}"
    state: absent
