---
- name: Registering network names
  set_fact:
    nname_ipam_0: "{{ name_prefix ~ '-network-ipam-0' }}"
    nname_ipam_1: "{{ name_prefix ~ '-network-ipam-1' }}"
    nname_ipam_2: "{{ name_prefix ~ '-network-ipam-2' }}"
    nname_ipam_3: "{{ name_prefix ~ '-network-ipam-3' }}"

- name: Registering network names
  set_fact:
    dnetworks: "{{ dnetworks + [nname_ipam_0, nname_ipam_1, nname_ipam_2, nname_ipam_3] }}"


#################### IPv4 IPAM config ####################

- name: Create network with custom IPAM config
  docker_network:
    name: "{{ nname_ipam_1 }}"
    ipam_config:
      - subnet: 10.25.120.0/24
        gateway: 10.25.120.2
        iprange: 10.25.120.0/26
        aux_addresses:
          host1: 10.25.120.3
          host2: 10.25.120.4
  register: network

- assert:
    that:
      - network is changed

- name: Create network with custom IPAM config (idempotence)
  docker_network:
    name: "{{ nname_ipam_1 }}"
    ipam_config:
      - subnet: 10.25.120.0/24
        gateway: 10.25.120.2
        iprange: 10.25.120.0/26
        aux_addresses:
          host1: 10.25.120.3
          host2: 10.25.120.4
  register: network

- assert:
    that:
      - network is not changed

- name: Change of network created with custom IPAM config
  docker_network:
    name: "{{ nname_ipam_1 }}"
    ipam_config:
      - subnet: 10.25.121.0/24
        gateway: 10.25.121.2
        iprange: 10.25.121.0/26
        aux_addresses:
          host1: 10.25.121.3
  register: network
  diff: yes

- assert:
    that:
      - network is changed
      - network.diff.differences | length == 4
      - '"ipam_config[0].subnet" in network.diff.differences'
      - '"ipam_config[0].gateway" in network.diff.differences'
      - '"ipam_config[0].iprange" in network.diff.differences'
      - '"ipam_config[0].aux_addresses" in network.diff.differences'

- name: Remove gateway and iprange of network with custom IPAM config
  docker_network:
    name: "{{ nname_ipam_1 }}"
    ipam_config:
      - subnet: 10.25.121.0/24
  register: network

- assert:
    that:
      - network is not changed

- name: Cleanup network with custom IPAM config
  docker_network:
    name: "{{ nname_ipam_1 }}"
    state: absent


#################### IPv6 IPAM config ####################

- name: Create network with IPv6 IPAM config
  docker_network:
    name: "{{ nname_ipam_2 }}"
    enable_ipv6: yes
    ipam_config:
      - subnet: fdd1:ac8c:0557:7ce0::/64
  register: network

- assert:
    that:
      - network is changed

- name: Create network with IPv6 IPAM config (idempotence)
  docker_network:
    name: "{{ nname_ipam_2 }}"
    enable_ipv6: yes
    ipam_config:
      - subnet: fdd1:ac8c:0557:7ce0::/64
  register: network

- assert:
    that:
      - network is not changed

- name: Change subnet of network with IPv6 IPAM config
  docker_network:
    name: "{{ nname_ipam_2 }}"
    enable_ipv6: yes
    ipam_config:
      - subnet: fdd1:ac8c:0557:7ce1::/64
  register: network
  diff: yes

- assert:
    that:
      - network is changed
      - network.diff.differences | length == 1
      - network.diff.differences[0] == "ipam_config[0].subnet"

- name: Change subnet of network with IPv6 IPAM config
  docker_network:
    name: "{{ nname_ipam_2 }}"
    enable_ipv6: yes
    ipam_config:
      - subnet: "fdd1:ac8c:0557:7ce1::"
  register: network
  ignore_errors: yes

- assert:
    that:
      - network is failed
      - "network.msg == '\"fdd1:ac8c:0557:7ce1::\" is not a valid CIDR'"

- name: Cleanup network with IPv6 IPAM config
  docker_network:
    name: "{{ nname_ipam_2 }}"
    state: absent


#################### IPv4 and IPv6 network ####################

