# Copyright 2022 Dell Inc. or its subsidiaries. All Rights Reserved.
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
---

- name: Convert timezone.txt to linux format
  command: dos2unix {{ role_path }}/files/timezone.txt
  failed_when: false
  changed_when: false

- name: Searching for timezone
  lineinfile:
    path: "{{ role_path }}/files/timezone.txt"
    line: "{{ timezone }}"
    state: present
  check_mode: yes
  register: timezone_search

- name: Assert timezone
  assert:
    that: timezone_search is not changed
    success_msg: "{{ success_timezone_msg }}"
    fail_msg: "{{ fail_timezone_msg }}"
  register: timezone_check

- name: Assert language for provisioning nodes
  fail:
    msg: "{{ fail_language }}"
  when: '"en-US" not in language'

- name: Assert provisioning method
  assert:
    that:
      - provision_method == "PXE" or provision_method == "idrac"
    success_msg: "{{ success_provision_method }}"
    fail_msg: "{{ fail_provision_method }}"

- name: Assert provision_state
  assert:
    that: 
      - provision_state == "stateful"
    fail_msg: "{{ provision_state_fail_msg }}"
    success_msg: "{{ provision_state_success_msg }}"

- name: Assert operating system
  assert:
    that:
      - provision_os == os_supported_centos or 
        provision_os == os_supported_rocky or
        provision_os == os_supported_leap
    fail_msg: "{{ provision_os_fail_msg }}"
    success_msg: "{{ provision_os_success_msg }}"

- name: Verify the iso_file_path
  stat:
    path: "{{ iso_file_path }}"
  register: result_path_iso_file

- name : Assert iso_file_path location
  fail:
    msg: "{{ missing_iso_file_path }}"
  when: not result_path_iso_file.stat.exists

- name: Validate iso_file_path name
  assert:
    that:
      - result_path_iso_file.stat.exists
      - '".iso" in iso_file_path'
      - provision_os in iso_file_path | lower
    fail_msg: "{{ invalid_iso_file_path }}"
    success_msg:  "{{ valid_iso_file_path }}"

- name: Warning - waiting for 30 seconds
  pause:
    seconds: 30
    prompt: "{{ dns_empty_warning_msg }}"
  when: 
     - primary_dns | length < 1
     - secondary_dns | length < 1  

- name: Validate primary_dns is not empty when secondary_dns provided
  fail:
    msg: "{{ primary_dns_empty_msg }}"
  when: 
    - primary_dns | length < 1
    - secondary_dns | length >1

- name: Validate dns inputs
  block:
    - name: Assert primary_dns when not empty
      assert:
        that:
          - primary_dns | ipv4
        success_msg: "{{ primary_dns_success_msg }}"
        fail_msg: "{{ primary_dns_fail_msg }}"

    - name: Test reachability of primary_dns
      command: ping -c3 {{ primary_dns }}
      failed_when: false
      changed_when: false
      register: primary_dns_check

    - name: Verify primary_dns is reachable
      fail:
        msg: "{{ primary_dns_not_reachable_msg }}"
      when: ping_search_key in primary_dns_check.stdout

    - name: Validate secondary_dns inputs
      block:
        - name: Assert secondary_dns when not empty
          assert:
            that:
              - secondary_dns | ipv4
              - secondary_dns != primary_dns
            success_msg: "{{ secondary_dns_success_msg }}"
            fail_msg: "{{ secondary_dns_fail_msg }}"

        - name: Test reachability of secondary_dns
          command: ping -c3 {{ secondary_dns }}
          failed_when: false
          changed_when: false
          register: secondary_dns_check

        - name: Verify secondary_dns is reachable
          debug:
            msg: "{{ secondary_dns_not_reachable_msg }}"
          when: ping_search_key in secondary_dns_check.stdout    
      when: secondary_dns | length > 1
  when: primary_dns | length > 1