Skip to content

Commit

Permalink
Merge pull request #37195 from mattburgess/detective-awssdkv2-migration
Browse files Browse the repository at this point in the history
detective: Migrate to AWS SDK v2
  • Loading branch information
jar-b committed Jul 16, 2024
2 parents 554774d + 1ad3ed2 commit 2177e08
Show file tree
Hide file tree
Showing 19 changed files with 351 additions and 259 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/datasync v1.40.3
github.com/aws/aws-sdk-go-v2/service/datazone v1.13.2
github.com/aws/aws-sdk-go-v2/service/dax v1.21.3
github.com/aws/aws-sdk-go-v2/service/detective v1.29.3
github.com/aws/aws-sdk-go-v2/service/devicefarm v1.25.2
github.com/aws/aws-sdk-go-v2/service/devopsguru v1.32.3
github.com/aws/aws-sdk-go-v2/service/directoryservice v1.27.3
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ github.com/aws/aws-sdk-go-v2/service/datazone v1.13.2 h1:9l6JiWZz/2Sp3ne9E/AXECw
github.com/aws/aws-sdk-go-v2/service/datazone v1.13.2/go.mod h1:li7vb6Ip/zyT59298XmAhs+dtXR2GqHXQlIdgL3QycE=
github.com/aws/aws-sdk-go-v2/service/dax v1.21.3 h1:uGHbOU0lBxntNZ/+Y2HbVo//AVFdl/BpMz7viHf/r8M=
github.com/aws/aws-sdk-go-v2/service/dax v1.21.3/go.mod h1:FNgKx9JXy9L0bThUl86EMV9gwUgqf2eexpitcne/AXc=
github.com/aws/aws-sdk-go-v2/service/detective v1.29.3 h1:HimZr2FJaLzxinq9QypFY2gGM+40pMWPwxB+ZNTkfNI=
github.com/aws/aws-sdk-go-v2/service/detective v1.29.3/go.mod h1:fiEtdUerGX5RHS/upeHldpHKikvfQz1MJCgquNFQeDo=
github.com/aws/aws-sdk-go-v2/service/devicefarm v1.25.2 h1:DSv0r8nKo8+ix2h5Rz/Zl62kkJPRxXIEQzmRI3CQVpY=
github.com/aws/aws-sdk-go-v2/service/devicefarm v1.25.2/go.mod h1:7Ev/BlW5/zbURomHu/2Ay8l/HAgoQAbaSP2XlMUED9I=
github.com/aws/aws-sdk-go-v2/service/devopsguru v1.32.3 h1:dVk+ogfz83rhZLaWSwSbgTQnxno+DIhZ3Q3KFdxTVmA=
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.