- name: Create network with IPv6 and custom IPv4 IPAM config
  docker_network:
    name: "{{ nname_ipam_3 }}"
    enable_ipv6: yes
    ipam_config:
      - subnet: 10.26.120.0/24
      - subnet: fdd1:ac8c:0557:7ce2::/64
  register: network

- assert:
    that:
      - network is changed

- name: Change subnet order of network with IPv6 and custom IPv4 IPAM config (idempotence)
  docker_network:
    name: "{{ nname_ipam_3 }}"
    enable_ipv6: yes
    ipam_config:
      - subnet: fdd1:ac8c:0557:7ce2::/64
      - subnet: 10.26.120.0/24
  register: network

- assert:
    that:
      - network is not changed

- name: Remove IPv6 from network with custom IPv4 and IPv6 IPAM config (change)
  docker_network:
    name: "{{ nname_ipam_3 }}"
    enable_ipv6: no
    ipam_config:
      - subnet: 10.26.120.0/24
  register: network
  diff: yes

- assert:
    that:
      - network is changed
      - network.diff.differences | length == 1
      - network.diff.differences[0] == "enable_ipv6"

- name: Cleanup network with IPv6 and custom IPv4 IPAM config
  docker_network:
    name: "{{ nname_ipam_3 }}"
    state: absent


#################### multiple IPv4 networks ####################

- block:
  - name: Create network with two IPv4 IPAM configs
    docker_network:
      name: "{{ nname_ipam_3 }}"
      driver: "macvlan"
      driver_options:
        parent: "{{ ansible_default_ipv4.alias }}"
      ipam_config:
        - subnet: 10.26.120.0/24
        - subnet: 10.26.121.0/24
    register: network

  - assert:
      that:
        - network is changed

  - name: Create network with two IPv4 IPAM configs (idempotence)
    docker_network:
      name: "{{ nname_ipam_3 }}"
      driver: "macvlan"
      driver_options:
        parent: "{{ ansible_default_ipv4.alias }}"
      ipam_config:
        - subnet: 10.26.121.0/24
        - subnet: 10.26.120.0/24
    register: network

  - assert:
      that:
        - network is not changed

  - name: Create network with two IPv4 IPAM configs (change)
    docker_network:
      name: "{{ nname_ipam_3 }}"
      driver: "macvlan"
      driver_options:
        parent: "{{ ansible_default_ipv4.alias }}"
      ipam_config:
        - subnet: 10.26.120.0/24
        - subnet: 10.26.122.0/24
    register: network
    diff: yes

  - assert:
      that:
        - network is changed
        - network.diff.differences | length == 1

  - name: Create network with one IPv4 IPAM config (no change)
    docker_network:
      name: "{{ nname_ipam_3 }}"
      driver: "macvlan"
      driver_options:
        parent: "{{ ansible_default_ipv4.alias }}"
      ipam_config:
        - subnet: 10.26.122.0/24
    register: network

  - assert:
      that:
        - network is not changed

  - name: Cleanup network
    docker_network:
      name: "{{ nname_ipam_3 }}"
      state: absent

  when: ansible_facts.virtualization_type != 'docker'


#################### IPAM driver options ####################

- name: Create network with IPAM driver options
  docker_network:
    name: "{{ nname_ipam_3 }}"
    ipam_driver: default
    ipam_driver_options:
      a: b
  register: network_1
  ignore_errors: yes
- name: Create network with IPAM driver options (idempotence)
  docker_network:
    name: "{{ nname_ipam_3 }}"
    ipam_driver: default
    ipam_driver_options:
      a: b
  diff: yes
  register: network_2
  ignore_errors: yes
- name: Create network with IPAM driver options (change)
  docker_network:
    name: "{{ nname_ipam_3 }}"
    ipam_driver: default
    ipam_driver_options:
      a: c
  diff: yes
  register: network_3
  ignore_errors: yes
- name: Cleanup network
  docker_network:
    name: "{{ nname_ipam_3 }}"
    state: absent

- assert:
    that:
      - network_1 is changed
      - network_2 is not changed
      - network_3 is changed
  when: docker_py_version is version('2.0.0', '>=')
- assert:
    that:
      - network_1 is failed
      - "('version is ' ~ docker_py_version ~ ' ') in network_1.msg"
      - "'Minimum version required is 2.0.0 ' in network_1.msg"
  when: docker_py_version is version('2.0.0', '<')
