Skip to content

Commit

Permalink
Merge pull request #38750 from DanielRieske/f/migrate-pinpoint-sdkv2
Browse files Browse the repository at this point in the history
Migrate `Pinpoint` resources to AWS SDK V2
  • Loading branch information
nam054 authored Aug 9, 2024
2 parents 7e5bba0 + c3ac013 commit ff74485
Show file tree
Hide file tree
Showing 35 changed files with 1,165 additions and 855 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ require (
github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.9.16 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.17 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.17.15 // indirect
github.com/aws/aws-sdk-go-v2/service/pinpoint v1.32.3 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.26.4 // indirect
github.com/bgentry/speakeasy v0.1.0 // indirect
github.com/boombuler/barcode v1.0.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,8 @@ github.com/aws/aws-sdk-go-v2/service/paymentcryptography v1.12.3 h1:9oQMCF4oLvWS
github.com/aws/aws-sdk-go-v2/service/paymentcryptography v1.12.3/go.mod h1:NNyvgUO7XweCVxGTSnllS6XdsD/9Il6Kc63D/stKgiM=
github.com/aws/aws-sdk-go-v2/service/pcaconnectorad v1.7.3 h1:xKVSPlN0K1r9VBe6MaKHgUi3EvJotLE9s4etstJq0jw=
github.com/aws/aws-sdk-go-v2/service/pcaconnectorad v1.7.3/go.mod h1:4Lk91jzPQQKOzml7LHOR/zAE5FF4+mL0CPrArI8vnCY=
github.com/aws/aws-sdk-go-v2/service/pinpoint v1.32.3 h1:uBukpBpEOhnT/iWfhiunEjbPTWXgurgLUr5NJlk7yJk=
github.com/aws/aws-sdk-go-v2/service/pinpoint v1.32.3/go.mod h1:yv43WVYryFKJFbThuv8mHV3zGt4RfnzB/+Om7iwRyxs=
github.com/aws/aws-sdk-go-v2/service/pipes v1.14.3 h1:fYZlFa1OvrgaFODrdf0KVDp4qCRHMZNr8S/F3aGNuno=
github.com/aws/aws-sdk-go-v2/service/pipes v1.14.3/go.mod h1:S0g2KF8IpU6Ptn46eSywrS+w1PMUwrf/xWF8szcTZ2Q=
github.com/aws/aws-sdk-go-v2/service/polly v1.42.3 h1:MuoVKFJr/TUimLdT6nvio+OehAPM7kILgNLF3rYcaP0=
Expand Down
6 changes: 3 additions & 3 deletions internal/acctest/acctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
"github.com/aws/aws-sdk-go-v2/service/inspector2"
inspector2types "github.com/aws/aws-sdk-go-v2/service/inspector2/types"
organizationstypes "github.com/aws/aws-sdk-go-v2/service/organizations/types"
"github.com/aws/aws-sdk-go-v2/service/pinpoint"
"github.com/aws/aws-sdk-go-v2/service/ssoadmin"
ssoadmintypes "github.com/aws/aws-sdk-go-v2/service/ssoadmin/types"
"github.com/aws/aws-sdk-go-v2/service/wafv2"
Expand All @@ -43,7 +44,6 @@ import (
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/service/outposts"
"github.com/aws/aws-sdk-go/service/pinpoint"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -1199,11 +1199,11 @@ func PreCheckOrganizationMemberAccountWithProvider(ctx context.Context, t *testi
}

func PreCheckPinpointApp(ctx context.Context, t *testing.T) {
conn := Provider.Meta().(*conns.AWSClient).PinpointConn(ctx)
conn := Provider.Meta().(*conns.AWSClient).PinpointClient(ctx)

input := &pinpoint.GetAppsInput{}

_, err := conn.GetAppsWithContext(ctx, input)
_, err := conn.GetApps(ctx, input)

if PreCheckSkipError(err) {
t.Skipf("skipping acceptance testing: %s", err)
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.

73 changes: 49 additions & 24 deletions internal/service/pinpoint/adm_channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,21 @@ import (
"context"
"log"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/pinpoint"
"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/pinpoint"
awstypes "github.com/aws/aws-sdk-go-v2/service/pinpoint/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-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_pinpoint_adm_channel")
func ResourceADMChannel() *schema.Resource {
// @SDKResource("aws_pinpoint_adm_channel", name="ADM Channel")
func resourceADMChannel() *schema.Resource {
return &schema.Resource{
CreateWithoutTimeout: resourceADMChannelUpsert,
ReadWithoutTimeout: resourceADMChannelRead,
Expand Down Expand Up @@ -55,11 +58,11 @@ func ResourceADMChannel() *schema.Resource {

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

applicationId := d.Get(names.AttrApplicationID).(string)

params := &pinpoint.ADMChannelRequest{}
params := &awstypes.ADMChannelRequest{}

params.ClientId = aws.String(d.Get(names.AttrClientID).(string))
params.ClientSecret = aws.String(d.Get(names.AttrClientSecret).(string))
Expand All @@ -70,7 +73,7 @@ func resourceADMChannelUpsert(ctx context.Context, d *schema.ResourceData, meta
ADMChannelRequest: params,
}

_, err := conn.UpdateAdmChannelWithContext(ctx, &req)
_, err := conn.UpdateAdmChannel(ctx, &req)
if err != nil {
return sdkdiag.AppendErrorf(diags, "updating Pinpoint ADM Channel: %s", err)
}
Expand All @@ -82,40 +85,39 @@ func resourceADMChannelUpsert(ctx context.Context, d *schema.ResourceData, meta

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

log.Printf("[INFO] Reading Pinpoint ADM Channel for application %s", d.Id())

channel, err := conn.GetAdmChannelWithContext(ctx, &pinpoint.GetAdmChannelInput{
ApplicationId: aws.String(d.Id()),
})
if err != nil {
if tfawserr.ErrCodeEquals(err, pinpoint.ErrCodeNotFoundException) {
log.Printf("[WARN] Pinpoint ADM Channel for application %s not found, removing from state", d.Id())
d.SetId("")
return diags
}
output, err := findADMChannelByApplicationId(ctx, conn, d.Id())

if !d.IsNewResource() && tfresource.NotFound(err) {
log.Printf("[WARN] Pinpoint ADM Channel (%s) not found, removing from state", d.Id())
d.SetId("")
return diags
}

return sdkdiag.AppendErrorf(diags, "getting Pinpoint ADM Channel for application %s: %s", d.Id(), err)
if err != nil {
return sdkdiag.AppendErrorf(diags, "reading Pinpoint ADM Channel (%s): %s", d.Id(), err)
}

d.Set(names.AttrApplicationID, channel.ADMChannelResponse.ApplicationId)
d.Set(names.AttrEnabled, channel.ADMChannelResponse.Enabled)
d.Set(names.AttrApplicationID, output.ApplicationId)
d.Set(names.AttrEnabled, output.Enabled)
// client_id and client_secret are never returned

return diags
}

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

log.Printf("[DEBUG] Pinpoint Delete ADM Channel: %s", d.Id())
_, err := conn.DeleteAdmChannelWithContext(ctx, &pinpoint.DeleteAdmChannelInput{
_, err := conn.DeleteAdmChannel(ctx, &pinpoint.DeleteAdmChannelInput{
ApplicationId: aws.String(d.Id()),
})

if tfawserr.ErrCodeEquals(err, pinpoint.ErrCodeNotFoundException) {
if errs.IsA[*awstypes.NotFoundException](err) {
return diags
}

Expand All @@ -124,3 +126,26 @@ func resourceADMChannelDelete(ctx context.Context, d *schema.ResourceData, meta
}
return diags
}

func findADMChannelByApplicationId(ctx context.Context, conn *pinpoint.Client, applicationId string) (*awstypes.ADMChannelResponse, error) {
input := &pinpoint.GetAdmChannelInput{
ApplicationId: aws.String(applicationId),
}

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

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

return output.ADMChannelResponse, nil
}
65 changes: 30 additions & 35 deletions internal/service/pinpoint/adm_channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (
"os"
"testing"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/pinpoint"
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
awstypes "github.com/aws/aws-sdk-go-v2/service/pinpoint/types"
"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"
tfpinpoint "github.com/hashicorp/terraform-provider-aws/internal/service/pinpoint"
"github.com/hashicorp/terraform-provider-aws/internal/tfresource"
"github.com/hashicorp/terraform-provider-aws/names"
)

Expand Down Expand Up @@ -50,7 +50,7 @@ func testAccADMChannelConfigurationFromEnv(t *testing.T) *testAccADMChannelConfi

func TestAccPinpointADMChannel_basic(t *testing.T) {
ctx := acctest.Context(t)
var channel pinpoint.ADMChannelResponse
var channel awstypes.ADMChannelResponse
resourceName := "aws_pinpoint_adm_channel.channel"

config := testAccADMChannelConfigurationFromEnv(t)
Expand Down Expand Up @@ -85,7 +85,7 @@ func TestAccPinpointADMChannel_basic(t *testing.T) {
})
}

func testAccCheckADMChannelExists(ctx context.Context, n string, channel *pinpoint.ADMChannelResponse) resource.TestCheckFunc {
func testAccCheckADMChannelExists(ctx context.Context, n string, channel *awstypes.ADMChannelResponse) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
Expand All @@ -96,61 +96,56 @@ func testAccCheckADMChannelExists(ctx context.Context, n string, channel *pinpoi
return fmt.Errorf("No Pinpoint ADM channel with that Application ID exists")
}

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

// Check if the ADM Channel exists
params := &pinpoint.GetAdmChannelInput{
ApplicationId: aws.String(rs.Primary.ID),
}
output, err := conn.GetAdmChannelWithContext(ctx, params)
output, err := tfpinpoint.FindADMChannelByApplicationId(ctx, conn, rs.Primary.ID)

if err != nil {
return err
}

*channel = *output.ADMChannelResponse
*channel = *output

return nil
}
}

func testAccADMChannelConfig_basic(conf *testAccADMChannelConfiguration) string {
return fmt.Sprintf(`
resource "aws_pinpoint_app" "test_app" {}
resource "aws_pinpoint_adm_channel" "channel" {
application_id = aws_pinpoint_app.test_app.application_id
client_id = "%s"
client_secret = "%s"
enabled = false
}
`, conf.ClientID, conf.ClientSecret)
}

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

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

// Check if the ADM channel exists by fetching its attributes
params := &pinpoint.GetAdmChannelInput{
ApplicationId: aws.String(rs.Primary.ID),
_, err := tfpinpoint.FindADMChannelByApplicationId(ctx, conn, rs.Primary.ID)

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

if err != nil {
if tfawserr.ErrCodeEquals(err, pinpoint.ErrCodeNotFoundException) {
continue
}
return err
}
return fmt.Errorf("ADM Channel exists when it should be destroyed!")

return fmt.Errorf("Pinpoint ADM Channel %s still exists", rs.Primary.ID)
}

return nil
}
}

func testAccADMChannelConfig_basic(conf *testAccADMChannelConfiguration) string {
return fmt.Sprintf(`
resource "aws_pinpoint_app" "test_app" {}
resource "aws_pinpoint_adm_channel" "channel" {
application_id = aws_pinpoint_app.test_app.application_id
client_id = "%s"
client_secret = "%s"
enabled = false
}
`, conf.ClientID, conf.ClientSecret)
}
Loading

0 comments on commit ff74485

Please sign in to comment.