# SPDX-License-Identifier: MIT
# This task files verifies that the provided port is configured in SQL Server
# and opened in firewall. It can verify if a port is closed too.
# It takes the following variables:
# __mssql_tcp_port_new - the configured port to verify
# __mssql_tcp_port_previous - optional: the previous port to verify it is closed
---
- name: List opened firewall ports
  command: firewall-cmd --list-ports
  register: __mssql_firewall_ports
  changed_when: false

- name: Verify that the port is open in firewall {{ __mssql_tcp_port_new }}
  assert:
    that: >-
      '{{ __mssql_tcp_port_new }}/tcp' in __mssql_firewall_ports.stdout
  when: __mssql_tcp_port_new is defined

- name: Verify that the firewall port is closed {{ __mssql_tcp_port_previous }}
  assert:
    that: >-
      '{{ __mssql_tcp_port_previous }}/tcp' not in
      __mssql_firewall_ports.stdout
  when: __mssql_tcp_port_previous is defined

- name: >-
    Verify that the port is configured in SQL Server {{ __mssql_tcp_port_new }}
  include_tasks: tasks/verify_settings.yml
  vars:
    __mssql_tcp_port_matches: "{{ __mssql_tcp_port_new }}"
  when: __mssql_tcp_port_new is defined
