---
# Copyright (c) Ansible Project
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
# SPDX-License-Identifier: GPL-3.0-or-later

- name: Registering network name
  set_fact:
    nname_1: "{{ name_prefix ~ '-network-1' }}"
- name: Registering network name
  set_fact:
    dnetworks: "{{ dnetworks + [nname_1] }}"

####################################################################
## internal ########################################################
####################################################################

- name: internal
  docker_network:
    name: "{{ nname_1 }}"
    internal: true
  register: internal_1

- name: internal (idempotency)
  docker_network:
    name: "{{ nname_1 }}"
    internal: true
  register: internal_2

- name: internal (change)
  docker_network:
    name: "{{ nname_1 }}"
    internal: false
  register: internal_3

- name: cleanup
  docker_network:
    name: "{{ nname_1 }}"
    state: absent
    force: true

- assert:
    that:
    - internal_1 is changed
    - internal_2 is not changed
    - internal_3 is changed

####################################################################
## driver_options ##################################################
####################################################################

- name: driver_options
  docker_network:
    name: "{{ nname_1 }}"
    driver_options:
      com.docker.network.bridge.enable_icc: 'false'
  register: driver_options_1

- name: driver_options (idempotency)
  docker_network:
    name: "{{ nname_1 }}"
    driver_options:
      com.docker.network.bridge.enable_icc: 'false'
  register: driver_options_2

- name: driver_options (idempotency with string translation)
  docker_network:
    name: "{{ nname_1 }}"
    driver_options:
      com.docker.network.bridge.enable_icc: false
  register: driver_options_3

- name: driver_options (change)
  docker_network:
    name: "{{ nname_1 }}"
    driver_options:
      com.docker.network.bridge.enable_icc: 'true'
  register: driver_options_4

- name: driver_options (idempotency with string translation)
  docker_network:
    name: "{{ nname_1 }}"
    driver_options:
      com.docker.network.bridge.enable_icc: true
  register: driver_options_5

- name: cleanup
  docker_network:
    name: "{{ nname_1 }}"
    state: absent
    force: true

- assert:
    that:
    - driver_options_1 is changed
    - driver_options_2 is not changed
    - driver_options_3 is not changed
    - driver_options_4 is changed
    - driver_options_5 is not changed

####################################################################
## scope ###########################################################
####################################################################

- block:
  - name: scope
    docker_network:
      name: "{{ nname_1 }}"
      driver: bridge
      scope: local
    register: scope_1

  - name: scope (idempotency)
    docker_network:
      name: "{{ nname_1 }}"
      driver: bridge
      scope: local
    register: scope_2

  - name: swarm
    docker_swarm:
      state: present
      advertise_addr: "{{ ansible_default_ipv4.address | default('127.0.0.1') }}"

  # Driver change alongside scope is intentional - bridge doesn't appear to support anything but local, and overlay can't downgrade to local. Additionally, overlay reports as swarm for swarm OR global, so no change is reported in that case.
  # Test output indicates that the scope is altered, at least, so manual inspection will be required to verify this going forward, unless we come up with a test driver that supports multiple scopes.
  - name: scope (change)
    docker_network:
      name: "{{ nname_1 }}"
      driver: overlay
      scope: swarm
    register: scope_3

  - name: cleanup network
    docker_network:
      name: "{{ nname_1 }}"
      state: absent
      force: true

  - assert:
      that:
      - scope_1 is changed
      - scope_2 is not changed
      - scope_3 is changed

  always:
  - name: cleanup swarm
    docker_swarm:
      state: absent
      force: true

####################################################################
## attachable ######################################################
####################################################################

- name: attachable
  docker_network:
    name: "{{ nname_1 }}"
    attachable: true
  register: attachable_1
  ignore_errors: true
  
- name: attachable (idempotency)
  docker_network:
    name: "{{ nname_1 }}"
    attachable: true
  register: attachable_2
  ignore_errors: true
  
- name: attachable (change)
  docker_network:
    name: "{{ nname_1 }}"
    attachable: false
  register: attachable_3
  ignore_errors: true
  
- name: cleanup
  docker_network:
    name: "{{ nname_1 }}"
    state: absent
    force: true

- assert:
    that:
    - attachable_1 is changed
    - attachable_2 is not changed
    - attachable_3 is changed

####################################################################
## labels ##########################################################
####################################################################

- name: labels
  docker_network:
    name: "{{ nname_1 }}"
    labels:
      ansible.test.1: hello
      ansible.test.2: world
  register: labels_1

- name: labels (idempotency)
  docker_network:
    name: "{{ nname_1 }}"
    labels:
      ansible.test.2: world
      ansible.test.1: hello
  register: labels_2

- name: labels (less labels)
  docker_network:
    name: "{{ nname_1 }}"
    labels:
      ansible.test.1: hello
  register: labels_3

- name: labels (more labels)
  docker_network:
    name: "{{ nname_1 }}"
    labels:
      ansible.test.1: hello
      ansible.test.3: ansible
  register: labels_4

- name: cleanup
  docker_network:
    name: "{{ nname_1 }}"
    state: absent
    force: true

- assert:
    that:
    - labels_1 is changed
    - labels_2 is not changed
    - labels_3 is not changed
    - labels_4 is changed
