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

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

- block:
  - name: "Configure radius server defaults"
    nxos_aaa_server_host: &configure_default_radius
      server_type: radius
      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_radius
    register: result

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

  - name: "Remove radius 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 radius server non defaults"
    nxos_aaa_server_host: &configure_radius_non_default
      server_type: radius
      address: 8.8.8.8
      host_timeout: 25
      auth_port: 2083
      acct_port: 2084
      provider: "{{ connection }}"
      state: present
    register: result
 
  - assert: *true

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

  - assert: *false

  - name: "Configure some defaults on radius server"
    nxos_aaa_server_host: &configure_some_radius_default
      server_type: radius
      address: 8.8.8.8
      host_timeout: default
      auth_port: 1000
      acct_port: default
      provider: "{{ connection }}"
      state: present
    register: result

  - assert: *true

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

  - assert: *false

  - name: "Configure radius server with clear text pwd"
    nxos_aaa_server_host: &configure_radius_clear_text
      server_type: radius
      address: 8.8.8.8
      host_timeout: 25
      auth_port: default
      acct_port: 2084
      encrypt_type: 0
      key: hello
      provider: "{{ connection }}"
      state: present
    register: result

  - assert: *true

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

  - assert: *true

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

  - assert: *true

  - name: "Configure radius server with type 7 encryption"
    nxos_aaa_server_host: &configure_radius_type7
      server_type: radius
      address: 8.8.8.8
      host_timeout: 25
      auth_port: 2083
      acct_port: 2084
      encrypt_type: 7
      key: hello
      provider: "{{ connection }}"
      state: present
    register: result

  - assert: *true

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

  - assert: *false

  - name: "Configure radius server with new type 7 encryption key"
    nxos_aaa_server_host: &configure_radius_new_type7
      server_type: radius
      address: 8.8.8.8
      host_timeout: 25
      auth_port: 2083
      acct_port: 2084
      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_radius_new_type7
    register: result

  - assert: *false

  - name: "Configure radius server with default key"
    nxos_aaa_server_host: &configure_radius_defkey
      server_type: radius
      address: 8.8.8.8
      host_timeout: default
      auth_port: 1000
      acct_port: default
      encrypt_type: 7
      key: default
      provider: "{{ connection }}"
      state: present
    register: result

  - assert: *true

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

  - assert: *false

  - name: "Configure radius server with all def"
    nxos_aaa_server_host: &configure_radius_alldef
      server_type: radius
      address: 8.8.8.8
      host_timeout: default
      auth_port: default
      acct_port: default
      key: default
      provider: "{{ connection }}"
      state: present
    register: result

  - assert: *true

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

  - assert: *false

  rescue:

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

  always:

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

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