---
- debug: msg="START connection={{ ansible_connection }} nxos_aaa_server_host tacacs.yaml sanity test"
- debug: msg="Using provider={{ connection.transport }}"
  when: ansible_connection == "local"

- name: "Enable feature tacacs+"
  nxos_feature:
    feature: tacacs+
    provider: "{{ connection }}"
    state: enabled

- name: "Setup"
  nxos_aaa_server_host: &remove
    server_type: tacacs
    address: 8.8.8.8
    provider: "{{ connection }}"
    state: absent
  ignore_errors: yes

- block:

  - name: "Configure tacacs server defaults"
    nxos_aaa_server_host: &configure_default_tacacs
      server_type: tacacs
      address: 8.8.8.8
      provider: "{{ connection }}"
      state: present
    register: result

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

  - name: "Check Idempotence"
    nxos_aaa_server_host: *configure_default_tacacs
    register: result

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

  - name: "Remove tacacs server configuration"
    nxos_aaa_server_host: *remove
    register: result

  - assert: *true

  - name: "Check Idempotence"
    nxos_aaa_server_host: *remove
    register: result

  - assert: *false
  
  - name: "Configure tacacs server non defaults"
    nxos_aaa_server_host: &configure_tacacs_non_default
      server_type: tacacs
      address: 8.8.8.8
      host_timeout: 25
      tacacs_port: 89
      provider: "{{ connection }}"
      state: present
    register: result
 
  - assert: *true

  - name: "Check Idempotence"
    nxos_aaa_server_host: *configure_tacacs_non_default
    register: result

  - assert: *false

  - name: "Configure some defaults on tacacs server"
    nxos_aaa_server_host: &configure_some_tacacs_default
      server_type: tacacs
      address: 8.8.8.8
      host_timeout: default
      tacacs_port: 100
      provider: "{{ connection }}"
      state: present
    register: result

  - assert: *true

  - name: "Check Idempotence"
    nxos_aaa_server_host: *configure_some_tacacs_default
    register: result

  - assert: *false

  - name: "Configure tacacs server with clear text pwd"
    nxos_aaa_server_host: &configure_tacacs_clear_text
      server_type: tacacs
      address: 8.8.8.8
      host_timeout: 25
      tacacs_port: default
      encrypt_type: 0
      key: hello
      provider: "{{ connection }}"
      state: present
    register: result
   
  - assert: *true

  - name: "Check NOT Idempotent"
    nxos_aaa_server_host: *configure_tacacs_clear_text
    register: result

  - assert: *true

  - name: "Remove tacacs server configuration"
    nxos_aaa_server_host: *remove
    register: result

  - assert: *true

  - name: "Configure tacacs server with type 7 encryption"
    nxos_aaa_server_host: &configure_tacacs_type7
      server_type: tacacs
      address: 8.8.8.8
      host_timeout: 25
      tacacs_port: 89
      encrypt_type: 7
      key: hello
      provider: "{{ connection }}"
      state: present
    register: result

  - assert: *true

  - name: "Check Idempotence"
    nxos_aaa_server_host: *configure_tacacs_type7
    register: result

  - assert: *false

  - name: "Configure tacacs server with new type 7 encryption key"
    nxos_aaa_server_host: &configure_tacacs_new_type7
      server_type: tacacs
      address: 8.8.8.8
      host_timeout: 25
      tacacs_port: 89
      encrypt_type: 7
      key: helloback
      provider: "{{ connection }}"
      state: present
    register: result

  - assert:
      that:
        - "result.changed == true"
        - "'key 7' in result.updates[0]"

  - name: "Check Idempotence"
    nxos_aaa_server_host: *configure_tacacs_new_type7
    register: result

  - assert: *false

  - name: "Configure tacacs server with default key"
    nxos_aaa_server_host: &configure_tacacs_defkey
      server_type: tacacs
      address: 8.8.8.8
      host_timeout: default
      tacacs_port: 89
      encrypt_type: 7
      key: default
      provider: "{{ connection }}"
      state: present
    register: result

  - assert: *true

  - name: "Check Idempotence"
    nxos_aaa_server_host: *configure_tacacs_defkey
    register: result

  - assert: *false

  - name: "Configure tacacs server with all def"
    nxos_aaa_server_host: &configure_tacacs_alldef
      server_type: tacacs
      address: 8.8.8.8
      host_timeout: default
      tacacs_port: default
      key: default
      provider: "{{ connection }}"
      state: present
    register: result

  - assert: *true

  - name: "Check Idempotence"
    nxos_aaa_server_host: *configure_tacacs_alldef
    register: result

  - assert: *false

  rescue:

  - debug: msg="connection={{ ansible_connection }} nxos_aaa_server_host failure detected"

  always:

  - name: "Remove tacacs server configuration"
    nxos_aaa_server_host: *remove
    register: result

  - name: "Disable feature tacacs+"
    nxos_feature:
      feature: tacacs+
      provider: "{{ connection }}"
      state: disabled

  - debug: msg="END connection={{ ansible_connection }} nxos_aaa_server_host tacacs.yaml sanity test"  
