# Test code for the Meraki VLAN 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

  - name: Create test network
    meraki_network:
      auth_key: '{{auth_key}}'
      state: present
      org_name: '{{test_org_name}}'
      net_name: '{{test_net_name}} - Malware'
      type: appliance
    delegate_to: localhost
    register: net

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

  - name: Enable malware protection with check mode
    meraki_malware:
      auth_key: '{{auth_key}}'
      state: present
      org_name: '{{test_org_name}}'
      net_name: '{{test_net_name}} - Malware'
      mode: enabled
    delegate_to: localhost
    check_mode: yes
    register: get_malware_check

  - assert:
      that:
        - get_malware_check is changed
        - get_malware_check.data is defined

  - name: Enable malware protection
    meraki_malware:
      auth_key: '{{auth_key}}'
      state: present
      org_name: '{{test_org_name}}'
      net_name: '{{test_net_name}} - Malware'
      mode: enabled
    delegate_to: localhost
    register: get_malware

  - debug:
      var: get_malware

  - assert:
      that:
        - get_malware is changed
        - get_malware.data.mode is defined

  - name: Enable malware protection with idempotency
    meraki_malware:
      auth_key: '{{auth_key}}'
      state: present
      org_name: '{{test_org_name}}'
      net_name: '{{test_net_name}} - Malware'
      mode: enabled
    delegate_to: localhost
    register: get_malware_idempotent

  - debug:
      var: get_malware_idempotent

  - assert:
      that:
        - get_malware_idempotent is not changed
        - get_malware_idempotent.data is defined

  - name: Test error when mode is not set
    meraki_malware:
      auth_key: '{{auth_key}}'
      state: present
      org_name: '{{test_org_name}}'
      net_name: '{{test_net_name}} - Malware'
      allowed_files:
        - sha256: e82c5f7d75004727e1f3b94426b9a11c8bc4c312a9170ac9a73abace40aef503
          comment: random zip
    delegate_to: localhost
    register: test_mode_err
    ignore_errors: yes

  - assert:
      that:
        - test_mode_err.msg == "mode must be set when allowed_files or allowed_urls is set."

  - name: Set whitelisted file with check mode
    meraki_malware:
      auth_key: '{{auth_key}}'
      state: present
      org_name: '{{test_org_name}}'
      net_name: '{{test_net_name}} - Malware'
      mode: enabled
      allowed_files:
        - sha256: e82c5f7d75004727e1f3b94426b9a11c8bc4c312a9170ac9a73abace40aef503
          comment: random zip
    delegate_to: localhost
    check_mode: yes
    register: set_file_check

  - debug:
      var:
        set_file_check

  - assert:
      that:
        - set_file_check is changed
        - set_file_check.data is defined

  - name: Set whitelisted file
    meraki_malware:
      auth_key: '{{auth_key}}'
      state: present
      org_name: '{{test_org_name}}'
      net_id: '{{net_id}}'
      mode: enabled
      allowed_files:
        - sha256: e82c5f7d75004727e1f3b94426b9a11c8bc4c312a9170ac9a73abace40aef503
          comment: random zip
    delegate_to: localhost
    register: set_file

  - debug:
      var: set_file

  - assert:
      that:
        - set_file is changed
        - set_file.data.mode is defined

  - name: Set whitelisted file with idempotency
    meraki_malware:
      auth_key: '{{auth_key}}'
      state: present
      org_name: '{{test_org_name}}'
      net_name: '{{test_net_name}} - Malware'
      mode: enabled
      allowed_files:
        - sha256: e82c5f7d75004727e1f3b94426b9a11c8bc4c312a9170ac9a73abace40aef503
          comment: random zip
    delegate_to: localhost
    register: set_file_idempotent

  - debug:
      var: set_file_idempotent

  - assert:
      that:
        - set_file_idempotent is not changed
        - set_file_idempotent.data is defined

  - name: Set whitelisted url with check mode
    meraki_malware:
      auth_key: '{{auth_key}}'
      state: present
      org_name: '{{test_org_name}}'
      net_name: '{{test_net_name}} - Malware'
      mode: enabled
      allowed_urls:
        - url: www.google.com
          comment: Google
    delegate_to: localhost
    check_mode: yes
    register: set_url_check

  - debug:
      var:
        set_url_check

  - assert:
      that:
        - set_url_check is changed
        - set_url_check.data is defined

  - name: Set whitelisted url
    meraki_malware:
      auth_key: '{{auth_key}}'
      state: present
      org_name: '{{test_org_name}}'
      net_name: '{{test_net_name}} - Malware'
      mode: enabled
      allowed_urls:
        - url: www.google.com
          comment: Google
    delegate_to: localhost
    register: set_url

  - debug:
      var: set_url

  - assert:
      that:
        - set_url is changed
        - set_url.data.mode is defined

  - name: Set whitelisted url with idempotency
    meraki_malware:
      auth_key: '{{auth_key}}'
      state: present
      org_name: '{{test_org_name}}'
      net_name: '{{test_net_name}} - Malware'
      mode: enabled
      allowed_urls:
        - url: www.google.com
          comment: Google
    delegate_to: localhost
    register: set_url_idempotent

  - debug:
      var: set_url_idempotent

  - assert:
      that:
        - set_url_idempotent is not changed
        - set_url_idempotent.data is defined

  - name: Get malware settings
    meraki_malware:
      auth_key: '{{auth_key}}'
      state: query
      org_name: '{{test_org_name}}'
      net_name: '{{test_net_name}} - Malware'
    delegate_to: localhost
    register: get_malware

  - assert:
      that:
        - get_malware.data is defined

  #############################################################################
  # Tear down starts here
  #############################################################################
  always:
  - name: Delete test network
    meraki_network:
      auth_key: '{{auth_key}}'
      state: absent
      org_name: '{{test_org_name}}'
      net_name: '{{test_net_name}} - Malware'
    delegate_to: localhost
