{{- if not .Values.scheduler.existingSecretConnections }}
{{- if .Values.scheduler.connections }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ include "airflow.fullname" . }}-connections
  labels:
    app: {{ include "airflow.labels.app" . }}
    chart: {{ include "airflow.labels.chart" . }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
type: Opaque
stringData:
  add-connections.sh: |
    #!/usr/bin/env bash
    {{- range .Values.scheduler.connections }}
    {{- if $.Values.scheduler.refreshConnections }}
    airflow connections --delete --conn_id {{ .id }}
    {{- end }}
    airflow connections --add --conn_id {{ .id }}
    {{- if .type }} --conn_type {{ .type | quote }} {{ end -}}
    {{- if .uri }} --conn_uri {{ .uri | quote }} {{ end -}}
    {{- if .host }} --conn_host {{ .host | quote }} {{ end -}}
    {{- if .login }} --conn_login {{ .login | quote }} {{ end -}}
    {{- if .password }} --conn_password {{ .password | quote }} {{ end -}}
    {{- if .schema }} --conn_schema {{ .schema | quote }} {{ end -}}
    {{- if .port }} --conn_port {{ .port }} {{ end -}}
    {{- if .extra }} --conn_extra {{ ( regexReplaceAll "[\r\n]+" .extra "" ) | quote }} {{ end -}}
    {{- end }}
{{- end }}
{{- end }}
