Skip to content

Commit

Permalink
Added management of LambdaConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
Ninir committed Nov 10, 2017
1 parent 91fdd94 commit 7d17d1d
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 0 deletions.
86 changes: 86 additions & 0 deletions aws/resource_aws_cognito_user_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,62 @@ func resourceAwsCognitoUserPool() *schema.Resource {
ValidateFunc: validateCognitoUserPoolEmailVerificationMessage,
},

"lambda_config": {
Type: schema.TypeList,
Optional: true,
MinItems: 0,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"create_auth_challenge": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateArn,
},
"custom_message": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateArn,
},
"define_auth_challenge": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateArn,
},
"post_authentication": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateArn,
},
"post_confirmation": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateArn,
},
"pre_authentication": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateArn,
},
"pre_sign_up": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateArn,
},
"pre_token_generation": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateArn,
},
"verify_auth_challenge_response": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateArn,
},
},
},
},

"mfa_configuration": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -323,6 +379,19 @@ func resourceAwsCognitoUserPoolCreate(d *schema.ResourceData, meta interface{})
params.EmailVerificationMessage = aws.String(v.(string))
}

if v, ok := d.GetOk("lambda_config"); ok {
configs := v.([]interface{})
config, ok := configs[0].(map[string]interface{})

if !ok {
return errors.New("lambda_config is <nil>")
}

if config != nil {
params.LambdaConfig = expandCognitoUserPoolLambdaConfig(config)
}
}

if v, ok := d.GetOk("mfa_configuration"); ok {
params.MfaConfiguration = aws.String(v.(string))
}
Expand Down Expand Up @@ -461,6 +530,10 @@ func resourceAwsCognitoUserPoolRead(d *schema.ResourceData, meta interface{}) er
return errwrap.Wrapf("Failed setting email_configuration: {{err}}", err)
}

if err := d.Set("lambda_config", flattenCognitoUserPoolLambdaConfig(resp.UserPool.LambdaConfig)); err != nil {
return errwrap.Wrapf("Failed setting lambda_config: {{err}}", err)
}

if resp.UserPool.Policies != nil && resp.UserPool.Policies.PasswordPolicy != nil {
if err := d.Set("password_policy", flattenCognitoUserPoolPasswordPolicy(resp.UserPool.Policies.PasswordPolicy)); err != nil {
return errwrap.Wrapf("Failed setting password_policy: {{err}}", err)
Expand Down Expand Up @@ -532,6 +605,19 @@ func resourceAwsCognitoUserPoolUpdate(d *schema.ResourceData, meta interface{})
params.EmailVerificationMessage = aws.String(v)
}

if d.HasChange("lambda_config") {
configs := d.Get("lambda_config").([]interface{})
config, ok := configs[0].(map[string]interface{})

if !ok {
return errors.New("lambda_config is <nil>")
}

if config != nil {
params.LambdaConfig = expandCognitoUserPoolLambdaConfig(config)
}
}

if d.HasChange("mfa_configuration") {
params.MfaConfiguration = aws.String(d.Get("mfa_configuration").(string))
}
Expand Down
88 changes: 88 additions & 0 deletions aws/structure.go
Original file line number Diff line number Diff line change
Expand Up @@ -2161,6 +2161,94 @@ func flattenCognitoUserPoolEmailConfiguration(s *cognitoidentityprovider.EmailCo
return []map[string]interface{}{}
}

func expandCognitoUserPoolLambdaConfig(config map[string]interface{}) *cognitoidentityprovider.LambdaConfigType {
configs := &cognitoidentityprovider.LambdaConfigType{}

if v, ok := config["create_auth_challenge"]; ok {
configs.CreateAuthChallenge = aws.String(v.(string))
}

if v, ok := config["custom_message"]; ok {
configs.CustomMessage = aws.String(v.(string))
}

if v, ok := config["define_auth_challenge"]; ok {
configs.DefineAuthChallenge = aws.String(v.(string))
}

if v, ok := config["post_authentication"]; ok {
configs.PostAuthentication = aws.String(v.(string))
}

if v, ok := config["post_confirmation"]; ok {
configs.PostConfirmation = aws.String(v.(string))
}

if v, ok := config["pre_authentication"]; ok {
configs.PreAuthentication = aws.String(v.(string))
}

if v, ok := config["pre_sign_up"]; ok {
configs.PreSignUp = aws.String(v.(string))
}

if v, ok := config["pre_token_generation"]; ok {
configs.PreTokenGeneration = aws.String(v.(string))
}

if v, ok := config["verify_auth_challenge_response"]; ok {
configs.VerifyAuthChallengeResponse = aws.String(v.(string))
}

return configs
}

func flattenCognitoUserPoolLambdaConfig(s *cognitoidentityprovider.LambdaConfigType) []map[string]interface{} {
m := map[string]interface{}{}

if s == nil {
return nil
}

if s.CreateAuthChallenge != nil {
m["create_auth_challenge"] = *s.CreateAuthChallenge
}

if s.CustomMessage != nil {
m["custom_message"] = *s.CustomMessage
}

if s.DefineAuthChallenge != nil {
m["define_auth_challenge"] = *s.DefineAuthChallenge
}

if s.PostAuthentication != nil {
m["post_authentication"] = *s.PostAuthentication
}

if s.PostConfirmation != nil {
m["post_confirmation"] = *s.PostConfirmation
}

if s.PreAuthentication != nil {
m["pre_authentication"] = *s.PreAuthentication
}

if s.PreSignUp != nil {
m["pre_sign_up"] = *s.PreSignUp
}

if s.PreTokenGeneration != nil {
m["pre_token_generation"] = *s.PreTokenGeneration
}

if s.VerifyAuthChallengeResponse != nil {
m["verify_auth_challenge_response"] = *s.VerifyAuthChallengeResponse
}

return []map[string]interface{}{m}
}

func expandCognitoUserPoolPasswordPolicy(config map[string]interface{}) *cognitoidentityprovider.PasswordPolicyType {
configs := &cognitoidentityprovider.PasswordPolicyType{}

Expand Down

0 comments on commit 7d17d1d

Please sign in to comment.