2 changes: 1 addition & 1 deletion internal/service/detective/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 -ServiceTagsMap -ListTags -UpdateTags
//go:generate go run ../../generate/tags/main.go -AWSSDKVersion=2 -KVTValues -ListTags -ServiceTagsMap -SkipTypesImp -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
59 changes: 29 additions & 30 deletions internal/service/detective/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ import (
"log"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/detective"
"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/detective"
awstypes "github.com/aws/aws-sdk-go-v2/service/detective/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"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"
tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
Expand Down Expand Up @@ -58,29 +59,29 @@ func resourceGraphCreate(ctx context.Context, d *schema.ResourceData, meta inter
const (
timeout = 4 * time.Minute
)
conn := meta.(*conns.AWSClient).DetectiveConn(ctx)
conn := meta.(*conns.AWSClient).DetectiveClient(ctx)

input := &detective.CreateGraphInput{
Tags: getTagsIn(ctx),
}

outputRaw, err := tfresource.RetryWhenAWSErrCodeEquals(ctx, timeout, func() (interface{}, error) {
return conn.CreateGraphWithContext(ctx, input)
}, detective.ErrCodeInternalServerException)
outputRaw, err := tfresource.RetryWhenIsA[*awstypes.InternalServerException](ctx, timeout, func() (interface{}, error) {
return conn.CreateGraph(ctx, input)
})

if err != nil {
return sdkdiag.AppendErrorf(diags, "creating Detective Graph: %s", err)
}

d.SetId(aws.StringValue(outputRaw.(*detective.CreateGraphOutput).GraphArn))
d.SetId(aws.ToString(outputRaw.(*detective.CreateGraphOutput).GraphArn))

return append(diags, resourceGraphRead(ctx, d, meta)...)
}

func resourceGraphRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

conn := meta.(*conns.AWSClient).DetectiveConn(ctx)
conn := meta.(*conns.AWSClient).DetectiveClient(ctx)

graph, err := FindGraphByARN(ctx, conn, d.Id())

Expand All @@ -94,7 +95,7 @@ func resourceGraphRead(ctx context.Context, d *schema.ResourceData, meta interfa
return sdkdiag.AppendErrorf(diags, "reading Detective Graph (%s): %s", d.Id(), err)
}

d.Set(names.AttrCreatedTime, aws.TimeValue(graph.CreatedTime).Format(time.RFC3339))
d.Set(names.AttrCreatedTime, aws.ToTime(graph.CreatedTime).Format(time.RFC3339))
d.Set("graph_arn", graph.Arn)

return diags
Expand All @@ -108,14 +109,14 @@ func resourceGraphUpdate(ctx context.Context, d *schema.ResourceData, meta inter
func resourceGraphDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

conn := meta.(*conns.AWSClient).DetectiveConn(ctx)
conn := meta.(*conns.AWSClient).DetectiveClient(ctx)

log.Printf("[DEBUG] Deleting Detective Graph: %s", d.Id())
_, err := conn.DeleteGraphWithContext(ctx, &detective.DeleteGraphInput{
_, err := conn.DeleteGraph(ctx, &detective.DeleteGraphInput{
GraphArn: aws.String(d.Id()),
})

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

Expand All @@ -126,43 +127,41 @@ func resourceGraphDelete(ctx context.Context, d *schema.ResourceData, meta inter
return diags
}

func FindGraphByARN(ctx context.Context, conn *detective.Detective, arn string) (*detective.Graph, error) {
func FindGraphByARN(ctx context.Context, conn *detective.Client, arn string) (*awstypes.Graph, error) {
input := &detective.ListGraphsInput{}

return findGraph(ctx, conn, input, func(v *detective.Graph) bool {
return aws.StringValue(v.Arn) == arn
return findGraph(ctx, conn, input, func(v awstypes.Graph) bool {
return aws.ToString(v.Arn) == arn
})
}

func findGraph(ctx context.Context, conn *detective.Detective, input *detective.ListGraphsInput, filter tfslices.Predicate[*detective.Graph]) (*detective.Graph, error) {
func findGraph(ctx context.Context, conn *detective.Client, input *detective.ListGraphsInput, filter tfslices.Predicate[awstypes.Graph]) (*awstypes.Graph, error) {
output, err := findGraphs(ctx, conn, input, filter)

if err != nil {
return nil, err
}

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

func findGraphs(ctx context.Context, conn *detective.Detective, input *detective.ListGraphsInput, filter tfslices.Predicate[*detective.Graph]) ([]*detective.Graph, error) {
var output []*detective.Graph
func findGraphs(ctx context.Context, conn *detective.Client, input *detective.ListGraphsInput, filter tfslices.Predicate[awstypes.Graph]) ([]awstypes.Graph, error) {
var output []awstypes.Graph

pages := detective.NewListGraphsPaginator(conn, input)

err := conn.ListGraphsPagesWithContext(ctx, input, func(page *detective.ListGraphsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
for pages.HasMorePages() {
page, err := pages.NextPage(ctx)

if err != nil {
return nil, err
}

for _, v := range page.GraphList {
if v != nil && filter(v) {
if filter(v) {
output = append(output, v)
}
}

return !lastPage
})

if err != nil {
return nil, err
}

return output, nil
Expand Down
14 changes: 7 additions & 7 deletions internal/service/detective/graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"fmt"
"testing"

"github.com/aws/aws-sdk-go/service/detective"
awstypes "github.com/aws/aws-sdk-go-v2/service/detective/types"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/hashicorp/terraform-provider-aws/internal/acctest"
Expand All @@ -20,7 +20,7 @@ import (

func testAccGraph_basic(t *testing.T) {
ctx := acctest.Context(t)
var graph detective.Graph
var graph awstypes.Graph
resourceName := "aws_detective_graph.test"

resource.Test(t, resource.TestCase{
Expand All @@ -47,7 +47,7 @@ func testAccGraph_basic(t *testing.T) {

func testAccGraph_disappears(t *testing.T) {
ctx := acctest.Context(t)
var graph detective.Graph
var graph awstypes.Graph
resourceName := "aws_detective_graph.test"

resource.Test(t, resource.TestCase{
Expand All @@ -70,7 +70,7 @@ func testAccGraph_disappears(t *testing.T) {

func testAccGraph_tags(t *testing.T) {
ctx := acctest.Context(t)
var graph detective.Graph
var graph awstypes.Graph
resourceName := "aws_detective_graph.test"

resource.Test(t, resource.TestCase{
Expand Down Expand Up @@ -116,7 +116,7 @@ func testAccGraph_tags(t *testing.T) {

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

for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_detective_graph" {
Expand All @@ -140,14 +140,14 @@ func testAccCheckGraphDestroy(ctx context.Context) resource.TestCheckFunc {
}
}

func testAccCheckGraphExists(ctx context.Context, n string, v *detective.Graph) resource.TestCheckFunc {
func testAccCheckGraphExists(ctx context.Context, n string, v *awstypes.Graph) 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).DetectiveConn(ctx)
conn := acctest.Provider.Meta().(*conns.AWSClient).DetectiveClient(ctx)

output, err := tfdetective.FindGraphByARN(ctx, conn, rs.Primary.ID)

Expand Down
51 changes: 25 additions & 26 deletions internal/service/detective/invitation_accepter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import (
"context"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/detective"
"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/detective"
awstypes "github.com/aws/aws-sdk-go-v2/service/detective/types"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"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"
tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
Expand Down Expand Up @@ -44,14 +45,14 @@ func ResourceInvitationAccepter() *schema.Resource {
func resourceInvitationAccepterCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

conn := meta.(*conns.AWSClient).DetectiveConn(ctx)
conn := meta.(*conns.AWSClient).DetectiveClient(ctx)

graphARN := d.Get("graph_arn").(string)
input := &detective.AcceptInvitationInput{
GraphArn: aws.String(graphARN),
}

_, err := conn.AcceptInvitationWithContext(ctx, input)
_, err := conn.AcceptInvitation(ctx, input)

if err != nil {
return sdkdiag.AppendErrorf(diags, "accepting Detective Invitation (%s): %s", graphARN, err)
Expand All @@ -65,7 +66,7 @@ func resourceInvitationAccepterCreate(ctx context.Context, d *schema.ResourceDat
func resourceInvitationAccepterRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

conn := meta.(*conns.AWSClient).DetectiveConn(ctx)
conn := meta.(*conns.AWSClient).DetectiveClient(ctx)

member, err := FindInvitationByGraphARN(ctx, conn, d.Id())

Expand All @@ -87,14 +88,14 @@ func resourceInvitationAccepterRead(ctx context.Context, d *schema.ResourceData,
func resourceInvitationAccepterDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics

conn := meta.(*conns.AWSClient).DetectiveConn(ctx)
conn := meta.(*conns.AWSClient).DetectiveClient(ctx)

log.Printf("[DEBUG] Deleting Detective Invitation Accepter: %s", d.Id())
_, err := conn.DisassociateMembershipWithContext(ctx, &detective.DisassociateMembershipInput{
_, err := conn.DisassociateMembership(ctx, &detective.DisassociateMembershipInput{
GraphArn: aws.String(d.Id()),
})

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

Expand All @@ -105,43 +106,41 @@ func resourceInvitationAccepterDelete(ctx context.Context, d *schema.ResourceDat
return diags
}

func FindInvitationByGraphARN(ctx context.Context, conn *detective.Detective, graphARN string) (*detective.MemberDetail, error) {
func FindInvitationByGraphARN(ctx context.Context, conn *detective.Client, graphARN string) (*awstypes.MemberDetail, error) {
input := &detective.ListInvitationsInput{}

return findInvitation(ctx, conn, input, func(v *detective.MemberDetail) bool {
return aws.StringValue(v.GraphArn) == graphARN
return findInvitation(ctx, conn, input, func(v awstypes.MemberDetail) bool {
return aws.ToString(v.GraphArn) == graphARN
})
}

func findInvitation(ctx context.Context, conn *detective.Detective, input *detective.ListInvitationsInput, filter tfslices.Predicate[*detective.MemberDetail]) (*detective.MemberDetail, error) {
func findInvitation(ctx context.Context, conn *detective.Client, input *detective.ListInvitationsInput, filter tfslices.Predicate[awstypes.MemberDetail]) (*awstypes.MemberDetail, error) {
output, err := findInvitations(ctx, conn, input, filter)

if err != nil {
return nil, err
}

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

func findInvitations(ctx context.Context, conn *detective.Detective, input *detective.ListInvitationsInput, filter tfslices.Predicate[*detective.MemberDetail]) ([]*detective.MemberDetail, error) {
var output []*detective.MemberDetail
func findInvitations(ctx context.Context, conn *detective.Client, input *detective.ListInvitationsInput, filter tfslices.Predicate[awstypes.MemberDetail]) ([]awstypes.MemberDetail, error) {
var output []awstypes.MemberDetail

err := conn.ListInvitationsPagesWithContext(ctx, input, func(page *detective.ListInvitationsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
pages := detective.NewListInvitationsPaginator(conn, input)

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

if err != nil {
return nil, err
}

for _, v := range page.Invitations {
if v != nil && filter(v) {
if filter(v) {
output = append(output, v)
}
}

return !lastPage
})

if err != nil {
return nil, err
}

return output, nil
Expand Down
4 changes: 2 additions & 2 deletions internal/service/detective/invitation_accepter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func testAccCheckInvitationAccepterExists(ctx context.Context, n string) resourc
return fmt.Errorf("Not found: %s", n)
}

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

_, err := tfdetective.FindInvitationByGraphARN(ctx, conn, rs.Primary.ID)

Expand All @@ -63,7 +63,7 @@ func testAccCheckInvitationAccepterExists(ctx context.Context, n string) resourc

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

for _, rs := range s.RootModule().Resources {
if rs.Type != "aws_detective_invitation_accepter" {
Expand Down
Loading

0 comments on commit 2177e08

Please sign in to comment.