Skip to content

Commit

Permalink
Add client_metadata, force_alias_creation and message_action
Browse files Browse the repository at this point in the history
  • Loading branch information
rtim75 committed Oct 12, 2021
1 parent cbd1393 commit d9a7743
Showing 1 changed file with 65 additions and 21 deletions.
86 changes: 65 additions & 21 deletions aws/resource_aws_cognito_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,39 @@ func resourceAwsCognitoUser() *schema.Resource {

// https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminCreateUser.html
Schema: map[string]*schema.Schema{
"username": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
"client_metadata": {
Type: schema.TypeMap,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
},
"user_pool_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
"desired_delivery_mediums": {
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice([]string{
cognitoidentityprovider.DeliveryMediumTypeSms,
cognitoidentityprovider.DeliveryMediumTypeEmail,
}, false),
},
Optional: true,
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"force_alias_creation": {
Type: schema.TypeBool,
Optional: true,
},
"message_action": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
cognitoidentityprovider.MessageActionTypeResend,
cognitoidentityprovider.MessageActionTypeSuppress,
}, false),
},
"user_attribute": {
Type: schema.TypeSet,
Elem: &schema.Resource{
Expand All @@ -50,23 +68,22 @@ func resourceAwsCognitoUser() *schema.Resource {
},
"value": {
Type: schema.TypeString,
Optional: true,
Required: true,
Sensitive: true,
},
},
},
Optional: true,
},
"desired_delivery_mediums": {
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice([]string{
cognitoidentityprovider.DeliveryMediumTypeSms,
cognitoidentityprovider.DeliveryMediumTypeEmail,
}, false),
},
Optional: true,
"user_pool_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"username": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
Expand All @@ -87,13 +104,30 @@ func resourceAwsCognitoUserCreate(d *schema.ResourceData, meta interface{}) erro

if v, ok := d.GetOk("desired_delivery_mediums"); ok {
mediums := v.(*schema.Set)
params.DesiredDeliveryMediums = expandDesiredDeliveryMediums(mediums)
params.DesiredDeliveryMediums = expandCognitoUserDesiredDeliveryMediums(mediums)
}

if v, ok := d.GetOk("force_alias_creation"); ok {
params.ForceAliasCreation = aws.Bool(v.(bool))
}

if v, ok := d.GetOk("message_action"); ok {
params.MessageAction = aws.String(v.(string))
}

if v, ok := d.GetOk("client_metadata"); ok {
metadata := v.(map[string]interface{})
params.ClientMetadata = expandCognitoUserClientMetadata(metadata)
}

log.Print("[DEBUG] Creating Cognito User")

resp, err := conn.AdminCreateUser(params)
if err != nil {
if isAWSErr(err, "AliasExistsException", "") {
log.Println("[ERROR] User alias already exists. To override the alias set `force_alias_creation` attribute to `true`.")
return nil
}
return fmt.Errorf("Error creating Cognito User: %s", err)
}

Expand Down Expand Up @@ -293,7 +327,7 @@ func flattenCognitoUserAttributes(apiList []*cognitoidentityprovider.AttributeTy
return tfSet
}

func expandDesiredDeliveryMediums(tfSet *schema.Set) []*string {
func expandCognitoUserDesiredDeliveryMediums(tfSet *schema.Set) []*string {
apiList := []*string{}

for _, elem := range tfSet.List() {
Expand Down Expand Up @@ -345,6 +379,16 @@ func computeCognitoUserAttributesUpdate(old interface{}, new interface{}) (*sche
return upd, del
}

// For ClientMetadata we only need expand since AWS doesn't store its value
func expandCognitoUserClientMetadata(tfMap map[string]interface{}) map[string]*string {
apiMap := map[string]*string{}
for k, v := range tfMap {
apiMap[k] = aws.String(v.(string))
}

return apiMap
}

func cognitoUserAttributeHash(attr interface{}) int {
attrMap := attr.(map[string]interface{})

Expand Down

0 comments on commit d9a7743

Please sign in to comment.