Skip to content

Commit

Permalink
Merge pull request hashicorp#37867 from hashicorp/td-docdb_v2_migrate
Browse files Browse the repository at this point in the history
td: migrate `docdb` service to AWS SDKv2
  • Loading branch information
johnsonaj authored Jun 7, 2024
2 parents ea7ba2b + da9258f commit ffa7011
Show file tree
Hide file tree
Showing 29 changed files with 818 additions and 747 deletions.
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/devopsguru v1.30.9
github.com/aws/aws-sdk-go-v2/service/directoryservice v1.24.9
github.com/aws/aws-sdk-go-v2/service/dlm v1.24.9
github.com/aws/aws-sdk-go-v2/service/docdb v1.34.6
github.com/aws/aws-sdk-go-v2/service/docdbelastic v1.9.8
github.com/aws/aws-sdk-go-v2/service/drs v1.26.5
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.32.7
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ github.com/aws/aws-sdk-go-v2/service/directoryservice v1.24.9 h1:IUT9X0mSw7nn163
github.com/aws/aws-sdk-go-v2/service/directoryservice v1.24.9/go.mod h1:D4kc1uNcsRFGQTJzZDZiLKU8slsvgeKfFvoLDWahYZQ=
github.com/aws/aws-sdk-go-v2/service/dlm v1.24.9 h1:rbCsQ+v8ZhPCyraCMGJ0ryoRNyOST+Epqf7BLFU6juw=
github.com/aws/aws-sdk-go-v2/service/dlm v1.24.9/go.mod h1:TDugCcN4kVVjmf3AyrgJxF+bMGo6xE0rIqzekOdBci0=
github.com/aws/aws-sdk-go-v2/service/docdb v1.34.6 h1:6aHlQCKKZt3I/k816Pf9p76ThfBOOFtd3iauHAfnCRQ=
github.com/aws/aws-sdk-go-v2/service/docdb v1.34.6/go.mod h1:b/JbDffezMACPOD8WkqS6zdegurMGSr76fWb69yfbmU=
github.com/aws/aws-sdk-go-v2/service/docdbelastic v1.9.8 h1:h2e8qCW13l+HidSl5AL/yyTm7SjG+1rccnL5v0H0DMs=
github.com/aws/aws-sdk-go-v2/service/docdbelastic v1.9.8/go.mod h1:sNRGOjnAEBY66qjElTl5VMEv1vm8bCD0HNjheIpsG8g=
github.com/aws/aws-sdk-go-v2/service/drs v1.26.5 h1:KGamj85Y3eghj2i/D4Hm72rjkHClGSQv3kiDb/17sUg=
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.

157 changes: 79 additions & 78 deletions internal/service/docdb/cluster.go

Large diffs are not rendered by default.

