---

- debug:
    msg: "Start nxos_lacp replaced integration tests connection={{ ansible_connection }}"

- name: Enable lacp feature
  nxos_feature:
    feature: lacp

- set_fact:
    mac1: "lacp system-mac 00c1.4c00.bd20 role primary"
  when: platform is search('N9K') and imagetag is not search('I[2-6]')

- set_fact:
    mac2:
      address: 00c1.4c00.bd15
      role: secondary
  when: platform is search('N9K') and imagetag is not search('I[2-6]')

- block:
  - name: Setup1
    cli_config:
      config: lacp system-priority 11

  - name: Setup2
    cli_config:
      config: "{{ mac1|default(omit) }}"

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

  - name: Replaced
    nxos_lacp: &replaced
      config:
        system:
          priority: 12
          mac: "{{ mac2|default(omit) }}"
      state: replaced
    register: result

  - assert:
      that:
        - "ansible_facts.network_resources.lacp == result.before"
        - "result.changed == true"
        - "result.commands|length == 2"
        - "'no lacp system-priority' in result.commands"
        - "'lacp system-priority 12' in result.commands"
    when: platform is not search('N9K')

  - assert:
      that:
        - "ansible_facts.network_resources.lacp == result.before"
        - "result.changed == true"
        - "'no lacp system-priority' in result.commands"
        - "'no lacp system-mac' in result.commands"
        - "'lacp system-priority 12' in result.commands"
        - "'lacp system-mac 00c1.4c00.bd15 role secondary' in result.commands"
        - "result.commands|length == 4"
    when: platform is search('N9K') and imagetag is not search('I[2-6]')

  - name: Gather lacp interfaces post facts
    nxos_facts: *facts

  - assert:
      that:
        - "ansible_facts.network_resources.lacp == result.after"
    when: platform is search('N9K') and imagetag is not search('I[2-6]')

  - name: Idempotence - Replaced
    nxos_lacp: *replaced
    register: result

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

  - name: Setup3
    cli_config:
      config: "{{ mac1|default(omit) }}"

  - name: Replaced
    nxos_lacp:
      state: replaced
    register: result
    when: platform is search('N9K') and imagetag is not search('I[2-6]')

  - assert:
      that:
        - "result.changed == true"
        - "result.commands|length == 2"
        - "'no lacp system-mac' in result.commands"
        - "'no lacp system-priority' in result.commands"
    when: platform is search('N9K') and imagetag is not search('I[2-6]')

  always:
  - name: teardown
    nxos_feature:
      feature: lacp
      state: disabled
