---
- name: Converge idrac_gather_facts for Power Supply
  hosts: all
  gather_facts: false
  vars:
    hostname: "{{ lookup('env', 'IDRAC_IP') }}"
    username: "{{ lookup('env', 'IDRAC_USER') }}"
    password: "{{ lookup('env', 'IDRAC_PASSWORD') }}"
    idrac_port: "{{ lookup('env', 'IDRAC_PORT') }}"
    validate_certs: false
    target:
      - PowerSupply
    idrac_gather_facts_uri_method: "GET"
    idrac_gather_facts_uri_headers:
      Accept: "application/json"
      Content-Type: "application/json"
      OData-Version: "4.0"
    idrac_gather_facts_uri_body_format: "json"
    idrac_gather_facts_uri_status_code:
      - 200
      - 400
      - 401
      - 404
      - -1
    idrac_gather_facts_uri_return_content: true
    diff_data: {}
    exclude_keys: []
    power_supply_api: "/PowerSubsystem/PowerSupplies?$expand=*($levels=1)"

  tasks:

    - name: Find the chassis API
      ansible.builtin.include_tasks:
        ../../tests/asserts/_check_chassis_url.yml

    - name: Fetching Power Supply info
      ansible.builtin.uri:
        url: "https://{{ hostname }}{{ api_chassis }}{{ power_supply_api }}"
        validate_certs: "{{ validate_certs }}"
        ca_path: "{{ ca_path | default(omit) }}"
        method: "{{ idrac_gather_facts_uri_method }}"
        user: "{{ username }}"
        password: "{{ password }}"
        headers: "{{ idrac_gather_facts_uri_headers }}"
        body_format: "{{ idrac_gather_facts_uri_body_format }}"
        status_code: "{{ idrac_gather_facts_uri_status_code }}"
        return_content: "{{ idrac_gather_facts_uri_return_content }}"
      register: powersupply_result
      ignore_errors: true
      no_log: true

    - name: Set powersupply facts
      ansible.builtin.set_fact:
        api_response:
          "{{ powersupply_result.json.Members |
            ansible.utils.remove_keys(target=['@odata.context',
            '@odata.id', '@odata.type', '@odata.etag',
            'ActiveInputVoltage@Redfish.Deprecated',
            'OperationalStatus@odata.count', 'RedTypeOfSet@odata.count']) }}"
      when: powersupply_result is defined and powersupply_result.status == 200

    - name: Gather Facts for the Power Supply component
      ansible.builtin.include_role:
        name: "idrac_gather_facts"

    - name: Assert power supply data
      ansible.builtin.assert:
        that:
          - "((api_response | length == 0) and (power_supply | length == 0))"
        fail_msg: "The response from the role does not match"
        success_msg: "The response from the role matches"
      when: api_response is defined and api_response | length == 0

    - name: Check whether output differs
      ansible.builtin.set_fact:
        result_diff: "{{ power_supply | symmetric_difference(api_response) }}"
      when: api_response is defined and api_response | length > 0

    - name: Assert the differences in List
      ansible.builtin.assert:
        fail_msg: "The response from the role does not match"
        success_msg: "The response from the role matches"
        that:
          - (result_diff | length) == 0
      when: api_response is defined and api_response | length > 0
