|
| 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/keycloak/terraform-provider-keycloak/keycloak" |
| 9 | +) |
| 10 | + |
| 11 | +func resourceKeycloakHardcodedAttributeMapper() *schema.Resource { |
| 12 | + return &schema.Resource{ |
| 13 | + CreateContext: resourceKeycloakHardcodedAttributeMapperCreate, |
| 14 | + ReadContext: resourceKeycloakHardcodedAttributeMapperRead, |
| 15 | + UpdateContext: resourceKeycloakHardcodedAttributeMapperUpdate, |
| 16 | + DeleteContext: resourceKeycloakHardcodedAttributeMapperDelete, |
| 17 | + // This resource can be imported using {{realm}}/{{provider_id}}/{{mapper_id}}. The Provider and Mapper IDs are displayed in the GUI |
| 18 | + Importer: &schema.ResourceImporter{ |
| 19 | + StateContext: resourceKeycloakLdapGenericMapperImport, |
| 20 | + }, |
| 21 | + Schema: map[string]*schema.Schema{ |
| 22 | + "name": { |
| 23 | + Type: schema.TypeString, |
| 24 | + Required: true, |
| 25 | + Description: "Display name of the mapper when displayed in the console.", |
| 26 | + }, |
| 27 | + "realm_id": { |
| 28 | + Type: schema.TypeString, |
| 29 | + Required: true, |
| 30 | + ForceNew: true, |
| 31 | + Description: "The realm in which the ldap user federation provider exists.", |
| 32 | + }, |
| 33 | + "ldap_user_federation_id": { |
| 34 | + Type: schema.TypeString, |
| 35 | + Required: true, |
| 36 | + ForceNew: true, |
| 37 | + Description: "The ldap user federation provider to attach this mapper to.", |
| 38 | + }, |
| 39 | + "attribute_name": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Required: true, |
| 42 | + ForceNew: true, |
| 43 | + Description: "Name of the user schema attribute", |
| 44 | + }, |
| 45 | + "attribute_value": { |
| 46 | + Type: schema.TypeString, |
| 47 | + Required: true, |
| 48 | + ForceNew: true, |
| 49 | + Description: "Value of the attribute. You can hardcode any value like 'foo'", |
| 50 | + }, |
| 51 | + }, |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func getHardcodedAttributeMapperFromData(data *schema.ResourceData) *keycloak.HardcodedAttributeMapper { |
| 56 | + return &keycloak.HardcodedAttributeMapper{ |
| 57 | + Id: data.Id(), |
| 58 | + Name: data.Get("name").(string), |
| 59 | + RealmId: data.Get("realm_id").(string), |
| 60 | + LdapUserFederationId: data.Get("ldap_user_federation_id").(string), |
| 61 | + AttributeName: data.Get("attribute_name").(string), |
| 62 | + AttributeValue: data.Get("attribute_value").(string), |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func setHardcodedAttributeMapperData(data *schema.ResourceData, ldapMapper *keycloak.HardcodedAttributeMapper) { |
| 67 | + data.SetId(ldapMapper.Id) |
| 68 | + data.Set("name", ldapMapper.Name) |
| 69 | + data.Set("realm_id", ldapMapper.RealmId) |
| 70 | + data.Set("ldap_user_federation_id", ldapMapper.LdapUserFederationId) |
| 71 | + data.Set("attribute_name", ldapMapper.AttributeName) |
| 72 | + data.Set("attribute_value", ldapMapper.AttributeValue) |
| 73 | +} |
| 74 | + |
| 75 | +func resourceKeycloakHardcodedAttributeMapperCreate(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 76 | + keycloakClient := meta.(*keycloak.KeycloakClient) |
| 77 | + |
| 78 | + ldapMapper := getHardcodedAttributeMapperFromData(data) |
| 79 | + |
| 80 | + err := keycloakClient.NewHardcodedAttributeMapper(ctx, ldapMapper) |
| 81 | + if err != nil { |
| 82 | + return diag.FromErr(err) |
| 83 | + } |
| 84 | + |
| 85 | + setHardcodedAttributeMapperData(data, ldapMapper) |
| 86 | + |
| 87 | + return resourceKeycloakHardcodedAttributeMapperRead(ctx, data, meta) |
| 88 | +} |
| 89 | + |
| 90 | +func resourceKeycloakHardcodedAttributeMapperRead(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 91 | + keycloakClient := meta.(*keycloak.KeycloakClient) |
| 92 | + |
| 93 | + realmId := data.Get("realm_id").(string) |
| 94 | + id := data.Id() |
| 95 | + |
| 96 | + ldapMapper, err := keycloakClient.GetHardcodedAttributeMapper(ctx, realmId, id) |
| 97 | + if err != nil { |
| 98 | + return handleNotFoundError(ctx, err, data) |
| 99 | + } |
| 100 | + |
| 101 | + setHardcodedAttributeMapperData(data, ldapMapper) |
| 102 | + |
| 103 | + return diag.FromErr(nil) |
| 104 | +} |
| 105 | + |
| 106 | +func resourceKeycloakHardcodedAttributeMapperUpdate(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 107 | + keycloakClient := meta.(*keycloak.KeycloakClient) |
| 108 | + |
| 109 | + ldapMapper := getHardcodedAttributeMapperFromData(data) |
| 110 | + |
| 111 | + err := keycloakClient.UpdateHardcodedAttributeMapper(ctx, ldapMapper) |
| 112 | + if err != nil { |
| 113 | + return diag.FromErr(err) |
| 114 | + } |
| 115 | + |
| 116 | + setHardcodedAttributeMapperData(data, ldapMapper) |
| 117 | + |
| 118 | + return diag.FromErr(nil) |
| 119 | +} |
| 120 | + |
| 121 | +func resourceKeycloakHardcodedAttributeMapperDelete(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 122 | + keycloakClient := meta.(*keycloak.KeycloakClient) |
| 123 | + |
| 124 | + realmId := data.Get("realm_id").(string) |
| 125 | + id := data.Id() |
| 126 | + |
| 127 | + err := keycloakClient.DeleteHardcodedAttributeMapper(ctx, realmId, id) |
| 128 | + |
| 129 | + return diag.FromErr(err) |
| 130 | +} |
0 commit comments