---
- debug:
    msg: "Start nxos_interfaces merged integration tests connection={{ ansible_connection }}"

- set_fact: 
    test_int1: "{{ nxos_int1 }}"
    test_int2: "{{ nxos_int2 }}"

- set_fact: enabled=true
  when: platform is not search('N3[5KL]|N[56]K|titanium')

- name: setup
  cli_config: &cleanup
    config: |
      default interface {{ test_int1 }}
      default interface {{ test_int2 }}

- block:
  - name: Merged
    nxos_interfaces: &merged
      config:
        - name: "{{ test_int1 }}"
          description: Configured by Ansible
          enabled: "{{ enabled|default(omit)}}"
      state:  merged
    register: result

  - assert:
      that:
        - "result.changed == true"
        - "'interface {{ test_int1 }}' in result.commands"
        - "'description Configured by Ansible' in result.commands"

  - assert:
      that:
        - "'no shutdown' in result.commands"
    when: enabled is defined

  - name: Gather interfaces facts
    nxos_facts:
      gather_subset:
        - '!all'
        - '!min'
      gather_network_resources: interfaces

  - assert:
      that:
        - "ansible_facts.network_resources.interfaces|symmetric_difference(result.after)|length == 0"

  - name: Idempotence - Merged
    nxos_interfaces: *merged
    register: result

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

  - name: "Populate {{ test_int2 }}"
    nxos_config:
      lines:
        - "description Test"
        - "switchport"
        - "no shutdown"
      parents: "interface {{ test_int2 }}"

  - name: Update interface state
    nxos_interfaces:
      config:
        - name: "{{ test_int2 }}"
          enabled: False
          mode: layer2
          description: Test
      state: merged
    register: result

  - assert:
      that:
        - "'interface {{ test_int2 }}' in result.commands"
        - "'shutdown' in result.commands"
        - result.changed == True
        - result.commands|length == 2

  always:
  - name: teardown
    cli_config: *cleanup
