{{- include "nplus.init" $ -}}

{{/*
Assemble List of Components
*/}}
{{- define "nplus.instance.getComponents" -}}
{{- $mylist := list -}}
{{- range $key, $value := .Values.components }}
  {{- if $value }}
    {{- $replicas := (( index $.Values $key ).replicaCount | default 1) | int }}
    {{- if (gt $replicas 1) }}
      {{- $mylist = append $mylist (printf "%s(%d)" ($key) $replicas) }}
    {{- else }}{{/* if (gt $replicas 1) */}}
      {{- $mylist = append $mylist (printf "%s" ($key)) }}
    {{- end }}
  {{- end }}{{/* if $value */}}
{{- end }}{{/* range $key, $value := .Values.components */}}
{{- join "," $mylist -}}
{{- end -}}

{{/*
Assemble List of Expectations
*/}}
{{- define "nplus.instance.getExpected" -}}
{{- range $key, $value := .Values.components }}
  {{- if $value }}
    {{- $replicas := (( index $.Values $key ).replicaCount | default 1) | int }}
- component: {{ $key }}
  replicaCount: {{ $replicas }}
  {{- end }}
{{- end }}{{/* range $key, $value := .Values.components */}}
{{- end -}}

apiVersion: nplus.cloud/v1beta1
kind: Instance
metadata:
  {{- if .this.utils.includeNamespace }}
  namespace: {{ .Release.Namespace }}
  {{- end }}
  name: {{ .Release.Name }}
  labels:
    {{- include "nplus.instanceLabels" . | nindent 4 }}
  annotations:
    {{- include "nplus.annotations" . | nindent 4 }}
    argocd.argoproj.io/sync-wave: "1"
spec:
  nscaleVersion: {{ .instance.version | quote }}
  components: {{ include "nplus.instance.getComponents" . }}
  handler: {{ .component.handler }}
  expected:
  {{- include "nplus.instance.getExpected" . | indent 2 }}
  tenant: {{ (.this.meta).tenant }}
  provider: {{ (.this.meta).provider }}
  url: {{ include "nplus.url" . | quote }}