---

- include_tasks: update_meteringconfig_status.yml
  vars:
    current_conditions:
      type: "Running"
      status: "True"
      message: "Reconciling reporting resources"
      lastTransitionTime: "{{ now(utc=False).isoformat() + 'Z' }}"

- name: Log event for reconciling the reporting resources
  k8s_event:
    state: present
    name: metering-operator-reconciling-reporting
    namespace: "{{ meta.namespace }}"
    message: Reconciling reporting resources
    reason: Reconciling
    type: Normal
    source:
      component: metering-ansible-operator
    involvedObject:
      apiVersion: metering.openshift.io
      kind: MeteringConfig
      name: "{{ meta.name }}"
      namespace: "{{ meta.namespace }}"

- name: Deploy reporting resources
  include_tasks: deploy_resources.yml
  vars:
    values_file: /tmp/metering-values.yaml
    resources:
      - template_file: templates/metering/default-storage-location.yaml
        apis: [ {kind: storagelocation, api_version: 'metering.openshift.io/v1'} ]
        prune_label_value: openshift-metering-default-storage-location
        create: "{{ meteringconfig_create_metering_default_storage }}"
      - template_file: templates/openshift-reporting/datasources/default-datasources.yaml
        apis: [ {kind: reportdatasource, api_version: 'metering.openshift.io/v1'} ]
        prune_label_value: default-datasources
      - template_file: templates/openshift-reporting/report-queries/cluster-capacity.yaml
        apis: [ {kind: reportquery, api_version: 'metering.openshift.io/v1'} ]
        prune_label_value: report-queries-cluster-capacity
      - template_file: templates/openshift-reporting/report-queries/cluster-usage.yaml
        apis: [ {kind: reportquery, api_version: 'metering.openshift.io/v1'} ]
        prune_label_value: report-queries-cluster-usage
      - template_file: templates/openshift-reporting/report-queries/cluster-utilization.yaml
        apis: [ {kind: reportquery, api_version: 'metering.openshift.io/v1'} ]
        prune_label_value: report-queries-cluster-utilization
      - template_file: templates/openshift-reporting/report-queries/node-cpu.yaml
        apis: [ {kind: reportquery, api_version: 'metering.openshift.io/v1'} ]
        prune_label_value: report-queries-node-cpu
      - template_file: templates/openshift-reporting/report-queries/node-memory.yaml
        apis: [ {kind: reportquery, api_version: 'metering.openshift.io/v1'} ]
        prune_label_value: report-queries-node-memory
      - template_file: templates/openshift-reporting/report-queries/persistentvolumeclaim-capacity.yaml
        apis: [ {kind: reportquery, api_version: 'metering.openshift.io/v1'} ]
        prune_label_value: report-queries-persistentvolumeclaim-capacity
      - template_file: templates/openshift-reporting/report-queries/persistentvolumeclaim-request.yaml
        apis: [ {kind: reportquery, api_version: 'metering.openshift.io/v1'} ]
        prune_label_value: report-queries-persistentvolumeclaim-request
      - template_file: templates/openshift-reporting/report-queries/persistentvolumeclaim-usage.yaml
        apis: [ {kind: reportquery, api_version: 'metering.openshift.io/v1'} ]
        prune_label_value: report-queries-persistentvolumeclaim-usage
      - template_file: templates/openshift-reporting/report-queries/pod-cpu.yaml
        apis: [ {kind: reportquery, api_version: 'metering.openshift.io/v1'} ]
        prune_label_value: report-queries-pod-cpu
      - template_file: templates/openshift-reporting/report-queries/pod-memory.yaml
        apis: [ {kind: reportquery, api_version: 'metering.openshift.io/v1'} ]
        prune_label_value: report-queries-pod-memory
      - template_file: templates/openshift-reporting/datasources/aws-datasources.yaml
        apis: [ {kind: reportdatasource, api_version: 'metering.openshift.io/v1'} ]
        prune_label_value: aws-datasources
        create: "{{ meteringconfig_enable_reporting_aws_billing }}"
      - template_file: templates/openshift-reporting/report-queries/aws-billing.yaml
        apis: [ {kind: reportquery, api_version: 'metering.openshift.io/v1'} ]
        prune_label_value: report-queries-aws-billing
        create: "{{ meteringconfig_enable_reporting_aws_billing }}"
      - template_file: templates/openshift-reporting/report-queries/pod-cpu-aws.yaml
        apis: [ {kind: reportquery, api_version: 'metering.openshift.io/v1'} ]
        prune_label_value: report-queries-pod-cpu-aws
        create: "{{ meteringconfig_enable_reporting_aws_billing }}"
      - template_file: templates/openshift-reporting/report-queries/pod-memory-aws.yaml
        apis: [ {kind: reportquery, api_version: 'metering.openshift.io/v1'} ]
        prune_label_value: report-queries-pod-memory-aws
        create: "{{ meteringconfig_enable_reporting_aws_billing }}"

- include_tasks: update_meteringconfig_status.yml
  vars:
    current_conditions:
      type: "Running"
      status: "True"
      message: "Finished reconciling reporting resources"
      lastTransitionTime: "{{ now(utc=False).isoformat() + 'Z' }}"
