---
- debug: msg="START nxapi/set_domain_list.yaml"


# nxapi will error if you try and remove a non-existent entry,
# Therefore we do this as a with_items loop with ignore_errors
- name: setup
  nxos_config:
    lines:
      - no ip domain-list {{ item }}
    match: none
    provider: "{{ nxapi }}"
  ignore_errors: yes
  with_items:
      - ansible.com
      - redhat.com

- name: configure domain_list
  nxos_system:
    domain_search:
      - ansible.com
      - redhat.com
    provider: "{{ nxapi }}"
  register: result

- assert:
    that:
      - result.changed == true
      - "'ip domain-list ansible.com' in result.commands"
      - "'ip domain-list redhat.com' in result.commands"

- name: verify domain_list
  nxos_system:
    domain_search:
      - ansible.com
      - redhat.com
    provider: "{{ nxapi }}"
  register: result

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

- name: remove one entry
  nxos_system:
    domain_search:
      - ansible.com
    provider: "{{ nxapi }}"
  register: result

- assert:
    that:
      - result.changed == true
      - "'no ip domain-list redhat.com' in result.commands"

- name: verify remove one entry
  nxos_system:
    domain_search:
      - ansible.com
    provider: "{{ nxapi }}"
  register: result

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

- name: add one entry
  nxos_system:
    domain_search:
      - ansible.com
      - redhat.com
    provider: "{{ nxapi }}"
  register: result

- assert:
    that:
      - result.changed == true
      - "'ip domain-list redhat.com' in result.commands"

- name: verify add one entry
  nxos_system:
    domain_search:
      - ansible.com
      - redhat.com
    provider: "{{ nxapi }}"
  register: result

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

- name: add and remove one entry
  nxos_system:
    domain_search:
      - ansible.com
      - eng.ansible.com
    provider: "{{ nxapi }}"
  register: result

- assert:
    that:
      - result.changed == true
      - "'no ip domain-list redhat.com' in result.commands"
      - "'ip domain-list eng.ansible.com' in result.commands"
      - result.commands|length == 2

- name: verify add and remove one entry
  nxos_system:
    domain_search:
      - ansible.com
      - eng.ansible.com
    provider: "{{ nxapi }}"
  register: result

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

- name: teardown
  nxos_config:
    lines:
      - no ip domain-list {{ item }}
    match: none
    provider: "{{ nxapi }}"
  ignore_errors: yes
  with_items:
      - ansible.com
      - redhat.com
      - eng.ansible.com

- debug: msg="END nxapi/set_domain_search.yaml"
