---
- name: Converge idrac_firmware for https share via proxy
  hosts: all
  gather_facts: false
  tasks:
    - name: Update firmware from repository on a HTTPS via parameter proxy Share
      ansible.builtin.import_role:
        name: "idrac_firmware"
      vars:
        hostname: "{{ lookup('env', 'IDRAC_IP') }}"
        username: "{{ lookup('env', 'IDRAC_USER') }}"
        password: "{{ lookup('env', 'IDRAC_PASSWORD') }}"
        validate_certs: false
        share_name: "{{ lookup('env', 'httpsproxy') }}"
        share_user: "{{ lookup('env', 'shareuser') }}"
        share_password: "{{ lookup('env', 'sharepassword') }}"
        reboot: true
        job_wait: true
        apply_update: true
        proxy_support: "ParametersProxy"
        proxy_server: "{{ lookup('env', 'proxyserver') }}"
        proxy_type: "HTTP"
        proxy_port: "{{ lookup('env', 'proxy_port') }}"
        proxy_uname: "{{ lookup('env', 'proxy_username') }}"
        proxy_passwd: "{{ lookup('env', 'proxy_password') }}"
        catalog_file_name: "{{ lookup('env', 'catalog_file_name') }}"

    - name: "Waiting for idrac to be reachable"
      ansible.builtin.include_tasks:
        file: "../resources/_wait_for_port_to_open.yaml"
      when: not ansible_check_mode
      tags: molecule-idempotence-notest

    - name: "Verifying update firmware from repository on a HTTPS via parameter proxy share in check mode"
      ansible.builtin.assert:
        that: idrac_firmware_out.msg == "Changes found to commit!"
      when: ansible_check_mode
      tags: molecule-idempotence-notest

    - name: "Verifying update firmware from repository on a HTTPS via parameter proxy share in normal mode"
      ansible.builtin.assert:
        that:
          - idrac_firmware_out.msg == "Successfully updated the firmware."
      when: not ansible_check_mode and idrac_firmware_out.changed

    - name: "Verifying update firmware from repository on a HTTPS via parameter proxy share in idempotence mode"
      ansible.builtin.assert:
        that:
          - idrac_firmware_out.msg == "Unable to complete the operation because the catalog name entered has either unsupported firmware packages
            or same version installed on the server."
      when: not ansible_check_mode and not idrac_firmware_out.changed

    - name: "Pre-requisites"
      ansible.builtin.include_tasks:
        file: "../resources/tests/downgrade.yml"
      when: not ansible_check_mode
      tags: molecule-idempotence-notest

    - name: Update firmware from repository on a HTTPS via default proxy Share
      ansible.builtin.import_role:
        name: "idrac_firmware"
      vars:
        hostname: "{{ lookup('env', 'IDRAC_IP') }}"
        username: "{{ lookup('env', 'IDRAC_USER') }}"
        password: "{{ lookup('env', 'IDRAC_PASSWORD') }}"
        share_name: "{{ lookup('env', 'httpsproxy') }}"
        share_user: "{{ lookup('env', 'shareuser') }}"
        share_password: "{{ lookup('env', 'sharepassword') }}"
        validate_certs: false
        reboot: true
        job_wait: true
        apply_update: true
        proxy_support: "DefaultProxy"
        catalog_file_name: "{{ lookup('env', 'catalog_file_name') }}"

    - name: "Waiting for idrac to be reachable"
      ansible.builtin.include_tasks:
        file: "../resources/_wait_for_port_to_open.yaml"
      when: not ansible_check_mode
      tags: molecule-idempotence-notest

    - name: "Verifying update firmware from repository on a HTTPS via default proxy share in check mode"
      ansible.builtin.assert:
        that: idrac_firmware_out.msg == "Changes found to commit!"
      when: ansible_check_mode
      tags: molecule-idempotence-notest

    - name: "Verifying update firmware from repository on a HTTPS via default proxy share in normal mode"
      ansible.builtin.assert:
        that:
          - idrac_firmware_out.msg == "Successfully updated the firmware."
      when: not ansible_check_mode and idrac_firmware_out.changed

    - name: "Verifying update firmware from repository on a HTTPS via default proxy share in idempotence mode"
      ansible.builtin.assert:
        that:
          - idrac_firmware_out.msg == "Unable to complete the operation because the catalog name entered has either unsupported firmware packages
            or same version installed on the server."
      when: not ansible_check_mode and not idrac_firmware_out.changed

    - name: "Pre-requisites"
      ansible.builtin.include_tasks:
        file: "../resources/tests/downgrade.yml"
      when: not ansible_check_mode
      tags: molecule-idempotence-notest

    - name: Update firmware from repository on a HTTPS via parameter proxy Share with proxy_type as SOCKS
      ansible.builtin.import_role:
        name: "idrac_firmware"
      vars:
        hostname: "{{ lookup('env', 'IDRAC_IP') }}"
        username: "{{ lookup('env', 'IDRAC_USER') }}"
        password: "{{ lookup('env', 'IDRAC_PASSWORD') }}"
        validate_certs: false
        share_name: "{{ lookup('env', 'httpsproxy') }}"
        share_user: "{{ lookup('env', 'shareuser') }}"
        share_password: "{{ lookup('env', 'sharepassword') }}"
        reboot: true
        job_wait: true
        apply_update: true
        proxy_support: "ParametersProxy"
        proxy_server: "{{ lookup('env', 'proxyserversocks') }}"
        proxy_type: "SOCKS"
        proxy_port: "{{ lookup('env', 'proxyserversocks_port') }}"
        catalog_file_name: "{{ lookup('env', 'catalog_file_name') }}"

    - name: "Waiting for idrac to be reachable"
      ansible.builtin.include_tasks:
        file: "../resources/_wait_for_port_to_open.yaml"
      when: not ansible_check_mode
      tags: molecule-idempotence-notest

    - name: "Verifying update firmware from repository on a HTTPS via parameter proxy with proxy_type as SOCKS share in check mode"
      ansible.builtin.assert:
        that: idrac_firmware_out.msg == "Changes found to commit!"
      when: ansible_check_mode
      tags: molecule-idempotence-notest

    - name: "Verifying update firmware from repository on a HTTPS via parameter proxy with proxy_type as SOCKS share in normal mode"
      ansible.builtin.assert:
        that:
          - idrac_firmware_out.msg == "Successfully updated the firmware."
      when: not ansible_check_mode and idrac_firmware_out.changed

    - name: "Verifying update firmware from repository on a HTTPS via parameter proxy share with proxy_type as SOCKS in idempotence mode"
      ansible.builtin.assert:
        that:
          - idrac_firmware_out.msg == "Unable to complete the operation because the catalog name entered has either unsupported firmware packages
            or same version installed on the server."
      when: not ansible_check_mode and not idrac_firmware_out.changed
