# Test code for the Meraki Organization module
# Copyright: (c) 2018, Kevin Breit (@kbreit)

# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
---
- block:
  - name: Test an API key is provided
    fail:
      msg: Please define an API key
    when: auth_key is not defined

  - set_fact:
      syslog_test_net_name: 'syslog_{{test_net_name}}'

  - name: Create network with type appliance and no timezone
    meraki_network:
      auth_key: '{{ auth_key }}'
      state: present
      org_name: '{{test_org_name}}'
      net_name: '{{test_net_name}}'
      type: appliance
    delegate_to: localhost
    register: new_net

  - set_fact:
      net_id: '{{new_net.data.id}}'

  - name: Query syslog settings
    meraki_syslog:
      auth_key: '{{auth_key}}'
      org_name: '{{test_org_name}}'
      net_name: '{{test_net_name}}'
      state: query
    delegate_to: localhost
    register: query_all

  - name: Set syslog server
    meraki_syslog:
      auth_key: '{{auth_key}}'
      org_name: '{{test_org_name}}'
      net_name: '{{test_net_name}}'
      state: present
      servers:
        - host: 192.0.1.2
          port: 514
          roles:
            - Appliance event log
    delegate_to: localhost
    register: create_server

  - assert:
      that:
        - create_server['data']['servers'][0]['host'] == "192.0.1.2"

  - name: Set syslog server with idempotency
    meraki_syslog:
      auth_key: '{{auth_key}}'
      org_name: '{{test_org_name}}'
      net_name: '{{test_net_name}}'
      state: present
      servers:
        - host: 192.0.1.2
          port: 514
          roles:
            - Appliance event log
    delegate_to: localhost
    register: create_server_idempotency

  - assert:
      that:
        - create_server_idempotency.changed == False
        - create_server_idempotency.data is defined

  - name: Set multiple syslog servers
    meraki_syslog:
      auth_key: '{{auth_key}}'
      org_name: '{{test_org_name}}'
      net_id: '{{net_id}}'
      state: present
      servers:
        - host: 192.0.1.3
          port: 514
          roles:
            - Appliance event log
        - host: 192.0.1.4
          port: 514
          roles:
            - Appliance event log
            - Flows
        - host: 192.0.1.5
          port: 514
          roles:
            - Flows
    delegate_to: localhost
    register: create_multiple_servers

  - assert:
      that:
        - create_multiple_servers['data']['servers'][0]['host'] == "192.0.1.3"
        - create_multiple_servers['data']['servers'][1]['host'] == "192.0.1.4"
        - create_multiple_servers['data']['servers'][2]['host'] == "192.0.1.5"
        - create_multiple_servers['data']['servers'] | length == 3

  - name: Create syslog server with bad name
    meraki_syslog:
      auth_key: '{{auth_key}}'
      org_name: '{{test_org_name}}'
      net_name: '{{test_net_name}}'      
      state: present
      servers:
        - host: 192.0.1.6
          port: 514
          roles:
            - Invalid role
    delegate_to: localhost
    register: invalid_role
    ignore_errors: yes

  - assert:
      that:
        - '"Invalid role found in servers" in invalid_role.msg'

  - name: Add role to existing syslog server  # Adding doesn't work, just creation
    meraki_syslog:
      auth_key: '{{auth_key}}'
      org_name: '{{test_org_name}}'
      net_name: '{{test_net_name}}'
      state: present
      servers:
        - host: 192.0.1.2
          port: 514
          roles:
            - Flows
    delegate_to: localhost
    register: add_role

  - debug:
      msg: '{{add_role.data}}'

  - assert:
      that:
        - add_role.data.servers.0.roles.0 == 'Flows'

  always:
    - name: Delete syslog test network
      meraki_network:
        auth_key: '{{ auth_key }}'
        state: absent
        org_name: '{{test_org_name}}'
        net_name: '{{test_net_name}}'
      delegate_to: localhost
      register: delete_all
      ignore_errors: yes
