#  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.
---  
- block: 

   - name: Include idrac vars
     include_vars: "../input_params/idrac_vars.yml"
  
   - name: Export SCP configuration to local file
     idrac_server_config_profile:
      idrac_ip: "{{ inventory_hostname }}"
      idrac_user: "{{ idrac_username }}"
      idrac_password: "{{ idrac_password }}"
      share_name: "{{ playbook_dir }}"
      command: "export"
      scp_file: "{{ inventory_hostname }}_scp.xml"
      job_wait: true
     run_once: true
         
   - name: Fetch the SCP configuration
     command: cat "{{ inventory_hostname }}_scp.xml"
     register: config_file
           
   - name: Testcase to verify the boot mode
     assert:
      that: 
        - config_file.stdout | regex_search( bootmode_regex1 )
      fail_msg: " {{ bootmode_check_fail_msg }}"
      success_msg: "{{ bootmode_check_success_msg }}"
     ignore_errors: true
         
   - name: Testcase to verify the SNMP configuration
     assert:
      that: 
       - config_file.stdout | regex_search(snmp_regex1)
      fail_msg: " {{ snmp_check_fail_msg }}"
      success_msg: "{{ snmp_check_success_msg }}"
     ignore_errors: true
     
   - name: Testcase to verify the system profile value Performance
     assert:
      that: 
       - config_file.stdout | regex_search(sysprofile_regex1)
      fail_msg: " {{ sysprofile_check_fail_msg }}"
      success_msg: "{{ sysprofile_check_success_msg }}"
     ignore_errors: true
     when: idrac_system_profile == sysprofile_value1
          
   - name: Testcase to verify the system profile value PerformancePerWatt(DAPC)
     assert:
      that: 
       - config_file.stdout | regex_search(sysprofile_regex2)
      fail_msg: " {{ sysprofile_check_fail_msg }}"
      success_msg: "{{ sysprofile_check_success_msg }}"
     ignore_errors: true
     when: idrac_system_profile == sysprofile_value2

   - name: Testcase to verify the system profile value WorkstationPerformance
     assert:
      that: 
       - config_file.stdout | regex_search(sysprofile_regex3)
      fail_msg: " {{ sysprofile_check_fail_msg }}"
      success_msg: "{{ sysprofile_check_success_msg }}"
     ignore_errors: true
     when: idrac_system_profile == sysprofile_value3

   - name: Testcase to verify the system profile value PerformancePerWatt(OS)
     assert:
      that: 
       - config_file.stdout | regex_search(sysprofile_regex4)
      fail_msg: " {{ sysprofile_check_fail_msg }}"
      success_msg: "{{ sysprofile_check_success_msg }}"
     ignore_errors: true
     when: idrac_system_profile == sysprofile_value4  

   - name: Testcase to verify the pxe device status
     assert:
      that: 
       - config_file.stdout | regex_search(pxedevice)
      fail_msg: " {{ pxedevice_check_fail_msg }}"
      success_msg: "{{ pxedevice_check_success_msg }}"
     ignore_errors: true
     when: not (enterprise_license or datacenter_license)
     
   - name: Initialized RAID status
     set_fact:
       raid_type: false
               
   - name: Get iDRAC info details
     idrac_system_info:
      idrac_ip: "{{ inventory_hostname }}"
      idrac_user: "{{ idrac_username }}"
      idrac_password: "{{ idrac_password }}"
     register: idrac_info 

   - name: Set RAID status
     set_fact:
       raid_type: true
     with_items: "{{ idrac_info.system_info.Controller }}"
     loop_control:
       index_var: my_idx3
     when: '"RAID" in idrac_info.system_info.ControllerSensor[my_idx3].FQDD'
                
   - name: Testcase to verify virtual disk creation status
     assert:
      that:
       - " 'omnia_vd' in idrac_info.system_info.VirtualDisk[0].Name "
      fail_msg: "{{ vd_fail_msg }}"
      success_msg: " {{ vd_success_msg }}"
     ignore_errors: true
     when: raid_type

   - name: Execute get pods command
     command: "kubectl get pods -n {{ awx_namespace }}"
     changed_when: true
     register: k8s_pods
     ignore_errors: true
     run_once: true
     
   - name: Get awx pod 
     set_fact:
      awx_pods: "{{ item | regex_search(awx_pod_regex) | trim  }}"
     with_items: 
       - "{{ k8s_pods.stdout_lines }}"
     run_once: true
     ignore_errors: true
     when: item | regex_search(awx_pod_item_regex)

   - name: Get awx cluster ip
     shell: "kubectl get svc awx-ui -n {{ awx_namespace }} -o jsonpath='{.spec.clusterIP}'"
     register: awx_cluster_ip
     changed_when: false
     ignore_errors: true

   - name: Get AWX admin password
     shell: "kubectl get secret awx-admin-password -n {{ awx_namespace }} -o jsonpath='{.data.password}' | base64 --decode"
     register: awx_admin_password
     changed_when: false
     ignore_errors: true
          
   - name: Execute awx get inventory hosts command
     shell: "awx --conf.host http://{{ awx_cluster_ip.stdout }}:8052 --conf.username admin --conf.password {{ awx_admin_password.stdout }} --conf.insecure hosts list --inventory {{ provisioned_inventory_name }} -f human --filter 'name'"
     changed_when: true
     register: idrac_hosts
     run_once: true
     ignore_errors: true
               
   - name: Testcase to verify IP added to provisioned_idrac_inventory
     assert:
      that: 
        - idrac_hosts.stdout | regex_search( inventory_hostname )
      fail_msg: "{{ provisioned_ip_fail_msg }}"
      success_msg: "{{ provisioned_ip_success_msg }}"
     ignore_errors: true 
          
   - name: Update firmware
     idrac_firmware:
      idrac_ip: "{{ inventory_hostname }}"
      idrac_user: "{{ idrac_username }}"
      idrac_password: "{{ idrac_password }}"
      share_name: "/var/nfs_repo/dellupdates"
      reboot: false
      job_wait: true
      apply_update: False
      catalog_file_name: "Catalog.xml"
     register: idrac_firmware
     when: firmware_update_required

   - name: Checking firmware update status
     assert:
      that:
        - idrac_firmware.msg in firmware_status
      fail_msg: "{{ firmware_fail_msg }}"
      success_msg: "{{ firmware_success_msg }}"
     ignore_errors: true
     when: firmware_update_required  
     
   - name: Remove the scp file
     file:
       path: "{{ inventory_hostname }}_scp.xml"
       state: absent
     run_once: true             
      
  rescue:
    - debug:
       msg: "{{ failed_msg }}"