---
- debug: msg="START connection={{ ansible_connection }} nxos_bgp parameter test"
- debug: msg="Using provider={{ connection.transport }}"
  when: ansible_connection == "local"

- set_fact: bgp_best_path_limit="false"
- set_fact: bgp_best_path_limit="true"
  when: imagetag is not search("I2")

- set_fact: bgp_suppress_fib_supported="false"
- set_fact: bgp_suppress_fib_supported="true"
  when: imagetag is not search("A8|D1|I2|I4")

- name: "Disable feature BGP"
  nxos_feature:
    feature: bgp
    state: disabled
  ignore_errors: yes

- name: "Enable feature BGP"
  nxos_feature:
    feature: bgp
    state: enabled
  ignore_errors: yes

- block:
  # this task will fail on n9k running I2
  # due to no support
  - name: "set bestpath limit"
    nxos_bgp: &set1
      asn: 65535
      vrf: "{{ item }}"
      timer_bestpath_limit: 255
    with_items: "{{ vrfs }}"
    register: result

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

  - name: "Check Idempotence"
    nxos_bgp: *set1
    with_items: "{{ vrfs }}"
    register: result

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

  - name: "reset bestpath limit"
    nxos_bgp: &reset1
      asn: 65535
      vrf: "{{ item }}"
      timer_bestpath_limit: default
    with_items: "{{ vrfs }}"
    register: result

  - assert: *true

  - name: "Check Idempotence"
    nxos_bgp: *reset1
    with_items: "{{ vrfs }}"
    register: result
    when: bgp_best_path_limit

  - assert: *false
    when: bgp_best_path_limit

  # Property does not nvgen on some images
  # still try and set it but don't check idempotence
  - name: "set suppress fib"
    nxos_bgp: &set2
      asn: 65535
      suppress_fib_pending: false
    register: result

  - assert: *true
    when: bgp_suppress_fib_supported

  - name: "Check Idempotence"
    nxos_bgp: *set2
    register: result
    when: bgp_suppress_fib_supported

  - assert: *false
    when: bgp_suppress_fib_supported

  - name: "reset suppress fib"
    nxos_bgp: &reset2
      asn: 65535
      suppress_fib_pending: true
    register: result

  - assert: *true
    when: bgp_suppress_fib_supported

  - name: "Check Idempotence"
    nxos_bgp: *reset2
    register: result
    when: bgp_suppress_fib_supported

  - assert: *false
    when: bgp_suppress_fib_supported

  rescue:
  - debug: msg="Tests can fail on I2/I4/A8/Fretta or helsinki images"

  always:
  - name: "Disable feature bgp"
    nxos_feature:
      feature: bgp
      state: disabled
    ignore_errors: yes

  - debug: msg="END connection={{ ansible_connection }} nxos_bgp parameter test"
