{{- if and .Values.anchoreEnterpriseGlobal.enabled .Values.anchoreEnterpriseUi.enabled -}}
{{- $component := "enterprise-ui" -}}

# Using a secret until UI app supports ENV vars inside the config file. Redis password is included in config.
kind: Secret
apiVersion: v1
metadata:
  name: {{ include "anchore-engine.enterprise-ui.fullname" . | quote }}
  labels:
    app: {{ include "anchore-engine.fullname" . | quote }}
    component: {{ $component }}
    chart: {{ .Chart.Name }}-{{ .Chart.Version }}
    release: {{ .Release.Name }}
    heritage: {{ .Release.Service }}
    {{- with .Values.anchoreGlobal.labels }}
    {{ toYaml . | nindent 4 }}
    {{- end }}
type: Opaque
stringData:
  config-ui.yaml: |
    {{- if .Values.anchoreGlobal.internalServicesSsl.enabled }}
    engine_uri: 'https://{{ template "anchore-engine.api.fullname" . }}:{{ .Values.anchoreApi.service.port }}/v1'
    {{- else }}
    engine_uri: 'http://{{ template "anchore-engine.api.fullname" . }}:{{ .Values.anchoreApi.service.port }}/v1'
    {{- end }}
    {{- if and (index .Values "anchore-ui-redis" "externalEndpoint") (not (index .Values "anchore-ui-redis" "enabled")) }}
    redis_uri: '{{ index .Values "anchore-ui-redis" "externalEndpoint" }}'
    {{- else }}
    redis_uri: 'redis://:{{ index .Values "anchore-ui-redis" "password" }}@{{ template "redis.fullname" . }}-master:6379'
    {{- end }}
  {{- if .Values.anchoreEnterpriseRbac.enabled }}
    {{- if .Values.anchoreGlobal.internalServicesSsl.enabled }}
    rbac_uri: 'https://{{ template "anchore-engine.api.fullname" . }}:{{ .Values.anchoreEnterpriseRbac.service.apiPort }}/v1'
    {{- else }}
    rbac_uri: 'http://{{ template "anchore-engine.api.fullname" . }}:{{ .Values.anchoreEnterpriseRbac.service.apiPort }}/v1'
    {{- end }}
  {{- end }}
  {{- if .Values.anchoreEnterpriseReports.enabled }}
    {{- if .Values.anchoreGlobal.internalServicesSsl.enabled }}
    reports_uri: 'https://{{ template "anchore-engine.api.fullname" . }}:{{ .Values.anchoreEnterpriseReports.service.port}}/v1'
    {{- else}}
    reports_uri: 'http://{{ template "anchore-engine.api.fullname" . }}:{{ .Values.anchoreEnterpriseReports.service.port}}/v1'
    {{- end }}
  {{- end }}
  {{- if .Values.anchoreEnterpriseNotifications.enabled }}
    {{- if .Values.anchoreGlobal.internalServicesSsl.enabled }}
    notifications_uri: 'https://{{ template "anchore-engine.api.fullname" . }}:{{ .Values.anchoreEnterpriseNotifications.service.port}}/v1'
    {{- else}}
    notifications_uri: 'http://{{ template "anchore-engine.api.fullname" . }}:{{ .Values.anchoreEnterpriseNotifications.service.port}}/v1'
    {{- end }}
  {{- end }}
    {{- if and (and .Values.postgresql.externalEndpoint (not .Values.postgresql.enabled)) .Values.anchoreGlobal.dbConfig.ssl }}
    appdb_uri: 'postgresql://{{ .Values.postgresql.postgresUser }}:{{ .Values.postgresql.postgresPassword }}@{{ .Values.postgresql.externalEndpoint }}/{{ .Values.postgresql.postgresDatabase }}?ssl=verify-full'
    {{- else if and .Values.postgresql.externalEndpoint (not .Values.postgresql.enabled) }}
    appdb_uri: 'postgresql://{{ .Values.postgresql.postgresUser }}:{{ .Values.postgresql.postgresPassword }}@{{ .Values.postgresql.externalEndpoint }}/{{ .Values.postgresql.postgresDatabase }}'
    {{- else if and (index .Values "cloudsql" "enabled") (not (index .Values "postgresql" "enabled")) }}
    appdb_uri: 'postgresql://{{ .Values.postgresql.postgresUser }}:{{ .Values.postgresql.postgresPassword }}@localhost:5432/{{ .Values.postgresql.postgresDatabase }}'
    {{- else }}
    appdb_uri: 'postgresql://{{ .Values.postgresql.postgresUser }}:{{ .Values.postgresql.postgresPassword }}@{{ template "postgres.fullname" . }}:5432/{{ .Values.postgresql.postgresDatabase }}'
    {{- end }}
    license_path: '/home/anchore/'
    enable_ssl: {{ .Values.anchoreEnterpriseUi.enableSsl }}
    enable_proxy: {{ .Values.anchoreEnterpriseUi.enableProxy }}
    allow_shared_login: {{ .Values.anchoreEnterpriseUi.enableSharedLogin }}
    redis_flushdb: {{ .Values.anchoreEnterpriseUi.redisFlushdb }}
    policy_hub_uri: {{ .Values.anchoreEnterpriseUi.policyHubUri }}
    {{- with .Values.anchoreEnterpriseUi.customLinks }}
    custom_links:
      title: {{ .title }}
      links:
      {{- range .links }}
        {{- with . }}
        - title: {{ .title }}
          uri: {{ .uri }}
        {{- end }}
      {{- end }}
    {{- end }}
    {{- with .Values.anchoreEnterpriseUi.enableAddRepositories }}
    enable_add_repositories:
      admin: {{ .admin }}
      standard: {{ .standard }}
    {{- end }}
{{- end -}}
