---
- 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+
    state: enabled

- name: "Setup"
  nxos_aaa_server_host: &remove
    server_type: tacacs
    address: 8.8.8.8
    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
      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
      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
      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
      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
      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 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
      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
      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+
      state: disabled

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