|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 9 | + "github.com/keycloak/terraform-provider-keycloak/keycloak" |
| 10 | +) |
| 11 | + |
| 12 | +func resourceKeycloakRealmLocalization() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + CreateContext: resourceKeycloakRealmLocalizationTextsUpdate, |
| 15 | + ReadContext: resourceKeycloakRealmLocalizationTextsRead, |
| 16 | + DeleteContext: resourceKeycloakRealmLocalizationTextsDelete, |
| 17 | + UpdateContext: resourceKeycloakRealmLocalizationTextsUpdate, |
| 18 | + Description: "Manage realm-level localization texts.", |
| 19 | + Schema: map[string]*schema.Schema{ |
| 20 | + "realm_id": { |
| 21 | + Type: schema.TypeString, |
| 22 | + Required: true, |
| 23 | + ForceNew: true, |
| 24 | + Description: "The realm in which the texts exists.", |
| 25 | + }, |
| 26 | + "locale": { |
| 27 | + Type: schema.TypeString, |
| 28 | + Required: true, |
| 29 | + ForceNew: true, |
| 30 | + Description: "The locale for the localization texts.", |
| 31 | + }, |
| 32 | + "texts": { |
| 33 | + Optional: true, |
| 34 | + Type: schema.TypeMap, |
| 35 | + Elem: &schema.Schema{ |
| 36 | + Type: schema.TypeString, |
| 37 | + }, |
| 38 | + Description: "The mapping of localization texts keys to values.", |
| 39 | + }, |
| 40 | + }, |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func resourceKeycloakRealmLocalizationTextsRead(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 45 | + keycloakClient := meta.(*keycloak.KeycloakClient) |
| 46 | + realmId := data.Get("realm_id").(string) |
| 47 | + locale := data.Get("locale").(string) |
| 48 | + realmLocaleTexts, err := keycloakClient.GetRealmLocalizationTexts(ctx, realmId, locale) |
| 49 | + if err != nil { |
| 50 | + return diag.FromErr(err) |
| 51 | + } |
| 52 | + data.Set("texts", realmLocaleTexts) |
| 53 | + return nil |
| 54 | +} |
| 55 | + |
| 56 | +func resourceKeycloakRealmLocalizationTextsUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 57 | + client := meta.(*keycloak.KeycloakClient) |
| 58 | + realm := d.Get("realm_id").(string) |
| 59 | + locale := d.Get("locale").(string) |
| 60 | + texts := d.Get("texts").(map[string]interface{}) |
| 61 | + textsConverted := convertTexts(texts) |
| 62 | + |
| 63 | + err := client.UpdateRealmLocalizationTexts(ctx, realm, locale, textsConverted) |
| 64 | + if err != nil { |
| 65 | + return diag.FromErr(err) |
| 66 | + } |
| 67 | + |
| 68 | + d.SetId(fmt.Sprintf("%s/%s", realm, locale)) // Set resource ID as "realm/locale" |
| 69 | + return resourceKeycloakRealmLocalizationTextsRead(ctx, d, meta) |
| 70 | +} |
| 71 | + |
| 72 | +func resourceKeycloakRealmLocalizationTextsDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 73 | + client := meta.(*keycloak.KeycloakClient) |
| 74 | + realm := d.Get("realm_id").(string) |
| 75 | + locale := d.Get("locale").(string) |
| 76 | + texts := d.Get("texts").(map[string]interface{}) |
| 77 | + textsConverted := convertTexts(texts) |
| 78 | + |
| 79 | + err := client.DeleteRealmLocalizationTexts(ctx, realm, locale, textsConverted) |
| 80 | + if err != nil { |
| 81 | + return diag.FromErr(err) |
| 82 | + } |
| 83 | + |
| 84 | + d.SetId("") |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +func convertTexts(texts map[string]interface{}) map[string]string { |
| 89 | + translionsConverted := make(map[string]string) |
| 90 | + for key, value := range texts { |
| 91 | + strValue, ok := value.(string) |
| 92 | + if !ok { |
| 93 | + panic(fmt.Sprintf("expected string, got %T for key %s", value, key)) |
| 94 | + } |
| 95 | + translionsConverted[key] = strValue |
| 96 | + } |
| 97 | + |
| 98 | + return translionsConverted |
| 99 | +} |
0 commit comments