Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

r/chimesdkvoice service: fix eventual consistency errors #34426

Merged
merged 4 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changelog/34426.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
```release-note:bug
resource/aws_chimesdkvoice_sip_rule: Fix eventual consistency errors when not using `us-east-1`
```

```release-note:bug
resource/aws_chimesdkvoice_sip_media_application: Fix eventual consistency errors when not using `us-east-1`
```
10 changes: 10 additions & 0 deletions internal/service/chimesdkvoice/exports_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package chimesdkvoice

// Exports for use in tests only.
var (
FindSIPMediaApplicationByID = findSIPMediaApplicationByID
FindSIPRuleByID = findSIPRuleByID
)
35 changes: 35 additions & 0 deletions internal/service/chimesdkvoice/find..go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package chimesdkvoice

import (
"context"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
)

const (
sipResourcePropagationTimeout = 1 * time.Minute
)

func FindSIPResourceWithRetry[T any](ctx context.Context, isNewResource bool, f func() (T, error)) (T, error) {
var resp T
err := tfresource.Retry(ctx, sipResourcePropagationTimeout, func() *retry.RetryError {
var err error
resp, err = f()
if isNewResource && tfresource.NotFound(err) {
return retry.RetryableError(err)
}

if err != nil {
return retry.NonRetryableError(err)
}

return nil
}, tfresource.WithDelay(5*time.Second))

return resp, err
}
48 changes: 35 additions & 13 deletions internal/service/chimesdkvoice/sip_media_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import (
"github.com/aws/aws-sdk-go/service/chimesdkvoice"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/internal/verify"
"github.com/hashicorp/terraform-provider-aws/names"
)
Expand Down Expand Up @@ -93,25 +95,20 @@ func resourceSipMediaApplicationRead(ctx context.Context, d *schema.ResourceData
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ChimeSDKVoiceConn(ctx)

getInput := &chimesdkvoice.GetSipMediaApplicationInput{
SipMediaApplicationId: aws.String(d.Id()),
}
resp, err := FindSIPResourceWithRetry(ctx, d.IsNewResource(), func() (*chimesdkvoice.SipMediaApplication, error) {
return findSIPMediaApplicationByID(ctx, conn, d.Id())
})

resp, err := conn.GetSipMediaApplicationWithContext(ctx, getInput)
if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, chimesdkvoice.ErrCodeNotFoundException) {
if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] Chime Sip Media Application %s not found", d.Id())
d.SetId("")
return diags
}

if err != nil || resp.SipMediaApplication == nil {
return sdkdiag.AppendErrorf(diags, "getting Sip Media Application (%s): %s", d.Id(), err)
}

d.Set("arn", resp.SipMediaApplication.SipMediaApplicationArn)
d.Set("aws_region", resp.SipMediaApplication.AwsRegion)
d.Set("name", resp.SipMediaApplication.Name)
d.Set("endpoints", flattenSipMediaApplicationEndpoints(resp.SipMediaApplication.Endpoints))
d.Set("arn", resp.SipMediaApplicationArn)
d.Set("aws_region", resp.AwsRegion)
d.Set("name", resp.Name)
d.Set("endpoints", flattenSipMediaApplicationEndpoints(resp.Endpoints))

return diags
}
Expand Down Expand Up @@ -178,3 +175,28 @@ func flattenSipMediaApplicationEndpoints(apiObject []*chimesdkvoice.SipMediaAppl
}
return rawSipMediaApplicationEndpoints
}

func findSIPMediaApplicationByID(ctx context.Context, conn *chimesdkvoice.ChimeSDKVoice, id string) (*chimesdkvoice.SipMediaApplication, error) {
in := &chimesdkvoice.GetSipMediaApplicationInput{
SipMediaApplicationId: aws.String(id),
}

resp, err := conn.GetSipMediaApplicationWithContext(ctx, in)

if tfawserr.ErrCodeEquals(err, chimesdkvoice.ErrCodeNotFoundException) {
return nil, &retry.NotFoundError{
LastError: err,
LastRequest: in,
}
}

if resp == nil || resp.SipMediaApplication == nil {
return nil, tfresource.NewEmptyResultError(in)
}

if err != nil {
return nil, err
}

return resp.SipMediaApplication, nil
}
45 changes: 23 additions & 22 deletions internal/service/chimesdkvoice/sip_media_application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/service/chime"
"github.com/aws/aws-sdk-go/service/chimesdkvoice"
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
Expand All @@ -18,6 +16,7 @@ import (
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
tfchimesdkvoice "github.com/hashicorp/terraform-provider-aws/internal/service/chimesdkvoice"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
)

