---
- name: Converge
  hosts: all
  gather_facts: false
  vars:
    ca_cert_path: "{{ lookup('env', 'ca_cert_path') }}"
    ca_cert_name: "{{ lookup('env', 'ca_cert_name') }}"
    import_cert_path: "{{ lookup('env', 'path_for_import_cert') }}"
    export_cert_path: "{{ lookup('env', 'path_for_export_cert') }}"

  tasks:
    - name: Fetching CA certificate from share
      ansible.builtin.include_tasks:
        file: ../__get_helper.yml
      vars:
        idrac_cert_name:
          - "{{ ca_cert_name }}"

    - name: Import CA certificate
      ansible.builtin.import_role:
        name: dellemc.openmanage.idrac_certificate
      vars:
        hostname: "{{ lookup('env', 'IDRAC_IP') }}"
        username: "{{ lookup('env', 'IDRAC_USER') }}"
        password: "{{ lookup('env', 'IDRAC_PASSWORD') }}"
        validate_certs: false
        command: "import"
        reset: false
        certificate_type: "CA"
        certificate_path: "{{ import_cert_path }}{{ ca_cert_name }}"

    - name: Waiting for idrac readiness
      ansible.builtin.wait_for:
        timeout: 30
      when:
        - not ansible_check_mode
        - idrac_certificate_out is defined
        - idrac_certificate_out.changed

    - name: Asserting operation with check mode.
      ansible.builtin.assert:
        that: idrac_certificate_out.msg == "Changes found to be applied."
      when: ansible_check_mode

    - name: Asserting operation with Normal/Idempotence mode.
      ansible.builtin.assert:
        that: idrac_certificate_out.msg == "Successfully performed the
          'import' certificate operation.Reset iDRAC to apply the new
          certificate. Until the iDRAC is reset, the old certificate will
          remain active."
      when: not ansible_check_mode and idrac_certificate_out.changed

    - name: Export CA certificate
      ansible.builtin.import_role:
        name: dellemc.openmanage.idrac_certificate
      vars:
        hostname: "{{ lookup('env', 'IDRAC_IP') }}"
        username: "{{ lookup('env', 'IDRAC_USER') }}"
        password: "{{ lookup('env', 'IDRAC_PASSWORD') }}"
        validate_certs: false
        command: "export"
        certificate_type: "CA"
        certificate_path: "{{ export_cert_path }}"
      when: not ansible_check_mode

    - name: Setting up CA certificate path for exported file
      when: idrac_certificate_out is defined
            and idrac_certificate_out.certificate_path is defined
      ansible.builtin.stat:
        path: "{{ idrac_certificate_out.certificate_path }}"
      register: ca_cert_file
      no_log: true

    - name: Asserting operation with Normal/Idempotence mode.
      ansible.builtin.assert:
        that:
          - ca_cert_file.stat.exists
          - not idrac_certificate_out.changed
          - not idrac_certificate_out.failed
          - idrac_certificate_out.msg == "Successfully performed the
                                        'export' certificate operation."
      when: not ansible_check_mode and not idrac_certificate_out.changed

    - name: Deleting the directory
      ansible.builtin.include_tasks:
        file: ../__delete_directory.yml
