Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/resources/realm.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ resource "keycloak_realm" "realm" {
- `organizations_enabled` - (Optional) When `true`, organization support is enabled. Defaults to `false`.
- `attributes` - (Optional) A map of custom attributes to add to the realm.
- `internal_id` - (Optional) When specified, this will be used as the realm's internal ID within Keycloak. When not specified, the realm's internal ID will be set to the realm's name.
- `deletion_protection` - (Optional) When set to true, the realm cannot be deleted. Defaults to false.

### Login Settings

Expand Down
12 changes: 12 additions & 0 deletions provider/resource_keycloak_realm.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ func resourceKeycloakRealm() *schema.Resource {
Optional: true,
Default: false,
},
"deletion_protection": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},

// Login Config
"registration_allowed": {
Expand Down Expand Up @@ -1513,6 +1518,10 @@ func resourceKeycloakRealmRead(ctx context.Context, data *schema.ResourceData, m
realm.SmtpServer.Password = smtpPassword
}

if _, ok := data.GetOk("deletion_protection"); !ok {
data.Set("deletion_protection", false)
}

setRealmData(data, realm, keycloakVersion)

return nil
Expand Down Expand Up @@ -1546,6 +1555,9 @@ func resourceKeycloakRealmUpdate(ctx context.Context, data *schema.ResourceData,
}

func resourceKeycloakRealmDelete(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
if data.Get("deletion_protection").(bool) {
return diag.Errorf("Deletion protection is enabled for keycloak_realm resource with realm %s (ID: %s). To delete this resource, first set `deletion_protection` to `false`.", data.Id(), data.Get("internal_id").(string))
}
keycloakClient := meta.(*keycloak.KeycloakClient)

return diag.FromErr(keycloakClient.DeleteRealm(ctx, data.Id()))
Expand Down