Skip to content

Commit

Permalink
Saved work
Browse files Browse the repository at this point in the history
  • Loading branch information
Ninir committed Oct 23, 2017
1 parent ae03884 commit 7e7867a
Show file tree
Hide file tree
Showing 5 changed files with 278 additions and 39 deletions.
165 changes: 152 additions & 13 deletions aws/resource_aws_cognito_user_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/aws/aws-sdk-go/service/cognitoidentityprovider"
"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/schema"
"github.com/pkg/errors"
)

func resourceAwsCognitoUserPool() *schema.Resource {
Expand All @@ -18,12 +19,6 @@ func resourceAwsCognitoUserPool() *schema.Resource {
Delete: resourceAwsCognitoUserPoolDelete,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"alias_attributes": {
Type: schema.TypeList,
Optional: true,
Expand All @@ -43,6 +38,27 @@ func resourceAwsCognitoUserPool() *schema.Resource {
},
},

"email_configuration": {
Type: schema.TypeList,
Optional: true,
MinItems: 0,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"reply_to_email_address": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateCognitoUserPoolReplyEmailAddress,
},
"source_arn": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateArn,
},
},
},
},

"email_verification_subject": {
Type: schema.TypeString,
Optional: true,
Expand All @@ -55,23 +71,50 @@ func resourceAwsCognitoUserPool() *schema.Resource {
ValidateFunc: validateCognitoUserPoolEmailVerificationMessage,
},

"sms_authentication_message": {
"mfa_configuration": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateCognitoUserPoolSmsAuthenticationMessage,
Default: cognitoidentityprovider.UserPoolMfaTypeOff,
ValidateFunc: validateCognitoUserPoolMfaConfiguration,
},

"sms_verification_message": {
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"sms_authentication_message": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateCognitoUserPoolSmsVerificationMessage,
ValidateFunc: validateCognitoUserPoolSmsAuthenticationMessage,
},

"mfa_configuration": {
"sms_configuration": {
Type: schema.TypeList,
Optional: true,
MinItems: 0,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"external_id": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateArn,
},
"sns_caller_arn": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateArn,
},
},
},
},

"sms_verification_message": {
Type: schema.TypeString,
Optional: true,
Default: cognitoidentityprovider.UserPoolMfaTypeOff,
ValidateFunc: validateCognitoUserPoolMfaConfiguration,
ValidateFunc: validateCognitoUserPoolSmsVerificationMessage,
},

"tags": tagsSchema(),
Expand All @@ -94,6 +137,29 @@ func resourceAwsCognitoUserPoolCreate(d *schema.ResourceData, meta interface{})
params.AutoVerifiedAttributes = expandStringList(v.([]interface{}))
}

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

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

if config != nil {
emailConfigurationType := &cognitoidentityprovider.EmailConfigurationType{}

if v, ok := config["reply_to_email_address"]; ok && v.(string) != "" {
emailConfigurationType.ReplyToEmailAddress = aws.String(v.(string))
}

if v, ok := config["source_arn"]; ok && v.(string) != "" {
emailConfigurationType.SourceArn = aws.String(v.(string))
}

params.EmailConfiguration = emailConfigurationType
}
}

if v, ok := d.GetOk("email_verification_subject"); ok {
params.EmailVerificationSubject = aws.String(v.(string))
}
Expand All @@ -110,6 +176,27 @@ func resourceAwsCognitoUserPoolCreate(d *schema.ResourceData, meta interface{})
params.SmsAuthenticationMessage = aws.String(v.(string))
}

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

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

if config != nil {
smsConfigurationType := &cognitoidentityprovider.SmsConfigurationType{
SnsCallerArn: aws.String(config["sns_caller_arn"].(string)),
}

if v, ok := config["external_id"]; ok && v.(string) != "" {
smsConfigurationType.ExternalId = aws.String(v.(string))
}

params.SmsConfiguration = smsConfigurationType
}
}

if v, ok := d.GetOk("sms_verification_message"); ok {
params.SmsVerificationMessage = aws.String(v.(string))
}
Expand Down Expand Up @@ -172,6 +259,14 @@ func resourceAwsCognitoUserPoolRead(d *schema.ResourceData, meta interface{}) er
d.Set("sms_authentication_message", *resp.UserPool.SmsAuthenticationMessage)
}

if err := d.Set("email_configuration", flattenCognitoUserPoolEmailConfiguration(resp.UserPool.EmailConfiguration)); err != nil {
return errwrap.Wrapf("Failed setting email_configuration: {{err}}", err)
}

