---

#
# Validate the user-provided cert/key/caCert fields are non-empty when top-level spec.tls.enabled is false
#
- name: Validate that the user-provided Presto server TLS fields are not empty
  block:
  - name: Validate that the user provided a non-empty TLS certificate value
    assert:
      that:
        - meteringconfig_spec.presto.spec.config.tls.certificate != ""
      msg: "presto.spec.config.tls.certificate cannot be empty if createSecret: true and secretName != ''"

  - name: Validate that the user provided a non-empty TLS CA certificate value
    assert:
      that:
        - meteringconfig_spec.presto.spec.config.tls.caCertificate != ""
      msg: "presto.spec.config.tls.caCertificate cannot be empty if createSecret: true and secretName != ''"

  - name: Validate that the user provided a non-empty TLS key value
    assert:
      that:
        - meteringconfig_spec.presto.spec.config.tls.key != ""
      msg: "presto.spec.config.tls.key cannot be empty if createSecret: true and secretName != ''"
  when: meteringconfig_template_presto_tls_secret

- name: Validate that the user-provided Presto client Auth fields are not empty
  block:
  - name: Validate that the user provided a non-empty auth certificate value
    assert:
      that:
        - meteringconfig_spec.presto.spec.config.auth.certificate != ""
      msg: "presto.spec.config.auth.certificate cannot be empty if createSecret: true and secretName != ''"

  - name: Validate that the user provided a non-empty auth CA certificate value
    assert:
      that:
        - meteringconfig_spec.presto.spec.config.auth.caCertificate != ""
      msg: "presto.spec.config.auth.caCertificate cannot be empty if createSecret: true and secretName != ''"

  - name: Validate that the user provided a non-empty auth key value
    assert:
      that:
        - meteringconfig_spec.presto.spec.config.auth.key != ""
      msg: "presto.spec.config.auth.key cannot be empty if createSecret: true and secretName != ''"

  - name: Validate that TLS is enabled when auth is enabled
    assert:
      that:
        - meteringconfig_spec.presto.spec.config.tls.enabled and meteringconfig_spec.presto.spec.config.auth.enabled
      msg: "Invalid configuration: you cannot enable auth but disable TLS."
  when: meteringconfig_template_presto_auth_secret

- name: Validate that the user provided Presto/Hive client TLS fields are not empty
  block:
  - name: Validate that the user provided a non-empty Hive client TLS certificate value
    assert:
      that:
        - meteringconfig_spec.presto.spec.config.connectors.hive.tls.certificate != ""
      msg: "spec.presto.spec.config.connectors.hive.tls.certificate cannot be empty when createSecret is set to true"

  - name: Validate that the user provided a non-empty Hive client TLS key value
    assert:
      that:
        - meteringconfig_spec.presto.spec.config.connectors.hive.tls.key != ""
      msg: "spec.presto.spec.config.connectors.hive.tls.key cannot be empty when createSecret is set to true"

  - name: Validate that the user provided a non-empty Hive client TLS CA certificate value
    assert:
      that:
        - meteringconfig_spec.presto.spec.config.connectors.hive.tls.caCertificate != ""
      msg: "spec.presto.spec.config.connectors.hive.tls.caCertificate cannot be empty when createSecret is set to true"
  when: meteringconfig_template_presto_hive_tls_secret
