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

Migrate Route 53 Recovery Control Config resources to AWS SDK V2 #38777

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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 @@ -189,6 +189,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/route53 v1.42.3
github.com/aws/aws-sdk-go-v2/service/route53domains v1.25.3
github.com/aws/aws-sdk-go-v2/service/route53profiles v1.2.3
github.com/aws/aws-sdk-go-v2/service/route53recoverycontrolconfig v1.23.3
github.com/aws/aws-sdk-go-v2/service/route53recoveryreadiness v1.19.3
github.com/aws/aws-sdk-go-v2/service/rum v1.19.3
github.com/aws/aws-sdk-go-v2/service/s3 v1.58.3
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,8 @@ github.com/aws/aws-sdk-go-v2/service/route53profiles v1.2.3 h1:N4f5sliNiWcp3abC+
github.com/aws/aws-sdk-go-v2/service/route53profiles v1.2.3/go.mod h1:r2B4BvTn3zSMK+BFHGl0q63B/nJMOk9/NukLZzqO8sY=
github.com/aws/aws-sdk-go-v2/service/route53recoveryreadiness v1.19.3 h1:M6D+IRT6YdeL+qLIdy4u4zEoMOqq3lJnNUGt0cxEI04=
github.com/aws/aws-sdk-go-v2/service/route53recoveryreadiness v1.19.3/go.mod h1:y4m7VlTWV77mBzMdAZWjudnobe0E77tKy5Z9+IQIku0=
github.com/aws/aws-sdk-go-v2/service/route53recoverycontrolconfig v1.23.3 h1:apYav6exfbLJ+kRGPa27GTrUuCS4ctI0mJEeiDxSeDE=
github.com/aws/aws-sdk-go-v2/service/route53recoverycontrolconfig v1.23.3/go.mod h1:citOcziml/EM6I2ycb7XHuBw0whC8jVD2y+vU7wQD4k=
github.com/aws/aws-sdk-go-v2/service/rum v1.19.3 h1:DR+GYJRPL7eEZknnGdwm+lH686LmUBB/X2YVQDHLNY4=
github.com/aws/aws-sdk-go-v2/service/rum v1.19.3/go.mod h1:5jFxbuc05P/+BbJvVbBspMbzDR2IFU0LegQG3iUvj8g=
github.com/aws/aws-sdk-go-v2/service/s3 v1.58.3 h1:hT8ZAZRIfqBqHbzKTII+CIiY8G2oC9OpLedkZ51DWl8=
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.

89 changes: 49 additions & 40 deletions internal/service/route53recoverycontrolconfig/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@ import (
"context"
"log"

"github.com/aws/aws-sdk-go/aws"
r53rcc "github.com/aws/aws-sdk-go/service/route53recoverycontrolconfig"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/aws/aws-sdk-go-v2/aws"
r53rcc "github.com/aws/aws-sdk-go-v2/service/route53recoverycontrolconfig"
awstypes "github.com/aws/aws-sdk-go-v2/service/route53recoverycontrolconfig/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"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"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/names"
)

