{{- if required "A valid appgw entry is required!" .Values.appgw }}
{{- end }}

{{- if not .Values.appgw.applicationGatewayID }}
  {{- if not .Values.appgw.name }}
    {{- if required "Please either provide appgw.applicationGatewayID or appgw.name. If application gateway doesn't exist already and you want AGIC to create a new one, specify appgw.name with appgw.subnetPrefix (ex: 10.1.0.0/16). AGIC requires these to create a new application gateway." .Values.appgw.applicationGatewayID }}
    {{- end }}
  {{- end }}
{{- end }}

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "application-gateway-kubernetes-ingress.configmapname" . }}
  labels:
    app: {{ template "application-gateway-kubernetes-ingress.name" . }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version }}
    heritage: {{ .Release.Service }}
    release: {{ .Release.Name }}
data:
  APPGW_VERBOSITY_LEVEL: {{ .Values.verbosityLevel | quote }}
  MULTI_CLUSTER_MODE: {{ .Values.kubernetes.multiClusterMode | quote }}
  HTTP_SERVICE_PORT:     {{ .Values.kubernetes.httpServicePort | quote }}
{{- if .Values.appgw.usePrivateIP }}
  USE_PRIVATE_IP:        {{ .Values.appgw.usePrivateIP | quote }}
{{- end }}
{{- if .Values.appgw.environment }}
  AZURE_ENVIRONMENT:     {{ .Values.appgw.environment | quote }}
{{- end -}}
{{- if .Values.appgw.applicationGatewayID }}
  APPGW_RESOURCE_ID: {{ .Values.appgw.applicationGatewayID | quote }}
{{- else }}
  APPGW_SUBSCRIPTION_ID: {{ default "" .Values.appgw.subscriptionId | quote }}
  APPGW_RESOURCE_GROUP:  {{ default "" .Values.appgw.resourceGroup | quote }}
  APPGW_NAME:            {{ .Values.appgw.name | quote }}

  {{- if or .Values.appgw.subnetID .Values.appgw.subnetName .Values.appgw.subnetPrefix }}
  #if subnet is provided, we treat it as a create case
  APPGW_ENABLE_DEPLOY:   "true"
  {{- end }}

  {{- if .Values.appgw.subnetID }}
  APPGW_SUBNET_ID: {{ .Values.appgw.subnetID | quote }}
  {{- else }}

  {{- if .Values.appgw.subnetPrefix }}
  APPGW_SUBNET_PREFIX: {{ .Values.appgw.subnetPrefix | quote }}
  {{- end }}

  {{- if .Values.appgw.skuName }}
  APPGW_SKU_NAME: {{ .Values.appgw.skuName | quote }}
  {{- end }}

  {{- if .Values.appgw.subnetName }}
  APPGW_SUBNET_NAME: {{ .Values.appgw.subnetName | quote }}
  {{- else }}
  APPGW_SUBNET_NAME: "{{ .Values.appgw.name }}-subnet"
  {{- end }}

  {{- end }}

{{- end }}

{{- if .Values.appgw.subResourceNamePrefix }}
  APPGW_CONFIG_NAME_PREFIX: {{ .Values.appgw.subResourceNamePrefix | quote }}
{{- end }}

{{- if .Values.appgw.shared }}
  APPGW_ENABLE_SHARED_APPGW: {{ .Values.appgw.shared | quote }}
{{- end }}

{{- if .Values.appgw.waf_listener }}
  ATTACH_WAF_POLICY_TO_LISTENER: {{ .Values.appgw.waf_listener | quote }}
{{- end }}

{{- if .Values.kubernetes.watchNamespace }}
  KUBERNETES_WATCHNAMESPACE: "{{ .Values.kubernetes.watchNamespace }}"
{{- end }}

{{- if .Values.armAuth -}}
{{- if or (eq .Values.armAuth.type "aadPodIdentity") (eq .Values.armAuth.type "workloadIdentity") }}
  AZURE_CLIENT_ID: "{{ .Values.armAuth.identityClientID }}"
  USE_MANAGED_IDENTITY_FOR_POD: "true"
{{- end }}
{{- end }}

{{- if .Values.reconcilePeriodSeconds }}
  RECONCILE_PERIOD_SECONDS: {{ .Values.reconcilePeriodSeconds | quote }}
{{- end }}

{{- if .Values.kubernetes.ingressClass}}
  INGRESS_CLASS: "{{ .Values.kubernetes.ingressClass }}"
{{- end}}

{{- if .Values.kubernetes.ingressClassResource.controllerValue}}
  INGRESS_CLASS_RESOURCE_ENABLED: "{{ .Values.kubernetes.ingressClassResource.enabled }}"
{{- end}}

{{- if .Values.kubernetes.ingressClassResource.name}}
  INGRESS_CLASS_RESOURCE_NAME: "{{ .Values.kubernetes.ingressClassResource.name }}"
{{- end}}

{{- if .Values.kubernetes.ingressClassResource.default}}
  INGRESS_CLASS_RESOURCE_DEFAULT: "{{ .Values.kubernetes.ingressClassResource.default }}"
{{- end}}

{{- if .Values.kubernetes.ingressClassResource.controllerValue}}
  INGRESS_CLASS_RESOURCE_CONTROLLER: "{{ .Values.kubernetes.ingressClassResource.controllerValue }}"
{{- end}}

{{- if .Values.addon }}
  ADDON_MODE: {{ .Values.addon | quote }}
{{- end }}