- name: Cloud collector setup
  debug:
    msg: |
      Working on cloud {{ this_cloud }} to setup metrics and events Smart Gateways

- name: Deploy Metrics Smart Gateway instance for each collector
  vars:
    data_type: 'metrics'
    manifest: './manifest_smartgateway_metrics.j2'
    this_smartgateway: "{{ meta.name }}-{{ this_cloud.name }}-{{ this_collector.collector_type[:4] }}-meter"
  include_tasks: base_smartgateway.yml
  loop: "{{ this_cloud.metrics.collectors }}"
  loop_control:
    loop_var: this_collector
    label: "{{ this_collector.collector_type }}"
  when:
  - servicetelemetry_vars.backends.metrics.prometheus.enabled
  - this_cloud.metrics is defined
  - this_cloud.metrics.collectors is defined
  - this_cloud.metrics.collectors is iterable

- name: Events Smart Gateway deployment
  block:
  - name: Lookup ElasticSearch BasicAuth
    k8s_info:
      api_version: v1
      kind: Secret
      namespace: '{{ meta.namespace }}'
      name: 'elasticsearch-es-elastic-user'
    register: elasticsearch_es_elastic_user

  - name: Filter out ElasticSearch password for BasicAuth
    set_fact:
      elastic_pass: "{{ elasticsearch_es_elastic_user | json_query('resources[0].data.elastic') | b64decode }}"
    no_log: true

  - name: Deploy Events Smart Gateway instance for each collector
    vars:
      data_type: 'events'
      manifest: './manifest_smartgateway_events.j2'
      this_smartgateway: "{{ meta.name }}-{{ this_cloud.name }}-{{ this_collector.collector_type[:4] }}-event"
    include_tasks: base_smartgateway.yml
    loop: "{{ this_cloud.events.collectors }}"
    loop_control:
      loop_var: this_collector
      label: "{{ this_collector.collector_type }}"
    when:
    - servicetelemetry_vars.backends.events.elasticsearch.enabled
    - this_cloud.events is defined
    - this_cloud.events.collectors is defined
    - this_cloud.events is iterable