// @SDKResource("aws_route53recoverycontrolconfig_cluster")
func ResourceCluster() *schema.Resource {
// @SDKResource("aws_route53recoverycontrolconfig_cluster", name="Cluster")
func resourceCluster() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceClusterCreate,
ReadWithoutTimeout: resourceClusterRead,
Expand Down Expand Up @@ -64,14 +67,14 @@ func ResourceCluster() *schema.Resource {

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

input := &r53rcc.CreateClusterInput{
ClientToken: aws.String(id.UniqueId()),
ClusterName: aws.String(d.Get(names.AttrName).(string)),
}

output, err := conn.CreateClusterWithContext(ctx, input)
output, err := conn.CreateCluster(ctx, input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "creating Route53 Recovery Control Config Cluster: %s", err)
Expand All @@ -82,7 +85,7 @@ func resourceClusterCreate(ctx context.Context, d *schema.ResourceData, meta int
}

result := output.Cluster
d.SetId(aws.StringValue(result.ClusterArn))
d.SetId(aws.ToString(result.ClusterArn))

if _, err := waitClusterCreated(ctx, conn, d.Id()); err != nil {
return sdkdiag.AppendErrorf(diags, "waiting for Route53 Recovery Control Config Cluster (%s) to be Deployed: %s", d.Id(), err)
Expand All @@ -93,15 +96,11 @@ func resourceClusterCreate(ctx context.Context, d *schema.ResourceData, meta int

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

input := &r53rcc.DescribeClusterInput{
ClusterArn: aws.String(d.Id()),
}

output, err := conn.DescribeClusterWithContext(ctx, input)
output, err := findClusterByARN(ctx, conn, d.Id())

if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, r53rcc.ErrCodeResourceNotFoundException) {
if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] Route53 Recovery Control Config Cluster (%s) not found, removing from state", d.Id())
d.SetId("")
return diags
Expand All @@ -111,16 +110,11 @@ func resourceClusterRead(ctx context.Context, d *schema.ResourceData, meta inter
return sdkdiag.AppendErrorf(diags, "describing Route53 Recovery Control Config Cluster: %s", err)
}

if output == nil || output.Cluster == nil {
return sdkdiag.AppendErrorf(diags, "describing Route53 Recovery Control Config Cluster: %s", "empty response")
}
d.Set(names.AttrARN, output.ClusterArn)
d.Set(names.AttrName, output.Name)
d.Set(names.AttrStatus, output.Status)

result := output.Cluster
d.Set(names.AttrARN, result.ClusterArn)
d.Set(names.AttrName, result.Name)
d.Set(names.AttrStatus, result.Status)

if err := d.Set("cluster_endpoints", flattenClusterEndpoints(result.ClusterEndpoints)); err != nil {
if err := d.Set("cluster_endpoints", flattenClusterEndpoints(output.ClusterEndpoints)); err != nil {
return sdkdiag.AppendErrorf(diags, "setting cluster_endpoints: %s", err)
}

Expand All @@ -129,14 +123,14 @@ func resourceClusterRead(ctx context.Context, d *schema.ResourceData, meta inter

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

log.Printf("[INFO] Deleting Route53 Recovery Control Config Cluster: %s", d.Id())
_, err := conn.DeleteClusterWithContext(ctx, &r53rcc.DeleteClusterInput{
_, err := conn.DeleteCluster(ctx, &r53rcc.DeleteClusterInput{
ClusterArn: aws.String(d.Id()),
})

if tfawserr.ErrCodeEquals(err, r53rcc.ErrCodeResourceNotFoundException) {
if errs.IsA[*awstypes.ResourceNotFoundException](err) {
return diags
}

Expand All @@ -146,7 +140,7 @@ func resourceClusterDelete(ctx context.Context, d *schema.ResourceData, meta int

_, err = waitClusterDeleted(ctx, conn, d.Id())

if tfawserr.ErrCodeEquals(err, r53rcc.ErrCodeResourceNotFoundException) {
if errs.IsA[*awstypes.ResourceNotFoundException](err) {
return diags
}

Expand All @@ -157,37 +151,52 @@ func resourceClusterDelete(ctx context.Context, d *schema.ResourceData, meta int
return diags
}

func flattenClusterEndpoints(endpoints []*r53rcc.ClusterEndpoint) []interface{} {
func findClusterByARN(ctx context.Context, conn *r53rcc.Client, arn string) (*awstypes.Cluster, error) {
input := &r53rcc.DescribeClusterInput{
ClusterArn: aws.String(arn),
}

output, err := conn.DescribeCluster(ctx, input)
if errs.IsA[*awstypes.ResourceNotFoundException](err) {
return nil, &retry.NotFoundError{
LastError: err,
LastRequest: input,
}
}
if err != nil {
return nil, err
}

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

return output.Cluster, nil
}

func flattenClusterEndpoints(endpoints []awstypes.ClusterEndpoint) []interface{} {
if len(endpoints) == 0 {
return nil
}

var tfList []interface{}

for _, endpoint := range endpoints {
if endpoint == nil {
continue
}

tfList = append(tfList, flattenClusterEndpoint(endpoint))
}

return tfList
}

func flattenClusterEndpoint(ce *r53rcc.ClusterEndpoint) map[string]interface{} {
if ce == nil {
return nil
}

func flattenClusterEndpoint(ce awstypes.ClusterEndpoint) map[string]interface{} {
tfMap := map[string]interface{}{}

if v := ce.Endpoint; v != nil {
tfMap[names.AttrEndpoint] = aws.StringValue(v)
tfMap[names.AttrEndpoint] = aws.ToString(v)
}

if v := ce.Region; v != nil {
tfMap[names.AttrRegion] = aws.StringValue(v)
tfMap[names.AttrRegion] = aws.ToString(v)
}

return tfMap
Expand Down
53 changes: 28 additions & 25 deletions internal/service/route53recoverycontrolconfig/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/aws"
r53rcc "github.com/aws/aws-sdk-go/service/route53recoverycontrolconfig"
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
tfroute53recoverycontrolconfig "github.com/hashicorp/terraform-provider-aws/internal/service/route53recoverycontrolconfig"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/names"
)

Expand All @@ -25,7 +24,10 @@ func testAccCluster_basic(t *testing.T) {
resourceName := "aws_route53recoverycontrolconfig_cluster.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); acctest.PreCheckPartitionHasService(t, r53rcc.EndpointsID) },
PreCheck: func() {
acctest.PreCheck(ctx, t)
acctest.PreCheckPartitionHasService(t, names.Route53RecoveryControlConfigEndpointID)
},
ErrorCheck: acctest.ErrorCheck(t, names.Route53RecoveryControlConfigServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckClusterDestroy(ctx),
Expand Down Expand Up @@ -55,7 +57,10 @@ func testAccCluster_disappears(t *testing.T) {
resourceName := "aws_route53recoverycontrolconfig_cluster.test"

resource.Test(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t); acctest.PreCheckPartitionHasService(t, r53rcc.EndpointsID) },
PreCheck: func() {
acctest.PreCheck(ctx, t)
acctest.PreCheckPartitionHasService(t, names.Route53RecoveryControlConfigEndpointID)
},
ErrorCheck: acctest.ErrorCheck(t, names.Route53RecoveryControlConfigServiceID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckClusterDestroy(ctx),
Expand All @@ -74,51 +79,49 @@ func testAccCluster_disappears(t *testing.T) {

func testAccCheckClusterDestroy(ctx context.Context) resource.TestCheckFunc {
return func(s *terraform.State) error {
conn := acctest.Provider.Meta().(*conns.AWSClient).Route53RecoveryControlConfigConn(ctx)
conn := acctest.Provider.Meta().(*conns.AWSClient).Route53RecoveryControlConfigClient(ctx)

for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_route53recoverycontrolconfig_cluster" {
continue
}

input := &r53rcc.DescribeClusterInput{
ClusterArn: aws.String(rs.Primary.ID),
}
_, err := tfroute53recoverycontrolconfig.FindClusterByARN(ctx, conn, rs.Primary.ID)

_, err := conn.DescribeClusterWithContext(ctx, input)
if tfresource.NotFound(err) {
continue
}

if err == nil {
return fmt.Errorf("Route53RecoveryControlConfig cluster (%s) not deleted", rs.Primary.ID)
if err != nil {
return err
}

return fmt.Errorf("Route53RecoveryControlConfig Cluster (%s) not deleted", rs.Primary.ID)
}

return nil
}
}

func testAccClusterConfig_basic(rName string) string {
return fmt.Sprintf(`
resource "aws_route53recoverycontrolconfig_cluster" "test" {
name = %[1]q
}
`, rName)
}

func testAccCheckClusterExists(ctx context.Context, name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}

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

input := &r53rcc.DescribeClusterInput{
ClusterArn: aws.String(rs.Primary.ID),
}
conn := acctest.Provider.Meta().(*conns.AWSClient).Route53RecoveryControlConfigClient(ctx)

_, err := conn.DescribeClusterWithContext(ctx, input)
_, err := tfroute53recoverycontrolconfig.FindClusterByARN(ctx, conn, rs.Primary.ID)

return err
}
}

func testAccClusterConfig_basic(rName string) string {
return fmt.Sprintf(`
resource "aws_route53recoverycontrolconfig_cluster" "test" {
name = %[1]q
}
`, rName)
}
Loading
Loading