if err := d.Set("sms_configuration", flattenCognitoUserPoolSmsConfiguration(resp.UserPool.SmsConfiguration)); err != nil {
return errwrap.Wrapf("Failed setting sms_configuration: {{err}}", err)
}

d.Set("tags", tagsToMapGeneric(resp.UserPool.UserPoolTags))

return nil
Expand All @@ -190,6 +285,29 @@ func resourceAwsCognitoUserPoolUpdate(d *schema.ResourceData, meta interface{})
params.AutoVerifiedAttributes = expandStringList(d.Get("auto_verified_attributes").([]interface{}))
}

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

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

if config != nil {
emailConfigurationType := &cognitoidentityprovider.EmailConfigurationType{}

if v, ok := config["reply_to_email_address"]; ok && v.(string) != "" {
emailConfigurationType.ReplyToEmailAddress = aws.String(v.(string))
}

if v, ok := config["source_arn"]; ok && v.(string) != "" {
emailConfigurationType.SourceArn = aws.String(v.(string))
}

params.EmailConfiguration = emailConfigurationType
}
}

if d.HasChange("email_verification_subject") {
params.EmailVerificationSubject = aws.String(d.Get("email_verification_subject").(string))
}
Expand All @@ -206,6 +324,27 @@ func resourceAwsCognitoUserPoolUpdate(d *schema.ResourceData, meta interface{})
params.SmsAuthenticationMessage = aws.String(d.Get("sms_authentication_message").(string))
}

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

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

if config != nil {
smsConfigurationType := &cognitoidentityprovider.SmsConfigurationType{
SnsCallerArn: aws.String(config["sns_caller_arn"].(string)),
}

if v, ok := config["external_id"]; ok && v.(string) != "" {
smsConfigurationType.ExternalId = aws.String(v.(string))
}

params.SmsConfiguration = smsConfigurationType
}
}

if d.HasChange("sms_verification_message") {
params.SmsVerificationMessage = aws.String(d.Get("sms_verification_message").(string))
}
Expand Down
81 changes: 55 additions & 26 deletions aws/resource_aws_cognito_user_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,31 @@ func TestAccAWSCognitoUserPool_withSmsVerificationMessage(t *testing.T) {
})
}

func TestAccAWSCognitoUserPool_withEmailConfiguration(t *testing.T) {
name := acctest.RandString(5)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCognitoUserPoolDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCognitoUserPoolConfig_basic(name),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSCognitoUserPoolExists("aws_cognito_user_pool.pool"),
),
},
{
Config: testAccAWSCognitoUserPoolConfig_withEmailConfiguration(name),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("aws_cognito_user_pool.pool", "email_configuration.#", "1"),
resource.TestCheckResourceAttr("aws_cognito_user_pool.pool", "email_configuration.1.reply_to_email_address", "foo.bar@baz"),
),
},
},
})
}