func TestAccChimeSDKVoiceSipMediaApplication_basic(t *testing.T) {
Expand All @@ -31,7 +30,6 @@ func TestAccChimeSDKVoiceSipMediaApplication_basic(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acctest.PreCheck(ctx, t)
acctest.PreCheckRegion(t, endpoints.UsEast1RegionID)
},
ErrorCheck: acctest.ErrorCheck(t, chimesdkvoice.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Expand All @@ -42,7 +40,7 @@ func TestAccChimeSDKVoiceSipMediaApplication_basic(t *testing.T) {
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckSipMediaApplicationExists(ctx, resourceName, chimeSipMediaApplication),
resource.TestCheckResourceAttrSet(resourceName, "arn"),
resource.TestCheckResourceAttr(resourceName, "aws_region", endpoints.UsEast1RegionID),
resource.TestCheckResourceAttrSet(resourceName, "aws_region"),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttrPair(resourceName, "endpoints.0.lambda_arn", lambdaFunctionResourceName, "arn"),
),
Expand All @@ -66,7 +64,6 @@ func TestAccChimeSDKVoiceSipMediaApplication_disappears(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acctest.PreCheck(ctx, t)
acctest.PreCheckRegion(t, endpoints.UsEast1RegionID)
},
ErrorCheck: acctest.ErrorCheck(t, chime.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Expand Down Expand Up @@ -96,7 +93,6 @@ func TestAccChimeSDKVoiceSipMediaApplication_update(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acctest.PreCheck(ctx, t)
acctest.PreCheckRegion(t, endpoints.UsEast1RegionID)
},
ErrorCheck: acctest.ErrorCheck(t, chimesdkvoice.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Expand All @@ -107,7 +103,7 @@ func TestAccChimeSDKVoiceSipMediaApplication_update(t *testing.T) {
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckSipMediaApplicationExists(ctx, resourceName, chimeSipMediaApplication),
resource.TestCheckResourceAttrSet(resourceName, "arn"),
resource.TestCheckResourceAttr(resourceName, "aws_region", endpoints.UsEast1RegionID),
resource.TestCheckResourceAttrSet(resourceName, "aws_region"),
resource.TestCheckResourceAttr(resourceName, "name", rName),
resource.TestCheckResourceAttrPair(resourceName, "endpoints.0.lambda_arn", lambdaFunctionResourceName, "arn"),
),
Expand All @@ -117,7 +113,7 @@ func TestAccChimeSDKVoiceSipMediaApplication_update(t *testing.T) {
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckSipMediaApplicationExists(ctx, resourceName, chimeSipMediaApplication),
resource.TestCheckResourceAttrSet(resourceName, "arn"),
resource.TestCheckResourceAttr(resourceName, "aws_region", endpoints.UsEast1RegionID),
resource.TestCheckResourceAttrSet(resourceName, "aws_region"),
resource.TestCheckResourceAttr(resourceName, "name", rNameUpdated),
resource.TestCheckResourceAttrPair(resourceName, "endpoints.0.lambda_arn", lambdaFunctionResourceName, "arn"),
),
Expand All @@ -141,7 +137,6 @@ func TestAccChimeSDKVoiceSipMediaApplication_tags(t *testing.T) {
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
acctest.PreCheck(ctx, t)
acctest.PreCheckRegion(t, endpoints.UsEast1RegionID)
},
ErrorCheck: acctest.ErrorCheck(t, chimesdkvoice.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
Expand Down Expand Up @@ -196,15 +191,16 @@ func testAccCheckSipMediaApplicationExists(ctx context.Context, name string, vc
}

conn := acctest.Provider.Meta().(*conns.AWSClient).ChimeSDKVoiceConn(ctx)
input := &chimesdkvoice.GetSipMediaApplicationInput{
SipMediaApplicationId: aws.String(rs.Primary.ID),
}
resp, err := conn.GetSipMediaApplicationWithContext(ctx, input)

resp, err := tfchimesdkvoice.FindSIPResourceWithRetry(ctx, false, func() (*chimesdkvoice.SipMediaApplication, error) {
return tfchimesdkvoice.FindSIPMediaApplicationByID(ctx, conn, rs.Primary.ID)
})

if err != nil {
return err
}

vc = resp.SipMediaApplication
vc = resp

return nil
}
Expand All @@ -217,17 +213,22 @@ func testAccCheckSipMediaApplicationDestroy(ctx context.Context) resource.TestCh
continue
}
conn := acctest.Provider.Meta().(*conns.AWSClient).ChimeSDKVoiceConn(ctx)
input := &chimesdkvoice.GetSipMediaApplicationInput{
SipMediaApplicationId: aws.String(rs.Primary.ID),

_, err := tfchimesdkvoice.FindSIPResourceWithRetry(ctx, false, func() (*chimesdkvoice.SipMediaApplication, error) {
return tfchimesdkvoice.FindSIPMediaApplicationByID(ctx, conn, rs.Primary.ID)
})

if tfresource.NotFound(err) {
continue
}
resp, err := conn.GetSipMediaApplicationWithContext(ctx, input)
if err == nil {
if resp.SipMediaApplication != nil && aws.StringValue(resp.SipMediaApplication.Name) != "" {
return fmt.Errorf("error ChimeSdkVoice Sip Media Application still exists")
}

if err != nil {
return err
}
return nil

return fmt.Errorf("sip media application still exists: (%s)", rs.Primary.ID)
}

return nil
}
}
Expand Down
50 changes: 36 additions & 14 deletions internal/service/chimesdkvoice/sip_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import (
"github.com/aws/aws-sdk-go/service/chimesdkvoice"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
)

// @SDKResource("aws_chimesdkvoice_sip_rule", name="Sip Rule")
Expand Down Expand Up @@ -107,26 +109,21 @@ func resourceSipRuleRead(ctx context.Context, d *schema.ResourceData, meta inter
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ChimeSDKVoiceConn(ctx)

getInput := &chimesdkvoice.GetSipRuleInput{
SipRuleId: aws.String(d.Id()),
}
resp, err := FindSIPResourceWithRetry(ctx, d.IsNewResource(), func() (*chimesdkvoice.SipRule, error) {
return findSIPRuleByID(ctx, conn, d.Id())
})

resp, err := conn.GetSipRuleWithContext(ctx, getInput)
if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, chimesdkvoice.ErrCodeNotFoundException) {
if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] ChimeSDKVoice Sip Rule %s not found", d.Id())
d.SetId("")
return diags
}

if err != nil || resp.SipRule == nil {
return sdkdiag.AppendErrorf(diags, "getting Sip Rule (%s): %s", d.Id(), err)
}

d.Set("name", resp.SipRule.Name)
d.Set("disabled", resp.SipRule.Disabled)
d.Set("trigger_type", resp.SipRule.TriggerType)
d.Set("trigger_value", resp.SipRule.TriggerValue)
d.Set("target_applications", flattenSipRuleTargetApplications(resp.SipRule.TargetApplications))
d.Set("name", resp.Name)
d.Set("disabled", resp.Disabled)
d.Set("trigger_type", resp.TriggerType)
d.Set("trigger_value", resp.TriggerValue)
d.Set("target_applications", flattenSipRuleTargetApplications(resp.TargetApplications))
return diags
}

Expand Down Expand Up @@ -204,3 +201,28 @@ func flattenSipRuleTargetApplications(apiObject []*chimesdkvoice.SipRuleTargetAp
}
return rawSipRuleTargetApplications
}

func findSIPRuleByID(ctx context.Context, conn *chimesdkvoice.ChimeSDKVoice, id string) (*chimesdkvoice.SipRule, error) {
in := &chimesdkvoice.GetSipRuleInput{
SipRuleId: aws.String(id),
}

resp, err := conn.GetSipRuleWithContext(ctx, in)

if tfawserr.ErrCodeEquals(err, chimesdkvoice.ErrCodeNotFoundException) {
return nil, &retry.NotFoundError{
LastError: err,
LastRequest: in,
}
}

if resp == nil || resp.SipRule == nil {
return nil, tfresource.NewEmptyResultError(in)
}

if err != nil {
return nil, err
}

return resp.SipRule, nil
}