# 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: Include base variable file base_vars.yml
  include_vars: "{{ base_vars_filename }}"
  no_log: true
  tags: init

- name: Validate input parameters of base_vars are not empty
  fail:
    msg: "{{ input_base_failure_msg }}"
  register: input_base_check
  tags: validate
  when:
    - ansible_conf_file_path | length < 1 or
      public_nic | length < 1 or
      appliance_k8s_pod_net_cidr | length < 1 or
      awx_organization | length < 1 or
      timezone | length < 1 or
      language | length < 1 or
      iso_file_path | length < 1 or
      host_network_nic | length < 1 or
      host_network_dhcp_start_range | length < 1 or
      host_network_dhcp_end_range | length < 1 or
      provision_method | length < 1 or
      default_lease_time | length < 1 or
      provision_os | length < 1 or
      provision_state | length < 1 or
      mount_location | length < 1

- name: Validate management network nic
  assert:
    that: mngmnt_network_nic | length > 1
    fail_msg: "{{ input_base_failure_msg }}"
  when: device_config_support
  
- name: Validate the value of device config support
  assert:
    that:
      - device_config_support | lower == "true" or device_config_support | lower == "false"
    success_msg: "{{ success_device_config }}"
    fail_msg: "{{ fail_device_config }}"
  tags: init

- name: Set fact for device_ip_list_path
  set_fact:
    device_config_ip_file: false
  tags: init

- name: Set the mapping file value for device network
  set_fact:
    device_config_ip_file: true
  when:
    - device_ip_list_path | length > 0
    - '"/" in device_ip_list_path'
    - idrac_support
  tags: init

- name: Warning msg if idrac_support is false and device_ip_list is given
  assert:
    that:
      - '"/"  in device_ip_list_path'
    success_msg: "{{ device_ip_list_not_supported  }}"
  when: not idrac_support
  failed_when: false

- name: Set status for device_config_support
  set_fact:
     device_support_status: false
  tags: init

- name: Validate contents of device_ip_list
  set_fact:
    device_support_status: true
  when: (device_config_support) or (idrac_support and device_config_ip_file)
  tags: init

- name: Assert valid mngmnt_mapping_file_path
  stat:
    path: "{{ device_ip_list_path }}"
  when: device_config_ip_file
  register: result_device_ip_file
  tags: init

- name : Valid device_ip_list
  fail:
    msg: "{{ invalid_mapping_file_path }} for configurations"
  when: device_config_ip_file and not result_device_ip_file.stat.exists
  tags: init

- name: Validate default lease time
  assert:
    that:
      - default_lease_time|int
      - default_lease_time|int <= 31536000
      - default_lease_time|int >= 21600
    success_msg: "{{ success_msg_lease_time }}"
    fail_msg: "{{ fail_msg_lease_time }}"
  tags: [ validate, pxe, network-device, network-ib ]

- name: Calculate max lease time
  set_fact:
    max_lease_time: "{{ default_lease_time|int + 10000 }}"
  tags: init

- name: Set facts to validate snmp support
  set_fact:
    snmp_enabled: false
    mngmnt_mapping_file: false
    host_mapping_file: false
  tags: init

- name: Verify snmp_trap_destination IP address
  set_fact:
    snmp_enabled: true
  when:
    - device_support_status
    - snmp_trap_destination | length > 1
  tags: init

- name: Assert snmp trap destination address
  assert:
    that:
      - snmp_enabled
      - snmp_trap_destination | length > 7
      - snmp_trap_destination | ipv4
    success_msg: "{{ success_snmp_trap_dest }}"
    fail_msg: "{{ fail_snmp_trap_dest }}"
  when: snmp_enabled
  tags: [ validate, idrac, network-device, network-ib ]

- name: Assert snmp community string
  assert:
    that:
      - snmp_enabled
      - snmp_community_name
    success_msg: "{{ success_snmp_comm_msg }}"
    fail_msg: "{{ fail_snmp_comm_msg }}"
  when: snmp_enabled
  tags: [ validate, idrac, network-device, network-ib ]

- name: Check whether ansible config file exists
  stat:
    path: "{{ ansible_conf_file_path }}/ansible.cfg"
  register: ansible_conf_exists
  tags: init

- name: Create the directory if it does not exist
  file:
    path: "{{ ansible_conf_file_path }}"
    state: directory
    mode: "{{ file_perm }}"
  when: not ansible_conf_exists.stat.exists
  changed_when: false
  tags: init

- name: Create ansible config file if it does not exist
  copy:
    dest: '{{ ansible_conf_file_path }}/ansible.cfg'
    mode: "{{ file_perm }}"
    content: |
      [defaults]
      log_path = /var/log/omnia.log
  when: not ansible_conf_exists.stat.exists
  tags: init

- name: Validate infiniband base_vars are not empty
  assert:
    that:
        - ib_network_nic | length > 2
        - ib_network_dhcp_start_range | length > 6
        - ib_network_dhcp_end_range | length > 6
    success_msg: "{{ success_msg_ib }}"
    fail_msg: "{{ fail_msg_ib }}"
    register: ib_check
  when: ib_switch_support
  tags: [ validate, network-ib ]

- name: Assert enable_security_support
  assert:
    that:
      - enable_security_support == true or enable_security_support == false
    success_msg: "{{ enable_security_support_success_msg }}"
    fail_msg: "{{ enable_security_support_fail_msg }}"
  tags: [ validate, init ]

- name: Assert kubernetes pod network CIDR
  assert:
    that:
      - appliance_k8s_pod_net_cidr | ipv4
      - appliance_k8s_pod_net_cidr | length > 9
      - '"/" in appliance_k8s_pod_net_cidr '
    success_msg: "{{ success_msg_k8s_pod_network_cidr }}"
    fail_msg: "{{ fail_msg_k8s_pod_network_cidr }}"
  tags: [ validate, init ]

- name: Assert organization in awx
  assert:
    that:
      - awx_organization | length >= min_username_length
      - awx_organization | length < max_length
      - '"-" not in awx_organization '
      - '"\\" not in awx_organization '
      - '"\"" not in awx_organization '
      - " \"'\" not in awx_organization "
    success_msg: "{{ success_awx_organization }}"
    fail_msg: "{{ fail_awx_organization }}"
  tags: [ validate, awx ]

- name: Make mount directory for grafana if it doesnt exist
  file:
    path: "{{ mount_location }}"
    state: directory
    mode: "{{ mount_dir_perm }}"
    group: root
    owner: root
  tags: [ init, monitoring ]

- name: Validate provision paramters
  import_tasks: validate_provision_vars.yml
  tags: [ validate, idrac, pxe ]

- name: Validate device_config_support
  import_tasks: device_config_validations.yml
  tags: [ validate, init ]

- name: Validate device_config_support
  import_tasks: validate_device_ip_file.yml
  when: device_config_ip_file
  tags: [ validate, init ]

- name: Validate NIC parameters
  import_tasks: validate_nic_vars.yml