{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{- if and .Values.msgTopologyOperator.enabled .Values.msgTopologyOperator.rbac.create -}}
{{- $readonlyVerbs := list "get" "list" "watch" }}
{{- $allVerbs := list "create" "delete" "deletecollection" "get" "list" "patch" "update" "watch" }}
{{- $roles := dict "view" $readonlyVerbs "edit" $allVerbs "admin" $allVerbs }}
{{- $versionLabel := dict "app.kubernetes.io/version" ( include "common.images.version" ( dict "imageRoot" .Values.msgTopologyOperator.image "chart" .Chart ) ) }}
{{- $labels := include "common.tplvalues.merge" ( dict "values" ( list .Values.commonLabels $versionLabel ) "context" . ) }}
{{- range $role, $verbs := $roles -}}
---
apiVersion: {{ include "common.capabilities.rbac.apiVersion" $ }}
kind: ClusterRole
metadata:
  name: {{ template "rmqco.msgTopologyOperator.fullname.namespace" $ }}-{{ $role }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" $labels "context" $ ) | nindent 4 }}
    app.kubernetes.io/component: messaging-topology-operator
    app.kubernetes.io/part-of: rabbitmq
    rbac.authorization.k8s.io/aggregate-to-{{ $role }}: "true"
  {{- if $.Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" $.Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
rules:
  - apiGroups:
      - rabbitmq.com
    resources:
      - bindings
      - exchanges
      - federations
      - operatorpolicies
      - permissions
      - policies
      - queues
      - schemareplications
      - shovels
      - superstreams
      - topicpermissions
      - users
      - vhosts
    verbs: {{ $verbs | toYaml | nindent 6 }}
{{ end }}
{{- end -}}
