Skip to content

Commit

Permalink
resource/aws_user_pool_domain: Remove from state when deleted + move …
Browse files Browse the repository at this point in the history
…waiters to their own package (#14732)

Output from acceptance testing:

```
--- PASS: TestAccAWSCognitoUserPoolDomain_disappears (15.35s)
--- PASS: TestAccAWSCognitoUserPoolDomain_basic (17.98s)
```
  • Loading branch information
DrFaust92 authored Aug 19, 2020
1 parent 691d064 commit 65a335c
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 58 deletions.
33 changes: 33 additions & 0 deletions aws/internal/service/cognitoidentityprovider/waiter/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package waiter

import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cognitoidentityprovider"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

const (
userPoolDomainStatusNotFound = "NotFound"
userPoolDomainStatusUnknown = "Unknown"
)

// UserPoolDomainStatus fetches the Operation and its Status
func UserPoolDomainStatus(conn *cognitoidentityprovider.CognitoIdentityProvider, domain string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
input := &cognitoidentityprovider.DescribeUserPoolDomainInput{
Domain: aws.String(domain),
}

output, err := conn.DescribeUserPoolDomain(input)

if err != nil {
return nil, userPoolDomainStatusUnknown, err
}

if output == nil {
return nil, userPoolDomainStatusNotFound, nil
}

return output, aws.StringValue(output.DomainDescription.Status), nil
}
}
56 changes: 56 additions & 0 deletions aws/internal/service/cognitoidentityprovider/waiter/waiter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package waiter

import (
"time"

"github.com/aws/aws-sdk-go/service/cognitoidentityprovider"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

const (
// Maximum amount of time to wait for an Operation to return Success
UserPoolDomainDeleteTimeout = 1 * time.Minute
)

// UserPoolDomainDeleted waits for an Operation to return Success
func UserPoolDomainDeleted(conn *cognitoidentityprovider.CognitoIdentityProvider, domain string) (*cognitoidentityprovider.DescribeUserPoolDomainOutput, error) {
stateConf := &resource.StateChangeConf{
Pending: []string{
cognitoidentityprovider.DomainStatusTypeUpdating,
cognitoidentityprovider.DomainStatusTypeDeleting,
},
Target: []string{""},
Refresh: UserPoolDomainStatus(conn, domain),
Timeout: UserPoolDomainDeleteTimeout,
}

outputRaw, err := stateConf.WaitForState()

if output, ok := outputRaw.(*cognitoidentityprovider.DescribeUserPoolDomainOutput); ok {
return output, err
}

return nil, err
}

func UserPoolDomainCreated(conn *cognitoidentityprovider.CognitoIdentityProvider, domain string, timeout time.Duration) (*cognitoidentityprovider.DescribeUserPoolDomainOutput, error) {
stateConf := &resource.StateChangeConf{
Pending: []string{
cognitoidentityprovider.DomainStatusTypeCreating,
cognitoidentityprovider.DomainStatusTypeUpdating,
},
Target: []string{
cognitoidentityprovider.DomainStatusTypeActive,
},
Refresh: UserPoolDomainStatus(conn, domain),
Timeout: timeout,
}

outputRaw, err := stateConf.WaitForState()

if output, ok := outputRaw.(*cognitoidentityprovider.DescribeUserPoolDomainOutput); ok {
return output, err
}

return nil, err
}
77 changes: 20 additions & 57 deletions aws/resource_aws_cognito_user_pool_domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cognitoidentityprovider"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/terraform-providers/terraform-provider-aws/aws/internal/service/cognitoidentityprovider/waiter"
)

