# SPDX-License-Identifier: BSD-3-Clause
---
- name: Play for configuring network using network state variable
  hosts: all
  vars:
    type: veth
    interface0: ethtest0
    interface1: ethtest1


  tasks:
    - name: "Set type={{ type }} and interface={{ interface0 }}"  # noqa name
      set_fact:
        type: "{{ type }}"
        interface: "{{ interface0 }}"
    - name: Include the task 'show_interfaces.yml'
      include_tasks: tasks/show_interfaces.yml
    - name: Include the task 'manage_test_interface.yml'
      include_tasks: tasks/manage_test_interface.yml
      vars:
        state: present
    - name: Include the task 'assert_device_present.yml'
      include_tasks: tasks/assert_device_present.yml
    - name: "Set type={{ type }} and interface={{ interface1 }}"  # noqa name
      set_fact:
        type: "{{ type }}"
        interface: "{{ interface1 }}"
    - name: Include the task 'show_interfaces.yml' again
      include_tasks: tasks/show_interfaces.yml
    - name: Include the task 'manage_test_interface.yml' after changing interface
      include_tasks: tasks/manage_test_interface.yml
      vars:
        state: present
    - name: Include the task 'assert_device_present.yml' after changing interface
      include_tasks: tasks/assert_device_present.yml


    - name: Configure the IP addresses
      import_role:
        name: linux-system-roles.network
      vars:
        network_state:
          interfaces:
            - name: ethtest0
              type: ethernet
              state: up
              ipv4:
                enabled: true
                address:
                  - ip: 192.168.122.250
                    prefix-length: 24
                dhcp: false
              ipv6:
                enabled: true
                address:
                  - ip: 2001:db8::1:1
                    prefix-length: 64
                autoconf: false
                dhcp: false
            - name: ethtest1
              type: ethernet
              state: up
              ipv4:
                enabled: true
                auto-dns: false
                address:
                  - ip: 192.168.122.88
                    prefix-length: 24
                dhcp: false
              ipv6:
                enabled: true
                auto-dns: false
                dhcp: true

    - name: Get the ethtest0 state configuration
      command: nmstatectl show ethtest0
      register: ethtest0_state
      ignore_errors: true  # noqa ignore-errors
      changed_when: false

    - name: Get the ethtest1 state configuration
      command: nmstatectl show ethtest1
      register: ethtest1_state
      ignore_errors: true  # noqa ignore-errors
      changed_when: false

    - name: Assert that the ethtest0 state configuration contains the specified
        settings
      assert:
        that:
          - ethtest0_state.stdout is search("192.168.122.250")
          - ethtest0_state.stdout is search("2001:db8::1:1")
        msg: the ethtest0 state configuration does not contain the specified
          settings

    - name: Assert that the ethtest1 state configuration contains the specified
        settings
      assert:
        that:
          - ethtest1_state.stdout is search("192.168.122.88")
        msg: the ethtest1 state configuration does not contain the specified
          settings

    - name: Configure the route
      import_role:
        name: linux-system-roles.network
      vars:
        network_state:
          routes:
            config:
              - destination: 192.0.2.100/30
                metric: 150
                next-hop-address: 192.168.122.250
                next-hop-interface: ethtest0
                table-id: 254

    - name: Get the route configuration
      command: nmstatectl show
      register: route
      ignore_errors: true  # noqa ignore-errors
      changed_when: false

    - name: Assert that the route configuration contains the specified route
      assert:
        that:
          - route.stdout is search("destination:(\s+)192.0.2.100/30")
          - route.stdout is search("metric:(\s+)150")
          - route.stdout is search("next-hop-address:(\s+)192.168.122.250")
          - route.stdout is search("next-hop-interface:(\s+)ethtest0")
          - route.stdout is search("table-id:(\s+)254")
        msg: the route configuration does not contain the specified route

    - name: Configure the DNS
      import_role:
        name: linux-system-roles.network
      vars:
        network_state:
          dns-resolver:
            config:
              search:
                - example.com
                - example.org
              server:
                - 2001:4860:4860::8888

    - name: Get the DNS configuration from nmstatectl
      command: nmstatectl show
      register: nmstatectl
      ignore_errors: true  # noqa ignore-errors
      changed_when: false

    - name: Get the DNS configuration from the file
        `/run/NetworkManager/no-stub-resolv.conf`
      command: cat /run/NetworkManager/no-stub-resolv.conf
      register: no_stub_resolvconf
      changed_when: false

    - name: Assert that the nmstatectl contains the specified DNS configuration
      assert:
        that:
          - nmstatectl.stdout is search("example.com")
          - nmstatectl.stdout is search("example.org")
          - nmstatectl.stdout is search("2001:4860:4860::8888")
        msg: the nmstatectl does not contain the specified DNS configuration

    - name: Assert that the file `/run/NetworkManager/no-stub-resolv.conf`
        contains the specified DNS configuration
      assert:
        that:
          - no_stub_resolvconf.stdout is search("example.com")
          - no_stub_resolvconf.stdout is search("example.org")
          - no_stub_resolvconf.stdout is search("2001:4860:4860::8888")
        msg: the file `/run/NetworkManager/no-stub-resolv.conf` does not
          contain the specified DNS configuration

    - name: Purge the DNS
      import_role:
        name: linux-system-roles.network
      vars:
        network_state:
          dns-resolver:
            config: {}

    - name: Include the task 'delete_interface.yml'
      include_tasks: tasks/delete_interface.yml
    - name: Include the task 'assert_device_absent.yml'
      include_tasks: tasks/assert_device_absent.yml
    - name: "Set interface={{ interface0 }}"
      set_fact:
        type: "{{ type }}"
        interface: "{{ interface0 }}"
    - name: Include the task 'delete_interface.yml' again
      include_tasks: tasks/delete_interface.yml
    - name: Include the task 'assert_device_absent.yml' again
      include_tasks: tasks/assert_device_absent.yml
    - name: Verify network state restored to default
      include_tasks: tasks/check_network_dns.yml
...
