#  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: Get iDRAC IP details
     dellemc.openmanage.idrac_system_info:
      idrac_ip: "{{ item | trim }}"
      idrac_user: "{{ idrac_username }}"
      idrac_password: "{{ idrac_password }}"
     register: idrac_ip
     run_once: true
     ignore_errors: yes 
     tags: "TC_004"   

   - name: Validate iDRAC IP
     assert:
      that: 
        - idrac_ip.system_info.iDRACNIC[0].ProductInfo == idrac_search_key
      fail_msg: "{{ item | trim }} {{ idrac_fail_msg }}"
      success_msg: "{{ item | trim }} {{ idrac_success_msg }}"
     tags: "TC_004" 

   - name: Authenticate infiniband Switch
     uri:
       url: http://{{ item | trim }}/admin/launch?script=rh&template=login&action=login
       method: POST
       body_format: form-urlencoded
       body:
         f_user_id: "{{ ib_username }}"
         f_password: "{{ ib_password }}"
         enter: Sign in
       status_code: "{{ infiniband_status_code }}"
     register: login
     ignore_errors: yes
     run_once: true
     tags: "TC_005"  
          
   - name: Assert infiniband switch
     uri:
       url: http://{{ item | trim }}/admin/launch?script=json
       method: POST
       body_format: json
       headers:
         Cookie: "{{ login.set_cookie.split(';')[0] }}"
       body:
         {
         "commands":
          [
            "show version"
          ]
         }
     register: infinibandswitch_info
     when: login.failed == false  or 'set_cookie' not in login
     ignore_errors: yes
     run_once: true
     tags: "TC_005"
              
   - name: Validate infiniband  IP
     assert:
      that: 
        - infinibandswitch_info.json.data['Product name'] == infiniband_search_key
      fail_msg: "{{ infiniband_fail_msg }}"
      success_msg: "{{ infiniband_success_msg }}"
     ignore_errors: yes
     tags: "TC_005"
          
   - name: Get ethernet IP details
     dellos10_command:
       provider:
         host: "{{ item | trim }}"
         username: "{{ ethernet_switch_username }}"
         password: "{{ ethernet_switch_password }}"
       commands: ['show version']
     register: ethernet_info
     ignore_errors: yes
     run_once: true
     when: ethernet_switch_support
     tags: "TC_006"

   - name: Validate ethernet  IP
     assert:
      that: 
        - ethernet_info.stdout | regex_search(ethernet_search_key)
      fail_msg: " {{ item | trim }} {{ ethernet_fail_msg }}"
      success_msg: "{{ item | trim }} {{ ethernet_success_msg }}"
     tags: "TC_006"
           
   - name: Get auth string for powervault
     shell: echo -n {{ powervault_me4_username }}_{{powervault_me4_password}} | sha256sum
     changed_when: false
     when: powervault_support 
     register: auth_string
     ignore_errors: yes
     run_once: true
     tags: "TC_007"
         
   - name: Get session key for powervault
     uri:
       url: https://{{ item | trim }}/api/login/{{ auth_string.stdout | replace(" -", "") }}
       method: GET
       headers:
         {'datatype': 'json'}
       validate_certs: no
     when: powervault_support 
     register: session_key
     ignore_errors: yes
     run_once: true
     tags: "TC_007"
        
   - name: Assert me4_powervault
     uri:
       url: https://{{ item | trim }}/api/show/system
       method: GET
       body_format: json
       validate_certs: no
       use_proxy: no
       headers:
         {'sessionKey': "{{ session_key.json.status[0].response }}", 'datatype':'json'} 
     register: system_info
     ignore_errors: yes
     run_once: true
     tags: "TC_007"

   - name: Validate me4_powervault  IP
     assert:
      that: 
        - "'{{ me4_powervault_search_key }}' in system_info.json.system[0]['scsi-product-id']"
      fail_msg: "{{ item | trim }} {{ powervault_fail_msg }}"
      success_msg: "{{ item | trim }} {{ powervault_success_msg }}"
     ignore_errors: yes
     tags: "TC_007"
       
  rescue:
    - debug:
       msg: "{{ failed_msg }}"
