# SPDX-License-Identifier: MIT
---
- name: Ensure that the grafana role works
  hosts: all

  roles:
    - role: performancecopilot.metrics.keyserver
    - role: performancecopilot.metrics.pcp
      vars:
        pcp_rest_api: true
        pcp_pmproxy_localonly: 1
    - role: performancecopilot.metrics.grafana

  pre_tasks:
    - name: Skip test if not supported by platform
      meta: end_host
      when: (ansible_facts['distribution'] in ['RedHat', 'CentOS'] and
             ansible_facts['distribution_major_version'] | int < 8) or
             ansible_facts['distribution'] not in ['Fedora', 'RedHat', 'CentOS']

    - name: Save state of services
      import_tasks: get_services_state.yml

  tasks:
    - name: Check if Grafana works
      include_tasks: "{{ item }}"
      loop:
        - check_grafana.yml
        - check_grafana_pcp.yml
        - check_default_datasources.yml

    - name: Check headers for ansible_managed, fingerprint
      include_tasks: check_header.yml
      vars:
        __fingerprint: "performancecopilot:ansible-pcp"
      loop:
        - "{{ __grafana_conf }}"
        - "{{ __grafana_provisioning_path }}/datasources/grafana-pcp.yaml"
      loop_control:
        loop_var: __file

  post_tasks:
    - name: Restore state of services
      import_tasks: restore_services_state.yml