func resourceAwsCognitoUserPoolDomain() *schema.Resource {
Expand Down Expand Up @@ -83,37 +83,13 @@ func resourceAwsCognitoUserPoolDomainCreate(d *schema.ResourceData, meta interfa

_, err := conn.CreateUserPoolDomain(params)
if err != nil {
return fmt.Errorf("Error creating Cognito User Pool Domain: %s", err)
return fmt.Errorf("Error creating Cognito User Pool Domain: %w", err)
}

d.SetId(domain)

stateConf := resource.StateChangeConf{
Pending: []string{
cognitoidentityprovider.DomainStatusTypeCreating,
cognitoidentityprovider.DomainStatusTypeUpdating,
},
Target: []string{
cognitoidentityprovider.DomainStatusTypeActive,
},
MinTimeout: 1 * time.Minute,
Timeout: timeout,
Refresh: func() (interface{}, string, error) {
domain, err := conn.DescribeUserPoolDomain(&cognitoidentityprovider.DescribeUserPoolDomainInput{
Domain: aws.String(d.Get("domain").(string)),
})
if err != nil {
return 42, "", err
}

desc := domain.DomainDescription

return domain, *desc.Status, nil
},
}
_, err = stateConf.WaitForState()
if err != nil {
return err
if _, err := waiter.UserPoolDomainCreated(conn, d.Id(), timeout); err != nil {
return fmt.Errorf("error waiting for User Pool Domain (%s) creation: %w", d.Id(), err)
}

return resourceAwsCognitoUserPoolDomainRead(d, meta)
Expand All @@ -127,7 +103,7 @@ func resourceAwsCognitoUserPoolDomainRead(d *schema.ResourceData, meta interface
Domain: aws.String(d.Id()),
})
if err != nil {
if isAWSErr(err, "ResourceNotFoundException", "") {
if isAWSErr(err, cognitoidentityprovider.ErrCodeResourceNotFoundException, "") {
log.Printf("[WARN] Cognito User Pool Domain %q not found, removing from state", d.Id())
d.SetId("")
return nil
Expand All @@ -137,6 +113,12 @@ func resourceAwsCognitoUserPoolDomainRead(d *schema.ResourceData, meta interface

desc := domain.DomainDescription

if desc.Status == nil {
log.Printf("[WARN] Cognito User Pool Domain %q not found, removing from state", d.Id())
d.SetId("")
return nil
}

d.Set("domain", d.Id())
d.Set("certificate_arn", "")
if desc.CustomDomainConfig != nil {
Expand All @@ -160,35 +142,16 @@ func resourceAwsCognitoUserPoolDomainDelete(d *schema.ResourceData, meta interfa
UserPoolId: aws.String(d.Get("user_pool_id").(string)),
})
if err != nil {
return err
return fmt.Errorf("Error deleting User Pool Domain: %w", err)
}

stateConf := resource.StateChangeConf{
Pending: []string{
cognitoidentityprovider.DomainStatusTypeUpdating,
cognitoidentityprovider.DomainStatusTypeDeleting,
},
Target: []string{""},
Timeout: 1 * time.Minute,
Refresh: func() (interface{}, string, error) {
domain, err := conn.DescribeUserPoolDomain(&cognitoidentityprovider.DescribeUserPoolDomainInput{
Domain: aws.String(d.Id()),
})
if err != nil {
if isAWSErr(err, "ResourceNotFoundException", "") {
return 42, "", nil
}
return 42, "", err
}

desc := domain.DomainDescription
if desc.Status == nil {
return 42, "", nil
}

return domain, *desc.Status, nil
},
if _, err := waiter.UserPoolDomainDeleted(conn, d.Id()); err != nil {
if isAWSErr(err, cognitoidentityprovider.ErrCodeResourceNotFoundException, "") {
return nil
}
return fmt.Errorf("error waiting for User Pool Domain (%s) deletion: %w", d.Id(), err)
}
_, err = stateConf.WaitForState()
return err

return nil

}
24 changes: 23 additions & 1 deletion aws/resource_aws_cognito_user_pool_domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,28 @@ func TestAccAWSCognitoUserPoolDomain_custom(t *testing.T) {
})
}

func TestAccAWSCognitoUserPoolDomain_disappears(t *testing.T) {
domainName := fmt.Sprintf("tf-acc-test-domain-%d", acctest.RandInt())
poolName := fmt.Sprintf("tf-acc-test-pool-%s", acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum))
resourceName := "aws_cognito_user_pool_domain.main"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSCognitoIdentityProvider(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCognitoUserPoolDomainDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCognitoUserPoolDomainConfig_basic(domainName, poolName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckAWSCognitoUserPoolDomainExists(resourceName),
testAccCheckResourceDisappears(testAccProvider, resourceAwsCognitoUserPoolDomain(), resourceName),
),
ExpectNonEmptyPlan: true,
},
},
})
}

func testAccCheckAWSCognitoUserPoolDomainExists(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
Expand Down Expand Up @@ -185,7 +207,7 @@ func testAccCheckAWSCognitoUserPoolDomainDestroy(s *terraform.State) error {
})

if err != nil {
if isAWSErr(err, "ResourceNotFoundException", "") {
if isAWSErr(err, cognitoidentityprovider.ErrCodeResourceNotFoundException, "") {
return nil
}
return err
Expand Down

0 comments on commit 65a335c

Please sign in to comment.