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 ram resources to AWS Go SDKv2 #37450

Merged
merged 13 commits into from
May 14, 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
3 changes: 3 additions & 0 deletions .changelog/37450.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_ram_principal_association: Correct plan-time validation of `principal` to fix `panic: unexpected format for ID parts ([...]), the following id parts indexes are blank ([1])`
```
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/pricing v1.28.2
github.com/aws/aws-sdk-go-v2/service/qbusiness v1.6.1
github.com/aws/aws-sdk-go-v2/service/qldb v1.21.5
github.com/aws/aws-sdk-go-v2/service/ram v1.25.5
github.com/aws/aws-sdk-go-v2/service/rbin v1.16.5
github.com/aws/aws-sdk-go-v2/service/rds v1.78.1
github.com/aws/aws-sdk-go-v2/service/redshift v1.44.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,8 @@ github.com/aws/aws-sdk-go-v2/service/qbusiness v1.6.1 h1:TjDg29LJwoLOe53kf5TJ7te
github.com/aws/aws-sdk-go-v2/service/qbusiness v1.6.1/go.mod h1:GA+mlGvbl5shamVdR+zkQDrdExUQ9WAEnPlMtdiuVr8=
github.com/aws/aws-sdk-go-v2/service/qldb v1.21.5 h1:wh+eFaiLFeISAoINkD9tLOJ/rrpLPy6sVhFXkEfKvxM=
github.com/aws/aws-sdk-go-v2/service/qldb v1.21.5/go.mod h1:T789CzkMLwKq1b5MxcUfQeoUisJ6jJhciaZTtAQtUOU=
github.com/aws/aws-sdk-go-v2/service/ram v1.25.5 h1:9g8PSZ1SmvmEAIu64JjfdTj+49+CL21DI8EL5jKEd4E=
github.com/aws/aws-sdk-go-v2/service/ram v1.25.5/go.mod h1:ZDVnnA45kEAe24PtJOB3pgU0GdKeoRAJPIDCIVXal9c=
github.com/aws/aws-sdk-go-v2/service/rbin v1.16.5 h1:/HQfwoS7nNnbuyrBAqJOCHSpYBzVKENQjY2JzsCiYxs=
github.com/aws/aws-sdk-go-v2/service/rbin v1.16.5/go.mod h1:BUtbswz07qEjzGypmeUdtP53noKx1PBKAnX9Fe0Mul4=
github.com/aws/aws-sdk-go-v2/service/rds v1.78.1 h1:D3XX2O6IzStNWEK2GU5EQTZVZ2r2Q4aRhWplndGIQR4=
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.

13 changes: 0 additions & 13 deletions internal/service/ram/consts.go

This file was deleted.

2 changes: 1 addition & 1 deletion internal/service/ram/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 -ListTagsInIDElem=ResourceShareArn -ServiceTagsSlice -TagInIDElem=ResourceShareArn -UpdateTags
//go:generate go run ../../generate/tags/main.go -AWSSDKVersion=2 -ListTagsInIDElem=ResourceShareArn -ServiceTagsSlice -TagInIDElem=ResourceShareArn -UpdateTags
//go:generate go run ../../generate/servicepackage/main.go
// ONLY generate directives and package declaration! Do not add anything else to this file.

Expand Down
66 changes: 36 additions & 30 deletions internal/service/ram/principal_association.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ import (
"log"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ram"
"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/ram"
awstypes "github.com/aws/aws-sdk-go-v2/service/ram/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
sdkid "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-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"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
Expand All @@ -44,9 +45,12 @@ func resourcePrincipalAssociation() *schema.Resource {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.Any(
verify.ValidAccountID,
verify.ValidARN,
ValidateFunc: validation.All(
validation.StringIsNotEmpty,
validation.Any(
verify.ValidAccountID,
verify.ValidARN,
),
),
},
"resource_share_arn": {
Expand All @@ -65,7 +69,7 @@ const (

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

resourceShareARN, principal := d.Get("resource_share_arn").(string), d.Get(names.AttrPrincipal).(string)
id := errs.Must(flex.FlattenResourceId([]string{resourceShareARN, principal}, principalAssociationResourceIDPartCount, false))
Expand All @@ -82,11 +86,11 @@ func resourcePrincipalAssociationCreate(ctx context.Context, d *schema.ResourceD

input := &ram.AssociateResourceShareInput{
ClientToken: aws.String(sdkid.UniqueId()),
Principals: []*string{aws.String(principal)},
Principals: []string{principal},
ResourceShareArn: aws.String(resourceShareARN),
}

_, err = conn.AssociateResourceShareWithContext(ctx, input)
_, err = conn.AssociateResourceShare(ctx, input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "creating RAM Principal Association (%s): %s", id, err)
Expand All @@ -108,7 +112,7 @@ func resourcePrincipalAssociationCreate(ctx context.Context, d *schema.ResourceD

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

parts, err := flex.ExpandResourceId(d.Id(), principalAssociationResourceIDPartCount, false)
if err != nil {
Expand Down Expand Up @@ -136,7 +140,7 @@ func resourcePrincipalAssociationRead(ctx context.Context, d *schema.ResourceDat

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

parts, err := flex.ExpandResourceId(d.Id(), principalAssociationResourceIDPartCount, false)
if err != nil {
Expand All @@ -145,12 +149,12 @@ func resourcePrincipalAssociationDelete(ctx context.Context, d *schema.ResourceD
resourceShareARN, principal := parts[0], parts[1]

log.Printf("[DEBUG] Deleting RAM Principal Association: %s", d.Id())
_, err = conn.DisassociateResourceShareWithContext(ctx, &ram.DisassociateResourceShareInput{
Principals: []*string{aws.String(principal)},
_, err = conn.DisassociateResourceShare(ctx, &ram.DisassociateResourceShareInput{
Principals: []string{principal},
ResourceShareArn: aws.String(resourceShareARN),
})

if tfawserr.ErrCodeEquals(err, ram.ErrCodeUnknownResourceException) {
if errs.IsA[*awstypes.UnknownResourceException](err) {
return diags
}

Expand All @@ -165,11 +169,11 @@ func resourcePrincipalAssociationDelete(ctx context.Context, d *schema.ResourceD
return diags
}

func findPrincipalAssociationByTwoPartKey(ctx context.Context, conn *ram.RAM, resourceShareARN, principal string) (*ram.ResourceShareAssociation, error) {
func findPrincipalAssociationByTwoPartKey(ctx context.Context, conn *ram.Client, resourceShareARN, principal string) (*awstypes.ResourceShareAssociation, error) {
input := &ram.GetResourceShareAssociationsInput{
AssociationType: aws.String(ram.ResourceShareAssociationTypePrincipal),
AssociationType: awstypes.ResourceShareAssociationTypePrincipal,
Principal: aws.String(principal),
ResourceShareArns: aws.StringSlice([]string{resourceShareARN}),
ResourceShareArns: []string{resourceShareARN},
}

output, err := findResourceShareAssociation(ctx, conn, input)
Expand All @@ -178,17 +182,17 @@ func findPrincipalAssociationByTwoPartKey(ctx context.Context, conn *ram.RAM, re
return nil, err
}

if status := aws.StringValue(output.Status); status == ram.ResourceShareAssociationStatusDisassociated {
if status := output.Status; status == awstypes.ResourceShareAssociationStatusDisassociated {
return nil, &retry.NotFoundError{
Message: status,
Message: string(status),
LastRequest: input,
}
}

return output, err
}

func statusPrincipalAssociation(ctx context.Context, conn *ram.RAM, resourceShareARN, principal string) retry.StateRefreshFunc {
func statusPrincipalAssociation(ctx context.Context, conn *ram.Client, resourceShareARN, principal string) retry.StateRefreshFunc {
return func() (interface{}, string, error) {
output, err := findPrincipalAssociationByTwoPartKey(ctx, conn, resourceShareARN, principal)

Expand All @@ -200,48 +204,50 @@ func statusPrincipalAssociation(ctx context.Context, conn *ram.RAM, resourceShar
return nil, "", err
}

return output, aws.StringValue(output.Status), nil
return output, string(output.Status), nil
}
}

func waitPrincipalAssociationCreated(ctx context.Context, conn *ram.RAM, resourceShareARN, principal string) (*ram.ResourceShareAssociation, error) {
func waitPrincipalAssociationCreated(ctx context.Context, conn *ram.Client, resourceShareARN, principal string) (*awstypes.ResourceShareAssociation, error) {
const (
timeout = 3 * time.Minute
)
stateConf := &retry.StateChangeConf{
Pending: []string{ram.ResourceShareAssociationStatusAssociating},
Target: []string{ram.ResourceShareAssociationStatusAssociated},
Pending: enum.Slice(awstypes.ResourceShareAssociationStatusAssociating),
Target: enum.Slice(awstypes.ResourceShareAssociationStatusAssociated),
Refresh: statusPrincipalAssociation(ctx, conn, resourceShareARN, principal),
Timeout: timeout,
NotFoundChecks: 20,
}

outputRaw, err := stateConf.WaitForStateContext(ctx)

if output, ok := outputRaw.(*ram.ResourceShareAssociation); ok {
tfresource.SetLastError(err, errors.New(aws.StringValue(output.StatusMessage)))
if output, ok := outputRaw.(*awstypes.ResourceShareAssociation); ok {
tfresource.SetLastError(err, errors.New(aws.ToString(output.StatusMessage)))

return output, err
}

return nil, err
}

func waitPrincipalAssociationDeleted(ctx context.Context, conn *ram.RAM, resourceShareARN, principal string) (*ram.ResourceShareAssociation, error) {
func waitPrincipalAssociationDeleted(ctx context.Context, conn *ram.Client, resourceShareARN, principal string) (*awstypes.ResourceShareAssociation, error) {
const (
timeout = 3 * time.Minute
)
stateConf := &retry.StateChangeConf{
Pending: []string{ram.ResourceShareAssociationStatusAssociated, ram.ResourceShareAssociationStatusDisassociating},
Pending: enum.Slice(awstypes.ResourceShareAssociationStatusAssociated, awstypes.ResourceShareAssociationStatusDisassociating),
Target: []string{},
Refresh: statusPrincipalAssociation(ctx, conn, resourceShareARN, principal),
Timeout: timeout,
}

outputRaw, err := stateConf.WaitForStateContext(ctx)

if v, ok := outputRaw.(*ram.ResourceShareAssociation); ok {
return v, err
if output, ok := outputRaw.(*awstypes.ResourceShareAssociation); ok {
tfresource.SetLastError(err, errors.New(aws.ToString(output.StatusMessage)))

return output, err
}

return nil, err
Expand Down
16 changes: 8 additions & 8 deletions internal/service/ram/principal_association_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"testing"

"github.com/YakDriver/regexache"
"github.com/aws/aws-sdk-go/service/ram"
awstypes "github.com/aws/aws-sdk-go-v2/service/ram/types"
sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
Expand All @@ -22,7 +22,7 @@ import (

func TestAccRAMPrincipalAssociation_basic(t *testing.T) {
ctx := acctest.Context(t)
var association ram.ResourceShareAssociation
var association awstypes.ResourceShareAssociation
resourceName := "aws_ram_principal_association.test"
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

Expand Down Expand Up @@ -52,7 +52,7 @@ func TestAccRAMPrincipalAssociation_basic(t *testing.T) {

func TestAccRAMPrincipalAssociation_AccountID(t *testing.T) {
ctx := acctest.Context(t)
var association ram.ResourceShareAssociation
var association awstypes.ResourceShareAssociation
resourceName := "aws_ram_principal_association.test"
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

Expand Down Expand Up @@ -82,7 +82,7 @@ func TestAccRAMPrincipalAssociation_AccountID(t *testing.T) {

func TestAccRAMPrincipalAssociation_disappears(t *testing.T) {
ctx := acctest.Context(t)
var association ram.ResourceShareAssociation
var association awstypes.ResourceShareAssociation
resourceName := "aws_ram_principal_association.test"
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)

Expand Down Expand Up @@ -140,14 +140,14 @@ func testAccPreCheckSharingWithOrganizationEnabled(ctx context.Context, t *testi
}
}

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

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

output, err := tfram.FindPrincipalAssociationByTwoPartKey(ctx, conn, rs.Primary.Attributes["resource_share_arn"], rs.Primary.Attributes[names.AttrPrincipal])

Expand All @@ -163,7 +163,7 @@ func testAccCheckPrincipalAssociationExists(ctx context.Context, n string, v *ra

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

for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_ram_principal_association" {
Expand All @@ -180,7 +180,7 @@ func testAccCheckPrincipalAssociationDestroy(ctx context.Context) resource.TestC
return err
}

return fmt.Errorf("RAM Resource Association %s still exists", rs.Primary.ID)
return fmt.Errorf("RAM Principal Association %s still exists", rs.Primary.ID)
}

return nil
Expand Down
Loading
Loading