---
- debug: msg="START cli/basic.yaml on connection={{ ansible_connection }}"

#- set_fact: switch_type="{{ switch_type }}"

- block:

  - name: setup - remove vlan used in test
    cnos_config:
      lines:
        - no vlan 100
        - no vlan 200
        - no vlan 300
      provider: "{{ cli }}"

  - name: setup - remove switchport settings on interfaces used in test
    cnos_config:
      lines:
        - switchport mode access
        - no switchport access vlan
      provider: "{{ cli }}"
      parents: "{{ item }}"
    loop:
      - interface ethernet1/33
      - interface ethernet1/44

  - name: create vlan
    cnos_vlan: &create
      vlan_id: 100
      name: test-vlan
      provider: "{{ cli }}"
    register: result

  - assert:
      that:
        - "result.changed == true"
        - "'vlan 100' in result.commands"
        - "'name test-vlan' in result.commands"

  - name: create vlan(idempotence)
    cnos_vlan: *create
    register: result

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

  - name: Add interfaces to vlan
    cnos_vlan: &interfaces
      vlan_id: 100
      interfaces:
        - Ethernet1/33
        - Ethernet1/44
      provider: "{{ cli }}"
    register: result

  - assert:
      that:
        - "result.changed == true"
        - "'interface Ethernet1/33' in result.commands"
        - "'switchport mode access' in result.commands"
        - "'switchport access vlan 100' in result.commands"
        - "'interface Ethernet1/44' in result.commands"
        - "'switchport mode access' in result.commands"
        - "'switchport access vlan 100' in result.commands"

  - name: Add interfaces to vlan(idempotence)
    cnos_vlan: *interfaces
    register: result

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

  - name: Remove interface from vlan
    cnos_vlan: &single_int
      vlan_id: 100
      interfaces:
        - Ethernet1/33
      provider: "{{ cli }}"
    register: result

  - assert:
      that:
        - "result.changed == true"
        - "'vlan 100' in result.commands"
#        - "'interface Ethernet1/33' in result.commands"
        - "'switchport mode access' in result.commands"
        - "'no switchport access vlan' in result.commands"

  - name: Remove interface from vlan(idempotence)
    cnos_vlan: *single_int
    register: result

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

  - name: Suspend vlan
    cnos_vlan:
      vlan_id: 100
      state: suspend
      provider: "{{ cli }}"
    register: result

  - assert:
      that:
        - "result.changed == true"
        - "'vlan 100' in result.commands"
        - "'state suspend' in result.commands"

  - name: Unsuspend vlan
    cnos_vlan:
      vlan_id: 100
      state: active
      provider: "{{ cli }}"
    register: result

  - assert:
      that:
        - "result.changed == true"
        - "'vlan 100' in result.commands"
        - "'state active' in result.commands"

  - name: delete vlan
    cnos_vlan: &delete
      vlan_id: 100
      provider: "{{ cli }}"
      state: absent
    register: result

  - assert:
      that:
        - "result.changed == true"
        - "'no vlan 100' in result.commands"

  - name: delete vlan(idempotence)
    cnos_vlan: *delete
    register: result

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

  - name: create vlans using aggregate
    cnos_vlan: &create_aggregate
      aggregate:
        - { vlan_id: 200, name: vlan-200 }
        - { vlan_id: 300, name: vlan-300 }
      provider: "{{ cli }}"
    register: result

  - assert:
      that:
        - "result.changed == true"
        - "'vlan 200' in result.commands"
        - "'name vlan-200' in result.commands"
        - "'vlan 300' in result.commands"
        - "'name vlan-300' in result.commands"

  - name: create vlans using aggregate(idempotence)
    cnos_vlan: *create_aggregate
    register: result

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

  - name: delete vlans using aggregate
    cnos_vlan: &delete_aggregate
      aggregate:
        - { vlan_id: 200, name: vlan-200 }
        - { vlan_id: 300, name: vlan-300 }
      state: absent
      provider: "{{ cli }}"
    register: result

  - assert:
      that:
        - "result.changed == true"
        - "'no vlan 200' in result.commands"
        - "'no vlan 300' in result.commands"

  - name: delete vlans using aggregate(idempotence)
    cnos_vlan: *delete_aggregate
    register: result

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

  - name: teardown(part1)
    cnos_config:
      lines:
        - no vlan 100
        - no vlan 200
        - no vlan 300
      provider: "{{ cli }}"

  - name: teardown(part2)
    cnos_config:
      lines:
        - switchport mode access
        - no switchport access vlan
      provider: "{{ cli }}"
      parents: "{{ item }}"
    loop:
      - interface Ethernet1/33
      - interface Ethernet1/44

#  when: switch_type == 'L2'

- debug: msg="END cli/basic.yaml on connection={{ ansible_connection }}"
