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

chime service: fix race condition when not using us-east-1 #34334

Merged
merged 22 commits into from
Nov 13, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .changelog/34334.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
```release-note:bug
resource/aws_chime_voice_connector: Fix `read` error when resource is not created in `us-east-1`
```

```release-note:bug
resource/aws_chime_voice_connector_termination: Fix `read` error when resource is not created in `us-east-1`
```

```release-note:bug
resource/aws_chime_voice_connector_group: Fix `read` error when resource is not created in `us-east-1`
```

```release-note:bug
resource/aws_chime_voice_connector_logging: Fix `read` error when resource is not created in `us-east-1`
```

```release-note:bug
resource/aws_chime_voice_connector_origination: Fix `read` error when resource is not created in `us-east-1`
```

```release-note:bug
resource/aws_chime_voice_connector_termination_credentials: Fix `read` error when resource is not created in `us-east-1`
```

```release-note:bug
resource/aws_chimesdkmediapipelines_media_insights_pipeline_configuration: Fix eventual consistency error when resource is not created in `us-east-1`
```
5 changes: 5 additions & 0 deletions internal/service/chime/chime_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ func TestAccChime_serial(t *testing.T) {
"disappears": testAccVoiceConnectorOrigination_disappears,
"update": testAccVoiceConnectorOrigination_update,
},
"VoiceConnectorStreaming": {
"basic": testAccVoiceConnectorStreaming_basic,
"disappears": testAccVoiceConnectorStreaming_disappears,
"update": testAccVoiceConnectorStreaming_update,
},
"VoiceConnectorTermination": {
"basic": testAccVoiceConnectorTermination_basic,
"disappears": testAccVoiceConnectorTermination_disappears,
Expand Down
14 changes: 14 additions & 0 deletions internal/service/chime/exports_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package chime

// Exports for use in tests only.
var (
FindVoiceConnectorByID = findVoiceConnectorByID
FindVoiceConnectorGroupByID = findVoiceConnectorGroupByID
FindVoiceConnectorLoggingByID = findVoiceConnectorLoggingByID
FindVoiceConnectorOriginationByID = findVoiceConnectorOriginationByID
FindVoiceConnectorTerminationByID = findVoiceConnectorTerminationByID
FindVoiceConnectorTerminationCredentialsByID = findVoiceConnectorTerminationCredentialsByID
)
35 changes: 35 additions & 0 deletions internal/service/chime/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 chime

import (
"context"
"time"

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

const (
voiceConnectorResourcePropagationTimeout = 1 * time.Minute
)

func FindVoiceConnectorResourceWithRetry[T any](ctx context.Context, isNewResource bool, f func() (T, error)) (T, error) {
var resp T
err := tfresource.Retry(ctx, voiceConnectorResourcePropagationTimeout, 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
}
2 changes: 1 addition & 1 deletion internal/service/chime/generate.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

//go:generate go run ../../generate/tags/main.go -ListTags -ListTagsInIDElem=ResourceARN -ServiceTagsSlice -TagInIDElem=ResourceARN -CreateTags -UpdateTags -AWSSDKServicePackage=chimesdkvoice
//go:generate go run ../../generate/tags/main.go -ListTags -ListTagsInIDElem=ResourceARN -ServiceTagsSlice -TagInIDElem=ResourceARN -UpdateTags -AWSSDKServicePackage=chimesdkvoice
//go:generate go run ../../generate/servicepackage/main.go
// ONLY generate directives and package declaration! Do not add anything else to this file.

Expand Down
9 changes: 0 additions & 9 deletions internal/service/chime/tags_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 59 additions & 19 deletions internal/service/chime/voice_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ 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/customdiff"
"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 All @@ -42,8 +45,8 @@ func ResourceVoiceConnector() *schema.Resource {
Type: schema.TypeString,
ForceNew: true,
Optional: true,
Default: chimesdkvoice.VoiceConnectorAwsRegionUsEast1,
ValidateFunc: validation.StringInSlice([]string{chimesdkvoice.VoiceConnectorAwsRegionUsEast1, chimesdkvoice.VoiceConnectorAwsRegionUsWest2}, false),
Computed: true,
ValidateFunc: validation.StringInSlice(chimesdkvoice.VoiceConnectorAwsRegion_Values(), false),
},
"name": {
Type: schema.TypeString,
Expand All @@ -62,17 +65,31 @@ func ResourceVoiceConnector() *schema.Resource {
names.AttrTagsAll: tftags.TagsSchemaComputed(),
},

CustomizeDiff: verify.SetTagsDiff,
CustomizeDiff: customdiff.All(
verify.SetTagsDiff,
resourceVoiceConnectorDefaultRegion,
),
}
}

func resourceVoiceConnectorDefaultRegion(ctx context.Context, diff *schema.ResourceDiff, meta interface{}) error {
if v, ok := diff.Get("aws_region").(string); !ok || v == "" {
if err := diff.SetNew("aws_region", meta.(*conns.AWSClient).Region); err != nil {
return err
}
}

return nil
}

func resourceVoiceConnectorCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ChimeSDKVoiceConn(ctx)

createInput := &chimesdkvoice.CreateVoiceConnectorInput{
Name: aws.String(d.Get("name").(string)),
RequireEncryption: aws.Bool(d.Get("require_encryption").(bool)),
Tags: getTagsIn(ctx),
}

if v, ok := d.GetOk("aws_region"); ok {
Expand All @@ -86,38 +103,36 @@ func resourceVoiceConnectorCreate(ctx context.Context, d *schema.ResourceData, m

d.SetId(aws.StringValue(resp.VoiceConnector.VoiceConnectorId))

tagsConn := meta.(*conns.AWSClient).ChimeSDKVoiceConn(ctx)
if err := createTags(ctx, tagsConn, aws.StringValue(resp.VoiceConnector.VoiceConnectorArn), getTagsIn(ctx)); err != nil {
return sdkdiag.AppendErrorf(diags, "setting Chime Voice Connector (%s) tags: %s", d.Id(), err)
}

return append(diags, resourceVoiceConnectorRead(ctx, d, meta)...)
}

func resourceVoiceConnectorRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ChimeSDKVoiceConn(ctx)

getInput := &chimesdkvoice.GetVoiceConnectorInput{
VoiceConnectorId: aws.String(d.Id()),
resp, err := FindVoiceConnectorResourceWithRetry(ctx, d.IsNewResource(), func() (*chimesdkvoice.VoiceConnector, error) {
return findVoiceConnectorByID(ctx, conn, d.Id())
})

if tfresource.TimedOut(err) {
resp, err = findVoiceConnectorByID(ctx, conn, d.Id())
}

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

if err != nil || resp.VoiceConnector == nil {
return sdkdiag.AppendErrorf(diags, "getting Voice connector (%s): %s", d.Id(), err)
if err != nil {
return sdkdiag.AppendErrorf(diags, "reading Voice Connector (%s): %s", d.Id(), err)
}

d.Set("arn", resp.VoiceConnector.VoiceConnectorArn)
d.Set("aws_region", resp.VoiceConnector.AwsRegion)
d.Set("outbound_host_name", resp.VoiceConnector.OutboundHostName)
d.Set("require_encryption", resp.VoiceConnector.RequireEncryption)
d.Set("name", resp.VoiceConnector.Name)
d.Set("arn", resp.VoiceConnectorArn)
d.Set("aws_region", resp.AwsRegion)
d.Set("outbound_host_name", resp.OutboundHostName)
d.Set("require_encryption", resp.RequireEncryption)
d.Set("name", resp.Name)

return diags
}
Expand Down Expand Up @@ -159,3 +174,28 @@ func resourceVoiceConnectorDelete(ctx context.Context, d *schema.ResourceData, m

return diags
}

func findVoiceConnectorByID(ctx context.Context, conn *chimesdkvoice.ChimeSDKVoice, id string) (*chimesdkvoice.VoiceConnector, error) {
in := &chimesdkvoice.GetVoiceConnectorInput{
VoiceConnectorId: aws.String(id),
}

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

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

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

if err != nil {
return nil, err
}

return resp.VoiceConnector, nil
}
52 changes: 43 additions & 9 deletions internal/service/chime/voice_connector_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +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_chime_voice_connector_group")
Expand Down Expand Up @@ -79,27 +82,33 @@ func resourceVoiceConnectorGroupCreate(ctx context.Context, d *schema.ResourceDa
}

func resourceVoiceConnectorGroupRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).ChimeSDKVoiceConn(ctx)

getInput := &chimesdkvoice.GetVoiceConnectorGroupInput{
VoiceConnectorGroupId: aws.String(d.Id()),
resp, err := FindVoiceConnectorResourceWithRetry(ctx, d.IsNewResource(), func() (*chimesdkvoice.VoiceConnectorGroup, error) {
return findVoiceConnectorGroupByID(ctx, conn, d.Id())
})

if tfresource.TimedOut(err) {
resp, err = findVoiceConnectorGroupByID(ctx, conn, d.Id())
}

resp, err := conn.GetVoiceConnectorGroupWithContext(ctx, getInput)
if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, chimesdkvoice.ErrCodeNotFoundException) {
if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] Chime Voice conector group %s not found", d.Id())
d.SetId("")
return nil
}
if err != nil || resp.VoiceConnectorGroup == nil {
return diag.Errorf("getting Chime Voice Connector group (%s): %s", d.Id(), err)

if err != nil {
return sdkdiag.AppendErrorf(diags, "reading Voice Connector Group (%s): %s", d.Id(), err)
}

d.Set("name", resp.VoiceConnectorGroup.Name)
d.Set("name", resp.Name)

if err := d.Set("connector", flattenVoiceConnectorItems(resp.VoiceConnectorGroup.VoiceConnectorItems)); err != nil {
return diag.Errorf("setting Chime Voice Connector group items (%s): %s", d.Id(), err)
if err := d.Set("connector", flattenVoiceConnectorItems(resp.VoiceConnectorItems)); err != nil {
return sdkdiag.AppendErrorf(diags, "setting Chime Voice Connector group items (%s): %s", d.Id(), err)
}

return nil
}

Expand Down Expand Up @@ -176,3 +185,28 @@ func flattenVoiceConnectorItems(connectors []*chimesdkvoice.VoiceConnectorItem)
}
return rawConnectors
}

func findVoiceConnectorGroupByID(ctx context.Context, conn *chimesdkvoice.ChimeSDKVoice, id string) (*chimesdkvoice.VoiceConnectorGroup, error) {
in := &chimesdkvoice.GetVoiceConnectorGroupInput{
VoiceConnectorGroupId: aws.String(id),
}

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

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

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

if err != nil {
return nil, err
}

return resp.VoiceConnectorGroup, nil
}
Loading
Loading