{{- define "connections.script" }}
  #!/bin/sh -e
  {{- range .Values.airflow.connections }}
  airflow connections --add --conn_type {{ .type }} --conn_id {{ .id }} 
  {{- if .uri }} --conn_uri {{ .uri | quote }} {{ end -}}
  {{- if .host }} --conn_host {{ .host }} {{ end -}}
  {{- if .login }} --conn_login {{ .login }} {{ end -}}
  {{- if .password }} --conn_password {{ .password }} {{ end -}}
  {{- if .schema }} --conn_schema {{ .schema }} {{ end -}}
  {{- if .port }} --conn_port {{ .port }} {{ end -}}
  {{- if .extra }} --conn_extra {{ .extra | quote }} {{ end -}}
  {{- end }}
{{- end }}
{{- if .Values.airflow.connections }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ template "airflow.fullname" . }}-connections
  labels:
    app: {{ template "airflow.fullname" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
type: Opaque
data:
  add-connections.sh: {{ include "connections.script" . | b64enc }}
{{- end }}