105 changes: 52 additions & 53 deletions internal/service/docdb/cluster_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,16 @@ import (
"strings"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/docdb"
"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/docdb"
awstypes "github.com/aws/aws-sdk-go-v2/service/docdb/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/create"
"github.com/hashicorp/terraform-provider-aws/internal/errs"
"github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag"
tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
Expand Down Expand Up @@ -183,7 +184,7 @@ func ResourceClusterInstance() *schema.Resource {

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

identifier := create.NewNameGenerator(
create.WithConfiguredName(d.Get(names.AttrIdentifier).(string)),
Expand All @@ -196,7 +197,7 @@ func resourceClusterInstanceCreate(ctx context.Context, d *schema.ResourceData,
DBInstanceClass: aws.String(d.Get("instance_class").(string)),
DBInstanceIdentifier: aws.String(identifier),
Engine: aws.String(d.Get(names.AttrEngine).(string)),
PromotionTier: aws.Int64(int64(d.Get("promotion_tier").(int))),
PromotionTier: aws.Int32(int32(d.Get("promotion_tier").(int))),
Tags: getTagsIn(ctx),
}

Expand All @@ -221,7 +222,7 @@ func resourceClusterInstanceCreate(ctx context.Context, d *schema.ResourceData,
}

_, err := tfresource.RetryWhenAWSErrMessageContains(ctx, propagationTimeout, func() (interface{}, error) {
return conn.CreateDBInstanceWithContext(ctx, input)
return conn.CreateDBInstance(ctx, input)
}, errCodeInvalidParameterValue, "IAM role ARN value is invalid or does not include the required permissions")

if err != nil {
Expand All @@ -239,9 +240,9 @@ func resourceClusterInstanceCreate(ctx context.Context, d *schema.ResourceData,

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

db, err := FindDBInstanceByID(ctx, conn, d.Id())
db, err := findDBInstanceByID(ctx, conn, d.Id())

if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] DocumentDB Cluster Instance (%s) not found, removing from state", d.Id())
Expand All @@ -253,8 +254,8 @@ func resourceClusterInstanceRead(ctx context.Context, d *schema.ResourceData, me
return sdkdiag.AppendErrorf(diags, "reading DocumentDB Cluster Instance (%s): %s", d.Id(), err)
}

clusterID := aws.StringValue(db.DBClusterIdentifier)
dbc, err := FindDBClusterByID(ctx, conn, clusterID)
clusterID := aws.ToString(db.DBClusterIdentifier)
dbc, err := findDBClusterByID(ctx, conn, clusterID)

if err != nil {
return sdkdiag.AppendErrorf(diags, "reading DocumentDB Cluster (%s): %s", clusterID, err)
Expand All @@ -280,7 +281,7 @@ func resourceClusterInstanceRead(ctx context.Context, d *schema.ResourceData, me
d.Set(names.AttrEngine, db.Engine)
d.Set(names.AttrEngineVersion, db.EngineVersion)
d.Set(names.AttrIdentifier, db.DBInstanceIdentifier)
d.Set("identifier_prefix", create.NamePrefixFromName(aws.StringValue(db.DBInstanceIdentifier)))
d.Set("identifier_prefix", create.NamePrefixFromName(aws.ToString(db.DBInstanceIdentifier)))
d.Set("instance_class", db.DBInstanceClass)
d.Set(names.AttrKMSKeyID, db.KmsKeyId)
// The AWS API does not expose 'PerformanceInsightsKMSKeyId' the line below should be uncommented
Expand All @@ -291,8 +292,8 @@ func resourceClusterInstanceRead(ctx context.Context, d *schema.ResourceData, me
d.Set("promotion_tier", db.PromotionTier)
d.Set(names.AttrPubliclyAccessible, db.PubliclyAccessible)
d.Set(names.AttrStorageEncrypted, db.StorageEncrypted)
if v := tfslices.Filter(dbc.DBClusterMembers, func(v *docdb.DBClusterMember) bool {
return aws.StringValue(v.DBInstanceIdentifier) == d.Id()
if v := tfslices.Filter(dbc.DBClusterMembers, func(v awstypes.DBClusterMember) bool {
return aws.ToString(v.DBInstanceIdentifier) == d.Id()
}); len(v) == 1 {
d.Set("writer", v[0].IsClusterWriter)
}
Expand All @@ -302,7 +303,7 @@ func resourceClusterInstanceRead(ctx context.Context, d *schema.ResourceData, me

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

if d.HasChangesExcept(names.AttrTags, names.AttrTagsAll) {
input := &docdb.ModifyDBInstanceInput{
Expand Down Expand Up @@ -339,11 +340,11 @@ func resourceClusterInstanceUpdate(ctx context.Context, d *schema.ResourceData,
}

if d.HasChange("promotion_tier") {
input.PromotionTier = aws.Int64(int64(d.Get("promotion_tier").(int)))
input.PromotionTier = aws.Int32(int32(d.Get("promotion_tier").(int)))
}

_, err := tfresource.RetryWhenAWSErrMessageContains(ctx, propagationTimeout, func() (interface{}, error) {
return conn.ModifyDBInstanceWithContext(ctx, input)
return conn.ModifyDBInstance(ctx, input)
}, errCodeInvalidParameterValue, "IAM role ARN value is invalid or does not include the required permissions")

if err != nil {
Expand All @@ -360,13 +361,17 @@ func resourceClusterInstanceUpdate(ctx context.Context, d *schema.ResourceData,

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

log.Printf("[DEBUG] Deleting DocumentDB Cluster Instance: %s", d.Id())
_, err := conn.DeleteDBInstanceWithContext(ctx, &docdb.DeleteDBInstanceInput{
_, err := conn.DeleteDBInstance(ctx, &docdb.DeleteDBInstanceInput{
DBInstanceIdentifier: aws.String(d.Id()),
})

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

if err != nil {
return sdkdiag.AppendErrorf(diags, "deleting DocumentDB Cluster Instance (%s): %s", d.Id(), err)
}
Expand All @@ -378,7 +383,7 @@ func resourceClusterInstanceDelete(ctx context.Context, d *schema.ResourceData,
return diags
}

func FindDBInstanceByID(ctx context.Context, conn *docdb.DocDB, id string) (*docdb.DBInstance, error) {
func findDBInstanceByID(ctx context.Context, conn *docdb.Client, id string) (*awstypes.DBInstance, error) {
input := &docdb.DescribeDBInstancesInput{
DBInstanceIdentifier: aws.String(id),
}
Expand All @@ -389,7 +394,7 @@ func FindDBInstanceByID(ctx context.Context, conn *docdb.DocDB, id string) (*doc
}

// Eventual consistency check.
if aws.StringValue(output.DBInstanceIdentifier) != id {
if aws.ToString(output.DBInstanceIdentifier) != id {
return nil, &retry.NotFoundError{
LastRequest: input,
}
Expand All @@ -398,50 +403,43 @@ func FindDBInstanceByID(ctx context.Context, conn *docdb.DocDB, id string) (*doc
return output, nil
}

func findDBInstance(ctx context.Context, conn *docdb.DocDB, input *docdb.DescribeDBInstancesInput) (*docdb.DBInstance, error) {
func findDBInstance(ctx context.Context, conn *docdb.Client, input *docdb.DescribeDBInstancesInput) (*awstypes.DBInstance, error) {
output, err := findDBInstances(ctx, conn, input)

if err != nil {
return nil, err
}

return tfresource.AssertSinglePtrResult(output)
return tfresource.AssertSingleValueResult(output)
}

func findDBInstances(ctx context.Context, conn *docdb.DocDB, input *docdb.DescribeDBInstancesInput) ([]*docdb.DBInstance, error) {
var output []*docdb.DBInstance
func findDBInstances(ctx context.Context, conn *docdb.Client, input *docdb.DescribeDBInstancesInput) ([]awstypes.DBInstance, error) {
var output []awstypes.DBInstance

err := conn.DescribeDBInstancesPagesWithContext(ctx, input, func(page *docdb.DescribeDBInstancesOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}
pages := docdb.NewDescribeDBInstancesPaginator(conn, input)
for pages.HasMorePages() {
page, err := pages.NextPage(ctx)

for _, v := range page.DBInstances {
if v != nil {
output = append(output, v)
if errs.IsA[*awstypes.DBInstanceNotFoundFault](err) {
return nil, &retry.NotFoundError{
LastRequest: input,
LastError: err,
}
}

return !lastPage
})

if tfawserr.ErrCodeEquals(err, docdb.ErrCodeDBInstanceNotFoundFault) {
return nil, &retry.NotFoundError{
LastError: err,
LastRequest: input,
if err != nil {
return nil, err
}
}

if err != nil {
return nil, err
output = append(output, page.DBInstances...)
}

return output, nil
}

func statusDBInstance(ctx context.Context, conn *docdb.DocDB, id string) retry.StateRefreshFunc {
func statusDBInstance(ctx context.Context, conn *docdb.Client, id string) retry.StateRefreshFunc {
return func() (interface{}, string, error) {
output, err := FindDBInstanceByID(ctx, conn, id)
output, err := findDBInstanceByID(ctx, conn, id)

if tfresource.NotFound(err) {
return nil, "", nil
Expand All @@ -451,11 +449,11 @@ func statusDBInstance(ctx context.Context, conn *docdb.DocDB, id string) retry.S
return nil, "", err
}

return output, aws.StringValue(output.DBInstanceStatus), nil
return output, aws.ToString(output.DBInstanceStatus), nil
}
}

func waitDBInstanceAvailable(ctx context.Context, conn *docdb.DocDB, id string, timeout time.Duration) (*docdb.DBInstance, error) { //nolint:unparam
func waitDBInstanceAvailable(ctx context.Context, conn *docdb.Client, id string, timeout time.Duration) (*awstypes.DBInstance, error) { //nolint:unparam
stateConf := &retry.StateChangeConf{
Pending: []string{
"backing-up",
Expand All @@ -472,23 +470,24 @@ func waitDBInstanceAvailable(ctx context.Context, conn *docdb.DocDB, id string,
"storage-optimization",
"upgrading",
},
Target: []string{"available"},
Refresh: statusDBInstance(ctx, conn, id),
Timeout: timeout,
MinTimeout: 10 * time.Second,
Delay: 30 * time.Second,
Target: []string{"available"},
Refresh: statusDBInstance(ctx, conn, id),
Timeout: timeout,
MinTimeout: 10 * time.Second,
Delay: 30 * time.Second,
ContinuousTargetOccurence: 2,
}

outputRaw, err := stateConf.WaitForStateContext(ctx)

if output, ok := outputRaw.(*docdb.DBInstance); ok {
if output, ok := outputRaw.(*awstypes.DBInstance); ok {
return output, err
}

return nil, err
}

func waitDBInstanceDeleted(ctx context.Context, conn *docdb.DocDB, id string, timeout time.Duration) (*docdb.DBInstance, error) {
func waitDBInstanceDeleted(ctx context.Context, conn *docdb.Client, id string, timeout time.Duration) (*awstypes.DBInstance, error) {
stateConf := &retry.StateChangeConf{
Pending: []string{
"configuring-log-exports",
Expand All @@ -504,7 +503,7 @@ func waitDBInstanceDeleted(ctx context.Context, conn *docdb.DocDB, id string, ti

outputRaw, err := stateConf.WaitForStateContext(ctx)

if output, ok := outputRaw.(*docdb.DBInstance); ok {
if output, ok := outputRaw.(*awstypes.DBInstance); ok {
return output, err
}

Expand Down
Loading

0 comments on commit ffa7011

Please sign in to comment.