---
- name: setup - remove vrf
  cnos_vrf:
    name: "{{ item }}"
    state: absent
  become: yes
  with_items:
  - test
  - test1
  - test2
  - test3
  - test4
  - test5

- name: Setup - Ensure interfaces are not switchport
  cnos_config:
    lines:
      - no shutdown
      - no switchport
      - no logging monitor
    parents:
      - "interface ethernet1/33"

- name: Create vrf
  cnos_vrf:
    name: test
    rd: 1:200
    state: present
  become: yes
  register: result

- assert:
    that:
      - "result.changed == true"
      - "'vrf context test' in result.commands"
      - "'rd 1:200' in result.commands"

- name: Create vrf again (idempotent)
  cnos_vrf:
    name: test
    rd: 1:200
    state: present
  become: yes
  register: result

- assert:
    that:
      - "result.changed == false"
      - "result.commands | length == 0"

- name: Modify rd
  cnos_vrf:
    name: test
    rd: 1:201
    state: present
  become: yes
  register: result

- assert:
    that:
      - "result.changed == true"
      - "'vrf context test' in result.commands"
      - "'rd 1:201' in result.commands"

- name: Modify rd again (idempotent)
  cnos_vrf:
    name: test
    rd: 1:201
    state: present
  become: yes
  register: result

- assert:
    that:
      - "result.changed == false"
      - "result.commands | length == 0"

- name: Add Ethernet1/33 to vrf and check interface assigned state
  cnos_vrf:
    name: test
    rd: 1:201
    state: present
    interfaces:
      - Ethernet1/33
    associated_interfaces:
      - Ethernet1/33
  become: yes
  register: result

- assert:
    that:
      - "result.changed == true"
      - "'interface ethernet1/33' in result.commands"
      - "'vrf member test' in result.commands"

- name: Add Ethernet1/33 to vrf again (idempotent)
  cnos_vrf:
    name: test
    rd: 1:201
    state: present
    interfaces:
      - ethernet 1/33  # interface name modified to test case insensitive and space scenario
  become: yes
  register: result

- assert:
    that:
      - "result.changed == false"
      - "result.commands | length == 0"

- name: Add multiple interfaces to vrf
  cnos_vrf:
    name: test1
    rd: 1:202
    state: present
    interfaces:
      - loopback 1
      - loopback 2
      - loopback 3
      - loopback 4
      - loopback 5
      - loopback 6
  become: yes
  register: result

- assert:
    that:
      - "result.changed == true"
      - "'interface loopback1' in result.commands"
      - "'vrf member test1' in result.commands"
      - "'interface loopback2' in result.commands"
      - "'vrf member test1' in result.commands"
      - "'interface loopback3' in result.commands"
      - "'vrf member test1' in result.commands"
      - "'interface loopback4' in result.commands"
      - "'vrf member test1' in result.commands"
      - "'interface loopback5' in result.commands"
      - "'vrf member test1' in result.commands"
      - "'interface loopback6' in result.commands"
      - "'vrf member test1' in result.commands"

- name: Add multiple interfaces to vrf (idempotent)
  cnos_vrf:
    name: test1
    rd: 1:202
    state: present
    interfaces:
      - loopback 1
      - loopback 2
      - loopback 3
      - loopback 4
      - loopback 5
      - loopback 6
  become: yes
  register: result

- assert:
    that:
      - "result.changed == false"
      - "result.commands | length == 0"

- name: setup - remove vrf
  cnos_vrf:
    name: "{{ item }}"
    state: absent
  become: yes
  with_items:
  - test1
  - test2
  - test3

- name: Create aggregate of VRFs
  cnos_vrf:
    aggregate:
      - { name: test2, rd: "1:202" }
      - { name: test3, rd: "1:203" }
    state: present
  register: result

- assert:
    that:
      - "result.changed == true"
      - "'vrf context test2' in result.commands"
      - "'rd 1:202' in result.commands"
      - "'vrf context test3' in result.commands"
      - "'rd 1:203' in result.commands"

- name: Create aggregate of VRFs again (idempotent)
  cnos_vrf:
    aggregate:
      - { name: test2, rd: "1:202" }
      - { name: test3, rd: "1:203" }
    state: present
  become: yes
  register: result

- assert:
    that:
      - "result.changed == false"
      - "result.commands | length == 0"

- name: Create aggregate of VRFs with purge
  cnos_vrf:
    aggregate:
      - { name: test4, rd: "1:204" }
      - { name: test5, rd: "1:205" }
    state: present
    purge: yes
  become: yes
  register: result

- assert:
    that:
      - "result.changed == true"
      - "'vrf context test4' in result.commands"
      - "'rd 1:204' in result.commands"
      - "'vrf context test5' in result.commands"
      - "'rd 1:205' in result.commands"
      - "'no vrf context test' in result.commands"
      - "'no vrf context test2' in result.commands"
      - "'no vrf context test3' in result.commands"

- name: Delete VRFs
  cnos_vrf:
    name: test
    state: absent
  become: yes

- name: Delete VRFs again (idempotent)
  cnos_vrf:
    name: test
    state: absent
  become: yes

- name: Delete aggregate of VRFs
  cnos_vrf:
    aggregate:
      - { name: test1 }
      - { name: test2 }
      - { name: test3 }
      - { name: test4 }
      - { name: test5 }
    state: absent
  become: yes

- name: Delete VRFs again (idempotent)
  cnos_vrf:
    aggregate:
      - { name: test1 }
      - { name: test2 }
      - { name: test3 }
      - { name: test4 }
      - { name: test5 }
    state: absent
  become: yes

- assert:
    that:
      - "result.changed == true"
