# Copyright 2021 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.
# limitations under the License.
---
- name: Check that host mapping file exists at mentioned path
  stat:
    path: "{{ host_mapping_file_path }}"
  register: stat_result
  tags: install

- name: Fail if config file doesn't exist
  fail:
    msg: "{{ fail_msg_mapping_file + host_mapping_file_path }}"
  when: not stat_result.stat.exists
  tags: install

- name: Read host mapping file from CSV file and return a dictionary
  read_csv:
    path: "{{ host_mapping_file_path }}"
    key: "{{ mapping_file_key }}"
  register: mapping_file
  delegate_to: localhost
  tags: install

- name: Initialize variable for role support in mapping file
  set_fact:
    component_role_support: false
  tags: install

- name: Check if header is present in mapping file
  shell:  set -o pipefail && awk 'NR==1 { print $1}' "{{ host_mapping_file_path }}"
  register: mngmnt_header
  changed_when: false
  tags: install

- name: Fail if header not in correct format
  assert:
    that: (mngmnt_header.stdout ==  host_mapping_header_format) or (mngmnt_header.stdout == host_mapping_header_with_role_format)
    fail_msg: "{{ fail_mapping_file_header }}"
  tags: install

- name: Check if mapping file is comma seperated
  shell: awk -F\, '{print NF-1}' "{{ host_mapping_file_path }}"
  register: mngmnt_comma_seperated
  changed_when: false
  tags: install

- name: Set variable if component roles given in mapping file
  set_fact:
    component_role_support: true
  when: mngmnt_header.stdout == host_mapping_header_with_role_format
  tags: install

- name: Fail if not comma seperated or if all fields are not given for MAC,Hostname,IP,Component_role
  fail:
    msg: "{{ fail_mapping_file_field_seperation }}"
  when: not(item =="3") and not (item == "-1") and component_role_support
  with_items: "{{ mngmnt_comma_seperated.stdout_lines }}"
  tags: install

- name: Fail if not comma seperated or if all fields are not given for MAC,Hostname,IP
  fail:
    msg: "{{ fail_mapping_file_field_seperation }}"
  when: not(item =="2") and not (item == "-1") and not(component_role_support)
  with_items: "{{ mngmnt_comma_seperated.stdout_lines }}"
  tags: install

- name: Initialize count variables
  set_fact:
    list_of_ips: []
    list_of_roles: []
    list_of_hostnames: []
    count_of_manager: 0
    count_of_compute: 0
    count_of_nfs_node: 0
    count_of_login: 0
    count_total_items: "{{ mapping_file.dict |length }}"
  tags: install

- name: Create list of IPs and component roles and hostnames defined in mapping file
  set_fact:
    list_of_ips: "{{ [ item.value.IP ] + list_of_ips }}"
    list_of_hostnames: "{{ [ item.value.Hostname ] + list_of_hostnames }}"
  loop: "{{ mapping_file.dict | dict2items }}"
  loop_control:
    label: "{{ item.value.MAC }}"
  tags: install

- name: Create list of component roles defined in mapping file
  set_fact:
    list_of_roles: "{{ [ item.value.Component_role ] + list_of_roles }}"
  loop: "{{ mapping_file.dict | dict2items }}"
  loop_control:
    label: "{{ item.value.MAC }}"
  when: component_role_support
  tags: install

- name: Assert hostnames
  assert:
    that:
      - '"_" not in item'
      - '"." not in item'
      - '" " not in item'
    quiet: yes
    fail_msg: "{{ fail_mapping_file_hostname_chars + item }}"
  with_items: "{{ list_of_hostnames }}"
  tags: install

- name: Find count of unique IPs
  set_fact:
    count_of_unique_ip : "{{ list_of_ips| unique| length }}"
  tags: install

- name: Validation to check if unique IPs are provided for each node
  fail:
    msg: "{{ fail_mapping_file_duplicate_ip + host_mapping_file_path }}"
  when: not(count_of_unique_ip|int == count_total_items|int)
  tags: install

- name: Find count of unique hostnames
  set_fact:
    count_of_unique_hostnames : "{{ list_of_hostnames | unique | length }}"
  tags: install

- name: Validation to check if unique hostnames are provided for each node
  fail:
    msg: "{{ fail_mapping_file_duplicate_hostname }}"
  when: not(count_of_unique_hostnames|int == count_total_items| int)
  tags: install

- name: Find count of each component role defined in mapping file
  include_tasks: count_component_roles.yml
  loop: "{{ list_of_roles }}"
  when: component_role_support
  tags: install

- block:
  - name: Validation to check if component roles for each node is defined
    fail:
      msg: "{{ fail_mapping_file_roles_error }}"
    when: not( count_total_items|int == (count_of_manager|int + count_of_compute|int + count_of_login|int + count_of_nfs_node|int))

  - name: Validation to check number of manager nodes defined
    fail:
      msg: "{{ fail_mapping_file_manager_role }}"
    when: not (count_of_manager | int  == 1)

  - name: Validation to check number of compute nodes defined
    fail:
      msg: "{{ fail_mapping_file_compute_role }}"
    when: count_of_compute|int  < 1

  - name: Validation to check number of login nodes defined
    fail:
      msg: "{{ fail_mapping_file_login_role }}"
    when: not ( count_of_login|int == 1)

  - name: Validation to check number of nfs nodes defined
    fail:
      msg: "{{ fail_mapping_file_nfs_role }}"
    when: powervault_support and not (count_of_nfs_node|int == 1)
  tags: install

  rescue:
  - name: Count of roles defined
    fail:
      msg: "{{ count_of_roles_defined }}"
    tags: install

  when: component_role_support
