{{- if .Values.setDefaultUser.enabled -}}
apiVersion: batch/v1
kind: Job
metadata:
  labels:
    app: {{ template "influxdb.fullname" . }}
    chart: "{{ .Chart.Name }}-{{ .Chart.Version }}"
    release: "{{ .Release.Name }}"
    heritage: "{{ .Release.Service }}"
  name: {{ template "influxdb.fullname" . }}-set-auth
  annotations:
    "helm.sh/hook": post-install
    "helm.sh/hook-delete-policy": hook-succeeded
spec:
  activeDeadlineSeconds: {{ .Values.setDefaultUser.activeDeadlineSeconds }}
  template:
    metadata:
      labels:
        app: {{ template "influxdb.fullname" . }}
        release: "{{ .Release.Name }}"
    spec:
      containers:
      - name: {{ template "influxdb.fullname" . }}-set-auth
        image: "{{ .Values.setDefaultUser.image }}"
        env:
          - name: INFLUXDB_USER
            valueFrom:
              secretKeyRef:
                name: {{ template "influxdb.fullname" . }}-auth
                key: influxdb-user
          - name: INFLUXDB_PASSWORD
            valueFrom:
              secretKeyRef:
                name: {{ template "influxdb.fullname" . }}-auth
                key: influxdb-password
        args:
          - "/bin/sh"
          - "-c"
          - |
             curl -X POST http://{{ template "influxdb.fullname" . }}:{{ .Values.config.http.bind_address }}/query \
             --data-urlencode \
             "q=CREATE USER \"${INFLUXDB_USER}\" WITH PASSWORD '${INFLUXDB_PASSWORD}' {{ .Values.setDefaultUser.user.privileges }}"
      restartPolicy: {{ .Values.setDefaultUser.restartPolicy }}
{{- end -}}
