Skip to content

Commit

Permalink
Merge pull request #37780 from mattburgess/dms-awssdkv2-migration
Browse files Browse the repository at this point in the history
dms: Migrate to AWS SDK v2
  • Loading branch information
ewbankkit authored Jul 16, 2024
2 parents d370607 + 46558ee commit 8539002
Show file tree
Hide file tree
Showing 32 changed files with 1,191 additions and 1,207 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/costexplorer v1.40.3
github.com/aws/aws-sdk-go-v2/service/costoptimizationhub v1.7.3
github.com/aws/aws-sdk-go-v2/service/customerprofiles v1.39.3
github.com/aws/aws-sdk-go-v2/service/databasemigrationservice v1.40.3
github.com/aws/aws-sdk-go-v2/service/databrew v1.31.3
github.com/aws/aws-sdk-go-v2/service/datasync v1.40.3
github.com/aws/aws-sdk-go-v2/service/datazone v1.13.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ github.com/aws/aws-sdk-go-v2/service/costoptimizationhub v1.7.3 h1:+AiQwAYmhOXn0
github.com/aws/aws-sdk-go-v2/service/costoptimizationhub v1.7.3/go.mod h1:Om/t/NhLjZu7rYMYBI1rWyGqEUfqSn/vk/k1/7pLEC8=
github.com/aws/aws-sdk-go-v2/service/customerprofiles v1.39.3 h1:Aq+7pnVWk59dS2BMVSOEDWN0yProaw0XhaUsRGbH7MM=
github.com/aws/aws-sdk-go-v2/service/customerprofiles v1.39.3/go.mod h1:4duVgMu+RBKpiU+Hz4FjPedMLWNFVL4lhauBVYz8OZ4=
github.com/aws/aws-sdk-go-v2/service/databasemigrationservice v1.40.3 h1:HzdVJzMjEhQhLjUB1xGRMhs4zjaemPLUbdhhA4wfnMI=
github.com/aws/aws-sdk-go-v2/service/databasemigrationservice v1.40.3/go.mod h1:2SvlhcMgqPNNVr53/0m91cxPTY6mUFvp6o+Kzi63zUM=
github.com/aws/aws-sdk-go-v2/service/databrew v1.31.3 h1:tFFs24+oIWlHLbTyluhnQIHaj8o4nc8yXHNnAc8PTN8=
github.com/aws/aws-sdk-go-v2/service/databrew v1.31.3/go.mod h1:WP7xXB608MyVv3yFzduKlLeYmU0AxMo7zeF9Cuwbvwc=
github.com/aws/aws-sdk-go-v2/service/datasync v1.40.3 h1:ZrKMl8jsL5YHurOLf0YVLb7JBYxGtqQQAknJ5g4MTz4=
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.

