|
| 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 | + |
| 9 | + "github.com/keycloak/terraform-provider-keycloak/keycloak" |
| 10 | +) |
| 11 | + |
| 12 | +func resourceKeycloakOpenIdSubProtocolMapper() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + CreateContext: resourceKeycloakOpenIdSubProtocolMapperCreate, |
| 15 | + ReadContext: resourceKeycloakOpenIdSubProtocolMapperRead, |
| 16 | + UpdateContext: resourceKeycloakOpenIdSubProtocolMapperUpdate, |
| 17 | + DeleteContext: resourceKeycloakOpenIdSubProtocolMapperDelete, |
| 18 | + Importer: &schema.ResourceImporter{ |
| 19 | + // import a mapper tied to a client: |
| 20 | + // {{realmId}}/client/{{clientId}}/{{protocolMapperId}} |
| 21 | + // or a client scope: |
| 22 | + // {{realmId}}/client-scope/{{clientScopeId}}/{{protocolMapperId}} |
| 23 | + StateContext: genericProtocolMapperImport, |
| 24 | + }, |
| 25 | + Schema: map[string]*schema.Schema{ |
| 26 | + "name": { |
| 27 | + Type: schema.TypeString, |
| 28 | + Required: true, |
| 29 | + Description: "A human-friendly name that will appear in the Keycloak console.", |
| 30 | + }, |
| 31 | + "realm_id": { |
| 32 | + Type: schema.TypeString, |
| 33 | + Required: true, |
| 34 | + ForceNew: true, |
| 35 | + Description: "The realm id where the associated client or client scope exists.", |
| 36 | + }, |
| 37 | + "client_id": { |
| 38 | + Type: schema.TypeString, |
| 39 | + Optional: true, |
| 40 | + ForceNew: true, |
| 41 | + Description: "The mapper's associated client. Cannot be used at the same time as client_scope_id.", |
| 42 | + ConflictsWith: []string{"client_scope_id"}, |
| 43 | + }, |
| 44 | + "client_scope_id": { |
| 45 | + Type: schema.TypeString, |
| 46 | + Optional: true, |
| 47 | + ForceNew: true, |
| 48 | + Description: "The mapper's associated client scope. Cannot be used at the same time as client_id.", |
| 49 | + ConflictsWith: []string{"client_id"}, |
| 50 | + }, |
| 51 | + "add_to_access_token": { |
| 52 | + Type: schema.TypeBool, |
| 53 | + Optional: true, |
| 54 | + Default: true, |
| 55 | + Description: "Indicates if the attribute should be a claim in the access token.", |
| 56 | + }, |
| 57 | + "add_to_token_introspection": { |
| 58 | + Type: schema.TypeBool, |
| 59 | + Optional: true, |
| 60 | + Default: true, |
| 61 | + Description: "Indicates if the attribute should be a claim in the token introspection response body.", |
| 62 | + }, |
| 63 | + }, |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func mapFromDataToOpenIdSubProtocolMapper(data *schema.ResourceData) *keycloak.OpenIdSubProtocolMapper { |
| 68 | + return &keycloak.OpenIdSubProtocolMapper{ |
| 69 | + Id: data.Id(), |
| 70 | + Name: data.Get("name").(string), |
| 71 | + RealmId: data.Get("realm_id").(string), |
| 72 | + ClientId: data.Get("client_id").(string), |
| 73 | + ClientScopeId: data.Get("client_scope_id").(string), |
| 74 | + AddToAccessToken: data.Get("add_to_access_token").(bool), |
| 75 | + AddToTokenIntrospection: data.Get("add_to_token_introspection").(bool), |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func mapFromOpenIdSubMapperToData(mapper *keycloak.OpenIdSubProtocolMapper, data *schema.ResourceData) { |
| 80 | + data.SetId(mapper.Id) |
| 81 | + data.Set("name", mapper.Name) |
| 82 | + data.Set("realm_id", mapper.RealmId) |
| 83 | + |
| 84 | + if mapper.ClientId != "" { |
| 85 | + data.Set("client_id", mapper.ClientId) |
| 86 | + } else { |
| 87 | + data.Set("client_scope_id", mapper.ClientScopeId) |
| 88 | + } |
| 89 | + |
| 90 | + data.Set("add_to_access_token", mapper.AddToAccessToken) |
| 91 | + data.Set("add_to_token_introspection", mapper.AddToTokenIntrospection) |
| 92 | +} |
| 93 | + |
| 94 | +func resourceKeycloakOpenIdSubProtocolMapperCreate(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 95 | + keycloakClient := meta.(*keycloak.KeycloakClient) |
| 96 | + |
| 97 | + openIdSubMapper := mapFromDataToOpenIdSubProtocolMapper(data) |
| 98 | + |
| 99 | + err := keycloakClient.ValidateOpenIdSubProtocolMapper(ctx, openIdSubMapper) |
| 100 | + if err != nil { |
| 101 | + return diag.FromErr(err) |
| 102 | + } |
| 103 | + |
| 104 | + err = keycloakClient.NewOpenIdSubProtocolMapper(ctx, openIdSubMapper) |
| 105 | + if err != nil { |
| 106 | + return diag.FromErr(err) |
| 107 | + } |
| 108 | + |
| 109 | + mapFromOpenIdSubMapperToData(openIdSubMapper, data) |
| 110 | + |
| 111 | + return resourceKeycloakOpenIdSubProtocolMapperRead(ctx, data, meta) |
| 112 | +} |
| 113 | + |
| 114 | +func resourceKeycloakOpenIdSubProtocolMapperRead(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 115 | + keycloakClient := meta.(*keycloak.KeycloakClient) |
| 116 | + realmId := data.Get("realm_id").(string) |
| 117 | + clientId := data.Get("client_id").(string) |
| 118 | + clientScopeId := data.Get("client_scope_id").(string) |
| 119 | + |
| 120 | + openIdSubMapper, err := keycloakClient.GetOpenIdSubProtocolMapper(ctx, realmId, clientId, clientScopeId, data.Id()) |
| 121 | + if err != nil { |
| 122 | + return handleNotFoundError(ctx, err, data) |
| 123 | + } |
| 124 | + |
| 125 | + mapFromOpenIdSubMapperToData(openIdSubMapper, data) |
| 126 | + |
| 127 | + return nil |
| 128 | +} |
| 129 | + |
| 130 | +func resourceKeycloakOpenIdSubProtocolMapperUpdate(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 131 | + keycloakClient := meta.(*keycloak.KeycloakClient) |
| 132 | + |
| 133 | + openIdSubMapper := mapFromDataToOpenIdSubProtocolMapper(data) |
| 134 | + |
| 135 | + err := keycloakClient.ValidateOpenIdSubProtocolMapper(ctx, openIdSubMapper) |
| 136 | + if err != nil { |
| 137 | + return diag.FromErr(err) |
| 138 | + } |
| 139 | + |
| 140 | + err = keycloakClient.UpdateOpenIdSubProtocolMapper(ctx, openIdSubMapper) |
| 141 | + if err != nil { |
| 142 | + return diag.FromErr(err) |
| 143 | + } |
| 144 | + |
| 145 | + return resourceKeycloakOpenIdSubProtocolMapperRead(ctx, data, meta) |
| 146 | +} |
| 147 | + |
| 148 | +func resourceKeycloakOpenIdSubProtocolMapperDelete(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 149 | + keycloakClient := meta.(*keycloak.KeycloakClient) |
| 150 | + |
| 151 | + realmId := data.Get("realm_id").(string) |
| 152 | + clientId := data.Get("client_id").(string) |
| 153 | + clientScopeId := data.Get("client_scope_id").(string) |
| 154 | + |
| 155 | + return diag.FromErr(keycloakClient.DeleteOpenIdSubProtocolMapper(ctx, realmId, clientId, clientScopeId, data.Id())) |
| 156 | +} |
0 commit comments