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

Tech Debt: convert mediastore to AWS Go SDKv2 #35665

Merged
merged 8 commits into from
Feb 7, 2024
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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/medialive v1.44.1
github.com/aws/aws-sdk-go-v2/service/mediapackage v1.28.7
github.com/aws/aws-sdk-go-v2/service/mediapackagev2 v1.7.7
github.com/aws/aws-sdk-go-v2/service/mediastore v1.18.6
github.com/aws/aws-sdk-go-v2/service/mq v1.20.7
github.com/aws/aws-sdk-go-v2/service/oam v1.7.7
github.com/aws/aws-sdk-go-v2/service/opensearchserverless v1.9.6
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ github.com/aws/aws-sdk-go-v2/service/mediapackage v1.28.7 h1:vdaaK74RcQKM7Db8GAE
github.com/aws/aws-sdk-go-v2/service/mediapackage v1.28.7/go.mod h1:02B7Q0lKo1j+EXy5xJdiQ1WXHSstaZzeP1pWaSngHEM=
github.com/aws/aws-sdk-go-v2/service/mediapackagev2 v1.7.7 h1:7K4U+KzcPCZFsnJdUtDYDeufXpCR94XWvGe7NDToUt4=
github.com/aws/aws-sdk-go-v2/service/mediapackagev2 v1.7.7/go.mod h1:pKEbhmBUQhflog7rfoQPSi6b/8+skFWq6CQQ5UOgsDQ=
github.com/aws/aws-sdk-go-v2/service/mediastore v1.18.6 h1:8iy67SQ3gJDytDEOmkMXxCqXMwb4QVBuXjieDxS/iNY=
github.com/aws/aws-sdk-go-v2/service/mediastore v1.18.6/go.mod h1:mjWnSGMBWSh9DOju44R6t8U3i9XlDxl9PlSnOPb2WTk=
github.com/aws/aws-sdk-go-v2/service/mq v1.20.7 h1:d4hynfHB+JsY5zcek4ITxVnVEkkxa7ZZDJW6OOPwEeQ=
github.com/aws/aws-sdk-go-v2/service/mq v1.20.7/go.mod h1:PHzqJZbmPmkFXCZiaOdEY9KoGFYWD2lVO2Rv8Om1hSg=
github.com/aws/aws-sdk-go-v2/service/oam v1.7.7 h1:b9/KbZcdS1XmP6vjKj+62bVoRbY+9fcf3iDDBxS4yKU=
Expand Down
6 changes: 3 additions & 3 deletions internal/conns/awsclient_gen.go

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

148 changes: 96 additions & 52 deletions internal/service/mediastore/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ package mediastore

