|
| 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 resourceKeycloakRealmAttributes() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + CreateContext: resourceKeycloakRealmAttributesCreate, |
| 15 | + ReadContext: resourceKeycloakRealmAttributesRead, |
| 16 | + UpdateContext: resourceKeycloakRealmAttributesUpdate, |
| 17 | + DeleteContext: resourceKeycloakRealmAttributesDelete, |
| 18 | + Schema: map[string]*schema.Schema{ |
| 19 | + "realm_id": { |
| 20 | + Type: schema.TypeString, |
| 21 | + Required: true, |
| 22 | + }, |
| 23 | + "attributes": { |
| 24 | + Type: schema.TypeMap, |
| 25 | + Required: true, |
| 26 | + Description: "A map of attributes for the realm", |
| 27 | + }, |
| 28 | + }, |
| 29 | + } |
| 30 | +} |
| 31 | +func resourceKeycloakRealmAttributesCreate(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 32 | + keycloakClient := meta.(*keycloak.KeycloakClient) |
| 33 | + realmId := data.Get("realm_id").(string) |
| 34 | + attributes := mapFromDataToRealmAttributes(data) |
| 35 | + |
| 36 | + err := keycloakClient.UpdateRealmAttributes(ctx, realmId, attributes) |
| 37 | + if err != nil { |
| 38 | + return diag.FromErr(err) |
| 39 | + } |
| 40 | + |
| 41 | + data.SetId(realmId) |
| 42 | + |
| 43 | + return resourceKeycloakRealmAttributesRead(ctx, data, meta) |
| 44 | +} |
| 45 | + |
| 46 | +func resourceKeycloakRealmAttributesRead(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 47 | + keycloakClient := meta.(*keycloak.KeycloakClient) |
| 48 | + realmId := data.Get("realm_id").(string) |
| 49 | + attributes, err := keycloakClient.GetRealmAttributes(ctx, realmId) |
| 50 | + if err != nil { |
| 51 | + return diag.FromErr(err) |
| 52 | + } |
| 53 | + if attributes == nil { |
| 54 | + return nil |
| 55 | + } |
| 56 | + |
| 57 | + mapFromRealmAttributesToData(attributes, data) |
| 58 | + |
| 59 | + return nil |
| 60 | +} |
| 61 | + |
| 62 | +func resourceKeycloakRealmAttributesUpdate(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 63 | + keycloakClient := meta.(*keycloak.KeycloakClient) |
| 64 | + realmId := data.Get("realm_id").(string) |
| 65 | + attributes := mapFromDataToRealmAttributes(data) |
| 66 | + |
| 67 | + err := keycloakClient.UpdateRealmAttributes(ctx, realmId, attributes) |
| 68 | + if err != nil { |
| 69 | + return diag.FromErr(err) |
| 70 | + } |
| 71 | + |
| 72 | + return resourceKeycloakRealmAttributesRead(ctx, data, meta) |
| 73 | +} |
| 74 | + |
| 75 | +func resourceKeycloakRealmAttributesDelete(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 76 | + keycloakClient := meta.(*keycloak.KeycloakClient) |
| 77 | + realmId := data.Get("realm_id").(string) |
| 78 | + attributes := &keycloak.RealmAttributes{ |
| 79 | + Attributes: map[string]interface{}{}, |
| 80 | + } |
| 81 | + |
| 82 | + err := keycloakClient.UpdateRealmAttributes(ctx, realmId, attributes) |
| 83 | + if err != nil { |
| 84 | + return diag.FromErr(err) |
| 85 | + } |
| 86 | + |
| 87 | + data.SetId("") |
| 88 | + |
| 89 | + return nil |
| 90 | +} |
| 91 | + |
| 92 | +func mapFromDataToRealmAttributes(data *schema.ResourceData) *keycloak.RealmAttributes { |
| 93 | + return &keycloak.RealmAttributes{ |
| 94 | + RealmId: data.Get("realm_id").(string), |
| 95 | + Attributes: data.Get("attributes").(map[string]interface{}), |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func mapFromRealmAttributesToData(attributes *keycloak.RealmAttributes, data *schema.ResourceData) { |
| 100 | + _attributes := map[string]interface{}{} |
| 101 | + if v, ok := data.GetOk("attributes"); ok { |
| 102 | + for key := range v.(map[string]interface{}) { |
| 103 | + _attributes[key] = attributes.Attributes[key] |
| 104 | + } |
| 105 | + } |
| 106 | + if err := data.Set("attributes", _attributes); err != nil { |
| 107 | + panic(fmt.Sprintf("Failed to set attributes: %v", err)) |
| 108 | + } |
| 109 | +} |
0 commit comments