# 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: Set model status
      set_fact:
        model_status: true
      when: item in idrac_info.system_info.System[0].Model
      with_items: "{{ poweredge_model.split(',') | map('trim') }}"

    - name: Update firmware (This task will take time based on current firmware version)
      dellemc.openmanage.idrac_firmware:
        idrac_ip: "{{ inventory_hostname }}"
        idrac_user: "{{ idrac_username }}"
        idrac_password: "{{ idrac_password }}"
        share_name: "{{ management_station_ip }}:{{ nfs_share_offline_repo }}/dellupdates"
        reboot: True
        job_wait: True
        apply_update: True
        catalog_file_name: "Catalog.xml"
      register: update_firmware
      when: model_status

    - name: Wait for one minute to complete firmware update
      wait_for:
        timeout: 60
      run_once: true
      when: model_status

    - name: Wait for iDRAC port to become active
      wait_for:
        host: "{{ inventory_hostname }}"
        port: "{{ idrac_port }}"
        state: started

    - name: Firmware update status
      debug:
        msg: "{{ firmware_job_success_msg }}"
      when:
        - model_status
        - not update_firmware.failed

    - name: Firmware update skip status
      debug:
        msg: "{{ firmware_job_skip_msg }}"
      when: not model_status

  rescue:
    - name: Firmware update job status
      debug:
        msg: "{{ update_firmware.msg }}"
      when:
        - update_firmware.failed
        - idrac_error_message in update_firmware.msg

    - name: Firmware update job failed
      fail:
        msg: "{{ firmware_job_fail_msg }}, Error: {{ update_firmware.msg }}"
      when:
        - update_firmware.failed
        - idrac_error_message not in update_firmware.msg