# 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.
#  See the License for the specific language governing permissions and
#  limitations under the License.
---

- name: Get auth string
  shell: echo -n {{ powervault_me4_username }}_{{ powervault_me4_password }} | sha256sum
  register: val_auth_string
  changed_when: false
  ignore_errors: yes
  tags: install

- name: Get session key
  uri:
    url: https://{{ inventory_hostname }}/api/login/{{ val_auth_string.stdout | replace(" -", "") }}
    method: GET
    headers:
      {'datatype': 'json'}
    validate_certs: no
  register: val_session_key
  tags: install

- name: Check if system name has space
  shell: echo {{ powervault_me4_system_name }} | grep '\s' -c
  register: space_count
  ignore_errors: true
  tags: install

- name: Fail if system name has space
  assert:
    that:
      - space_count.stdout|int == 0
      - powervault_me4_system_name | length < 30
    msg: "{{ system_name_wrong }}"

- name: Check if volume name has space
  shell: echo {{ powervault_me4_k8s_volume_name }} | grep '\s' -c
  register: vol_count1
  ignore_errors: true
  tags: install

- name: Fail if volume name has space
  assert:
    that:
      - vol_count1.stdout == "0"
      - powervault_me4_k8s_volume_name | length < 30
    msg: "{{ system_name_wrong }}"

- name: Check if volume name has space
  shell: echo {{ powervault_me4_slurm_volume_name }} | grep '\s' -c
  register: vol_count2
  ignore_errors: true
  tags: install

- name: Assert if volume name has space
  assert:
    that:
      - vol_count2.stdout == "0"
      - powervault_me4_slurm_volume_name | length < 30
    msg: "{{ system_name_wrong }}"

- name: Check if disk name has space
  shell: echo {{ powervault_me4_disk_group_name }} | grep '\s' -c
  register: disk_count
  when: powervault_me4_disk_group_name | length > 1
  ignore_errors: true
  tags: install

- name: Assert if disk name has space
  assert:
    that:
      - disk_count.stdout == "0"
      - powervault_me4_disk_group_name | length < 30
    msg: "{{ system_name_wrong }}" 

- name: Assert snmp notification level
  assert:
    that:
      - powervault_me4_snmp_notify_level | length >1
      - powervault_me4_snmp_notify_level == "crit" or powervault_me4_snmp_notify_level == "error" or powervault_me4_snmp_notify_level == "warn" or powervault_me4_snmp_notify_level == "resolved" or powervault_me4_snmp_notify_level == "info" or powervault_me4_snmp_notify_level == "none"
    fail_msg: "{{ snmp_wrong_value }}"
    success_msg: "{{ snmp_success }}" 

- name: Assert RAID value
  assert:
    that: >
      powervault_me4_raid_levels| lower == "r1" or powervault_me4_raid_levels| lower == "raid1" or
      powervault_me4_raid_levels| lower == "r5" or powervault_me4_raid_levels| lower == "raid5" or
      powervault_me4_raid_levels| lower == "r6" or powervault_me4_raid_levels| lower == "raid6" or
      powervault_me4_raid_levels| lower == "r10" or powervault_me4_raid_levels| lower == "raid10" or
      powervault_me4_raid_levels| lower == "adapt"
    msg: "{{ raid_level_wrong }}"

- name: Assert disk range
  assert:
    that:
      - powervault_me4_disk_range | length >1
    fail_msg: "{{ wrong_disk_range }}"
    success_msg: "{{ correct_disk_range }}"

- name: Check pool value
  assert: 
    that: 
      - powervault_me4_pool == "a" or powervault_me4_pool == "A" or powervault_me4_pool == "b" or powervault_me4_pool == "B"
    msg: "{{ wrong_pool }}"

- name: Check parition percentage
  assert:
    that:
      - powervault_me4_disk_partition_size|int
      - powervault_me4_disk_partition_size|int < 99
      - powervault_me4_disk_partition_size|int > 5
    msg: "{{ wrong_partition }}"

- name: Check the volume size
  assert:
    that: >
      'GB' in powervault_me4_volume_size or 'TB' in powervault_me4_volume_size or 
      'MB' in powervault_me4_volume_size or 
      'B' in powervault_me4_volume_size 
    msg: "{{ wrong_vol_size  }}"

- name: Assert the nic provided
  assert:
    that:
      - powervault_me4_server_nic | length > 2
