# SPDX-License-Identifier: MIT
---
- name: Basic insights-client register/unregister
  hosts: all
  gather_facts: false

  tasks:
    - name: Setup Insights
      import_tasks: tasks/setup_insights.yml

    - name: Test Insights registration
      block:
        - name: Register insights
          include_role:
            name: linux-system-roles.rhc
          vars:
            rhc_auth:
              login:
                username: "{{ lsr_rhc_test_data.reg_username }}"
                password: "{{ lsr_rhc_test_data.reg_password }}"
            rhc_insights:
              remediation: absent
              state: present
            rhc_organization: "{{ lsr_rhc_test_data.reg_organization }}"
            rhc_server:
              hostname: "{{ lsr_rhc_test_data.candlepin_host }}"
              port: "{{ lsr_rhc_test_data.candlepin_port }}"
              prefix: "{{ lsr_rhc_test_data.candlepin_prefix }}"
              insecure: "{{ lsr_rhc_test_data.candlepin_insecure }}"
            rhc_baseurl: "{{ lsr_rhc_test_data.baseurl | d(omit) }}"

        - name: Register insights (noop)
          include_role:
            name: linux-system-roles.rhc
          vars:
            rhc_auth:
              login:
                username: "{{ lsr_rhc_test_data.reg_username }}"
                password: "{{ lsr_rhc_test_data.reg_password }}"
            rhc_insights:
              remediation: absent
              state: present
            rhc_organization: "{{ lsr_rhc_test_data.reg_organization }}"
            rhc_server:
              hostname: "{{ lsr_rhc_test_data.candlepin_host }}"
              port: "{{ lsr_rhc_test_data.candlepin_port }}"
              prefix: "{{ lsr_rhc_test_data.candlepin_prefix }}"
              insecure: "{{ lsr_rhc_test_data.candlepin_insecure }}"
            rhc_baseurl: "{{ lsr_rhc_test_data.baseurl | d(omit) }}"

        - name: Get insights UUID
          include_tasks: tasks/get_insights_uuid.yml

        - name: Rename the insights UUID to test_insights_uuid_before
          set_fact:
            test_insights_uuid_before: "{{ test_insights_uuid }}"

        - name: Register (force)
          include_role:
            name: linux-system-roles.rhc
          vars:
            rhc_auth:
              login:
                username: "{{ lsr_rhc_test_data.reg_username }}"
                password: "{{ lsr_rhc_test_data.reg_password }}"
            rhc_insights:
              remediation: absent
            rhc_organization: "{{ lsr_rhc_test_data.reg_organization }}"
            rhc_server:
              hostname: "{{ lsr_rhc_test_data.candlepin_host }}"
              port: "{{ lsr_rhc_test_data.candlepin_port }}"
              prefix: "{{ lsr_rhc_test_data.candlepin_prefix }}"
              insecure: "{{ lsr_rhc_test_data.candlepin_insecure }}"
            rhc_state: reconnect
            rhc_baseurl: "{{ lsr_rhc_test_data.baseurl | d(omit) }}"

        - name: Get rhsm UUID
          include_tasks: tasks/get_rhsm_uuid.yml

        - name: Get insights UUID
          include_tasks: tasks/get_insights_uuid.yml

        - name: Rename the insights UUID to test_insights_uuid_after
          set_fact:
            test_insights_uuid_after: "{{ test_insights_uuid }}"

        - name: Check Insights UUID changed or same as rhsm UUID
          assert:
            that: >
              test_insights_uuid_before != test_insights_uuid_after
              or test_insights_uuid_after == test_rhsm_uuid

        - name: Unregister insights
          include_role:
            name: linux-system-roles.rhc
          vars:
            rhc_insights:
              state: absent

        - name: Unregister insights (noop)
          include_role:
            name: linux-system-roles.rhc
          vars:
            rhc_insights:
              state: absent

      always:
        - name: Unregister
          include_role:
            name: linux-system-roles.rhc
          vars:
            rhc_state: absent
