|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 9 | + "github.com/keycloak/terraform-provider-keycloak/keycloak" |
| 10 | +) |
| 11 | + |
| 12 | +func resourceKeycloakOpenidClientAuthorizationClientScopePolicy() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + CreateContext: resourceKeycloakOpenidClientAuthorizationClientScopePolicyCreate, |
| 15 | + ReadContext: resourceKeycloakOpenidClientAuthorizationClientScopePolicyRead, |
| 16 | + DeleteContext: resourceKeycloakOpenidClientAuthorizationClientScopePolicyDelete, |
| 17 | + UpdateContext: resourceKeycloakOpenidClientAuthorizationClientScopePolicyUpdate, |
| 18 | + Importer: &schema.ResourceImporter{ |
| 19 | + StateContext: genericResourcePolicyImport, |
| 20 | + }, |
| 21 | + Schema: map[string]*schema.Schema{ |
| 22 | + "resource_server_id": { |
| 23 | + Type: schema.TypeString, |
| 24 | + Required: true, |
| 25 | + }, |
| 26 | + "realm_id": { |
| 27 | + Type: schema.TypeString, |
| 28 | + Required: true, |
| 29 | + }, |
| 30 | + "name": { |
| 31 | + Type: schema.TypeString, |
| 32 | + Required: true, |
| 33 | + }, |
| 34 | + "decision_strategy": { |
| 35 | + Type: schema.TypeString, |
| 36 | + Optional: true, |
| 37 | + ValidateFunc: validation.StringInSlice(keycloakOpenidClientResourcePermissionDecisionStrategies, false), |
| 38 | + Default: "UNANIMOUS", |
| 39 | + }, |
| 40 | + "logic": { |
| 41 | + Type: schema.TypeString, |
| 42 | + Optional: true, |
| 43 | + ValidateFunc: validation.StringInSlice(keycloakPolicyLogicTypes, false), |
| 44 | + Default: "POSITIVE", |
| 45 | + }, |
| 46 | + "description": { |
| 47 | + Type: schema.TypeString, |
| 48 | + Optional: true, |
| 49 | + }, |
| 50 | + "scope": { |
| 51 | + Type: schema.TypeSet, |
| 52 | + Required: true, |
| 53 | + MinItems: 1, |
| 54 | + Elem: &schema.Resource{ |
| 55 | + Schema: map[string]*schema.Schema{ |
| 56 | + "id": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Required: true, |
| 59 | + }, |
| 60 | + "required": { |
| 61 | + Type: schema.TypeBool, |
| 62 | + Optional: true, |
| 63 | + Default: false, |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + }, |
| 68 | + }, |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func getOpenidClientAuthorizationClientScopePolicyResourceFromData(data *schema.ResourceData) *keycloak.OpenidClientAuthorizationClientScopePolicy { |
| 73 | + var clientScopes []keycloak.OpenidClientAuthorizationClientScope |
| 74 | + |
| 75 | + if v, ok := data.Get("scope").(*schema.Set); ok { |
| 76 | + for _, clientScope := range v.List() { // Use List() for TypeSet |
| 77 | + clientScopeMap := clientScope.(map[string]interface{}) |
| 78 | + clientScopes = append(clientScopes, keycloak.OpenidClientAuthorizationClientScope{ |
| 79 | + Id: clientScopeMap["id"].(string), |
| 80 | + Required: clientScopeMap["required"].(bool), |
| 81 | + }) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + resource := keycloak.OpenidClientAuthorizationClientScopePolicy{ |
| 86 | + Id: data.Id(), |
| 87 | + ResourceServerId: data.Get("resource_server_id").(string), |
| 88 | + RealmId: data.Get("realm_id").(string), |
| 89 | + DecisionStrategy: data.Get("decision_strategy").(string), |
| 90 | + Logic: data.Get("logic").(string), |
| 91 | + Name: data.Get("name").(string), |
| 92 | + Type: "client-scope", |
| 93 | + Scope: clientScopes, |
| 94 | + Description: data.Get("description").(string), |
| 95 | + } |
| 96 | + |
| 97 | + return &resource |
| 98 | +} |
| 99 | + |
| 100 | +func setOpenidClientAuthorizationClientScopePolicyResourceData(ctx context.Context, keycloakClient *keycloak.KeycloakClient, policy *keycloak.OpenidClientAuthorizationClientScopePolicy, data *schema.ResourceData) error { |
| 101 | + data.SetId(policy.Id) |
| 102 | + |
| 103 | + data.Set("resource_server_id", policy.ResourceServerId) |
| 104 | + data.Set("realm_id", policy.RealmId) |
| 105 | + data.Set("name", policy.Name) |
| 106 | + data.Set("decision_strategy", policy.DecisionStrategy) |
| 107 | + data.Set("logic", policy.Logic) |
| 108 | + data.Set("description", policy.Description) |
| 109 | + |
| 110 | + // Convert scope slice to a set for consistent Terraform state |
| 111 | + clientScopesSet := make([]interface{}, len(policy.Scope)) |
| 112 | + for i, g := range policy.Scope { |
| 113 | + clientScopesSet[i] = map[string]interface{}{ |
| 114 | + "id": g.Id, |
| 115 | + "required": g.Required, |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + if err := data.Set("scope", clientScopesSet); err != nil { |
| 120 | + return err |
| 121 | + } |
| 122 | + |
| 123 | + return nil |
| 124 | +} |
| 125 | + |
| 126 | +func resourceKeycloakOpenidClientAuthorizationClientScopePolicyCreate(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 127 | + keycloakClient := meta.(*keycloak.KeycloakClient) |
| 128 | + |
| 129 | + resource := getOpenidClientAuthorizationClientScopePolicyResourceFromData(data) |
| 130 | + |
| 131 | + err := keycloakClient.NewOpenidClientAuthorizationClientScopePolicy(ctx, resource) |
| 132 | + if err != nil { |
| 133 | + return diag.FromErr(err) |
| 134 | + } |
| 135 | + |
| 136 | + err = setOpenidClientAuthorizationClientScopePolicyResourceData(ctx, keycloakClient, resource, data) |
| 137 | + if err != nil { |
| 138 | + return diag.FromErr(err) |
| 139 | + } |
| 140 | + |
| 141 | + return resourceKeycloakOpenidClientAuthorizationClientScopePolicyRead(ctx, data, meta) |
| 142 | +} |
| 143 | + |
| 144 | +func resourceKeycloakOpenidClientAuthorizationClientScopePolicyRead(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 145 | + keycloakClient := meta.(*keycloak.KeycloakClient) |
| 146 | + |
| 147 | + realmId := data.Get("realm_id").(string) |
| 148 | + resourceServerId := data.Get("resource_server_id").(string) |
| 149 | + id := data.Id() |
| 150 | + |
| 151 | + resource, err := keycloakClient.GetOpenidClientAuthorizationClientScopePolicy(ctx, realmId, resourceServerId, id) |
| 152 | + if err != nil { |
| 153 | + return handleNotFoundError(ctx, err, data) |
| 154 | + } |
| 155 | + |
| 156 | + err = setOpenidClientAuthorizationClientScopePolicyResourceData(ctx, keycloakClient, resource, data) |
| 157 | + if err != nil { |
| 158 | + return diag.FromErr(err) |
| 159 | + } |
| 160 | + |
| 161 | + return nil |
| 162 | +} |
| 163 | + |
| 164 | +func resourceKeycloakOpenidClientAuthorizationClientScopePolicyUpdate(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 165 | + keycloakClient := meta.(*keycloak.KeycloakClient) |
| 166 | + |
| 167 | + resource := getOpenidClientAuthorizationClientScopePolicyResourceFromData(data) |
| 168 | + |
| 169 | + err := keycloakClient.UpdateOpenidClientAuthorizationClientScopePolicy(ctx, resource) |
| 170 | + if err != nil { |
| 171 | + return diag.FromErr(err) |
| 172 | + } |
| 173 | + |
| 174 | + err = setOpenidClientAuthorizationClientScopePolicyResourceData(ctx, keycloakClient, resource, data) |
| 175 | + if err != nil { |
| 176 | + return diag.FromErr(err) |
| 177 | + } |
| 178 | + |
| 179 | + return nil |
| 180 | +} |
| 181 | + |
| 182 | +func resourceKeycloakOpenidClientAuthorizationClientScopePolicyDelete(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 183 | + keycloakClient := meta.(*keycloak.KeycloakClient) |
| 184 | + |
| 185 | + realmId := data.Get("realm_id").(string) |
| 186 | + resourceServerId := data.Get("resource_server_id").(string) |
| 187 | + id := data.Id() |
| 188 | + |
| 189 | + return diag.FromErr(keycloakClient.DeleteOpenidClientAuthorizationClientScopePolicy(ctx, realmId, resourceServerId, id)) |
| 190 | +} |
0 commit comments