func TestAccAWSCognitoUserPool_withTags(t *testing.T) {
name := acctest.RandString(5)

Expand Down Expand Up @@ -122,37 +147,30 @@ func TestAccAWSCognitoUserPool_withTags(t *testing.T) {

//func TestAccAWSCognitoUserPool_attributes(t *testing.T) {
// name := acctest.RandString(5)
// subject := acctest.RandString(10)
// message := fmt.Sprintf("%s {####}", acctest.RandString(10))
// authenticationMessage := fmt.Sprintf("%s {####}", acctest.RandString(10))
// verificationMessage := fmt.Sprintf("%s {####}", acctest.RandString(10))
//
// resource.Test(t, resource.TestCase{
// PreCheck: func() { testAccPreCheck(t) },
// Providers: testAccProviders,
// CheckDestroy: testAccCheckAWSCognitoUserPoolDestroy,
// Steps: []resource.TestStep{
// {
// Config: testAccAWSCognitoUserPoolConfig_attributes(name, authenticationMessage, verificationMessage, subject, message),
// Config: testAccAWSCognitoUserPoolConfig_attributes(name),
// Check: resource.ComposeAggregateTestCheckFunc(
// testAccCheckAWSCognitoUserPoolExists("aws_cognito_user_pool.pool"),
// resource.TestCheckResourceAttr("aws_cognito_user_pool.pool", "alias_attributes.#", "3"),
// resource.TestCheckResourceAttr("aws_cognito_user_pool.pool", "alias_attributes.0", "email"),
// resource.TestCheckResourceAttr("aws_cognito_user_pool.pool", "alias_attributes.1", "phone_number"),
// resource.TestCheckResourceAttr("aws_cognito_user_pool.pool", "alias_attributes.2", "preferred_username"),
// resource.TestCheckResourceAttr("aws_cognito_user_pool.pool", "auto_verified_attributes.#", "2"),
// resource.TestCheckResourceAttr("aws_cognito_user_pool.pool", "auto_verified_attributes.0", "email"),
// resource.TestCheckResourceAttr("aws_cognito_user_pool.pool", "auto_verified_attributes.1", "phone_number"),
// resource.TestCheckResourceAttr("aws_cognito_user_pool.pool", "alias_attributes.#", "1"),
// resource.TestCheckResourceAttr("aws_cognito_user_pool.pool", "alias_attributes.0", "preferred_username"),
// resource.TestCheckNoResourceAttr("aws_cognito_user_pool.pool", "auto_verified_attributes.#"),
// ),
// },
// {
// Config: testAccAWSCognitoUserPoolConfig_attributesUpdated(name, subject, message),
// Config: testAccAWSCognitoUserPoolConfig_attributesUpdated(name),
// Check: resource.ComposeAggregateTestCheckFunc(
// resource.TestCheckResourceAttr("aws_cognito_user_pool.pool", "alias_attributes.#", "2"),
// resource.TestCheckResourceAttr("aws_cognito_user_pool.pool", "alias_attributes.0", "email"),
// resource.TestCheckResourceAttr("aws_cognito_user_pool.pool", "alias_attributes.1", "preferred_username"),
// resource.TestCheckResourceAttr("aws_cognito_user_pool.pool", "auto_verified_attributes.#", "1"),
// resource.TestCheckResourceAttr("aws_cognito_user_pool.pool", "auto_verified_attributes.#", "2"),
// resource.TestCheckResourceAttr("aws_cognito_user_pool.pool", "auto_verified_attributes.0", "email"),
// resource.TestCheckResourceAttr("aws_cognito_user_pool.pool", "auto_verified_attributes.1", "phone_number"),
// ),
// },
// },
Expand Down Expand Up @@ -214,7 +232,7 @@ func testAccCheckAWSCognitoUserPoolExists(name string) resource.TestCheckFunc {
func testAccAWSCognitoUserPoolConfig_basic(name string) string {
return fmt.Sprintf(`
resource "aws_cognito_user_pool" "pool" {
name = "%s"
name = "terraform-test-pool-%s"
}`, name)
}

Expand Down Expand Up @@ -249,6 +267,17 @@ resource "aws_cognito_user_pool" "pool" {
}`, name)
}

func testAccAWSCognitoUserPoolConfig_withEmailConfiguration(name string) string {
return fmt.Sprintf(`
resource "aws_cognito_user_pool" "pool" {
name = "terraform-test-pool-%s"
email_configuration {
reply_to_email_address = "foo.bar@baz"
}
}`, name)
}

func testAccAWSCognitoUserPoolConfig_withTagsUpdated(name string) string {
return fmt.Sprintf(`
resource "aws_cognito_user_pool" "pool" {
Expand All @@ -261,21 +290,21 @@ resource "aws_cognito_user_pool" "pool" {
}`, name)
}

//func testAccAWSCognitoUserPoolConfig_attributes(name, authenticationMessage, verificationMessage, subject, message string) string {
//func testAccAWSCognitoUserPoolConfig_attributes(name string) string {
// return fmt.Sprintf(`
// resource "aws_cognito_user_pool" "pool" {
// name = "terraform-test-pool-%s"
//resource "aws_cognito_user_pool" "pool" {
// name = "terraform-test-pool-%s"
//
// alias_attributes = ["preferred_username"]
// }`, name, subject, message, authenticationMessage, verificationMessage)
// alias_attributes = ["preferred_username"]
//}`, name)
//}
//
//func testAccAWSCognitoUserPoolConfig_attributesUpdated(name, subject, message string) string {
//func testAccAWSCognitoUserPoolConfig_attributesUpdated(name string) string {
// return fmt.Sprintf(`
// resource "aws_cognito_user_pool" "pool" {
// name = "terraform-test-pool-%s"
//resource "aws_cognito_user_pool" "pool" {
// name = "terraform-test-pool-%s"
//
// alias_attributes = ["email", "preferred_username"]
// auto_verified_attributes = ["email", "phone_number"]
// }`, name, subject, message)
// alias_attributes = ["email", "preferred_username"]
// auto_verified_attributes = ["email", "phone_number"]
//}`, name)
//}
Loading

0 comments on commit 7e7867a

Please sign in to comment.