---
- name: Installing service telemetry
  debug:
    msg: INSTALLING SERVICE TELEMETRY

# Default setup and platform querying
- name: Pre-setup
  include_tasks: pre.yml

# --> qdr
- name: Check if we have interconnect API
  set_fact:
    has_interconnect_api: "{{ True if 'interconnectedcloud.github.io' in api_groups else False }}"

- name: Check if we have certmanager.k8s.io API
  set_fact:
    has_certmanager_api: "{{ True if 'certmanager.k8s.io' in api_groups else False }}"

- name: Create QDR instance
  include_tasks: component_qdr.yml
  when:
    - has_interconnect_api | bool
    - has_certmanager_api | bool

# --> backends.metrics
- name: Check if we have monitoring.coreos.com API
  set_fact:
    has_monitoring_coreos_api: "{{ True if 'monitoring.coreos.com' in api_groups else False }}"

- block:
  - name: Create Prometheus instance
    include_tasks: component_prometheus.yml

  # --> alerting
  - name: Create Alertmanager instance
    include_tasks: component_alertmanager.yml
  when:
    - has_monitoring_coreos_api | bool
    - observability_strategy == 'use_community'

# --> backends.events
- name: Check if we have elasticsearch API
  set_fact:
    has_elasticsearch_api: "{{ True if 'elasticsearch.k8s.elastic.co' in api_groups else False }}"

- name: Deploy ElasticSearch events backend
  block:
  - name: Setup Certificates for ElasticSearch
    include_tasks: component_certificates.yml

  - name: Setup ElasticSearch
    include_tasks: component_elasticsearch.yml
  when:
    - has_elasticsearch_api | bool
    - has_certmanager_api | bool
    - observability_strategy == 'use_community'

# --> backends.logs
- name: Check if we have loki API
  set_fact:
    has_loki_api: "{{ True if 'loki.openshift.io' in api_groups else False }}"

- name: Create Loki instance
  include_tasks: component_loki.yml
  when:
    - has_loki_api | bool
    - observability_strategy == 'use_community'

# --> graphing
- name: Check if we have integreatly.org API
  set_fact:
    has_integreatly_api: "{{ True if 'integreatly.org' in api_groups else False }}"

- name: Deploy graphing
  block:
  - name: Create Grafana instance
    include_tasks: component_grafana.yml

# TODO
#  - name: Create dashboards
#    include_tasks: component_dashboards.yml
  when:
    - has_integreatly_api | bool
    - observability_strategy == 'use_community'

# --> clouds
- name: Get data about clouds
  debug:
    var: servicetelemetry_vars.clouds

- name: Loop through cloud instances to setup transport receivers
  include_tasks: component_clouds.yml
  loop: "{{ servicetelemetry_vars.clouds }}"
  loop_control:
    loop_var: this_cloud

# Post deployment tasks
- name: Post-setup
  include_tasks: post.yml