90 changes: 40 additions & 50 deletions internal/service/dms/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,17 @@ import (
"log"

"github.com/YakDriver/regexache"
"github.com/aws/aws-sdk-go/aws"
dms "github.com/aws/aws-sdk-go/service/databasemigrationservice"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/aws/aws-sdk-go-v2/aws"
dms "github.com/aws/aws-sdk-go-v2/service/databasemigrationservice"
awstypes "github.com/aws/aws-sdk-go-v2/service/databasemigrationservice/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/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"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
itypes "github.com/hashicorp/terraform-provider-aws/internal/types"
Expand All @@ -26,7 +28,7 @@ import (

// @SDKResource("aws_dms_certificate", name="Certificate")
// @Tags(identifierAttribute="certificate_arn")
func ResourceCertificate() *schema.Resource {
func resourceCertificate() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceCertificateCreate,
ReadWithoutTimeout: resourceCertificateRead,
Expand Down Expand Up @@ -77,7 +79,7 @@ func ResourceCertificate() *schema.Resource {

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

certificateID := d.Get("certificate_id").(string)
input := &dms.ImportCertificateInput{
Expand All @@ -97,7 +99,7 @@ func resourceCertificateCreate(ctx context.Context, d *schema.ResourceData, meta
input.CertificateWallet = v
}

_, err := conn.ImportCertificateWithContext(ctx, input)
_, err := conn.ImportCertificate(ctx, input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "creating DMS Certificate (%s): %s", certificateID, err)
Expand All @@ -110,9 +112,9 @@ func resourceCertificateCreate(ctx context.Context, d *schema.ResourceData, meta

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

certificate, err := FindCertificateByID(ctx, conn, d.Id())
certificate, err := findCertificateByID(ctx, conn, d.Id())

if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] DMS Certificate (%s) not found, removing from state", d.Id())
Expand All @@ -124,7 +126,15 @@ func resourceCertificateRead(ctx context.Context, d *schema.ResourceData, meta i
return sdkdiag.AppendErrorf(diags, "reading DMS Certificate (%s): %s", d.Id(), err)
}

resourceCertificateSetState(d, certificate)
d.SetId(aws.ToString(certificate.CertificateIdentifier))
d.Set("certificate_id", certificate.CertificateIdentifier)
d.Set(names.AttrCertificateARN, certificate.CertificateArn)
if v := aws.ToString(certificate.CertificatePem); v != "" {
d.Set("certificate_pem", v)
}
if certificate.CertificateWallet != nil && len(certificate.CertificateWallet) != 0 {
d.Set("certificate_wallet", itypes.Base64EncodeOnce(certificate.CertificateWallet))
}

return diags
}
Expand All @@ -139,14 +149,14 @@ func resourceCertificateUpdate(ctx context.Context, d *schema.ResourceData, meta

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

log.Printf("[DEBUG] Deleting DMS Certificate: %s", d.Id())
_, err := conn.DeleteCertificateWithContext(ctx, &dms.DeleteCertificateInput{
_, err := conn.DeleteCertificate(ctx, &dms.DeleteCertificateInput{
CertificateArn: aws.String(d.Get(names.AttrCertificateARN).(string)),
})

if tfawserr.ErrCodeEquals(err, dms.ErrCodeResourceNotFoundFault) {
if errs.IsA[*awstypes.ResourceNotFoundFault](err) {
return diags
}

Expand All @@ -157,34 +167,20 @@ func resourceCertificateDelete(ctx context.Context, d *schema.ResourceData, meta
return diags
}

func resourceCertificateSetState(d *schema.ResourceData, cert *dms.Certificate) {
d.SetId(aws.StringValue(cert.CertificateIdentifier))

d.Set("certificate_id", cert.CertificateIdentifier)
d.Set(names.AttrCertificateARN, cert.CertificateArn)

if aws.StringValue(cert.CertificatePem) != "" {
d.Set("certificate_pem", cert.CertificatePem)
}
if cert.CertificateWallet != nil && len(cert.CertificateWallet) != 0 {
d.Set("certificate_wallet", itypes.Base64EncodeOnce(cert.CertificateWallet))
}
}

func FindCertificateByID(ctx context.Context, conn *dms.DatabaseMigrationService, id string) (*dms.Certificate, error) {
func findCertificateByID(ctx context.Context, conn *dms.Client, id string) (*awstypes.Certificate, error) {
input := &dms.DescribeCertificatesInput{
Filters: []*dms.Filter{
Filters: []awstypes.Filter{
{
Name: aws.String("certificate-id"),
Values: []*string{aws.String(id)},
Values: []string{id},
},
},
}

return findCertificate(ctx, conn, input)
}

func findCertificate(ctx context.Context, conn *dms.DatabaseMigrationService, input *dms.DescribeCertificatesInput) (*dms.Certificate, error) {
func findCertificate(ctx context.Context, conn *dms.Client, input *dms.DescribeCertificatesInput) (*awstypes.Certificate, error) {
output, err := findCertificates(ctx, conn, input)

if err != nil {
Expand All @@ -194,33 +190,27 @@ func findCertificate(ctx context.Context, conn *dms.DatabaseMigrationService, in
return tfresource.AssertSinglePtrResult(output)
}

func findCertificates(ctx context.Context, conn *dms.DatabaseMigrationService, input *dms.DescribeCertificatesInput) ([]*dms.Certificate, error) {
var output []*dms.Certificate
func findCertificates(ctx context.Context, conn *dms.Client, input *dms.DescribeCertificatesInput) ([]*awstypes.Certificate, error) {
var output []awstypes.Certificate

err := conn.DescribeCertificatesPagesWithContext(ctx, input, func(page *dms.DescribeCertificatesOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}
pages := dms.NewDescribeCertificatesPaginator(conn, input)

for pages.HasMorePages() {
page, err := pages.NextPage(ctx)

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

return !lastPage
})

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

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

return output, nil
return tfslices.ToPointers(output), nil
}
15 changes: 8 additions & 7 deletions internal/service/dms/certificate_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"context"

"github.com/YakDriver/regexache"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
Expand All @@ -18,8 +18,8 @@ import (
"github.com/hashicorp/terraform-provider-aws/names"
)

// @SDKDataSource("aws_dms_certificate")
func DataSourceCertificate() *schema.Resource {
// @SDKDataSource("aws_dms_certificate", name="Certificate")
func dataSourceCertificate() *schema.Resource {
return &schema.Resource{
ReadWithoutTimeout: dataSourceCertificateRead,

Expand Down Expand Up @@ -79,19 +79,19 @@ func DataSourceCertificate() *schema.Resource {

func dataSourceCertificateRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).DMSConn(ctx)
conn := meta.(*conns.AWSClient).DMSClient(ctx)
defaultTagsConfig := meta.(*conns.AWSClient).DefaultTagsConfig
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

certificateID := d.Get("certificate_id").(string)
out, err := FindCertificateByID(ctx, conn, certificateID)
out, err := findCertificateByID(ctx, conn, certificateID)

if err != nil {
return sdkdiag.AppendErrorf(diags, "reading DMS Certificate (%s): %s", certificateID, err)
}

d.SetId(aws.StringValue(out.CertificateIdentifier))
arn := aws.StringValue(out.CertificateArn)
d.SetId(aws.ToString(out.CertificateIdentifier))
arn := aws.ToString(out.CertificateArn)
d.Set(names.AttrCertificateARN, arn)
d.Set("certificate_id", out.CertificateIdentifier)
d.Set("certificate_pem", out.CertificatePem)
Expand All @@ -104,6 +104,7 @@ func dataSourceCertificateRead(ctx context.Context, d *schema.ResourceData, meta
d.Set("valid_to_date", out.ValidToDate.String())

tags, err := listTags(ctx, conn, arn)

if err != nil {
return sdkdiag.AppendErrorf(diags, "listing tags for DMS Certificate (%s): %s", arn, err)
}
Expand Down
4 changes: 2 additions & 2 deletions internal/service/dms/certificate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func testAccCheckCertificateDestroy(ctx context.Context) resource.TestCheckFunc
continue
}

conn := acctest.Provider.Meta().(*conns.AWSClient).DMSConn(ctx)
conn := acctest.Provider.Meta().(*conns.AWSClient).DMSClient(ctx)

_, err := tfdms.FindCertificateByID(ctx, conn, rs.Primary.ID)

Expand All @@ -173,7 +173,7 @@ func testAccCertificateExists(ctx context.Context, n string) resource.TestCheckF
return fmt.Errorf("Not found: %s", n)
}

conn := acctest.Provider.Meta().(*conns.AWSClient).DMSConn(ctx)
conn := acctest.Provider.Meta().(*conns.AWSClient).DMSClient(ctx)

_, err := tfdms.FindCertificateByID(ctx, conn, rs.Primary.ID)

Expand Down
Loading

0 comments on commit 8539002

Please sign in to comment.