import (
"context"
"fmt"
"log"
"time"

"github.com/YakDriver/regexache"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/mediastore"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/mediastore"
awstypes "github.com/aws/aws-sdk-go-v2/service/mediastore/types"
"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/enum"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"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"
Expand Down Expand Up @@ -61,30 +62,22 @@ func ResourceContainer() *schema.Resource {

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

input := &mediastore.CreateContainerInput{
ContainerName: aws.String(d.Get("name").(string)),
Tags: getTagsIn(ctx),
}

resp, err := conn.CreateContainerWithContext(ctx, input)
resp, err := conn.CreateContainer(ctx, input)
if err != nil {
return sdkdiag.AppendErrorf(diags, "creating MediaStore Container: %s", err)
}

d.SetId(aws.StringValue(resp.Container.Name))
d.SetId(aws.ToString(resp.Container.Name))

stateConf := &retry.StateChangeConf{
Pending: []string{mediastore.ContainerStatusCreating},
Target: []string{mediastore.ContainerStatusActive},
Refresh: containerRefreshStatusFunc(ctx, conn, d.Id()),
Timeout: 10 * time.Minute,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}
_, err = waitContainerActive(ctx, conn, d.Id())

_, err = stateConf.WaitForStateContext(ctx)
if err != nil {
return sdkdiag.AppendErrorf(diags, "creating MediaStore Container (%s): waiting for completion: %s", d.Id(), err)
}
Expand All @@ -94,25 +87,24 @@ func resourceContainerCreate(ctx context.Context, d *schema.ResourceData, meta i

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

input := &mediastore.DescribeContainerInput{
ContainerName: aws.String(d.Id()),
}
resp, err := conn.DescribeContainerWithContext(ctx, input)
if tfawserr.ErrCodeEquals(err, mediastore.ErrCodeContainerNotFoundException) {
resp, err := findContainerByName(ctx, conn, d.Id())

if tfresource.NotFound(err) {
log.Printf("[WARN] No Container found: %s, removing from state", d.Id())
d.SetId("")
return diags
}

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

arn := aws.StringValue(resp.Container.ARN)
arn := aws.ToString(resp.ARN)
d.Set("arn", arn)
d.Set("name", resp.Container.Name)
d.Set("endpoint", resp.Container.Endpoint)
d.Set("name", resp.Name)
d.Set("endpoint", resp.Endpoint)

return diags
}
Expand All @@ -127,51 +119,103 @@ func resourceContainerUpdate(ctx context.Context, d *schema.ResourceData, meta i

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

input := &mediastore.DeleteContainerInput{
ContainerName: aws.String(d.Id()),
}
_, err := conn.DeleteContainerWithContext(ctx, input)
_, err := conn.DeleteContainer(ctx, input)

if errs.IsA[*awstypes.ContainerNotFoundException](err) {
return diags
}

if err != nil {
if tfawserr.ErrCodeEquals(err, mediastore.ErrCodeContainerNotFoundException) {
return diags
}
return sdkdiag.AppendErrorf(diags, "deleting MediaStore Container (%s): %s", d.Id(), err)
}

dcinput := &mediastore.DescribeContainerInput{
ContainerName: aws.String(d.Id()),
}
err = retry.RetryContext(ctx, 5*time.Minute, func() *retry.RetryError {
_, err := conn.DescribeContainerWithContext(ctx, dcinput)
if err != nil {
if tfawserr.ErrCodeEquals(err, mediastore.ErrCodeContainerNotFoundException) {
return nil
}
return retry.NonRetryableError(err)
}
return retry.RetryableError(fmt.Errorf("Media Store Container (%s) still exists", d.Id()))
})
if tfresource.TimedOut(err) {
_, err = conn.DescribeContainerWithContext(ctx, dcinput)
}
_, err = waitContainerDeleted(ctx, conn, d.Id())

if err != nil {
return sdkdiag.AppendErrorf(diags, "deleting MediaStore Container (%s): waiting for completion: %s", d.Id(), err)
}

return diags
}

func containerRefreshStatusFunc(ctx context.Context, conn *mediastore.MediaStore, cn string) retry.StateRefreshFunc {
func containerRefreshStatusFunc(ctx context.Context, conn *mediastore.Client, cn string) retry.StateRefreshFunc {
return func() (interface{}, string, error) {
input := &mediastore.DescribeContainerInput{
ContainerName: aws.String(cn),
resp, err := findContainerByName(ctx, conn, cn)

if tfresource.NotFound(err) {
return nil, "", nil
}
resp, err := conn.DescribeContainerWithContext(ctx, input)

if err != nil {
return nil, "failed", err
return nil, "", err
}

return resp, string(resp.Status), nil
}
}

func findContainerByName(ctx context.Context, conn *mediastore.Client, id string) (*awstypes.Container, error) {
input := &mediastore.DescribeContainerInput{
ContainerName: aws.String(id),
}

output, err := conn.DescribeContainer(ctx, input)

if errs.IsA[*awstypes.ContainerNotFoundException](err) {
return nil, &retry.NotFoundError{
LastError: err,
LastRequest: input,
}
return resp, *resp.Container.Status, nil
}

if err != nil {
return nil, err
}

if output == nil || output.Container == nil {
return nil, tfresource.NewEmptyResultError(input)
}

return output.Container, nil
}

func waitContainerActive(ctx context.Context, conn *mediastore.Client, id string) (*awstypes.Container, error) {
stateConf := &retry.StateChangeConf{
Pending: enum.Slice(awstypes.ContainerStatusCreating),
Target: enum.Slice(awstypes.ContainerStatusActive),
Refresh: containerRefreshStatusFunc(ctx, conn, id),
Timeout: 10 * time.Minute,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}

outputRaw, err := stateConf.WaitForStateContext(ctx)
if v, ok := outputRaw.(*awstypes.Container); ok {
return v, err
}

return nil, err
}

func waitContainerDeleted(ctx context.Context, conn *mediastore.Client, id string) (*awstypes.Container, error) {
stateConf := &retry.StateChangeConf{
Pending: enum.Slice(awstypes.ContainerStatusDeleting),
Target: []string{},
Refresh: containerRefreshStatusFunc(ctx, conn, id),
Timeout: 10 * time.Minute,
Delay: 10 * time.Second,
MinTimeout: 3 * time.Second,
}

outputRaw, err := stateConf.WaitForStateContext(ctx)
if v, ok := outputRaw.(*awstypes.Container); ok {
return v, err
}

return nil, err
}
Loading
Loading