{{- range $name, $db := .Values.databases }}
{{ $dbDNSName := replace "_" "-" $name }}
---
apiVersion: k8s.mariadb.com/v1alpha1
kind: Database
metadata:
  name: {{ $.Release.Name }}-{{ $dbDNSName }}
spec:
  name: {{ $name }}
  mariaDbRef:
    name: {{ $.Release.Name }}
  characterSet: utf8
  collate: utf8_general_ci
{{- range $user := $db.roles.admin }}
{{ $userDNSName := replace "_" "-" $user }}
---
apiVersion: k8s.mariadb.com/v1alpha1
kind: Grant
metadata:
  name: {{ $.Release.Name }}-{{ $dbDNSName }}-{{ $userDNSName }}
spec:
  mariaDbRef:
    name: {{ $.Release.Name }}
  privileges: ['ALL']
  database: {{ $name }}
  table: "*"
  username: {{ $user }}
  grantOption: true
{{- end }}
{{- range $user := $db.roles.readonly }}
{{ $userDNSName := replace "_" "-" $user }}
---
apiVersion: k8s.mariadb.com/v1alpha1
kind: Grant
metadata:
  name: {{ $.Release.Name }}-{{ $dbDNSName }}-{{ $userDNSName }}
spec:
  mariaDbRef:
    name: {{ $.Release.Name }}
  privileges: ['SELECT']
  database: {{ $name }}
  table: "*"
  username: {{ $user }}
  grantOption: true
{{- end }}

{{- end }}
