---
- name: Converge idrac gather facts for bios
  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:
      - BIOS
    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: []

  tasks:

    - name: Find the system API
      ansible.builtin.include_tasks:
        ../../tests/asserts/_check_system_url.yml

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

    - name: Assert bios dict for length
      ansible.builtin.assert:
        that:
          - "{{ bios | length > 0 }}"

    - name: Fetching BIOS info
      ansible.builtin.uri:
        url: "https://{{ hostname }}{{ api_system }}/Bios"
        validate_certs: false
        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: bios_result
      no_log: false

    - name: Response filter
      ansible.builtin.set_fact:
        api_response:
          "{{ bios_result.json | ansible.utils.remove_keys(target=['@odata.context',
          '@odata.type', '@odata.id', 'SettingsObject', 'Actions', 'AttributeRegistry', 'Description',
          'Id', 'Links', 'Name']) }}"
      vars:
        jquery: "Oem.Dell.DellSystem"

    - name: Set the keys diff
      ansible.builtin.set_fact:
        diff_keys: "{{ bios.keys() | list | symmetric_difference((api_response.keys() | list)) }}"

    - name: Set a diff of dict
      ansible.builtin.set_fact:
        diff_data: "{{ diff_data | combine({item: bios[item]}) }}"
      loop: "{{ bios.keys() }}"
      when:
        - diff_keys | length == 0
        - bios[item] != api_response[item]
        - item not in exclude_keys

    - name: Assert the difference in Keys
      ansible.builtin.assert:
        that:
          - "{{ (diff_keys | length) == 0 }}"
          - "{{ (diff_data | length) == 0 }}"
        fail_msg: "The response from the role does not match | Diff Keys : {{ diff_keys }} Diff Data : {{ diff_data }}"
        success_msg: "The response from the role matches |  Diff Keys : {{ diff_keys }} Diff Data : {{ diff_data }}"
