- name: Construct oauth redirect reference
  set_fact:
    grafana_oauth_redir_ref:
      kind: OAuthRedirectReference
      apiVersion: v1
      reference:
        kind: Route
        name: 'grafana-route'

- name: Create htpasswd secret for grafana admin
  no_log: true
  k8s:
    definition:
      api_version: v1
      kind: Secret
      metadata:
        name: '{{ meta.name }}-grafana-htpasswd'
        namespace: '{{ meta.namespace }}'
      type: Opaque
      stringData:
        auth: '{{ servicetelemetry_vars.graphing.grafana.admin_user }}:{{ servicetelemetry_vars.graphing.grafana.admin_password | htpasswd_sha1 }}'

- name: Lookup template
  debug:
    msg: "{{ lookup('template', './manifest_grafana.j2') | from_yaml }}"

- name: Set default Grafana manifest
  set_fact:
    grafana_manifest: "{{ lookup('template', './manifest_grafana.j2') | from_yaml }}"
  when: grafana_manifest is not defined

- name: Create an instance of Grafana
  k8s:
    state: '{{ "present" if servicetelemetry_vars.graphing.enabled else "absent" }}'
    definition:
      '{{ grafana_manifest }}'
  when: servicetelemetry_vars.graphing.enabled

- when: servicetelemetry_vars.graphing.enabled
  block:
  - when: servicetelemetry_vars.backends.metrics.prometheus.enabled
    block:
    - name: Retrieve configmap for OAUTH CA certs
      k8s_info:
        api_version: v1
        kind: ConfigMap
        name: serving-certs-ca-bundle
        namespace: '{{ meta.namespace }}'
      register: serving_certs_ca

    - name: Retrieve prometheus secret
      k8s_info:
        api_version: v1
        kind: Secret
        namespace: '{{ meta.namespace }}'
        name: '{{ meta.name }}-prometheus-htpasswd'
      register: prometheus_secret

    - name: Decode prometheus password
      no_log: true
      set_fact:
        prom_basicauth_passwd: '{{ prometheus_secret.resources[0].data.password | b64decode }}'

  - when: servicetelemetry_vars.backends.events.elasticsearch.enabled
    block:
    - name: Retrieve elastic search secret
      k8s_info:
        api_version: v1
        kind: Secret
        name: elasticsearch-es-elastic-user
        namespace: '{{ meta.namespace }}'
      register: es_secret

    - name: Decode elasticsearch password
      set_fact:
        elasticsearch_pass: '{{ es_secret.resources[0].data.elastic | b64decode }}'

  # Lookup existing datasources
  - name: Remove legacy datasources
    k8s:
      api_version: integreatly.org/v1alpha1
      name: '{{ meta.name }}-ds-prometheus'
      kind: GrafanaDataSource
      namespace: '{{ meta.namespace }}'
      state: absent

  - name: Set datasources
    set_fact:
      ds_manifest: "{{ lookup('template', './manifest_grafana_ds.j2') | from_yaml }}"
    when: ds_manifest is not defined

  - name: Create the datasources
    k8s:
      state: '{{ "present" if servicetelemetry_vars.graphing.enabled else "absent" }}'
      definition:
        '{{ ds_manifest }}'
