{{/*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{/*
Return the proper neo4j image name
*/}}
{{- define "neo4j.image" -}}
{{ include "common.images.image" (dict "imageRoot" .Values.image "global" .Values.global) }}
{{- end -}}

{{/*
Return the proper image name (for the init container volume-permissions image)
*/}}
{{- define "neo4j.volumePermissions.image" -}}
{{- include "common.images.image" ( dict "imageRoot" .Values.volumePermissions.image "global" .Values.global ) -}}
{{- end -}}

{{/*
Return the proper Docker Image Registry Secret Names
*/}}
{{- define "neo4j.imagePullSecrets" -}}
{{- include "common.images.renderPullSecrets" (dict "images" (list .Values.image .Values.volumePermissions.image) "context" $) -}}
{{- end -}}

{{/*
Create the name of the service account to use
*/}}
{{- define "neo4j.serviceAccountName" -}}
{{- if .Values.serviceAccount.create -}}
    {{ default (include "common.names.fullname" .) .Values.serviceAccount.name }}
{{- else -}}
    {{ default "default" .Values.serviceAccount.name }}
{{- end -}}
{{- end -}}

{{/*
Get the secret name
*/}}
{{- define "neo4j.secretName" -}}
{{- if .Values.auth.existingSecret -}}
  {{- printf "%s" .Values.auth.existingSecret -}}
{{- else -}}
  {{- printf "%s" (include "common.names.fullname" .) -}}
{{- end -}}
{{- end -}}

{{/*
Return true if cert-manager required annotations for TLS signed certificates are set in the Ingress annotations
Ref: https://cert-manager.io/docs/usage/ingress/#supported-annotations
*/}}
{{- define "neo4j.ingress.certManagerRequest" -}}
{{ if or (hasKey . "cert-manager.io/cluster-issuer") (hasKey . "cert-manager.io/issuer") }}
    {{- true -}}
{{- end -}}
{{- end -}}

{{/*
Get the user defined LoadBalancerIP for this release.
Note, returns 127.0.0.1 if using ClusterIP.
*/}}
{{- define "neo4j.serviceIP" -}}
{{- if eq .Values.service.type "ClusterIP" -}}
127.0.0.1
{{- else -}}
{{- .Values.service.loadBalancerIP | default "" -}}
{{- end -}}
{{- end -}}

{{/*
Gets the host to be used for this application.
If not using ClusterIP, or if a host or LoadBalancerIP is not defined, the value will be empty.
When using Ingress, it will be set to the Ingress hostname.
*/}}
{{- define "neo4j.host" -}}
{{- if and .Values.ingress.enabled .Values.ingress.hostname }}
{{- print .Values.ingress.hostname  -}}
{{- else if .Values.advertisedHost -}}
{{- print .Values.advertisedHost  -}}
{{- else -}}
{{- print (include "neo4j.serviceIP" .) -}}
{{- end -}}
{{- end -}}

{{/*
Return the Neo4j configuration configmap.
*/}}
{{- define "neo4j.configMapName" -}}
{{- if .Values.existingConfigmap -}}
    {{- print (tpl .Values.existingConfigmap $) -}}
{{- else -}}
    {{- print (include "common.names.fullname" .) -}}
{{- end -}}
{{- end -}}

{{/*
Returns true if Neo4j Bolt TLS is enabled.
*/}}
{{- define "neo4j.tls.bolt.enabled" -}}
{{- if or (eq (upper .Values.tls.bolt.level) "REQUIRED") (eq (upper .Values.tls.bolt.level) "OPTIONAL") -}}
    {{- true -}}
{{- end -}}
{{- end -}}

{{/*
Neo4j HTTPS TLS secret name.
*/}}
{{- define "neo4j.tls.https.secretName" -}}
{{- coalesce .Values.tls.https.existingSecret (printf "%s-https-certs" (include "common.names.fullname" .)) -}}
{{- end -}}

{{/*
Neo4j Bolt TLS secret name.
*/}}
{{- define "neo4j.tls.bolt.secretName" -}}
{{- coalesce .Values.tls.bolt.existingSecret (printf "%s-bolt-certs" (include "common.names.fullname" .)) -}}
{{- end -}}

{{/*
Validates the value provided for Bolt TLS level.
*/}}
{{- define "neo4j.validateValues.tls.bolt.level" -}}
{{- if not (or (eq (upper .Values.tls.bolt.level) "REQUIRED") (eq (upper .Values.tls.bolt.level) "OPTIONAL") (eq (upper .Values.tls.bolt.level) "DISABLED")) -}}
neo4j: tls.bolt.level
    The value tls.bolt.level must be either 'REQUIRED', 'OPTIONAL' or 'DISABLED'
{{- end -}}
{{- end -}}

{{/*
Validate Bolt certificates have been provided if TLS enabled.
*/}}
{{- define "neo4j.validateValues.tls.bolt.enabled" -}}
{{- if and (include "neo4j.tls.bolt.enabled" .) (not .Values.tls.bolt.autoGenerated) (not .Values.tls.bolt.existingSecret) -}}
neo4j: tls.https.enabled
    The value tls.bolt.level has been set to 'REQUIRED' or 'OPTIONAL', but neither 'tls.bolt.existingSecret' or 'tls.bolt.autoGenerated' have been provided
{{- end -}}
{{- end -}}

{{/*
Validate HTTPS certificates have been provided if enabled.
*/}}
{{- define "neo4j.validateValues.tls.https.enabled" -}}
{{- if and .Values.tls.https.enabled (not .Values.tls.https.autoGenerated) (not .Values.tls.https.existingSecret) -}}
neo4j: tls.https.enabled
    The value tls.https.enabled has been set to 'true', but neither 'tls.https.existingSecret' or 'tls.https.autoGenerated' have been provided
{{- end -}}
{{- end -}}

{{/*
Compile all warnings into a single message.
*/}}
{{- define "neo4j.validateValues" -}}
{{- $messages := list -}}
{{- $messages := without $messages "" -}}
{{- $messages := append $messages (include "neo4j.validateValues.tls.bolt.level" .) -}}
{{- $messages := append $messages (include "neo4j.validateValues.tls.bolt.enabled" .) -}}
{{- $messages := append $messages (include "neo4j.validateValues.tls.https.enabled" .) -}}
{{- $message := join "\n" $messages -}}

{{- if $message -}}
{{-   printf "\nVALUES VALIDATION:\n%s" $message -}}
{{- end -}}
{{- end -}}
