feat: remove realm update & add display name

This commit is contained in:
unteem 2024-02-16 20:05:58 +01:00
parent d62b133df2
commit 74ea9b1445
3 changed files with 9 additions and 14 deletions

View file

@ -58,6 +58,9 @@ type RealmSpec struct {
// Disable realm
// +kubebuilder:validation:Optional
Disable bool `json:"disable,omitempty"`
// Display Name for realm, if not set default de Realm Name
// +kubebuilder:validation:Optional
DisplayName string `json:"displayName,omitempty"`
// Realm host, if not set it will be provider host.
// +kubebuilder:validation:Optional
Host string `json:"host,omitempty"`

View file

@ -111,12 +111,12 @@ func (r *RealmReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
return internal.HandleError(ctx, r, patcher, &realm, err)
}
realmRepresentation, err := keycloakClient.GetRealm(ctx, keycloakClient.AccessToken(), realm.Name)
_, err = keycloakClient.GetRealm(ctx, keycloakClient.AccessToken(), realm.Name)
if err != nil {
apiError := err.(*gocloak.APIError)
if apiError.Code == 404 && apiError.Message == "404 Not Found: Realm not found." {
log.Log.Info("Creating Realm")
realmRepresentation = &gocloak.RealmRepresentation{}
realmRepresentation := &gocloak.RealmRepresentation{}
MutateRealmRepresentation(realm, realmRepresentation)
_, err := keycloakClient.CreateRealm(ctx, keycloakClient.AccessToken(), *realmRepresentation)
if err != nil {
@ -128,13 +128,6 @@ func (r *RealmReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl
}
}
r.log.Info("Updating Realm")
MutateRealmRepresentation(realm, realmRepresentation)
err = keycloakClient.UpdateRealm(ctx, keycloakClient.AccessToken(), *realmRepresentation)
if err != nil {
return internal.HandleError(ctx, r, patcher, &realm, err)
}
secret := &corev1.Secret{}
secret.Name = lshcore.IDPSecretName
secret.Namespace = realm.Namespace
@ -602,6 +595,9 @@ func MutateRealmRepresentation(realm keycloakv1alpha1.Realm, realmRepresentation
realmRepresentation.Realm = &realm.Name
realmRepresentation.Enabled = gocloak.BoolP(!realm.Spec.Disable)
if len(realm.Spec.DisplayName) > 0 {
realmRepresentation.DisplayName = &realm.Spec.DisplayName
}
if len(realm.Spec.Themes.Login) > 0 {
realmRepresentation.LoginTheme = &realm.Spec.Themes.Login
}
@ -616,7 +612,6 @@ func MutateRealmRepresentation(realm keycloakv1alpha1.Realm, realmRepresentation
}
realmRepresentation.BruteForceProtected = gocloak.BoolP(!realm.Spec.DisableBruteForce)
realmRepresentation.InternationalizationEnabled = gocloak.BoolP(true)
// Set default
@ -632,9 +627,6 @@ func MutateRealmRepresentation(realm keycloakv1alpha1.Realm, realmRepresentation
realmRepresentation.SupportedLocales = &realm.Spec.Locale.Available
// Following fields are only set at creation, modifications made in ui will prevail
if realmRepresentation.DisplayName == nil {
realmRepresentation.DisplayName = &realm.Name
}
if realmRepresentation.DisplayNameHTML == nil {
realmRepresentation.DisplayNameHTML = &realm.Name
}