{{- $configurationUri := .Values.gateway.configuration.uri | required ".Values.gateway.configuration.uri is required." -}}
{{- $authenticationType := .Values.gateway.auth.type | required ".Values.gateway.auth.type is required." -}}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ include "azure-api-management-gateway.fullname" . }}
  namespace: {{ .Release.Namespace | quote }}
  labels:
    {{- include "azure-api-management-gateway.labels" . | nindent 4 }}
data:
  config.service.endpoint: {{ $configurationUri | quote }}
{{- if eq (lower $authenticationType) "azureadapp" }}
  {{- $adTenantId := .Values.gateway.auth.azureAd.tenant.id | required "No Azure AD tenant ID was specified in gateway.auth.azureAd.tenant.id." }}
  {{- $adAppid := .Values.gateway.auth.azureAd.app.id | required "No Azure AD application ID was specified in gateway.auth.azureAd.app.id." }}
  {{- $gatewayName := .Values.gateway.name | required "No gateway name was specified in gateway.name." }}
  gateway.name : {{ $gatewayName | quote }}
  config.service.auth.azureAd.tenantId : {{ $adTenantId | quote }}
  config.service.auth.azureAd.clientId : {{ $adAppid | quote }}
  {{- if .Values.gateway.auth.azureAd.authority }}
  config.service.auth.azureAd.authority : {{ .Values.gateway.auth.azureAd.authority | quote }}
  {{- end }}
{{- end }}
{{- if .Values.ingress.controller.enabled }}
  k8s.ingress.enabled : {{ .Values.ingress.controller.enabled | quote }}
  k8s.ingress.namespace : {{ .Values.ingress.controller.namespace | default ( .Release.Namespace ) | quote }}
  k8s.ingress.dns.suffix: {{ .Values.ingress.controller.dns.suffix | quote }}
{{- end }}
  telemetry.metrics.cloud: {{ .Values.observability.azureMonitor.metrics.enabled | quote }}
  telemetry.logs.std: {{ .Values.observability.logs.std.format | quote }}
  telemetry.logs.std.level: {{ .Values.observability.logs.std.level | quote }}
  telemetry.logs.std.color:  {{ .Values.observability.logs.std.useColor | quote }}
  telemetry.logs.local: {{ .Values.observability.logs.local.type | lower | quote }}
{{- if .Values.observability.logs.local.localsyslog.endpoint }}
  telemetry.logs.local.localsyslog.endpoint: {{ .Values.observability.logs.local.localsyslog.endpoint | quote }}
{{- end }}
{{- if .Values.observability.logs.local.localsyslog.facility }}
  telemetry.logs.local.localsyslog.facility: {{ .Values.observability.logs.local.localsyslog.facility | quote }}
{{- end }}
{{- if .Values.observability.logs.local.rfc5424.endpoint }}
  telemetry.logs.local.rfc5424.endpoint: {{ .Values.observability.logs.local.rfc5424.endpoint | quote }}
{{- end }}
{{- if .Values.observability.logs.local.rfc5424.facility }}
  telemetry.logs.local.rfc5424.facility: {{ .Values.observability.logs.local.rfc5424.facility | quote }}
{{- end }}
{{- if .Values.observability.logs.local.journal.endpoint }}
  telemetry.logs.local.journal.endpoint: {{ .Values.observability.logs.local.journal.endpoint | quote }}
{{- end }}
{{- if .Values.observability.logs.local.json.endpoint }}
  telemetry.logs.local.json.endpoint: {{ .Values.observability.logs.local.json.endpoint | quote }}
{{- end }}
{{- if eq .Values.observability.statsD.enabled true }}
{{- $statsdEndpoint := .Values.observability.statsD.endpoint | required ".Values.observability.statsD.endpoint is required when StatsD is enabled." }}
{{- $statsdSampling := .Values.observability.statsD.sampling | required ".Values.observability.statsD.sampling is required when StatsD is enabled." }}
{{- $statsdTagFormat := .Values.observability.statsD.tagFormat | required ".Values.observability.statsD.tagFormat is required when StatsD is enabled." }}
  telemetry.metrics.local: "statsd"
  telemetry.metrics.local.statsd.endpoint: {{ $statsdEndpoint | quote }}
  telemetry.metrics.local.statsd.sampling: {{ $statsdSampling | quote }}
  telemetry.metrics.local.statsd.tag-format: {{ $statsdTagFormat | quote }}
{{- else }}
  telemetry.metrics.local: "none"
{{- end }}
  observability.opentelemetry.enabled: {{ .Values.observability.opentelemetry.enabled | quote }}
{{- if eq .Values.observability.opentelemetry.enabled true }}
  observability.opentelemetry.collector.uri: {{ .Values.observability.opentelemetry.collector.uri | quote }}
  {{ if .Values.observability.opentelemetry.histogram.buckets -}}
  observability.opentelemetry.histogram.buckets: {{ .Values.observability.opentelemetry.histogram.buckets | quote }}
  {{ end }}
{{- end }}
{{- if .Values.security.tls.server.ciphers.allowedSuites }}
  net.server.tls.ciphers.allowed-suites: {{ .Values.security.tls.server.ciphers.allowedSuites | quote }}
{{- end }}
{{- if .Values.security.tls.client.ciphers.allowedSuites }}
  net.client.tls.ciphers.allowed-suites: {{ .Values.security.tls.client.ciphers.allowedSuites | quote }}
{{- end }}
{{- with .Values.gateway.configuration.additional }}
{{- toYaml . | nindent 2 }}
{{- end }}
