func()

in controllers/keycloakrealmrolebatch/keycloakrealmrolebatch_controller.go [151:204]


func (r *ReconcileKeycloakRealmRoleBatch) putRoles(
	ctx context.Context,
	batch *keycloakApi.KeycloakRealmRoleBatch,
) (roles []keycloakApi.KeycloakRealmRole, resultErr error) {
	log := ctrl.LoggerFrom(ctx)
	log.Info("Start putting keycloak cr role batch")

	for _, role := range batch.Spec.Roles {
		roleName := batch.FormattedRoleName(role.Name)

		var crRole keycloakApi.KeycloakRealmRole

		err := r.client.Get(ctx, types.NamespacedName{Namespace: batch.Namespace, Name: roleName}, &crRole)
		if err != nil && !k8sErrors.IsNotFound(err) {
			return nil, errors.Wrap(err, "unable to check batch role")
		} else if err == nil {
			if r.isOwner(batch, &crRole) {
				log.Info("Role already created")

				roles = append(roles, crRole)

				continue
			}

			return nil, errors.New("one of batch role already exists")
		}

		newRole := keycloakApi.KeycloakRealmRole{
			ObjectMeta: metav1.ObjectMeta{Name: roleName,
				Namespace: batch.Namespace,
				OwnerReferences: []metav1.OwnerReference{
					{Name: batch.Name, Kind: batch.Kind, BlockOwnerDeletion: gocloak.BoolP(true), UID: batch.UID,
						APIVersion: batch.APIVersion},
				}},
			Spec: keycloakApi.KeycloakRealmRoleSpec{
				Name:        role.Name,
				RealmRef:    batch.GetRealmRef(),
				Composite:   role.Composite,
				Composites:  role.Composites,
				Description: role.Description,
				Attributes:  role.Attributes,
				IsDefault:   role.IsDefault,
			}}
		if err := r.client.Create(ctx, &newRole); err != nil {
			return nil, errors.Wrap(err, "unable to create child role from batch")
		}

		roles = append(roles, newRole)
	}

	log.Info("Realm role batch put successfully")

	return
}