---

- include_tasks: update_meteringconfig_status.yml
  vars:
    current_conditions:
      type: "Running"
      status: "True"
      message: "Starting the validation process"
      lastTransitionTime: "{{ now(utc=False).isoformat() + 'Z' }}"

- name: Log event for validating MeteringConfig configuration
  k8s_event:
    state: present
    name: metering-operator-validating-configuration
    namespace: "{{ meta.namespace }}"
    message: Validating the user-provided configuration
    reason: Validating
    type: Normal
    source:
      component: metering-ansible-operator
    involvedObject:
      apiVersion: metering.openshift.io
      kind: MeteringConfig
      name: "{{ meta.name }}"
      namespace: "{{ meta.namespace }}"

#
# Validate OCP-only features were properly disabled
#
- name: Validate fields when OCP-only features are disabled
  block:
  - name: Validate that top-level TLS key is unset/disabled
    assert:
      that:
        - meteringconfig_spec | json_query('tls.enabled') != true
      msg: "Invalid configuration for non-OKD distributions: You cannot set the tls.enabled key to true when disabling OCP-only features."

  - name: Validate that the Prometheus URL is set
    assert:
      that:
        - meteringconfig_spec['reporting-operator'] | json_query('spec.config.prometheus.url') != ""
      msg: "Invalid configuration for non-OKD distributions: You must set the reporting-operator.spec.config.prometheus.url."

  - name: Validate that the reporting-operator Openshift Route is not enabled
    assert:
      that:
      - meteringconfig_spec['reporting-operator'] | json_query('spec.route.enabled') != true
      msg: "Invalid configuration for non-OKD distributions: You cannot enable the Openshift-only reporting-operator route."

  - name: Validate that the reporting-operator OAuth authProxy is not enabled
    assert:
      that:
      - meteringconfig_spec['reporting-operator'] | json_query('spec.authProxy.enabled') != true
      msg: "Invalid configuration for non-OKD distributions: You cannot enable the Openshift-only reporting-operator authProxy."
  rescue:
  - include_tasks: update_meteringconfig_status.yml
    vars:
      current_conditions:
        type: "Invalid"
        status: "True"
        message: |
          "{{ ansible_failed_result.msg }}"
        lastTransitionTime: "{{ now(utc=False).isoformat() + 'Z' }}"
      end_play_after_updating_status: true
    when:
    - ansible_failed_result is defined
    - ansible_failed_result.msg | length > 0
  when: meteringconfig_ocp_disabled

#
# Validate the user-provided storage configuration
#
- name: Validate the storage configuration
  block:
  - name: Validate the user-provided storage type
    assert:
      that:
        - storageType is not undefined and storageType == 'hive'
      msg: "Unsupported spec.storage.type, only 'hive' is  a supported option"

  - name: Validate the user-provided Hive storage configuration matches a support option
    assert:
      that:
        - hiveStorageType is not undefined and hiveStorageType in ['s3', 'sharedPVC', 'hdfs', 'azure', 'gcs' , 's3Compatible']
      msg: "Invalid spec.storage.hive.type: '{{ hiveStorageType }}', must be one of hdfs, s3, azure, gcs, s3Compatible or sharedPVC"
  vars:
    storageType: "{{ meteringconfig_spec_overrides | json_query('storage.type') }}"
    hiveStorageType: "{{ meteringconfig_spec_overrides | json_query('storage.hive.type') }}"
  rescue:
  - include_tasks: update_meteringconfig_status.yml
    vars:
      current_conditions:
        type: "Invalid"
        status: "True"
        message: |
          "{{ ansible_failed_result.msg }}"
        lastTransitionTime: "{{ now(utc=False).isoformat() + 'Z' }}"
      end_play_after_updating_status: true
    when:
    - ansible_failed_result is defined
    - ansible_failed_result.msg | length > 0

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