{{/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/}}
{{- if (.Values.global).subchart_release_name }}
{{- $_ := set . "deployment_name" .Chart.Name }}
{{- else }}
{{- $_ := set . "deployment_name" .Release.Name }}
{{- end }}

{{- define "skyline.configmap.etc" }}
{{- $configMapName := index . 0 }}
{{- $envAll := index . 1 }}
{{- with $envAll }}

{{- if empty .Values.conf.skyline.openstack.keystone_url -}}
{{- $_ := tuple "identity" "internal" "api" . | include "helm-toolkit.endpoints.keystone_endpoint_uri_lookup" | set .Values.conf.skyline.openstack "keystone_url" -}}
{{- end -}}

{{- if empty .Values.conf.skyline.openstack.default_region -}}
{{- $_ := set .Values.conf.skyline.openstack "default_region" .Values.endpoints.identity.auth.skyline.region_name -}}
{{- end -}}
{{- if empty .Values.conf.skyline.openstack.system_project -}}
{{- $_ := set .Values.conf.skyline.openstack "system_project" .Values.endpoints.identity.auth.skyline.project_name -}}
{{- end -}}
{{- if empty .Values.conf.skyline.openstack.system_project_domain -}}
{{- $_ := set .Values.conf.skyline.openstack "system_project_domain" .Values.endpoints.identity.auth.skyline.project_domain_name -}}
{{- end -}}
{{- if empty .Values.conf.skyline.openstack.system_user_domain -}}
{{- $_ := set .Values.conf.skyline.openstack "system_user_domain" .Values.endpoints.identity.auth.skyline.user_domain_name -}}
{{- end -}}
{{- if empty .Values.conf.skyline.openstack.system_user_name -}}
{{- $_ := set .Values.conf.skyline.openstack "system_user_name" .Values.endpoints.identity.auth.skyline.username -}}
{{- end -}}
{{- if empty .Values.conf.skyline.openstack.system_user_password -}}
{{- $_ := set .Values.conf.skyline.openstack "system_user_password" .Values.endpoints.identity.auth.skyline.password -}}
{{- end -}}

{{- if empty .Values.conf.skyline.default.database_url -}}
{{- $connection := tuple "oslo_db" "skyline" "skyline" "mysql" . | include "helm-toolkit.endpoints.authenticated_endpoint_uri_lookup" -}}
{{- if .Values.manifests.certificates -}}
{{- $_ := (printf "%s?charset=utf8&ssl_ca=/etc/mysql/certs/ca.crt&ssl_key=/etc/mysql/certs/tls.key&ssl_cert=/etc/mysql/certs/tls.crt&ssl_verify_cert" $connection ) | set .Values.conf.skyline.default.database_url "connection" -}}
{{- else -}}
{{- $_ := set .Values.conf.skyline.default "database_url" $connection -}}
{{- end -}}
{{- end -}}

---
apiVersion: v1
kind: Secret
metadata:
  name: {{ $configMapName }}
type: Opaque
data:
  skyline.yaml: {{ .Values.conf.skyline | toYaml | b64enc }}
  gunicorn.py: {{ .Values.conf.gunicorn | b64enc }}
{{- end }}
{{- end }}

{{- if .Values.manifests.configmap_etc }}
{{- list "skyline-etc" . | include "skyline.configmap.etc" }}
{{- end }}
