From b5d9a76d88b37ed99cbda10d5470e2ce63eada3a Mon Sep 17 00:00:00 2001 From: Daniel Rieske Date: Wed, 7 Aug 2024 21:08:34 +0200 Subject: [PATCH 01/19] feat: generate `v2` client --- go.mod | 1 + go.sum | 2 + internal/conns/awsclient_gen.go | 6 +- internal/service/pinpoint/generate.go | 2 +- .../pinpoint/service_endpoint_resolver_gen.go | 66 ++++---- .../pinpoint/service_endpoints_gen_test.go | 146 +++++++++++++++--- .../service/pinpoint/service_package_gen.go | 60 +++---- internal/service/pinpoint/tags_gen.go | 36 ++--- names/data/names_data.hcl | 2 +- 9 files changed, 213 insertions(+), 108 deletions(-) diff --git a/go.mod b/go.mod index 8bfbcc32d43..e5254638008 100644 --- a/go.mod +++ b/go.mod @@ -289,6 +289,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 diff --git a/go.sum b/go.sum index 40b6a11ab8d..6d07af4a59e 100644 --- a/go.sum +++ b/go.sum @@ -358,6 +358,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= diff --git a/internal/conns/awsclient_gen.go b/internal/conns/awsclient_gen.go index 5ab8aef9a39..c8bd9e6a812 100644 --- a/internal/conns/awsclient_gen.go +++ b/internal/conns/awsclient_gen.go @@ -156,6 +156,7 @@ import ( osis_sdkv2 "github.com/aws/aws-sdk-go-v2/service/osis" paymentcryptography_sdkv2 "github.com/aws/aws-sdk-go-v2/service/paymentcryptography" pcaconnectorad_sdkv2 "github.com/aws/aws-sdk-go-v2/service/pcaconnectorad" + pinpoint_sdkv2 "github.com/aws/aws-sdk-go-v2/service/pinpoint" pipes_sdkv2 "github.com/aws/aws-sdk-go-v2/service/pipes" polly_sdkv2 "github.com/aws/aws-sdk-go-v2/service/polly" pricing_sdkv2 "github.com/aws/aws-sdk-go-v2/service/pricing" @@ -236,7 +237,6 @@ import ( opensearchservice_sdkv1 "github.com/aws/aws-sdk-go/service/opensearchservice" opsworks_sdkv1 "github.com/aws/aws-sdk-go/service/opsworks" outposts_sdkv1 "github.com/aws/aws-sdk-go/service/outposts" - pinpoint_sdkv1 "github.com/aws/aws-sdk-go/service/pinpoint" quicksight_sdkv1 "github.com/aws/aws-sdk-go/service/quicksight" rds_sdkv1 "github.com/aws/aws-sdk-go/service/rds" redshift_sdkv1 "github.com/aws/aws-sdk-go/service/redshift" @@ -933,8 +933,8 @@ func (c *AWSClient) PaymentCryptographyClient(ctx context.Context) *paymentcrypt return errs.Must(client[*paymentcryptography_sdkv2.Client](ctx, c, names.PaymentCryptography, make(map[string]any))) } -func (c *AWSClient) PinpointConn(ctx context.Context) *pinpoint_sdkv1.Pinpoint { - return errs.Must(conn[*pinpoint_sdkv1.Pinpoint](ctx, c, names.Pinpoint, make(map[string]any))) +func (c *AWSClient) PinpointClient(ctx context.Context) *pinpoint_sdkv2.Client { + return errs.Must(client[*pinpoint_sdkv2.Client](ctx, c, names.Pinpoint, make(map[string]any))) } func (c *AWSClient) PipesClient(ctx context.Context) *pipes_sdkv2.Client { diff --git a/internal/service/pinpoint/generate.go b/internal/service/pinpoint/generate.go index b52ebd52b1a..72d3614d352 100644 --- a/internal/service/pinpoint/generate.go +++ b/internal/service/pinpoint/generate.go @@ -1,7 +1,7 @@ // Copyright (c) HashiCorp, Inc. // SPDX-License-Identifier: MPL-2.0 -//go:generate go run ../../generate/tags/main.go -ListTags -ListTagsOutTagsElem=TagsModel.Tags -ServiceTagsMap "-TagInCustomVal=&pinpoint.TagsModel{Tags: Tags(updatedTags.IgnoreAWS())}" -TagInTagsElem=TagsModel -UpdateTags +//go:generate go run ../../generate/tags/main.go -AWSSDKVersion=2 -ListTags -ListTagsOutTagsElem=TagsModel.Tags -ServiceTagsMap "-TagInCustomVal=&awstypes.TagsModel{Tags: Tags(updatedTags.IgnoreAWS())}" -TagInTagsElem=TagsModel -UpdateTags -KVTValues //go:generate go run ../../generate/servicepackage/main.go // ONLY generate directives and package declaration! Do not add anything else to this file. diff --git a/internal/service/pinpoint/service_endpoint_resolver_gen.go b/internal/service/pinpoint/service_endpoint_resolver_gen.go index 59d160d67cb..f6005571bad 100644 --- a/internal/service/pinpoint/service_endpoint_resolver_gen.go +++ b/internal/service/pinpoint/service_endpoint_resolver_gen.go @@ -6,65 +6,63 @@ import ( "context" "fmt" "net" - "net/url" - endpoints_sdkv1 "github.com/aws/aws-sdk-go/aws/endpoints" + aws_sdkv2 "github.com/aws/aws-sdk-go-v2/aws" + pinpoint_sdkv2 "github.com/aws/aws-sdk-go-v2/service/pinpoint" + smithyendpoints "github.com/aws/smithy-go/endpoints" "github.com/hashicorp/terraform-plugin-log/tflog" "github.com/hashicorp/terraform-provider-aws/internal/errs" ) -var _ endpoints_sdkv1.Resolver = resolverSDKv1{} +var _ pinpoint_sdkv2.EndpointResolverV2 = resolverSDKv2{} -type resolverSDKv1 struct { - ctx context.Context +type resolverSDKv2 struct { + defaultResolver pinpoint_sdkv2.EndpointResolverV2 } -func newEndpointResolverSDKv1(ctx context.Context) resolverSDKv1 { - return resolverSDKv1{ - ctx: ctx, +func newEndpointResolverSDKv2() resolverSDKv2 { + return resolverSDKv2{ + defaultResolver: pinpoint_sdkv2.NewDefaultEndpointResolverV2(), } } -func (r resolverSDKv1) EndpointFor(service, region string, opts ...func(*endpoints_sdkv1.Options)) (endpoint endpoints_sdkv1.ResolvedEndpoint, err error) { - ctx := r.ctx +func (r resolverSDKv2) ResolveEndpoint(ctx context.Context, params pinpoint_sdkv2.EndpointParameters) (endpoint smithyendpoints.Endpoint, err error) { + params = params.WithDefaults() + useFIPS := aws_sdkv2.ToBool(params.UseFIPS) - var opt endpoints_sdkv1.Options - opt.Set(opts...) - - useFIPS := opt.UseFIPSEndpoint == endpoints_sdkv1.FIPSEndpointStateEnabled + if eps := params.Endpoint; aws_sdkv2.ToString(eps) != "" { + tflog.Debug(ctx, "setting endpoint", map[string]any{ + "tf_aws.endpoint": endpoint, + }) - defaultResolver := endpoints_sdkv1.DefaultResolver() + if useFIPS { + tflog.Debug(ctx, "endpoint set, ignoring UseFIPSEndpoint setting") + params.UseFIPS = aws_sdkv2.Bool(false) + } - if useFIPS { + return r.defaultResolver.ResolveEndpoint(ctx, params) + } else if useFIPS { ctx = tflog.SetField(ctx, "tf_aws.use_fips", useFIPS) - endpoint, err = defaultResolver.EndpointFor(service, region, opts...) + endpoint, err = r.defaultResolver.ResolveEndpoint(ctx, params) if err != nil { return endpoint, err } tflog.Debug(ctx, "endpoint resolved", map[string]any{ - "tf_aws.endpoint": endpoint.URL, + "tf_aws.endpoint": endpoint.URI.String(), }) - var endpointURL *url.URL - endpointURL, err = url.Parse(endpoint.URL) - if err != nil { - return endpoint, err - } - - hostname := endpointURL.Hostname() + hostname := endpoint.URI.Hostname() _, err = net.LookupHost(hostname) if err != nil { if dnsErr, ok := errs.As[*net.DNSError](err); ok && dnsErr.IsNotFound { tflog.Debug(ctx, "default endpoint host not found, disabling FIPS", map[string]any{ "tf_aws.hostname": hostname, }) - opts = append(opts, func(o *endpoints_sdkv1.Options) { - o.UseFIPSEndpoint = endpoints_sdkv1.FIPSEndpointStateDisabled - }) + params.UseFIPS = aws_sdkv2.Bool(false) } else { - err = fmt.Errorf("looking up accessanalyzer endpoint %q: %s", hostname, err) + err = fmt.Errorf("looking up pinpoint endpoint %q: %s", hostname, err) return } } else { @@ -72,5 +70,13 @@ func (r resolverSDKv1) EndpointFor(service, region string, opts ...func(*endpoin } } - return defaultResolver.EndpointFor(service, region, opts...) + return r.defaultResolver.ResolveEndpoint(ctx, params) +} + +func withBaseEndpoint(endpoint string) func(*pinpoint_sdkv2.Options) { + return func(o *pinpoint_sdkv2.Options) { + if endpoint != "" { + o.BaseEndpoint = aws_sdkv2.String(endpoint) + } + } } diff --git a/internal/service/pinpoint/service_endpoints_gen_test.go b/internal/service/pinpoint/service_endpoints_gen_test.go index 5bf7bed558c..a0134fe633e 100644 --- a/internal/service/pinpoint/service_endpoints_gen_test.go +++ b/internal/service/pinpoint/service_endpoints_gen_test.go @@ -4,18 +4,22 @@ package pinpoint_test import ( "context" + "errors" "fmt" "maps" "net" "net/url" "os" "path/filepath" + "reflect" "strings" "testing" - aws_sdkv1 "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/aws/endpoints" - pinpoint_sdkv1 "github.com/aws/aws-sdk-go/service/pinpoint" + aws_sdkv2 "github.com/aws/aws-sdk-go-v2/aws" + awsmiddleware "github.com/aws/aws-sdk-go-v2/aws/middleware" + pinpoint_sdkv2 "github.com/aws/aws-sdk-go-v2/service/pinpoint" + "github.com/aws/smithy-go/middleware" + smithyhttp "github.com/aws/smithy-go/transport/http" "github.com/google/go-cmp/cmp" "github.com/hashicorp/aws-sdk-go-base/v2/servicemocks" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" @@ -240,54 +244,63 @@ func TestEndpointConfiguration(t *testing.T) { //nolint:paralleltest // uses t.S } func defaultEndpoint(region string) (url.URL, error) { - r := endpoints.DefaultResolver() + r := pinpoint_sdkv2.NewDefaultEndpointResolverV2() - ep, err := r.EndpointFor(pinpoint_sdkv1.EndpointsID, region) + ep, err := r.ResolveEndpoint(context.Background(), pinpoint_sdkv2.EndpointParameters{ + Region: aws_sdkv2.String(region), + }) if err != nil { return url.URL{}, err } - url, _ := url.Parse(ep.URL) - - if url.Path == "" { - url.Path = "/" + if ep.URI.Path == "" { + ep.URI.Path = "/" } - return *url, nil + return ep.URI, nil } func defaultFIPSEndpoint(region string) (url.URL, error) { - r := endpoints.DefaultResolver() + r := pinpoint_sdkv2.NewDefaultEndpointResolverV2() - ep, err := r.EndpointFor(pinpoint_sdkv1.EndpointsID, region, func(opt *endpoints.Options) { - opt.UseFIPSEndpoint = endpoints.FIPSEndpointStateEnabled + ep, err := r.ResolveEndpoint(context.Background(), pinpoint_sdkv2.EndpointParameters{ + Region: aws_sdkv2.String(region), + UseFIPS: aws_sdkv2.Bool(true), }) if err != nil { return url.URL{}, err } - url, _ := url.Parse(ep.URL) - - if url.Path == "" { - url.Path = "/" + if ep.URI.Path == "" { + ep.URI.Path = "/" } - return *url, nil + return ep.URI, nil } func callService(ctx context.Context, t *testing.T, meta *conns.AWSClient) apiCallParams { t.Helper() - client := meta.PinpointConn(ctx) + client := meta.PinpointClient(ctx) - req, _ := client.GetAppsRequest(&pinpoint_sdkv1.GetAppsInput{}) + var result apiCallParams - req.HTTPRequest.URL.Path = "/" - - return apiCallParams{ - endpoint: req.HTTPRequest.URL.String(), - region: aws_sdkv1.StringValue(client.Config.Region), + _, err := client.GetApps(ctx, &pinpoint_sdkv2.GetAppsInput{}, + func(opts *pinpoint_sdkv2.Options) { + opts.APIOptions = append(opts.APIOptions, + addRetrieveEndpointURLMiddleware(t, &result.endpoint), + addRetrieveRegionMiddleware(&result.region), + addCancelRequestMiddleware(), + ) + }, + ) + if err == nil { + t.Fatal("Expected an error, got none") + } else if !errors.Is(err, errCancelOperation) { + t.Fatalf("Unexpected error: %s", err) } + + return result } func withNoConfig(_ *caseSetup) { @@ -466,6 +479,89 @@ func testEndpointCase(t *testing.T, region string, testcase endpointTestCase, ca } } +func addRetrieveEndpointURLMiddleware(t *testing.T, endpoint *string) func(*middleware.Stack) error { + return func(stack *middleware.Stack) error { + return stack.Finalize.Add( + retrieveEndpointURLMiddleware(t, endpoint), + middleware.After, + ) + } +} + +func retrieveEndpointURLMiddleware(t *testing.T, endpoint *string) middleware.FinalizeMiddleware { + return middleware.FinalizeMiddlewareFunc( + "Test: Retrieve Endpoint", + func(ctx context.Context, in middleware.FinalizeInput, next middleware.FinalizeHandler) (middleware.FinalizeOutput, middleware.Metadata, error) { + t.Helper() + + request, ok := in.Request.(*smithyhttp.Request) + if !ok { + t.Fatalf("Expected *github.com/aws/smithy-go/transport/http.Request, got %s", fullTypeName(in.Request)) + } + + url := request.URL + url.RawQuery = "" + url.Path = "/" + + *endpoint = url.String() + + return next.HandleFinalize(ctx, in) + }) +} + +func addRetrieveRegionMiddleware(region *string) func(*middleware.Stack) error { + return func(stack *middleware.Stack) error { + return stack.Serialize.Add( + retrieveRegionMiddleware(region), + middleware.After, + ) + } +} + +func retrieveRegionMiddleware(region *string) middleware.SerializeMiddleware { + return middleware.SerializeMiddlewareFunc( + "Test: Retrieve Region", + func(ctx context.Context, in middleware.SerializeInput, next middleware.SerializeHandler) (middleware.SerializeOutput, middleware.Metadata, error) { + *region = awsmiddleware.GetRegion(ctx) + + return next.HandleSerialize(ctx, in) + }, + ) +} + +var errCancelOperation = fmt.Errorf("Test: Canceling request") + +func addCancelRequestMiddleware() func(*middleware.Stack) error { + return func(stack *middleware.Stack) error { + return stack.Finalize.Add( + cancelRequestMiddleware(), + middleware.After, + ) + } +} + +// cancelRequestMiddleware creates a Smithy middleware that intercepts the request before sending and cancels it +func cancelRequestMiddleware() middleware.FinalizeMiddleware { + return middleware.FinalizeMiddlewareFunc( + "Test: Cancel Requests", + func(_ context.Context, in middleware.FinalizeInput, next middleware.FinalizeHandler) (middleware.FinalizeOutput, middleware.Metadata, error) { + return middleware.FinalizeOutput{}, middleware.Metadata{}, errCancelOperation + }) +} + +func fullTypeName(i interface{}) string { + return fullValueTypeName(reflect.ValueOf(i)) +} + +func fullValueTypeName(v reflect.Value) string { + if v.Kind() == reflect.Ptr { + return "*" + fullValueTypeName(reflect.Indirect(v)) + } + + requestType := v.Type() + return fmt.Sprintf("%s.%s", requestType.PkgPath(), requestType.Name()) +} + func generateSharedConfigFile(config configFile) string { var buf strings.Builder diff --git a/internal/service/pinpoint/service_package_gen.go b/internal/service/pinpoint/service_package_gen.go index 08d563cff4a..00a4870d71c 100644 --- a/internal/service/pinpoint/service_package_gen.go +++ b/internal/service/pinpoint/service_package_gen.go @@ -5,10 +5,8 @@ package pinpoint import ( "context" - aws_sdkv1 "github.com/aws/aws-sdk-go/aws" - session_sdkv1 "github.com/aws/aws-sdk-go/aws/session" - pinpoint_sdkv1 "github.com/aws/aws-sdk-go/service/pinpoint" - "github.com/hashicorp/terraform-plugin-log/tflog" + aws_sdkv2 "github.com/aws/aws-sdk-go-v2/aws" + pinpoint_sdkv2 "github.com/aws/aws-sdk-go-v2/service/pinpoint" "github.com/hashicorp/terraform-provider-aws/internal/conns" "github.com/hashicorp/terraform-provider-aws/internal/types" "github.com/hashicorp/terraform-provider-aws/names" @@ -31,27 +29,32 @@ func (p *servicePackage) SDKDataSources(ctx context.Context) []*types.ServicePac func (p *servicePackage) SDKResources(ctx context.Context) []*types.ServicePackageSDKResource { return []*types.ServicePackageSDKResource{ { - Factory: ResourceADMChannel, + Factory: resourceADMChannel, TypeName: "aws_pinpoint_adm_channel", + Name: "ADM Channel", }, { - Factory: ResourceAPNSChannel, + Factory: resourceAPNSChannel, TypeName: "aws_pinpoint_apns_channel", + Name: "APNS Channel", }, { - Factory: ResourceAPNSSandboxChannel, + Factory: resourceAPNSSandboxChannel, TypeName: "aws_pinpoint_apns_sandbox_channel", + Name: "APNS Sandbox Channel", }, { - Factory: ResourceAPNSVoIPChannel, + Factory: resourceAPNSVoIPChannel, TypeName: "aws_pinpoint_apns_voip_channel", + Name: "APNS VoIP Channel", }, { - Factory: ResourceAPNSVoIPSandboxChannel, + Factory: resourceAPNSVoIPSandboxChannel, TypeName: "aws_pinpoint_apns_voip_sandbox_channel", + Name: "APNS VoIP Sandbox Channel", }, { - Factory: ResourceApp, + Factory: resourceApp, TypeName: "aws_pinpoint_app", Name: "App", Tags: &types.ServicePackageResourceTags{ @@ -59,24 +62,29 @@ func (p *servicePackage) SDKResources(ctx context.Context) []*types.ServicePacka }, }, { - Factory: ResourceBaiduChannel, + Factory: resourceBaiduChannel, TypeName: "aws_pinpoint_baidu_channel", + Name: "Baidu Channel", }, { - Factory: ResourceEmailChannel, + Factory: resourceEmailChannel, TypeName: "aws_pinpoint_email_channel", + Name: "Email Channel", }, { - Factory: ResourceEventStream, + Factory: resourceEventStream, TypeName: "aws_pinpoint_event_stream", + Name: "Event Stream", }, { - Factory: ResourceGCMChannel, + Factory: resourceGCMChannel, TypeName: "aws_pinpoint_gcm_channel", + Name: "GCM Channel", }, { - Factory: ResourceSMSChannel, + Factory: resourceSMSChannel, TypeName: "aws_pinpoint_sms_channel", + Name: "SMS Channel", }, } } @@ -85,22 +93,14 @@ func (p *servicePackage) ServicePackageName() string { return names.Pinpoint } -// NewConn returns a new AWS SDK for Go v1 client for this service package's AWS API. -func (p *servicePackage) NewConn(ctx context.Context, config map[string]any) (*pinpoint_sdkv1.Pinpoint, error) { - sess := config[names.AttrSession].(*session_sdkv1.Session) +// NewClient returns a new AWS SDK for Go v2 client for this service package's AWS API. +func (p *servicePackage) NewClient(ctx context.Context, config map[string]any) (*pinpoint_sdkv2.Client, error) { + cfg := *(config["aws_sdkv2_config"].(*aws_sdkv2.Config)) - cfg := aws_sdkv1.Config{} - - if endpoint := config[names.AttrEndpoint].(string); endpoint != "" { - tflog.Debug(ctx, "setting endpoint", map[string]any{ - "tf_aws.endpoint": endpoint, - }) - cfg.Endpoint = aws_sdkv1.String(endpoint) - } else { - cfg.EndpointResolver = newEndpointResolverSDKv1(ctx) - } - - return pinpoint_sdkv1.New(sess.Copy(&cfg)), nil + return pinpoint_sdkv2.NewFromConfig(cfg, + pinpoint_sdkv2.WithEndpointResolverV2(newEndpointResolverSDKv2()), + withBaseEndpoint(config[names.AttrEndpoint].(string)), + ), nil } func ServicePackage(ctx context.Context) conns.ServicePackage { diff --git a/internal/service/pinpoint/tags_gen.go b/internal/service/pinpoint/tags_gen.go index a3761746b5d..6da55659717 100644 --- a/internal/service/pinpoint/tags_gen.go +++ b/internal/service/pinpoint/tags_gen.go @@ -5,9 +5,9 @@ import ( "context" "fmt" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/pinpoint" - "github.com/aws/aws-sdk-go/service/pinpoint/pinpointiface" + "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-log/tflog" "github.com/hashicorp/terraform-provider-aws/internal/conns" "github.com/hashicorp/terraform-provider-aws/internal/logging" @@ -19,12 +19,12 @@ import ( // listTags lists pinpoint service tags. // The identifier is typically the Amazon Resource Name (ARN), although // it may also be a different identifier depending on the service. -func listTags(ctx context.Context, conn pinpointiface.PinpointAPI, identifier string) (tftags.KeyValueTags, error) { +func listTags(ctx context.Context, conn *pinpoint.Client, identifier string, optFns ...func(*pinpoint.Options)) (tftags.KeyValueTags, error) { input := &pinpoint.ListTagsForResourceInput{ ResourceArn: aws.String(identifier), } - output, err := conn.ListTagsForResourceWithContext(ctx, input) + output, err := conn.ListTagsForResource(ctx, input, optFns...) if err != nil { return tftags.New(ctx, nil), err @@ -36,7 +36,7 @@ func listTags(ctx context.Context, conn pinpointiface.PinpointAPI, identifier st // ListTags lists pinpoint service tags and set them in Context. // It is called from outside this package. func (p *servicePackage) ListTags(ctx context.Context, meta any, identifier string) error { - tags, err := listTags(ctx, meta.(*conns.AWSClient).PinpointConn(ctx), identifier) + tags, err := listTags(ctx, meta.(*conns.AWSClient).PinpointClient(ctx), identifier) if err != nil { return err @@ -49,21 +49,21 @@ func (p *servicePackage) ListTags(ctx context.Context, meta any, identifier stri return nil } -// map[string]*string handling +// map[string]string handling // Tags returns pinpoint service tags. -func Tags(tags tftags.KeyValueTags) map[string]*string { - return aws.StringMap(tags.Map()) +func Tags(tags tftags.KeyValueTags) map[string]string { + return tags.Map() } // KeyValueTags creates tftags.KeyValueTags from pinpoint service tags. -func KeyValueTags(ctx context.Context, tags map[string]*string) tftags.KeyValueTags { +func KeyValueTags(ctx context.Context, tags map[string]string) tftags.KeyValueTags { return tftags.New(ctx, tags) } // getTagsIn returns pinpoint service tags from Context. // nil is returned if there are no input tags. -func getTagsIn(ctx context.Context) map[string]*string { +func getTagsIn(ctx context.Context) map[string]string { if inContext, ok := tftags.FromContext(ctx); ok { if tags := Tags(inContext.TagsIn.UnwrapOrDefault()); len(tags) > 0 { return tags @@ -74,7 +74,7 @@ func getTagsIn(ctx context.Context) map[string]*string { } // setTagsOut sets pinpoint service tags in Context. -func setTagsOut(ctx context.Context, tags map[string]*string) { +func setTagsOut(ctx context.Context, tags map[string]string) { if inContext, ok := tftags.FromContext(ctx); ok { inContext.TagsOut = option.Some(KeyValueTags(ctx, tags)) } @@ -83,7 +83,7 @@ func setTagsOut(ctx context.Context, tags map[string]*string) { // updateTags updates pinpoint service tags. // The identifier is typically the Amazon Resource Name (ARN), although // it may also be a different identifier depending on the service. -func updateTags(ctx context.Context, conn pinpointiface.PinpointAPI, identifier string, oldTagsMap, newTagsMap any) error { +func updateTags(ctx context.Context, conn *pinpoint.Client, identifier string, oldTagsMap, newTagsMap any, optFns ...func(*pinpoint.Options)) error { oldTags := tftags.New(ctx, oldTagsMap) newTags := tftags.New(ctx, newTagsMap) @@ -94,10 +94,10 @@ func updateTags(ctx context.Context, conn pinpointiface.PinpointAPI, identifier if len(removedTags) > 0 { input := &pinpoint.UntagResourceInput{ ResourceArn: aws.String(identifier), - TagKeys: aws.StringSlice(removedTags.Keys()), + TagKeys: removedTags.Keys(), } - _, err := conn.UntagResourceWithContext(ctx, input) + _, err := conn.UntagResource(ctx, input, optFns...) if err != nil { return fmt.Errorf("untagging resource (%s): %w", identifier, err) @@ -109,10 +109,10 @@ func updateTags(ctx context.Context, conn pinpointiface.PinpointAPI, identifier if len(updatedTags) > 0 { input := &pinpoint.TagResourceInput{ ResourceArn: aws.String(identifier), - TagsModel: &pinpoint.TagsModel{Tags: Tags(updatedTags.IgnoreAWS())}, + TagsModel: &awstypes.TagsModel{Tags: Tags(updatedTags.IgnoreAWS())}, } - _, err := conn.TagResourceWithContext(ctx, input) + _, err := conn.TagResource(ctx, input, optFns...) if err != nil { return fmt.Errorf("tagging resource (%s): %w", identifier, err) @@ -125,5 +125,5 @@ func updateTags(ctx context.Context, conn pinpointiface.PinpointAPI, identifier // UpdateTags updates pinpoint service tags. // It is called from outside this package. func (p *servicePackage) UpdateTags(ctx context.Context, meta any, identifier string, oldTags, newTags any) error { - return updateTags(ctx, meta.(*conns.AWSClient).PinpointConn(ctx), identifier, oldTags, newTags) + return updateTags(ctx, meta.(*conns.AWSClient).PinpointClient(ctx), identifier, oldTags, newTags) } diff --git a/names/data/names_data.hcl b/names/data/names_data.hcl index 760b2c2f93a..1d6158a4fc1 100644 --- a/names/data/names_data.hcl +++ b/names/data/names_data.hcl @@ -6936,7 +6936,7 @@ service "pinpoint" { sdk { id = "Pinpoint" - client_version = [1] + client_version = [2] } names { From e65144848c3f2cda1648f075e0835766bed886d8 Mon Sep 17 00:00:00 2001 From: Daniel Rieske Date: Wed, 7 Aug 2024 21:09:34 +0200 Subject: [PATCH 02/19] feat: migrate `PreCheckPinpointApp` to SDKv2 --- internal/acctest/acctest.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/acctest/acctest.go b/internal/acctest/acctest.go index 1a1490b766f..c5a071cee72 100644 --- a/internal/acctest/acctest.go +++ b/internal/acctest/acctest.go @@ -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" @@ -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" @@ -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) From c4f1c47b3e286ae2a9ce79067ef35383644e0e49 Mon Sep 17 00:00:00 2001 From: Daniel Rieske Date: Wed, 7 Aug 2024 21:09:52 +0200 Subject: [PATCH 03/19] feat: migrate r/adm_channel to SDKv2 --- internal/service/pinpoint/adm_channel.go | 73 +++++++++++++------ internal/service/pinpoint/adm_channel_test.go | 65 ++++++++--------- 2 files changed, 79 insertions(+), 59 deletions(-) diff --git a/internal/service/pinpoint/adm_channel.go b/internal/service/pinpoint/adm_channel.go index 3ad66b15b43..75cca6b9c28 100644 --- a/internal/service/pinpoint/adm_channel.go +++ b/internal/service/pinpoint/adm_channel.go @@ -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, @@ -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)) @@ -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) } @@ -82,25 +85,24 @@ 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 @@ -108,14 +110,14 @@ func resourceADMChannelRead(ctx context.Context, d *schema.ResourceData, meta in 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 } @@ -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 +} diff --git a/internal/service/pinpoint/adm_channel_test.go b/internal/service/pinpoint/adm_channel_test.go index 66997c03595..1f1f6469025 100644 --- a/internal/service/pinpoint/adm_channel_test.go +++ b/internal/service/pinpoint/adm_channel_test.go @@ -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" ) @@ -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) @@ -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 { @@ -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) +} From a07a3b190ec24d597a7fc9808c20e9df156c76e1 Mon Sep 17 00:00:00 2001 From: Daniel Rieske Date: Wed, 7 Aug 2024 21:10:04 +0200 Subject: [PATCH 04/19] feat: migrate r/apns_channel to SDKv2 --- internal/service/pinpoint/apns_channel.go | 71 +++++++++++------- .../service/pinpoint/apns_channel_test.go | 75 +++++++++---------- 2 files changed, 81 insertions(+), 65 deletions(-) diff --git a/internal/service/pinpoint/apns_channel.go b/internal/service/pinpoint/apns_channel.go index e13de5ec550..0850680ab94 100644 --- a/internal/service/pinpoint/apns_channel.go +++ b/internal/service/pinpoint/apns_channel.go @@ -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_apns_channel") -func ResourceAPNSChannel() *schema.Resource { +// @SDKResource("aws_pinpoint_apns_channel", name="APNS Channel") +func resourceAPNSChannel() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourceAPNSChannelUpsert, ReadWithoutTimeout: resourceAPNSChannelRead, @@ -91,11 +94,11 @@ func resourceAPNSChannelUpsert(ctx context.Context, d *schema.ResourceData, meta return sdkdiag.AppendErrorf(diags, "At least one set of credentials is required; either [certificate, private_key] or [bundle_id, team_id, token_key, token_key_id]") } - conn := meta.(*conns.AWSClient).PinpointConn(ctx) + conn := meta.(*conns.AWSClient).PinpointClient(ctx) applicationId := d.Get(names.AttrApplicationID).(string) - params := &pinpoint.APNSChannelRequest{} + params := &awstypes.APNSChannelRequest{} params.DefaultAuthenticationMethod = aws.String(d.Get("default_authentication_method").(string)) params.Enabled = aws.Bool(d.Get(names.AttrEnabled).(bool)) @@ -113,7 +116,7 @@ func resourceAPNSChannelUpsert(ctx context.Context, d *schema.ResourceData, meta APNSChannelRequest: params, } - _, err := conn.UpdateApnsChannelWithContext(ctx, &req) + _, err := conn.UpdateApnsChannel(ctx, &req) if err != nil { return sdkdiag.AppendErrorf(diags, "updating Pinpoint APNs Channel for Application %s: %s", applicationId, err) } @@ -125,26 +128,21 @@ func resourceAPNSChannelUpsert(ctx context.Context, d *schema.ResourceData, meta func resourceAPNSChannelRead(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 APNs Channel for Application %s", d.Id()) - output, err := conn.GetApnsChannelWithContext(ctx, &pinpoint.GetApnsChannelInput{ - ApplicationId: aws.String(d.Id()), - }) - if err != nil { - if tfawserr.ErrCodeEquals(err, pinpoint.ErrCodeNotFoundException) { - log.Printf("[WARN] Pinpoint APNs Channel for application %s not found, removing from state", d.Id()) - d.SetId("") - return diags - } + output, err := findAPNSChannelByApplicationId(ctx, conn, d.Id()) - return sdkdiag.AppendErrorf(diags, "getting Pinpoint APNs Channel for application %s: %s", d.Id(), err) + if !d.IsNewResource() && tfresource.NotFound(err) { + log.Printf("[WARN] Pinpoint APNS Channel (%s) not found, removing from state", d.Id()) + d.SetId("") + return diags } - d.Set(names.AttrApplicationID, output.APNSChannelResponse.ApplicationId) - d.Set("default_authentication_method", output.APNSChannelResponse.DefaultAuthenticationMethod) - d.Set(names.AttrEnabled, output.APNSChannelResponse.Enabled) + d.Set(names.AttrApplicationID, output.ApplicationId) + d.Set("default_authentication_method", output.DefaultAuthenticationMethod) + d.Set(names.AttrEnabled, output.Enabled) // Sensitive params are not returned return diags @@ -152,14 +150,14 @@ func resourceAPNSChannelRead(ctx context.Context, d *schema.ResourceData, meta i func resourceAPNSChannelDelete(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] Deleting Pinpoint APNs Channel: %s", d.Id()) - _, err := conn.DeleteApnsChannelWithContext(ctx, &pinpoint.DeleteApnsChannelInput{ + _, err := conn.DeleteApnsChannel(ctx, &pinpoint.DeleteApnsChannelInput{ ApplicationId: aws.String(d.Id()), }) - if tfawserr.ErrCodeEquals(err, pinpoint.ErrCodeNotFoundException) { + if errs.IsA[*awstypes.NotFoundException](err) { return diags } @@ -168,3 +166,26 @@ func resourceAPNSChannelDelete(ctx context.Context, d *schema.ResourceData, meta } return diags } + +func findAPNSChannelByApplicationId(ctx context.Context, conn *pinpoint.Client, applicationId string) (*awstypes.APNSChannelResponse, error) { + input := &pinpoint.GetApnsChannelInput{ + ApplicationId: aws.String(applicationId), + } + + output, err := conn.GetApnsChannel(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.APNSChannelResponse == nil { + return nil, tfresource.NewEmptyResultError(input) + } + + return output.APNSChannelResponse, nil +} diff --git a/internal/service/pinpoint/apns_channel_test.go b/internal/service/pinpoint/apns_channel_test.go index 442bd28bed5..2efdf338d55 100644 --- a/internal/service/pinpoint/apns_channel_test.go +++ b/internal/service/pinpoint/apns_channel_test.go @@ -11,13 +11,13 @@ import ( "strings" "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" ) @@ -97,7 +97,7 @@ func testAccAPNSChannelTokenConfigurationFromEnv(t *testing.T) *testAccAPNSChann func TestAccPinpointAPNSChannel_basicCertificate(t *testing.T) { ctx := acctest.Context(t) - var channel pinpoint.APNSChannelResponse + var channel awstypes.APNSChannelResponse resourceName := "aws_pinpoint_apns_channel.test_apns_channel" configuration := testAccAPNSChannelCertConfigurationFromEnv(t) @@ -132,7 +132,7 @@ func TestAccPinpointAPNSChannel_basicCertificate(t *testing.T) { func TestAccPinpointAPNSChannel_basicToken(t *testing.T) { ctx := acctest.Context(t) - var channel pinpoint.APNSChannelResponse + var channel awstypes.APNSChannelResponse resourceName := "aws_pinpoint_apns_channel.test_apns_channel" configuration := testAccAPNSChannelTokenConfigurationFromEnv(t) @@ -165,7 +165,7 @@ func TestAccPinpointAPNSChannel_basicToken(t *testing.T) { }) } -func testAccCheckAPNSChannelExists(ctx context.Context, n string, channel *pinpoint.APNSChannelResponse) resource.TestCheckFunc { +func testAccCheckAPNSChannelExists(ctx context.Context, n string, channel *awstypes.APNSChannelResponse) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] if !ok { @@ -176,19 +176,41 @@ func testAccCheckAPNSChannelExists(ctx context.Context, n string, channel *pinpo return fmt.Errorf("No Pinpoint APNs 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 app exists - params := &pinpoint.GetApnsChannelInput{ - ApplicationId: aws.String(rs.Primary.ID), - } - output, err := conn.GetApnsChannelWithContext(ctx, params) + output, err := tfpinpoint.FindAPNSChannelByApplicationId(ctx, conn, rs.Primary.ID) if err != nil { return err } - *channel = *output.APNSChannelResponse + *channel = *output + + return nil + } +} + +func testAccCheckAPNSChannelDestroy(ctx context.Context) resource.TestCheckFunc { + return func(s *terraform.State) error { + conn := acctest.Provider.Meta().(*conns.AWSClient).PinpointClient(ctx) + + for _, rs := range s.RootModule().Resources { + if rs.Type != "aws_pinpoint_apns_channel" { + continue + } + + _, err := tfpinpoint.FindAPNSChannelByApplicationId(ctx, conn, rs.Primary.ID) + + if tfresource.NotFound(err) { + continue + } + + if err != nil { + return err + } + + return fmt.Errorf("Pinpoint APNS Channel %s still exists", rs.Primary.ID) + } return nil } @@ -225,30 +247,3 @@ resource "aws_pinpoint_apns_channel" "test_apns_channel" { } `, conf.BundleId, conf.TeamId, conf.TokenKey, conf.TokenKeyId) } - -func testAccCheckAPNSChannelDestroy(ctx context.Context) resource.TestCheckFunc { - return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).PinpointConn(ctx) - - for _, rs := range s.RootModule().Resources { - if rs.Type != "aws_pinpoint_apns_channel" { - continue - } - - // Check if the channel exists - params := &pinpoint.GetApnsChannelInput{ - ApplicationId: aws.String(rs.Primary.ID), - } - _, err := conn.GetApnsChannelWithContext(ctx, params) - if err != nil { - if tfawserr.ErrCodeEquals(err, pinpoint.ErrCodeNotFoundException) { - continue - } - return err - } - return fmt.Errorf("APNs Channel exists when it should be destroyed!") - } - - return nil - } -} From 28fcc4a7839f2f4bc0d6ec12032eb9d51e8acf9c Mon Sep 17 00:00:00 2001 From: Daniel Rieske Date: Wed, 7 Aug 2024 21:10:20 +0200 Subject: [PATCH 05/19] feat: migrate r/apns_sandbox_channel to SDKv2 --- .../service/pinpoint/apns_sandbox_channel.go | 71 +++++++++++------- .../pinpoint/apns_sandbox_channel_test.go | 75 +++++++++---------- 2 files changed, 81 insertions(+), 65 deletions(-) diff --git a/internal/service/pinpoint/apns_sandbox_channel.go b/internal/service/pinpoint/apns_sandbox_channel.go index d9df7ab4e25..59c673ed4ae 100644 --- a/internal/service/pinpoint/apns_sandbox_channel.go +++ b/internal/service/pinpoint/apns_sandbox_channel.go @@ -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_apns_sandbox_channel") -func ResourceAPNSSandboxChannel() *schema.Resource { +// @SDKResource("aws_pinpoint_apns_sandbox_channel", name="APNS Sandbox Channel") +func resourceAPNSSandboxChannel() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourceAPNSSandboxChannelUpsert, ReadWithoutTimeout: resourceAPNSSandboxChannelRead, @@ -91,11 +94,11 @@ func resourceAPNSSandboxChannelUpsert(ctx context.Context, d *schema.ResourceDat return sdkdiag.AppendErrorf(diags, "At least one set of credentials is required; either [certificate, private_key] or [bundle_id, team_id, token_key, token_key_id]") } - conn := meta.(*conns.AWSClient).PinpointConn(ctx) + conn := meta.(*conns.AWSClient).PinpointClient(ctx) applicationId := d.Get(names.AttrApplicationID).(string) - params := &pinpoint.APNSSandboxChannelRequest{} + params := &awstypes.APNSSandboxChannelRequest{} params.DefaultAuthenticationMethod = aws.String(d.Get("default_authentication_method").(string)) params.Enabled = aws.Bool(d.Get(names.AttrEnabled).(bool)) @@ -113,7 +116,7 @@ func resourceAPNSSandboxChannelUpsert(ctx context.Context, d *schema.ResourceDat APNSSandboxChannelRequest: params, } - _, err := conn.UpdateApnsSandboxChannelWithContext(ctx, &req) + _, err := conn.UpdateApnsSandboxChannel(ctx, &req) if err != nil { return sdkdiag.AppendErrorf(diags, "updating Pinpoint APNs Sandbox Channel for Application %s: %s", applicationId, err) } @@ -125,26 +128,21 @@ func resourceAPNSSandboxChannelUpsert(ctx context.Context, d *schema.ResourceDat func resourceAPNSSandboxChannelRead(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 APNs Channel for Application %s", d.Id()) - output, err := conn.GetApnsSandboxChannelWithContext(ctx, &pinpoint.GetApnsSandboxChannelInput{ - ApplicationId: aws.String(d.Id()), - }) - if err != nil { - if tfawserr.ErrCodeEquals(err, pinpoint.ErrCodeNotFoundException) { - log.Printf("[WARN] Pinpoint APNs Sandbox Channel for application %s not found, removing from state", d.Id()) - d.SetId("") - return diags - } + output, err := findAPNSSandboxChannelByApplicationId(ctx, conn, d.Id()) - return sdkdiag.AppendErrorf(diags, "getting Pinpoint APNs Sandbox Channel for application %s: %s", d.Id(), err) + if !d.IsNewResource() && tfresource.NotFound(err) { + log.Printf("[WARN] Pinpoint APNS Sandbox Channel (%s) not found, removing from state", d.Id()) + d.SetId("") + return diags } - d.Set(names.AttrApplicationID, output.APNSSandboxChannelResponse.ApplicationId) - d.Set("default_authentication_method", output.APNSSandboxChannelResponse.DefaultAuthenticationMethod) - d.Set(names.AttrEnabled, output.APNSSandboxChannelResponse.Enabled) + d.Set(names.AttrApplicationID, output.ApplicationId) + d.Set("default_authentication_method", output.DefaultAuthenticationMethod) + d.Set(names.AttrEnabled, output.Enabled) // Sensitive params are not returned return diags @@ -152,14 +150,14 @@ func resourceAPNSSandboxChannelRead(ctx context.Context, d *schema.ResourceData, func resourceAPNSSandboxChannelDelete(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] Deleting Pinpoint APNs Sandbox Channel: %s", d.Id()) - _, err := conn.DeleteApnsSandboxChannelWithContext(ctx, &pinpoint.DeleteApnsSandboxChannelInput{ + _, err := conn.DeleteApnsSandboxChannel(ctx, &pinpoint.DeleteApnsSandboxChannelInput{ ApplicationId: aws.String(d.Id()), }) - if tfawserr.ErrCodeEquals(err, pinpoint.ErrCodeNotFoundException) { + if errs.IsA[*awstypes.NotFoundException](err) { return diags } @@ -168,3 +166,26 @@ func resourceAPNSSandboxChannelDelete(ctx context.Context, d *schema.ResourceDat } return diags } + +func findAPNSSandboxChannelByApplicationId(ctx context.Context, conn *pinpoint.Client, applicationId string) (*awstypes.APNSSandboxChannelResponse, error) { + input := &pinpoint.GetApnsSandboxChannelInput{ + ApplicationId: aws.String(applicationId), + } + + output, err := conn.GetApnsSandboxChannel(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.APNSSandboxChannelResponse == nil { + return nil, tfresource.NewEmptyResultError(input) + } + + return output.APNSSandboxChannelResponse, nil +} diff --git a/internal/service/pinpoint/apns_sandbox_channel_test.go b/internal/service/pinpoint/apns_sandbox_channel_test.go index 18f7b99c906..2dff8e2666e 100644 --- a/internal/service/pinpoint/apns_sandbox_channel_test.go +++ b/internal/service/pinpoint/apns_sandbox_channel_test.go @@ -11,13 +11,13 @@ import ( "strings" "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" ) @@ -97,7 +97,7 @@ func testAccAPNSSandboxChannelTokenConfigurationFromEnv(t *testing.T) *testAccAP func TestAccPinpointAPNSSandboxChannel_basicCertificate(t *testing.T) { ctx := acctest.Context(t) - var channel pinpoint.APNSSandboxChannelResponse + var channel awstypes.APNSSandboxChannelResponse resourceName := "aws_pinpoint_apns_sandbox_channel.test_channel" configuration := testAccAPNSSandboxChannelCertConfigurationFromEnv(t) @@ -132,7 +132,7 @@ func TestAccPinpointAPNSSandboxChannel_basicCertificate(t *testing.T) { func TestAccPinpointAPNSSandboxChannel_basicToken(t *testing.T) { ctx := acctest.Context(t) - var channel pinpoint.APNSSandboxChannelResponse + var channel awstypes.APNSSandboxChannelResponse resourceName := "aws_pinpoint_apns_sandbox_channel.test_channel" configuration := testAccAPNSSandboxChannelTokenConfigurationFromEnv(t) @@ -165,7 +165,7 @@ func TestAccPinpointAPNSSandboxChannel_basicToken(t *testing.T) { }) } -func testAccCheckAPNSSandboxChannelExists(ctx context.Context, n string, channel *pinpoint.APNSSandboxChannelResponse) resource.TestCheckFunc { +func testAccCheckAPNSSandboxChannelExists(ctx context.Context, n string, channel *awstypes.APNSSandboxChannelResponse) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] if !ok { @@ -176,19 +176,41 @@ func testAccCheckAPNSSandboxChannelExists(ctx context.Context, n string, channel return fmt.Errorf("No Pinpoint APNs 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 app exists - params := &pinpoint.GetApnsSandboxChannelInput{ - ApplicationId: aws.String(rs.Primary.ID), - } - output, err := conn.GetApnsSandboxChannelWithContext(ctx, params) + output, err := tfpinpoint.FindAPNSSandboxChannelByApplicationId(ctx, conn, rs.Primary.ID) if err != nil { return err } - *channel = *output.APNSSandboxChannelResponse + *channel = *output + + return nil + } +} + +func testAccCheckAPNSSandboxChannelDestroy(ctx context.Context) resource.TestCheckFunc { + return func(s *terraform.State) error { + conn := acctest.Provider.Meta().(*conns.AWSClient).PinpointClient(ctx) + + for _, rs := range s.RootModule().Resources { + if rs.Type != "aws_pinpoint_apns_sandbox_channel" { + continue + } + + _, err := tfpinpoint.FindAPNSChannelByApplicationId(ctx, conn, rs.Primary.ID) + + if tfresource.NotFound(err) { + continue + } + + if err != nil { + return err + } + + return fmt.Errorf("Pinpoint APNS Sandbox Channel %s still exists", rs.Primary.ID) + } return nil } @@ -225,30 +247,3 @@ resource "aws_pinpoint_apns_sandbox_channel" "test_channel" { } `, conf.BundleId, conf.TeamId, conf.TokenKey, conf.TokenKeyId) } - -func testAccCheckAPNSSandboxChannelDestroy(ctx context.Context) resource.TestCheckFunc { - return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).PinpointConn(ctx) - - for _, rs := range s.RootModule().Resources { - if rs.Type != "aws_pinpoint_apns_sandbox_channel" { - continue - } - - // Check if the channel exists - params := &pinpoint.GetApnsSandboxChannelInput{ - ApplicationId: aws.String(rs.Primary.ID), - } - _, err := conn.GetApnsSandboxChannelWithContext(ctx, params) - if err != nil { - if tfawserr.ErrCodeEquals(err, pinpoint.ErrCodeNotFoundException) { - continue - } - return err - } - return fmt.Errorf("APNs Sandbox Channel exists when it should be destroyed!") - } - - return nil - } -} From afdbdeb5c74ddd9fb00bdbf0854a5505613a0c14 Mon Sep 17 00:00:00 2001 From: Daniel Rieske Date: Wed, 7 Aug 2024 21:10:32 +0200 Subject: [PATCH 06/19] feat: migrate r/apns_voip_channel to SDKv2 --- .../service/pinpoint/apns_voip_channel.go | 79 ++++++++++++------- .../pinpoint/apns_voip_channel_test.go | 77 +++++++++--------- 2 files changed, 86 insertions(+), 70 deletions(-) diff --git a/internal/service/pinpoint/apns_voip_channel.go b/internal/service/pinpoint/apns_voip_channel.go index c25ddabff0b..a25e9f56e1d 100644 --- a/internal/service/pinpoint/apns_voip_channel.go +++ b/internal/service/pinpoint/apns_voip_channel.go @@ -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_apns_voip_channel") -func ResourceAPNSVoIPChannel() *schema.Resource { +// @SDKResource("aws_pinpoint_apns_voip_channel", name="APNS VoIP Channel") +func resourceAPNSVoIPChannel() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourceAPNSVoIPChannelUpsert, ReadWithoutTimeout: resourceAPNSVoIPChannelRead, @@ -91,11 +94,11 @@ func resourceAPNSVoIPChannelUpsert(ctx context.Context, d *schema.ResourceData, return sdkdiag.AppendErrorf(diags, "At least one set of credentials is required; either [certificate, private_key] or [bundle_id, team_id, token_key, token_key_id]") } - conn := meta.(*conns.AWSClient).PinpointConn(ctx) + conn := meta.(*conns.AWSClient).PinpointClient(ctx) applicationId := d.Get(names.AttrApplicationID).(string) - params := &pinpoint.APNSVoipChannelRequest{} + params := &awstypes.APNSVoipChannelRequest{} params.DefaultAuthenticationMethod = aws.String(d.Get("default_authentication_method").(string)) params.Enabled = aws.Bool(d.Get(names.AttrEnabled).(bool)) @@ -113,9 +116,9 @@ func resourceAPNSVoIPChannelUpsert(ctx context.Context, d *schema.ResourceData, APNSVoipChannelRequest: params, } - _, err := conn.UpdateApnsVoipChannelWithContext(ctx, &req) + _, err := conn.UpdateApnsVoipChannel(ctx, &req) if err != nil { - return sdkdiag.AppendErrorf(diags, "updating Pinpoint APNs Voip Channel for Application %s: %s", applicationId, err) + return sdkdiag.AppendErrorf(diags, "updating Pinpoint APNs VoIP Channel for Application %s: %s", applicationId, err) } d.SetId(applicationId) @@ -125,26 +128,21 @@ func resourceAPNSVoIPChannelUpsert(ctx context.Context, d *schema.ResourceData, func resourceAPNSVoIPChannelRead(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 APNs Voip Channel for Application %s", d.Id()) + log.Printf("[INFO] Reading Pinpoint APNs VoIP Channel for Application %s", d.Id()) - output, err := conn.GetApnsVoipChannelWithContext(ctx, &pinpoint.GetApnsVoipChannelInput{ - ApplicationId: aws.String(d.Id()), - }) - if err != nil { - if tfawserr.ErrCodeEquals(err, pinpoint.ErrCodeNotFoundException) { - log.Printf("[WARN] Pinpoint APNs Voip Channel for application %s not found, removing from state", d.Id()) - d.SetId("") - return diags - } + output, err := findAPNSVoIPChannelByApplicationId(ctx, conn, d.Id()) - return sdkdiag.AppendErrorf(diags, "getting Pinpoint APNs Voip Channel for application %s: %s", d.Id(), err) + if !d.IsNewResource() && tfresource.NotFound(err) { + log.Printf("[WARN] Pinpoint APNS VoIP Channel (%s) not found, removing from state", d.Id()) + d.SetId("") + return diags } - d.Set(names.AttrApplicationID, output.APNSVoipChannelResponse.ApplicationId) - d.Set("default_authentication_method", output.APNSVoipChannelResponse.DefaultAuthenticationMethod) - d.Set(names.AttrEnabled, output.APNSVoipChannelResponse.Enabled) + d.Set(names.AttrApplicationID, output.ApplicationId) + d.Set("default_authentication_method", output.DefaultAuthenticationMethod) + d.Set(names.AttrEnabled, output.Enabled) // Sensitive params are not returned return diags @@ -152,19 +150,42 @@ func resourceAPNSVoIPChannelRead(ctx context.Context, d *schema.ResourceData, me func resourceAPNSVoIPChannelDelete(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] Deleting Pinpoint APNs Voip Channel: %s", d.Id()) - _, err := conn.DeleteApnsVoipChannelWithContext(ctx, &pinpoint.DeleteApnsVoipChannelInput{ + log.Printf("[DEBUG] Deleting Pinpoint APNs VoIP Channel: %s", d.Id()) + _, err := conn.DeleteApnsVoipChannel(ctx, &pinpoint.DeleteApnsVoipChannelInput{ ApplicationId: aws.String(d.Id()), }) - if tfawserr.ErrCodeEquals(err, pinpoint.ErrCodeNotFoundException) { + if errs.IsA[*awstypes.NotFoundException](err) { return diags } if err != nil { - return sdkdiag.AppendErrorf(diags, "deleting Pinpoint APNs Voip Channel for Application %s: %s", d.Id(), err) + return sdkdiag.AppendErrorf(diags, "deleting Pinpoint APNs VoIP Channel for Application %s: %s", d.Id(), err) } return diags } + +func findAPNSVoIPChannelByApplicationId(ctx context.Context, conn *pinpoint.Client, applicationId string) (*awstypes.APNSVoipChannelResponse, error) { + input := &pinpoint.GetApnsVoipChannelInput{ + ApplicationId: aws.String(applicationId), + } + + output, err := conn.GetApnsVoipChannel(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.APNSVoipChannelResponse == nil { + return nil, tfresource.NewEmptyResultError(input) + } + + return output.APNSVoipChannelResponse, nil +} diff --git a/internal/service/pinpoint/apns_voip_channel_test.go b/internal/service/pinpoint/apns_voip_channel_test.go index 2bf8e651c76..86b7f89af7d 100644 --- a/internal/service/pinpoint/apns_voip_channel_test.go +++ b/internal/service/pinpoint/apns_voip_channel_test.go @@ -11,13 +11,13 @@ import ( "strings" "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" ) @@ -97,7 +97,7 @@ func testAccAPNSVoIPChannelTokenConfigurationFromEnv(t *testing.T) *testAccAPNSV func TestAccPinpointAPNSVoIPChannel_basicCertificate(t *testing.T) { ctx := acctest.Context(t) - var channel pinpoint.APNSVoipChannelResponse + var channel awstypes.APNSVoipChannelResponse resourceName := "aws_pinpoint_apns_voip_channel.test_channel" configuration := testAccAPNSVoIPChannelCertConfigurationFromEnv(t) @@ -132,7 +132,7 @@ func TestAccPinpointAPNSVoIPChannel_basicCertificate(t *testing.T) { func TestAccPinpointAPNSVoIPChannel_basicToken(t *testing.T) { ctx := acctest.Context(t) - var channel pinpoint.APNSVoipChannelResponse + var channel awstypes.APNSVoipChannelResponse resourceName := "aws_pinpoint_apns_voip_channel.test_channel" configuration := testAccAPNSVoIPChannelTokenConfigurationFromEnv(t) @@ -165,7 +165,7 @@ func TestAccPinpointAPNSVoIPChannel_basicToken(t *testing.T) { }) } -func testAccCheckAPNSVoIPChannelExists(ctx context.Context, n string, channel *pinpoint.APNSVoipChannelResponse) resource.TestCheckFunc { +func testAccCheckAPNSVoIPChannelExists(ctx context.Context, n string, channel *awstypes.APNSVoipChannelResponse) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] if !ok { @@ -173,22 +173,44 @@ func testAccCheckAPNSVoIPChannelExists(ctx context.Context, n string, channel *p } if rs.Primary.ID == "" { - return fmt.Errorf("No Pinpoint APNs Voip Channel with that Application ID exists") + return fmt.Errorf("No Pinpoint APNs VoIP 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 app exists - params := &pinpoint.GetApnsVoipChannelInput{ - ApplicationId: aws.String(rs.Primary.ID), - } - output, err := conn.GetApnsVoipChannelWithContext(ctx, params) + output, err := tfpinpoint.FindAPNSVoIPChannelByApplicationId(ctx, conn, rs.Primary.ID) if err != nil { return err } - *channel = *output.APNSVoipChannelResponse + *channel = *output + + return nil + } +} + +func testAccCheckAPNSVoIPChannelDestroy(ctx context.Context) resource.TestCheckFunc { + return func(s *terraform.State) error { + conn := acctest.Provider.Meta().(*conns.AWSClient).PinpointClient(ctx) + + for _, rs := range s.RootModule().Resources { + if rs.Type != "aws_pinpoint_apns_voip_channel" { + continue + } + + _, err := tfpinpoint.FindAPNSVoIPChannelByApplicationId(ctx, conn, rs.Primary.ID) + + if tfresource.NotFound(err) { + continue + } + + if err != nil { + return err + } + + return fmt.Errorf("Pinpoint APNS VoIP Channel %s still exists", rs.Primary.ID) + } return nil } @@ -225,30 +247,3 @@ resource "aws_pinpoint_apns_voip_channel" "test_channel" { } `, conf.BundleId, conf.TeamId, conf.TokenKey, conf.TokenKeyId) } - -func testAccCheckAPNSVoIPChannelDestroy(ctx context.Context) resource.TestCheckFunc { - return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).PinpointConn(ctx) - - for _, rs := range s.RootModule().Resources { - if rs.Type != "aws_pinpoint_apns_voip_channel" { - continue - } - - // Check if the channel exists - params := &pinpoint.GetApnsVoipChannelInput{ - ApplicationId: aws.String(rs.Primary.ID), - } - _, err := conn.GetApnsVoipChannelWithContext(ctx, params) - if err != nil { - if tfawserr.ErrCodeEquals(err, pinpoint.ErrCodeNotFoundException) { - continue - } - return err - } - return fmt.Errorf("APNs Voip Channel exists when it should be destroyed!") - } - - return nil - } -} From 10241bdeaa48edfd048159c4354f1ebc479c408e Mon Sep 17 00:00:00 2001 From: Daniel Rieske Date: Wed, 7 Aug 2024 21:10:46 +0200 Subject: [PATCH 07/19] feat: migrate r/apns_voip_sandbox_channel to SDKv2 --- .../pinpoint/apns_voip_sandbox_channel.go | 79 ++++++++++++------- .../apns_voip_sandbox_channel_test.go | 77 +++++++++--------- 2 files changed, 86 insertions(+), 70 deletions(-) diff --git a/internal/service/pinpoint/apns_voip_sandbox_channel.go b/internal/service/pinpoint/apns_voip_sandbox_channel.go index 39bfc6abe34..87cc5315163 100644 --- a/internal/service/pinpoint/apns_voip_sandbox_channel.go +++ b/internal/service/pinpoint/apns_voip_sandbox_channel.go @@ -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_apns_voip_sandbox_channel") -func ResourceAPNSVoIPSandboxChannel() *schema.Resource { +// @SDKResource("aws_pinpoint_apns_voip_sandbox_channel", name="APNS VoIP Sandbox Channel") +func resourceAPNSVoIPSandboxChannel() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourceAPNSVoIPSandboxChannelUpsert, ReadWithoutTimeout: resourceAPNSVoIPSandboxChannelRead, @@ -91,11 +94,11 @@ func resourceAPNSVoIPSandboxChannelUpsert(ctx context.Context, d *schema.Resourc return sdkdiag.AppendErrorf(diags, "At least one set of credentials is required; either [certificate, private_key] or [bundle_id, team_id, token_key, token_key_id]") } - conn := meta.(*conns.AWSClient).PinpointConn(ctx) + conn := meta.(*conns.AWSClient).PinpointClient(ctx) applicationId := d.Get(names.AttrApplicationID).(string) - params := &pinpoint.APNSVoipSandboxChannelRequest{} + params := &awstypes.APNSVoipSandboxChannelRequest{} params.DefaultAuthenticationMethod = aws.String(d.Get("default_authentication_method").(string)) params.Enabled = aws.Bool(d.Get(names.AttrEnabled).(bool)) @@ -113,9 +116,9 @@ func resourceAPNSVoIPSandboxChannelUpsert(ctx context.Context, d *schema.Resourc APNSVoipSandboxChannelRequest: params, } - _, err := conn.UpdateApnsVoipSandboxChannelWithContext(ctx, &req) + _, err := conn.UpdateApnsVoipSandboxChannel(ctx, &req) if err != nil { - return sdkdiag.AppendErrorf(diags, "updating Pinpoint APNs Voip Sandbox Channel for Application %s: %s", applicationId, err) + return sdkdiag.AppendErrorf(diags, "updating Pinpoint APNS VoIP Sandbox Channel for Application %s: %s", applicationId, err) } d.SetId(applicationId) @@ -125,26 +128,21 @@ func resourceAPNSVoIPSandboxChannelUpsert(ctx context.Context, d *schema.Resourc func resourceAPNSVoIPSandboxChannelRead(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 APNs Voip Sandbox Channel for Application %s", d.Id()) + log.Printf("[INFO] Reading Pinpoint APNS VoIP Sandbox Channel for Application %s", d.Id()) - output, err := conn.GetApnsVoipSandboxChannelWithContext(ctx, &pinpoint.GetApnsVoipSandboxChannelInput{ - ApplicationId: aws.String(d.Id()), - }) - if err != nil { - if tfawserr.ErrCodeEquals(err, pinpoint.ErrCodeNotFoundException) { - log.Printf("[WARN] Pinpoint APNs Voip Sandbox Channel for application %s not found, removing from state", d.Id()) - d.SetId("") - return diags - } + output, err := findAPNSVoIPSandboxChannelByApplicationId(ctx, conn, d.Id()) - return sdkdiag.AppendErrorf(diags, "getting Pinpoint APNs Voip Sandbox Channel for application %s: %s", d.Id(), err) + if !d.IsNewResource() && tfresource.NotFound(err) { + log.Printf("[WARN] Pinpoint APNS VoIP Sandbox Channel (%s) not found, removing from state", d.Id()) + d.SetId("") + return diags } - d.Set(names.AttrApplicationID, output.APNSVoipSandboxChannelResponse.ApplicationId) - d.Set("default_authentication_method", output.APNSVoipSandboxChannelResponse.DefaultAuthenticationMethod) - d.Set(names.AttrEnabled, output.APNSVoipSandboxChannelResponse.Enabled) + d.Set(names.AttrApplicationID, output.ApplicationId) + d.Set("default_authentication_method", output.DefaultAuthenticationMethod) + d.Set(names.AttrEnabled, output.Enabled) // Sensitive params are not returned return diags @@ -152,19 +150,42 @@ func resourceAPNSVoIPSandboxChannelRead(ctx context.Context, d *schema.ResourceD func resourceAPNSVoIPSandboxChannelDelete(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] Deleting Pinpoint APNs Voip Sandbox Channel: %s", d.Id()) - _, err := conn.DeleteApnsVoipSandboxChannelWithContext(ctx, &pinpoint.DeleteApnsVoipSandboxChannelInput{ + log.Printf("[DEBUG] Deleting Pinpoint APNS VoIP Sandbox Channel: %s", d.Id()) + _, err := conn.DeleteApnsVoipSandboxChannel(ctx, &pinpoint.DeleteApnsVoipSandboxChannelInput{ ApplicationId: aws.String(d.Id()), }) - if tfawserr.ErrCodeEquals(err, pinpoint.ErrCodeNotFoundException) { + if errs.IsA[*awstypes.NotFoundException](err) { return diags } if err != nil { - return sdkdiag.AppendErrorf(diags, "deleting Pinpoint APNs Voip Sandbox Channel for Application %s: %s", d.Id(), err) + return sdkdiag.AppendErrorf(diags, "deleting Pinpoint APNS VoIP Sandbox Channel for Application %s: %s", d.Id(), err) } return diags } + +func findAPNSVoIPSandboxChannelByApplicationId(ctx context.Context, conn *pinpoint.Client, applicationId string) (*awstypes.APNSVoipSandboxChannelResponse, error) { + input := &pinpoint.GetApnsVoipSandboxChannelInput{ + ApplicationId: aws.String(applicationId), + } + + output, err := conn.GetApnsVoipSandboxChannel(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.APNSVoipSandboxChannelResponse == nil { + return nil, tfresource.NewEmptyResultError(input) + } + + return output.APNSVoipSandboxChannelResponse, nil +} diff --git a/internal/service/pinpoint/apns_voip_sandbox_channel_test.go b/internal/service/pinpoint/apns_voip_sandbox_channel_test.go index d569278a53a..42cee6b462e 100644 --- a/internal/service/pinpoint/apns_voip_sandbox_channel_test.go +++ b/internal/service/pinpoint/apns_voip_sandbox_channel_test.go @@ -11,13 +11,13 @@ import ( "strings" "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" ) @@ -97,7 +97,7 @@ func testAccAPNSVoIPSandboxChannelTokenConfigurationFromEnv(t *testing.T) *testA func TestAccPinpointAPNSVoIPSandboxChannel_basicCertificate(t *testing.T) { ctx := acctest.Context(t) - var channel pinpoint.APNSVoipSandboxChannelResponse + var channel awstypes.APNSVoipSandboxChannelResponse resourceName := "aws_pinpoint_apns_voip_sandbox_channel.test_channel" configuration := testAccAPNSVoIPSandboxChannelCertConfigurationFromEnv(t) @@ -132,7 +132,7 @@ func TestAccPinpointAPNSVoIPSandboxChannel_basicCertificate(t *testing.T) { func TestAccPinpointAPNSVoIPSandboxChannel_basicToken(t *testing.T) { ctx := acctest.Context(t) - var channel pinpoint.APNSVoipSandboxChannelResponse + var channel awstypes.APNSVoipSandboxChannelResponse resourceName := "aws_pinpoint_apns_voip_sandbox_channel.test_channel" configuration := testAccAPNSVoIPSandboxChannelTokenConfigurationFromEnv(t) @@ -165,7 +165,7 @@ func TestAccPinpointAPNSVoIPSandboxChannel_basicToken(t *testing.T) { }) } -func testAccCheckAPNSVoIPSandboxChannelExists(ctx context.Context, n string, channel *pinpoint.APNSVoipSandboxChannelResponse) resource.TestCheckFunc { +func testAccCheckAPNSVoIPSandboxChannelExists(ctx context.Context, n string, channel *awstypes.APNSVoipSandboxChannelResponse) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] if !ok { @@ -173,22 +173,44 @@ func testAccCheckAPNSVoIPSandboxChannelExists(ctx context.Context, n string, cha } if rs.Primary.ID == "" { - return fmt.Errorf("No Pinpoint APNs Voip Sandbox Channel with that Application ID exists") + return fmt.Errorf("No Pinpoint APNS VoIP Sandbox 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 app exists - params := &pinpoint.GetApnsVoipSandboxChannelInput{ - ApplicationId: aws.String(rs.Primary.ID), - } - output, err := conn.GetApnsVoipSandboxChannelWithContext(ctx, params) + output, err := tfpinpoint.FindAPNSVoIPSandboxChannelByApplicationId(ctx, conn, rs.Primary.ID) if err != nil { return err } - *channel = *output.APNSVoipSandboxChannelResponse + *channel = *output + + return nil + } +} + +func testAccCheckAPNSVoIPSandboxChannelDestroy(ctx context.Context) resource.TestCheckFunc { + return func(s *terraform.State) error { + conn := acctest.Provider.Meta().(*conns.AWSClient).PinpointClient(ctx) + + for _, rs := range s.RootModule().Resources { + if rs.Type != "aws_pinpoint_apns_voip_sandbox_channel" { + continue + } + + _, err := tfpinpoint.FindAPNSVoIPSandboxChannelByApplicationId(ctx, conn, rs.Primary.ID) + + if tfresource.NotFound(err) { + continue + } + + if err != nil { + return err + } + + return fmt.Errorf("Pinpoint APNS VoIP Sandbox Channel %s still exists", rs.Primary.ID) + } return nil } @@ -225,30 +247,3 @@ resource "aws_pinpoint_apns_voip_sandbox_channel" "test_channel" { } `, conf.BundleId, conf.TeamId, conf.TokenKey, conf.TokenKeyId) } - -func testAccCheckAPNSVoIPSandboxChannelDestroy(ctx context.Context) resource.TestCheckFunc { - return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).PinpointConn(ctx) - - for _, rs := range s.RootModule().Resources { - if rs.Type != "aws_pinpoint_apns_voip_sandbox_channel" { - continue - } - - // Check if the channel exists - params := &pinpoint.GetApnsVoipSandboxChannelInput{ - ApplicationId: aws.String(rs.Primary.ID), - } - _, err := conn.GetApnsVoipSandboxChannelWithContext(ctx, params) - if err != nil { - if tfawserr.ErrCodeEquals(err, pinpoint.ErrCodeNotFoundException) { - continue - } - return err - } - return fmt.Errorf("APNs Voip Sandbox Channel exists when it should be destroyed!") - } - - return nil - } -} From 2e4c6b1f527edc130c986daef27d99776a92564f Mon Sep 17 00:00:00 2001 From: Daniel Rieske Date: Wed, 7 Aug 2024 21:10:59 +0200 Subject: [PATCH 08/19] feat: migrate r/apns_baidu_channel to SDKv2 --- internal/service/pinpoint/baidu_channel.go | 69 ++++++++++++------- .../service/pinpoint/baidu_channel_test.go | 65 ++++++++--------- 2 files changed, 75 insertions(+), 59 deletions(-) diff --git a/internal/service/pinpoint/baidu_channel.go b/internal/service/pinpoint/baidu_channel.go index 9ceb08c9432..48d1d5f2d07 100644 --- a/internal/service/pinpoint/baidu_channel.go +++ b/internal/service/pinpoint/baidu_channel.go @@ -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_baidu_channel") -func ResourceBaiduChannel() *schema.Resource { +// @SDKResource("aws_pinpoint_baidu_channel", name="Baidu Channel") +func resourceBaiduChannel() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourceBaiduChannelUpsert, ReadWithoutTimeout: resourceBaiduChannelRead, @@ -55,11 +58,11 @@ func ResourceBaiduChannel() *schema.Resource { func resourceBaiduChannelUpsert(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.BaiduChannelRequest{} + params := &awstypes.BaiduChannelRequest{} params.Enabled = aws.Bool(d.Get(names.AttrEnabled).(bool)) params.ApiKey = aws.String(d.Get("api_key").(string)) @@ -70,7 +73,7 @@ func resourceBaiduChannelUpsert(ctx context.Context, d *schema.ResourceData, met BaiduChannelRequest: params, } - _, err := conn.UpdateBaiduChannelWithContext(ctx, &req) + _, err := conn.UpdateBaiduChannel(ctx, &req) if err != nil { return sdkdiag.AppendErrorf(diags, "updating Pinpoint Baidu Channel for application %s: %s", applicationId, err) } @@ -82,25 +85,20 @@ func resourceBaiduChannelUpsert(ctx context.Context, d *schema.ResourceData, met func resourceBaiduChannelRead(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 Baidu Channel for application %s", d.Id()) - output, err := conn.GetBaiduChannelWithContext(ctx, &pinpoint.GetBaiduChannelInput{ - ApplicationId: aws.String(d.Id()), - }) - if err != nil { - if tfawserr.ErrCodeEquals(err, pinpoint.ErrCodeNotFoundException) { - log.Printf("[WARN] Pinpoint Baidu Channel for application %s not found, removing from state", d.Id()) - d.SetId("") - return diags - } + output, err := findBaiduChannelByApplicationId(ctx, conn, d.Id()) - return sdkdiag.AppendErrorf(diags, "getting Pinpoint Baidu Channel for application %s: %s", d.Id(), err) + if !d.IsNewResource() && tfresource.NotFound(err) { + log.Printf("[WARN] Pinpoint Baidu Channel (%s) not found, removing from state", d.Id()) + d.SetId("") + return diags } - d.Set(names.AttrApplicationID, output.BaiduChannelResponse.ApplicationId) - d.Set(names.AttrEnabled, output.BaiduChannelResponse.Enabled) + d.Set(names.AttrApplicationID, output.ApplicationId) + d.Set(names.AttrEnabled, output.Enabled) // ApiKey and SecretKey are never returned return diags @@ -108,14 +106,14 @@ func resourceBaiduChannelRead(ctx context.Context, d *schema.ResourceData, meta func resourceBaiduChannelDelete(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] Deleting Pinpoint Baidu Channel for application %s", d.Id()) - _, err := conn.DeleteBaiduChannelWithContext(ctx, &pinpoint.DeleteBaiduChannelInput{ + _, err := conn.DeleteBaiduChannel(ctx, &pinpoint.DeleteBaiduChannelInput{ ApplicationId: aws.String(d.Id()), }) - if tfawserr.ErrCodeEquals(err, pinpoint.ErrCodeNotFoundException) { + if errs.IsA[*awstypes.NotFoundException](err) { return diags } @@ -124,3 +122,26 @@ func resourceBaiduChannelDelete(ctx context.Context, d *schema.ResourceData, met } return diags } + +func findBaiduChannelByApplicationId(ctx context.Context, conn *pinpoint.Client, applicationId string) (*awstypes.BaiduChannelResponse, error) { + input := &pinpoint.GetBaiduChannelInput{ + ApplicationId: aws.String(applicationId), + } + + output, err := conn.GetBaiduChannel(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.BaiduChannelResponse == nil { + return nil, tfresource.NewEmptyResultError(input) + } + + return output.BaiduChannelResponse, nil +} diff --git a/internal/service/pinpoint/baidu_channel_test.go b/internal/service/pinpoint/baidu_channel_test.go index 5a99d683c3f..47b0b540d6b 100644 --- a/internal/service/pinpoint/baidu_channel_test.go +++ b/internal/service/pinpoint/baidu_channel_test.go @@ -8,19 +8,19 @@ import ( "fmt" "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" ) func TestAccPinpointBaiduChannel_basic(t *testing.T) { ctx := acctest.Context(t) - var channel pinpoint.BaiduChannelResponse + var channel awstypes.BaiduChannelResponse resourceName := "aws_pinpoint_baidu_channel.channel" apiKey := "123" @@ -61,7 +61,7 @@ func TestAccPinpointBaiduChannel_basic(t *testing.T) { }) } -func testAccCheckBaiduChannelExists(ctx context.Context, n string, channel *pinpoint.BaiduChannelResponse) resource.TestCheckFunc { +func testAccCheckBaiduChannelExists(ctx context.Context, n string, channel *awstypes.BaiduChannelResponse) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] if !ok { @@ -72,61 +72,56 @@ func testAccCheckBaiduChannelExists(ctx context.Context, n string, channel *pinp return fmt.Errorf("No Pinpoint Baidu 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 Baidu Channel exists - params := &pinpoint.GetBaiduChannelInput{ - ApplicationId: aws.String(rs.Primary.ID), - } - output, err := conn.GetBaiduChannelWithContext(ctx, params) + output, err := tfpinpoint.FindBaiduChannelByApplicationId(ctx, conn, rs.Primary.ID) if err != nil { return err } - *channel = *output.BaiduChannelResponse + *channel = *output return nil } } -func testAccBaiduChannelConfig_basic(apiKey, secretKey string) string { - return fmt.Sprintf(` -resource "aws_pinpoint_app" "test_app" {} - -resource "aws_pinpoint_baidu_channel" "channel" { - application_id = aws_pinpoint_app.test_app.application_id - - enabled = "false" - api_key = "%s" - secret_key = "%s" -} -`, apiKey, secretKey) -} - func testAccCheckBaiduChannelDestroy(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_baidu_channel" { continue } - // Check if the Baidu channel exists by fetching its attributes - params := &pinpoint.GetBaiduChannelInput{ - ApplicationId: aws.String(rs.Primary.ID), + _, err := tfpinpoint.FindBaiduChannelByApplicationId(ctx, conn, rs.Primary.ID) + + if tfresource.NotFound(err) { + continue } - _, err := conn.GetBaiduChannelWithContext(ctx, params) + if err != nil { - if tfawserr.ErrCodeEquals(err, pinpoint.ErrCodeNotFoundException) { - continue - } return err } - return fmt.Errorf("Baidu Channel exists when it should be destroyed!") + + return fmt.Errorf("Pinpoint Baidu Channel %s still exists", rs.Primary.ID) } return nil } } + +func testAccBaiduChannelConfig_basic(apiKey, secretKey string) string { + return fmt.Sprintf(` +resource "aws_pinpoint_app" "test_app" {} + +resource "aws_pinpoint_baidu_channel" "channel" { + application_id = aws_pinpoint_app.test_app.application_id + + enabled = "false" + api_key = "%s" + secret_key = "%s" +} +`, apiKey, secretKey) +} From 7fa2da44dc8568012993f524bdc7a11984a3d595 Mon Sep 17 00:00:00 2001 From: Daniel Rieske Date: Wed, 7 Aug 2024 21:11:23 +0200 Subject: [PATCH 09/19] feat: migrate r/email_channel to SDKv2 --- internal/service/pinpoint/email_channel.go | 80 ++++++++++++------- .../service/pinpoint/email_channel_test.go | 78 +++++++++--------- 2 files changed, 86 insertions(+), 72 deletions(-) diff --git a/internal/service/pinpoint/email_channel.go b/internal/service/pinpoint/email_channel.go index 0e8adbc4d18..a0c75a52a37 100644 --- a/internal/service/pinpoint/email_channel.go +++ b/internal/service/pinpoint/email_channel.go @@ -7,19 +7,22 @@ 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/internal/verify" "github.com/hashicorp/terraform-provider-aws/names" ) -// @SDKResource("aws_pinpoint_email_channel") -func ResourceEmailChannel() *schema.Resource { +// @SDKResource("aws_pinpoint_email_channel", name="Email Channel") +func resourceEmailChannel() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourceEmailChannelUpsert, ReadWithoutTimeout: resourceEmailChannelRead, @@ -68,11 +71,11 @@ func ResourceEmailChannel() *schema.Resource { func resourceEmailChannelUpsert(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.EmailChannelRequest{} + params := &awstypes.EmailChannelRequest{} params.Enabled = aws.Bool(d.Get(names.AttrEnabled).(bool)) params.FromAddress = aws.String(d.Get("from_address").(string)) @@ -91,7 +94,7 @@ func resourceEmailChannelUpsert(ctx context.Context, d *schema.ResourceData, met EmailChannelRequest: params, } - _, err := conn.UpdateEmailChannelWithContext(ctx, &req) + _, err := conn.UpdateEmailChannel(ctx, &req) if err != nil { return sdkdiag.AppendErrorf(diags, "updating Pinpoint Email Channel for application %s: %s", applicationId, err) } @@ -103,45 +106,39 @@ func resourceEmailChannelUpsert(ctx context.Context, d *schema.ResourceData, met func resourceEmailChannelRead(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 Email Channel for application %s", d.Id()) - output, err := conn.GetEmailChannelWithContext(ctx, &pinpoint.GetEmailChannelInput{ - ApplicationId: aws.String(d.Id()), - }) - if err != nil { - if tfawserr.ErrCodeEquals(err, pinpoint.ErrCodeNotFoundException) { - log.Printf("[WARN] Pinpoint Email Channel for application %s not found, removing from state", d.Id()) - d.SetId("") - return diags - } + output, err := findEmailChannelByApplicationId(ctx, conn, d.Id()) - return sdkdiag.AppendErrorf(diags, "getting Pinpoint Email Channel for application %s: %s", d.Id(), err) + if !d.IsNewResource() && tfresource.NotFound(err) { + log.Printf("[WARN] Pinpoint Email Channel (%s) not found, removing from state", d.Id()) + d.SetId("") + return diags } - res := output.EmailChannelResponse - d.Set(names.AttrApplicationID, res.ApplicationId) - d.Set(names.AttrEnabled, res.Enabled) - d.Set("from_address", res.FromAddress) - d.Set("identity", res.Identity) - d.Set(names.AttrRoleARN, res.RoleArn) - d.Set("configuration_set", res.ConfigurationSet) - d.Set("messages_per_second", res.MessagesPerSecond) + d.Set(names.AttrApplicationID, output.ApplicationId) + d.Set(names.AttrEnabled, output.Enabled) + d.Set("from_address", output.FromAddress) + d.Set("identity", output.Identity) + d.Set(names.AttrRoleARN, output.RoleArn) + d.Set("configuration_set", output.ConfigurationSet) + d.Set("messages_per_second", output.MessagesPerSecond) return diags } func resourceEmailChannelDelete(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] Deleting Pinpoint Email Channel for application %s", d.Id()) - _, err := conn.DeleteEmailChannelWithContext(ctx, &pinpoint.DeleteEmailChannelInput{ + _, err := conn.DeleteEmailChannel(ctx, &pinpoint.DeleteEmailChannelInput{ ApplicationId: aws.String(d.Id()), }) - if tfawserr.ErrCodeEquals(err, pinpoint.ErrCodeNotFoundException) { + if errs.IsA[*awstypes.NotFoundException](err) { return diags } @@ -150,3 +147,26 @@ func resourceEmailChannelDelete(ctx context.Context, d *schema.ResourceData, met } return diags } + +func findEmailChannelByApplicationId(ctx context.Context, conn *pinpoint.Client, applicationId string) (*awstypes.EmailChannelResponse, error) { + input := &pinpoint.GetEmailChannelInput{ + ApplicationId: aws.String(applicationId), + } + + output, err := conn.GetEmailChannel(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.EmailChannelResponse == nil { + return nil, tfresource.NewEmptyResultError(input) + } + + return output.EmailChannelResponse, nil +} diff --git a/internal/service/pinpoint/email_channel_test.go b/internal/service/pinpoint/email_channel_test.go index d4d75e702a3..24c121ee482 100644 --- a/internal/service/pinpoint/email_channel_test.go +++ b/internal/service/pinpoint/email_channel_test.go @@ -8,21 +8,20 @@ import ( "fmt" "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" 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" 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" ) func TestAccPinpointEmailChannel_basic(t *testing.T) { ctx := acctest.Context(t) - var channel pinpoint.EmailChannelResponse + var channel awstypes.EmailChannelResponse resourceName := "aws_pinpoint_email_channel.test" domain := acctest.RandomDomainName() @@ -66,7 +65,7 @@ func TestAccPinpointEmailChannel_basic(t *testing.T) { func TestAccPinpointEmailChannel_set(t *testing.T) { ctx := acctest.Context(t) - var channel pinpoint.EmailChannelResponse + var channel awstypes.EmailChannelResponse resourceName := "aws_pinpoint_email_channel.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) @@ -97,7 +96,7 @@ func TestAccPinpointEmailChannel_set(t *testing.T) { func TestAccPinpointEmailChannel_noRole(t *testing.T) { ctx := acctest.Context(t) - var channel pinpoint.EmailChannelResponse + var channel awstypes.EmailChannelResponse resourceName := "aws_pinpoint_email_channel.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) @@ -128,7 +127,7 @@ func TestAccPinpointEmailChannel_noRole(t *testing.T) { func TestAccPinpointEmailChannel_disappears(t *testing.T) { ctx := acctest.Context(t) - var channel pinpoint.EmailChannelResponse + var channel awstypes.EmailChannelResponse resourceName := "aws_pinpoint_email_channel.test" domain := acctest.RandomDomainName() @@ -152,7 +151,7 @@ func TestAccPinpointEmailChannel_disappears(t *testing.T) { }) } -func testAccCheckEmailChannelExists(ctx context.Context, n string, channel *pinpoint.EmailChannelResponse) resource.TestCheckFunc { +func testAccCheckEmailChannelExists(ctx context.Context, n string, channel *awstypes.EmailChannelResponse) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] if !ok { @@ -163,19 +162,41 @@ func testAccCheckEmailChannelExists(ctx context.Context, n string, channel *pinp return fmt.Errorf("No Pinpoint Email 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 app exists - params := &pinpoint.GetEmailChannelInput{ - ApplicationId: aws.String(rs.Primary.ID), - } - output, err := conn.GetEmailChannelWithContext(ctx, params) + output, err := tfpinpoint.FindEmailChannelByApplicationId(ctx, conn, rs.Primary.ID) if err != nil { return err } - *channel = *output.EmailChannelResponse + *channel = *output + + return nil + } +} + +func testAccCheckEmailChannelDestroy(ctx context.Context) resource.TestCheckFunc { + return func(s *terraform.State) error { + conn := acctest.Provider.Meta().(*conns.AWSClient).PinpointClient(ctx) + + for _, rs := range s.RootModule().Resources { + if rs.Type != "aws_pinpoint_email_channel" { + continue + } + + _, err := tfpinpoint.FindEmailChannelByApplicationId(ctx, conn, rs.Primary.ID) + + if tfresource.NotFound(err) { + continue + } + + if err != nil { + return err + } + + return fmt.Errorf("Pinpoint Email Channel %s still exists", rs.Primary.ID) + } return nil } @@ -321,30 +342,3 @@ resource "aws_ses_domain_identity" "test" { } `, domain, fromAddress, rName) } - -func testAccCheckEmailChannelDestroy(ctx context.Context) resource.TestCheckFunc { - return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).PinpointConn(ctx) - - for _, rs := range s.RootModule().Resources { - if rs.Type != "aws_pinpoint_email_channel" { - continue - } - - // Check if the event stream exists - params := &pinpoint.GetEmailChannelInput{ - ApplicationId: aws.String(rs.Primary.ID), - } - _, err := conn.GetEmailChannelWithContext(ctx, params) - if err != nil { - if tfawserr.ErrCodeEquals(err, pinpoint.ErrCodeNotFoundException) { - continue - } - return err - } - return fmt.Errorf("Email Channel exists when it should be destroyed!") - } - - return nil - } -} From 55e1ba37263776b72da82ed4b66368579ef451e2 Mon Sep 17 00:00:00 2001 From: Daniel Rieske Date: Wed, 7 Aug 2024 21:11:36 +0200 Subject: [PATCH 10/19] feat: migrate r/app to SDKv2 --- internal/service/pinpoint/app.go | 100 +++++++++++++------------- internal/service/pinpoint/app_test.go | 32 ++++----- 2 files changed, 67 insertions(+), 65 deletions(-) diff --git a/internal/service/pinpoint/app.go b/internal/service/pinpoint/app.go index b354a97cdd3..f3d005e6408 100644 --- a/internal/service/pinpoint/app.go +++ b/internal/service/pinpoint/app.go @@ -7,15 +7,17 @@ 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-plugin-sdk/v2/helper/validation" "github.com/hashicorp/terraform-provider-aws/internal/conns" "github.com/hashicorp/terraform-provider-aws/internal/create" + "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" tftags "github.com/hashicorp/terraform-provider-aws/internal/tags" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" @@ -25,7 +27,7 @@ import ( // @SDKResource("aws_pinpoint_app", name="App") // @Tags(identifierAttribute="arn") -func ResourceApp() *schema.Resource { +func resourceApp() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourceAppCreate, ReadWithoutTimeout: resourceAppRead, @@ -56,9 +58,9 @@ func ResourceApp() *schema.Resource { Optional: true, }, names.AttrMode: { - Type: schema.TypeString, - Optional: true, - ValidateFunc: validation.StringInSlice(pinpoint.Mode_Values(), false), + Type: schema.TypeString, + Optional: true, + ValidateDiagFunc: enum.Validate[awstypes.Mode](), }, "web_url": { Type: schema.TypeString, @@ -144,32 +146,32 @@ func ResourceApp() *schema.Resource { func resourceAppCreate(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) name := create.Name(d.Get(names.AttrName).(string), d.Get(names.AttrNamePrefix).(string)) input := &pinpoint.CreateAppInput{ - CreateApplicationRequest: &pinpoint.CreateApplicationRequest{ + CreateApplicationRequest: &awstypes.CreateApplicationRequest{ Name: aws.String(name), Tags: getTagsIn(ctx), }, } - output, err := conn.CreateAppWithContext(ctx, input) + output, err := conn.CreateApp(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "creating Pinpoint App (%s): %s", name, err) } - d.SetId(aws.StringValue(output.ApplicationResponse.Id)) + d.SetId(aws.ToString(output.ApplicationResponse.Id)) return append(diags, resourceAppUpdate(ctx, d, meta)...) } func resourceAppRead(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) - app, err := FindAppByID(ctx, conn, d.Id()) + app, err := findAppByID(ctx, conn, d.Id()) if !d.IsNewResource() && tfresource.NotFound(err) { log.Printf("[WARN] Pinpoint App (%s) not found, removing from state", d.Id()) @@ -196,7 +198,7 @@ func resourceAppRead(ctx context.Context, d *schema.ResourceData, meta interface return sdkdiag.AppendErrorf(diags, "setting limits: %s", err) } d.Set(names.AttrName, app.Name) - d.Set(names.AttrNamePrefix, create.NamePrefixFromName(aws.StringValue(app.Name))) + d.Set(names.AttrNamePrefix, create.NamePrefixFromName(aws.ToString(app.Name))) if err := d.Set("quiet_time", flattenQuietTime(settings.QuietTime)); err != nil { return sdkdiag.AppendErrorf(diags, "setting quiet_time: %s", err) } @@ -206,10 +208,10 @@ func resourceAppRead(ctx context.Context, d *schema.ResourceData, meta interface func resourceAppUpdate(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) if d.HasChangesExcept(names.AttrTags, names.AttrTagsAll) { - appSettings := &pinpoint.WriteApplicationSettingsRequest{} + appSettings := &awstypes.WriteApplicationSettingsRequest{} if d.HasChange("campaign_hook") { appSettings.CampaignHook = expandCampaignHook(d.Get("campaign_hook").([]interface{})) @@ -232,7 +234,7 @@ func resourceAppUpdate(ctx context.Context, d *schema.ResourceData, meta interfa WriteApplicationSettingsRequest: appSettings, } - _, err := conn.UpdateApplicationSettingsWithContext(ctx, input) + _, err := conn.UpdateApplicationSettings(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "updating Pinpoint App (%s) settings: %s", d.Id(), err) @@ -244,14 +246,14 @@ func resourceAppUpdate(ctx context.Context, d *schema.ResourceData, meta interfa func resourceAppDelete(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] Deleting Pinpoint App: %s", d.Id()) - _, err := conn.DeleteAppWithContext(ctx, &pinpoint.DeleteAppInput{ + _, err := conn.DeleteApp(ctx, &pinpoint.DeleteAppInput{ ApplicationId: aws.String(d.Id()), }) - if tfawserr.ErrCodeEquals(err, pinpoint.ErrCodeNotFoundException) { + if errs.IsA[*awstypes.NotFoundException](err) { return diags } @@ -262,14 +264,14 @@ func resourceAppDelete(ctx context.Context, d *schema.ResourceData, meta interfa return diags } -func FindAppByID(ctx context.Context, conn *pinpoint.Pinpoint, id string) (*pinpoint.ApplicationResponse, error) { +func findAppByID(ctx context.Context, conn *pinpoint.Client, id string) (*awstypes.ApplicationResponse, error) { input := &pinpoint.GetAppInput{ ApplicationId: aws.String(id), } - output, err := conn.GetAppWithContext(ctx, input) + output, err := conn.GetApp(ctx, input) - if tfawserr.ErrCodeEquals(err, pinpoint.ErrCodeNotFoundException) { + if errs.IsA[*awstypes.NotFoundException](err) { return nil, &retry.NotFoundError{ LastError: err, LastRequest: input, @@ -287,14 +289,14 @@ func FindAppByID(ctx context.Context, conn *pinpoint.Pinpoint, id string) (*pinp return output.ApplicationResponse, nil } -func findAppSettingsByID(ctx context.Context, conn *pinpoint.Pinpoint, id string) (*pinpoint.ApplicationSettingsResource, error) { +func findAppSettingsByID(ctx context.Context, conn *pinpoint.Client, id string) (*awstypes.ApplicationSettingsResource, error) { input := &pinpoint.GetApplicationSettingsInput{ ApplicationId: aws.String(id), } - output, err := conn.GetApplicationSettingsWithContext(ctx, input) + output, err := conn.GetApplicationSettings(ctx, input) - if tfawserr.ErrCodeEquals(err, pinpoint.ErrCodeNotFoundException) { + if errs.IsA[*awstypes.NotFoundException](err) { return nil, &retry.NotFoundError{ LastError: err, LastRequest: input, @@ -312,21 +314,21 @@ func findAppSettingsByID(ctx context.Context, conn *pinpoint.Pinpoint, id string return output.ApplicationSettingsResource, nil } -func expandCampaignHook(configs []interface{}) *pinpoint.CampaignHook { +func expandCampaignHook(configs []interface{}) *awstypes.CampaignHook { if len(configs) == 0 || configs[0] == nil { return nil } m := configs[0].(map[string]interface{}) - ch := &pinpoint.CampaignHook{} + ch := &awstypes.CampaignHook{} if v, ok := m["lambda_function_name"]; ok { ch.LambdaFunctionName = aws.String(v.(string)) } if v, ok := m[names.AttrMode]; ok { - ch.Mode = aws.String(v.(string)) + ch.Mode = awstypes.Mode(v.(string)) } if v, ok := m["web_url"]; ok { @@ -336,71 +338,71 @@ func expandCampaignHook(configs []interface{}) *pinpoint.CampaignHook { return ch } -func flattenCampaignHook(ch *pinpoint.CampaignHook) []interface{} { +func flattenCampaignHook(ch *awstypes.CampaignHook) []interface{} { l := make([]interface{}, 0) m := map[string]interface{}{} - m["lambda_function_name"] = aws.StringValue(ch.LambdaFunctionName) - m[names.AttrMode] = aws.StringValue(ch.Mode) - m["web_url"] = aws.StringValue(ch.WebUrl) + m["lambda_function_name"] = aws.ToString(ch.LambdaFunctionName) + m[names.AttrMode] = ch.Mode + m["web_url"] = aws.ToString(ch.WebUrl) l = append(l, m) return l } -func expandCampaignLimits(configs []interface{}) *pinpoint.CampaignLimits { +func expandCampaignLimits(configs []interface{}) *awstypes.CampaignLimits { if len(configs) == 0 || configs[0] == nil { return nil } m := configs[0].(map[string]interface{}) - cl := pinpoint.CampaignLimits{} + cl := awstypes.CampaignLimits{} if v, ok := m["daily"]; ok { - cl.Daily = aws.Int64(int64(v.(int))) + cl.Daily = aws.Int32(int32(v.(int))) } if v, ok := m["maximum_duration"]; ok { - cl.MaximumDuration = aws.Int64(int64(v.(int))) + cl.MaximumDuration = aws.Int32(int32(v.(int))) } if v, ok := m["messages_per_second"]; ok { - cl.MessagesPerSecond = aws.Int64(int64(v.(int))) + cl.MessagesPerSecond = aws.Int32(int32(v.(int))) } if v, ok := m["total"]; ok { - cl.Total = aws.Int64(int64(v.(int))) + cl.Total = aws.Int32(int32(v.(int))) } return &cl } -func flattenCampaignLimits(cl *pinpoint.CampaignLimits) []interface{} { +func flattenCampaignLimits(cl *awstypes.CampaignLimits) []interface{} { l := make([]interface{}, 0) m := map[string]interface{}{} - m["daily"] = aws.Int64Value(cl.Daily) - m["maximum_duration"] = aws.Int64Value(cl.MaximumDuration) - m["messages_per_second"] = aws.Int64Value(cl.MessagesPerSecond) - m["total"] = aws.Int64Value(cl.Total) + m["daily"] = aws.ToInt32(cl.Daily) + m["maximum_duration"] = aws.ToInt32(cl.MaximumDuration) + m["messages_per_second"] = aws.ToInt32(cl.MessagesPerSecond) + m["total"] = aws.ToInt32(cl.Total) l = append(l, m) return l } -func expandQuietTime(configs []interface{}) *pinpoint.QuietTime { +func expandQuietTime(configs []interface{}) *awstypes.QuietTime { if len(configs) == 0 || configs[0] == nil { return nil } m := configs[0].(map[string]interface{}) - qt := pinpoint.QuietTime{} + qt := awstypes.QuietTime{} if v, ok := m["end"]; ok { qt.End = aws.String(v.(string)) @@ -413,13 +415,13 @@ func expandQuietTime(configs []interface{}) *pinpoint.QuietTime { return &qt } -func flattenQuietTime(qt *pinpoint.QuietTime) []interface{} { +func flattenQuietTime(qt *awstypes.QuietTime) []interface{} { l := make([]interface{}, 0) m := map[string]interface{}{} - m["end"] = aws.StringValue(qt.End) - m["start"] = aws.StringValue(qt.Start) + m["end"] = aws.ToString(qt.End) + m["start"] = aws.ToString(qt.Start) l = append(l, m) diff --git a/internal/service/pinpoint/app_test.go b/internal/service/pinpoint/app_test.go index 0077b962b4e..96939f654ee 100644 --- a/internal/service/pinpoint/app_test.go +++ b/internal/service/pinpoint/app_test.go @@ -8,7 +8,7 @@ import ( "fmt" "testing" - "github.com/aws/aws-sdk-go/service/pinpoint" + awstypes "github.com/aws/aws-sdk-go-v2/service/pinpoint/types" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/id" sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" @@ -25,7 +25,7 @@ import ( func TestAccPinpointApp_basic(t *testing.T) { ctx := acctest.Context(t) - var application pinpoint.ApplicationResponse + var application awstypes.ApplicationResponse rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) resourceName := "aws_pinpoint_app.test" @@ -81,7 +81,7 @@ func TestAccPinpointApp_basic(t *testing.T) { func TestAccPinpointApp_disappears(t *testing.T) { ctx := acctest.Context(t) - var application pinpoint.ApplicationResponse + var application awstypes.ApplicationResponse rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) resourceName := "aws_pinpoint_app.test" @@ -105,7 +105,7 @@ func TestAccPinpointApp_disappears(t *testing.T) { func TestAccPinpointApp_nameGenerated(t *testing.T) { ctx := acctest.Context(t) - var application pinpoint.ApplicationResponse + var application awstypes.ApplicationResponse resourceName := "aws_pinpoint_app.test" resource.ParallelTest(t, resource.TestCase{ @@ -128,7 +128,7 @@ func TestAccPinpointApp_nameGenerated(t *testing.T) { func TestAccPinpointApp_namePrefix(t *testing.T) { ctx := acctest.Context(t) - var application pinpoint.ApplicationResponse + var application awstypes.ApplicationResponse resourceName := "aws_pinpoint_app.test" resource.ParallelTest(t, resource.TestCase{ @@ -151,7 +151,7 @@ func TestAccPinpointApp_namePrefix(t *testing.T) { func TestAccPinpointApp_tags(t *testing.T) { ctx := acctest.Context(t) - var application pinpoint.ApplicationResponse + var application awstypes.ApplicationResponse rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) resourceName := "aws_pinpoint_app.test" @@ -197,7 +197,7 @@ func TestAccPinpointApp_tags(t *testing.T) { func TestAccPinpointApp_campaignHookLambda(t *testing.T) { ctx := acctest.Context(t) - var application pinpoint.ApplicationResponse + var application awstypes.ApplicationResponse rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) resourceName := "aws_pinpoint_app.test" @@ -217,7 +217,7 @@ func TestAccPinpointApp_campaignHookLambda(t *testing.T) { statecheck.ExpectKnownValue(resourceName, tfjsonpath.New("campaign_hook"), knownvalue.ListExact([]knownvalue.Check{ knownvalue.ObjectExact(map[string]knownvalue.Check{ "lambda_function_name": knownvalue.NotNull(), // Should be a Pair function, waiting on https://github.com/hashicorp/terraform-plugin-testing/pull/330 - names.AttrMode: knownvalue.StringExact(pinpoint.ModeDelivery), + names.AttrMode: knownvalue.StringExact(string(awstypes.ModeDelivery)), "web_url": knownvalue.StringExact(""), }), })), @@ -234,7 +234,7 @@ func TestAccPinpointApp_campaignHookLambda(t *testing.T) { func TestAccPinpointApp_campaignHookEmpty(t *testing.T) { ctx := acctest.Context(t) - var application pinpoint.ApplicationResponse + var application awstypes.ApplicationResponse rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) resourceName := "aws_pinpoint_app.test" @@ -270,7 +270,7 @@ func TestAccPinpointApp_campaignHookEmpty(t *testing.T) { func TestAccPinpointApp_limits(t *testing.T) { ctx := acctest.Context(t) - var application pinpoint.ApplicationResponse + var application awstypes.ApplicationResponse rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) resourceName := "aws_pinpoint_app.test" @@ -307,7 +307,7 @@ func TestAccPinpointApp_limits(t *testing.T) { func TestAccPinpointApp_limitsEmpty(t *testing.T) { ctx := acctest.Context(t) - var application pinpoint.ApplicationResponse + var application awstypes.ApplicationResponse rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) resourceName := "aws_pinpoint_app.test" @@ -344,7 +344,7 @@ func TestAccPinpointApp_limitsEmpty(t *testing.T) { func TestAccPinpointApp_quietTime(t *testing.T) { ctx := acctest.Context(t) - var application pinpoint.ApplicationResponse + var application awstypes.ApplicationResponse rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) resourceName := "aws_pinpoint_app.test" @@ -379,7 +379,7 @@ func TestAccPinpointApp_quietTime(t *testing.T) { func TestAccPinpointApp_quietTimeEmpty(t *testing.T) { ctx := acctest.Context(t) - var application pinpoint.ApplicationResponse + var application awstypes.ApplicationResponse rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) resourceName := "aws_pinpoint_app.test" @@ -419,7 +419,7 @@ func testAccPreCheckApp(ctx context.Context, t *testing.T) { func testAccCheckAppDestroy(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_app" { @@ -443,14 +443,14 @@ func testAccCheckAppDestroy(ctx context.Context) resource.TestCheckFunc { } } -func testAccCheckAppExists(ctx context.Context, n string, v *pinpoint.ApplicationResponse) resource.TestCheckFunc { +func testAccCheckAppExists(ctx context.Context, n string, v *awstypes.ApplicationResponse) 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).PinpointConn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).PinpointClient(ctx) output, err := tfpinpoint.FindAppByID(ctx, conn, rs.Primary.ID) From 8e4a2bbce915f47d0ccb63a0f7123e8d54af88f5 Mon Sep 17 00:00:00 2001 From: Daniel Rieske Date: Wed, 7 Aug 2024 21:11:46 +0200 Subject: [PATCH 11/19] feat: migrate r/event_stream to SDKv2 --- internal/service/pinpoint/event_stream.go | 86 ++++++++++--------- .../service/pinpoint/event_stream_test.go | 74 ++++++++-------- 2 files changed, 81 insertions(+), 79 deletions(-) diff --git a/internal/service/pinpoint/event_stream.go b/internal/service/pinpoint/event_stream.go index c3a8d70c1ac..31049b89595 100644 --- a/internal/service/pinpoint/event_stream.go +++ b/internal/service/pinpoint/event_stream.go @@ -7,21 +7,22 @@ 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/internal/verify" "github.com/hashicorp/terraform-provider-aws/names" ) -// @SDKResource("aws_pinpoint_event_stream") -func ResourceEventStream() *schema.Resource { +// @SDKResource("aws_pinpoint_event_stream", name="Event Stream") +func resourceEventStream() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourceEventStreamUpsert, ReadWithoutTimeout: resourceEventStreamRead, @@ -53,11 +54,11 @@ func ResourceEventStream() *schema.Resource { func resourceEventStreamUpsert(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.WriteEventStream{ + params := &awstypes.WriteEventStream{ DestinationStreamArn: aws.String(d.Get("destination_stream_arn").(string)), RoleArn: aws.String(d.Get(names.AttrRoleARN).(string)), } @@ -68,22 +69,12 @@ func resourceEventStreamUpsert(ctx context.Context, d *schema.ResourceData, meta } // Retry for IAM eventual consistency - err := retry.RetryContext(ctx, propagationTimeout, func() *retry.RetryError { - _, err := conn.PutEventStreamWithContext(ctx, &req) - - if tfawserr.ErrMessageContains(err, pinpoint.ErrCodeBadRequestException, "make sure the IAM Role is configured correctly") { - return retry.RetryableError(err) - } - - if err != nil { - return retry.NonRetryableError(err) - } - - return nil - }) + _, err := tfresource.RetryWhenIsAErrorMessageContains[*awstypes.BadRequestException](ctx, propagationTimeout, func() (interface{}, error) { + return conn.PutEventStream(ctx, &req) + }, "make sure the IAM Role is configured correctly") if tfresource.TimedOut(err) { - _, err = conn.PutEventStreamWithContext(ctx, &req) + _, err = conn.PutEventStream(ctx, &req) } if err != nil { @@ -97,41 +88,35 @@ func resourceEventStreamUpsert(ctx context.Context, d *schema.ResourceData, meta func resourceEventStreamRead(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 Event Stream for application %s", d.Id()) - output, err := conn.GetEventStreamWithContext(ctx, &pinpoint.GetEventStreamInput{ - ApplicationId: aws.String(d.Id()), - }) - if err != nil { - if tfawserr.ErrCodeEquals(err, pinpoint.ErrCodeNotFoundException) { - log.Printf("[WARN] Pinpoint Event Stream for application %s not found, removing from state", d.Id()) - d.SetId("") - return diags - } + output, err := findEventStreamByApplicationId(ctx, conn, d.Id()) - return sdkdiag.AppendErrorf(diags, "getting Pinpoint Event Stream for application %s: %s", d.Id(), err) + if !d.IsNewResource() && tfresource.NotFound(err) { + log.Printf("[WARN] Pinpoint Event Stream (%s) not found, removing from state", d.Id()) + d.SetId("") + return diags } - res := output.EventStream - d.Set(names.AttrApplicationID, res.ApplicationId) - d.Set("destination_stream_arn", res.DestinationStreamArn) - d.Set(names.AttrRoleARN, res.RoleArn) + d.Set(names.AttrApplicationID, output.ApplicationId) + d.Set("destination_stream_arn", output.DestinationStreamArn) + d.Set(names.AttrRoleARN, output.RoleArn) return diags } func resourceEventStreamDelete(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 Event Stream: %s", d.Id()) - _, err := conn.DeleteEventStreamWithContext(ctx, &pinpoint.DeleteEventStreamInput{ + _, err := conn.DeleteEventStream(ctx, &pinpoint.DeleteEventStreamInput{ ApplicationId: aws.String(d.Id()), }) - if tfawserr.ErrCodeEquals(err, pinpoint.ErrCodeNotFoundException) { + if errs.IsA[*awstypes.NotFoundException](err) { return diags } @@ -140,3 +125,26 @@ func resourceEventStreamDelete(ctx context.Context, d *schema.ResourceData, meta } return diags } + +func findEventStreamByApplicationId(ctx context.Context, conn *pinpoint.Client, applicationId string) (*awstypes.EventStream, error) { + input := &pinpoint.GetEventStreamInput{ + ApplicationId: aws.String(applicationId), + } + + output, err := conn.GetEventStream(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.EventStream == nil { + return nil, tfresource.NewEmptyResultError(input) + } + + return output.EventStream, nil +} diff --git a/internal/service/pinpoint/event_stream_test.go b/internal/service/pinpoint/event_stream_test.go index 5c0926f72de..8b0a424f4a8 100644 --- a/internal/service/pinpoint/event_stream_test.go +++ b/internal/service/pinpoint/event_stream_test.go @@ -8,21 +8,20 @@ import ( "fmt" "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" 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" 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" ) func TestAccPinpointEventStream_basic(t *testing.T) { ctx := acctest.Context(t) - var stream pinpoint.EventStream + var stream awstypes.EventStream resourceName := "aws_pinpoint_event_stream.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) rName2 := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) @@ -62,7 +61,7 @@ func TestAccPinpointEventStream_basic(t *testing.T) { func TestAccPinpointEventStream_disappears(t *testing.T) { ctx := acctest.Context(t) - var stream pinpoint.EventStream + var stream awstypes.EventStream resourceName := "aws_pinpoint_event_stream.test" rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) @@ -84,7 +83,7 @@ func TestAccPinpointEventStream_disappears(t *testing.T) { }) } -func testAccCheckEventStreamExists(ctx context.Context, n string, stream *pinpoint.EventStream) resource.TestCheckFunc { +func testAccCheckEventStreamExists(ctx context.Context, n string, stream *awstypes.EventStream) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] if !ok { @@ -95,19 +94,41 @@ func testAccCheckEventStreamExists(ctx context.Context, n string, stream *pinpoi return fmt.Errorf("No Pinpoint event stream with that ID exists") } - conn := acctest.Provider.Meta().(*conns.AWSClient).PinpointConn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).PinpointClient(ctx) - // Check if the app exists - params := &pinpoint.GetEventStreamInput{ - ApplicationId: aws.String(rs.Primary.ID), - } - output, err := conn.GetEventStreamWithContext(ctx, params) + output, err := tfpinpoint.FindEventStreamByApplicationId(ctx, conn, rs.Primary.ID) if err != nil { return err } - *stream = *output.EventStream + *stream = *output + + return nil + } +} + +func testAccCheckEventStreamDestroy(ctx context.Context) resource.TestCheckFunc { + return func(s *terraform.State) error { + conn := acctest.Provider.Meta().(*conns.AWSClient).PinpointClient(ctx) + + for _, rs := range s.RootModule().Resources { + if rs.Type != "aws_pinpoint_event_stream" { + continue + } + + _, err := tfpinpoint.FindEventStreamByApplicationId(ctx, conn, rs.Primary.ID) + + if tfresource.NotFound(err) { + continue + } + + if err != nil { + return err + } + + return fmt.Errorf("Pinpoint Event Stream %s still exists", rs.Primary.ID) + } return nil } @@ -170,30 +191,3 @@ EOF } `, rName, streamName) } - -func testAccCheckEventStreamDestroy(ctx context.Context) resource.TestCheckFunc { - return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).PinpointConn(ctx) - - for _, rs := range s.RootModule().Resources { - if rs.Type != "aws_pinpoint_event_stream" { - continue - } - - // Check if the event stream exists - params := &pinpoint.GetEventStreamInput{ - ApplicationId: aws.String(rs.Primary.ID), - } - _, err := conn.GetEventStreamWithContext(ctx, params) - if err != nil { - if tfawserr.ErrCodeEquals(err, pinpoint.ErrCodeNotFoundException) { - continue - } - return err - } - return fmt.Errorf("Event stream exists when it should be destroyed!") - } - - return nil - } -} From 9f2a262c474091b707c291201e821dcef981b3a6 Mon Sep 17 00:00:00 2001 From: Daniel Rieske Date: Wed, 7 Aug 2024 21:11:56 +0200 Subject: [PATCH 12/19] feat: migrate r/gcm_channel to SDKv2 --- internal/service/pinpoint/gcm_channel.go | 69 ++++++++++++------- internal/service/pinpoint/gcm_channel_test.go | 61 ++++++++-------- 2 files changed, 73 insertions(+), 57 deletions(-) diff --git a/internal/service/pinpoint/gcm_channel.go b/internal/service/pinpoint/gcm_channel.go index a4770ebddf1..dfd9de4c1a9 100644 --- a/internal/service/pinpoint/gcm_channel.go +++ b/internal/service/pinpoint/gcm_channel.go @@ -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_gcm_channel") -func ResourceGCMChannel() *schema.Resource { +// @SDKResource("aws_pinpoint_gcm_channel", name="GCM Channel") +func resourceGCMChannel() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourceGCMChannelUpsert, ReadWithoutTimeout: resourceGCMChannelRead, @@ -50,11 +53,11 @@ func ResourceGCMChannel() *schema.Resource { func resourceGCMChannelUpsert(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.GCMChannelRequest{} + params := &awstypes.GCMChannelRequest{} params.ApiKey = aws.String(d.Get("api_key").(string)) params.Enabled = aws.Bool(d.Get(names.AttrEnabled).(bool)) @@ -64,7 +67,7 @@ func resourceGCMChannelUpsert(ctx context.Context, d *schema.ResourceData, meta GCMChannelRequest: params, } - _, err := conn.UpdateGcmChannelWithContext(ctx, &req) + _, err := conn.UpdateGcmChannel(ctx, &req) if err != nil { return sdkdiag.AppendErrorf(diags, "putting Pinpoint GCM Channel for application %s: %s", applicationId, err) } @@ -76,25 +79,20 @@ func resourceGCMChannelUpsert(ctx context.Context, d *schema.ResourceData, meta func resourceGCMChannelRead(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 GCM Channel for application %s", d.Id()) - output, err := conn.GetGcmChannelWithContext(ctx, &pinpoint.GetGcmChannelInput{ - ApplicationId: aws.String(d.Id()), - }) - if err != nil { - if tfawserr.ErrCodeEquals(err, pinpoint.ErrCodeNotFoundException) { - log.Printf("[WARN] Pinpoint GCM Channel for application %s not found, removing from state", d.Id()) - d.SetId("") - return diags - } + output, err := findGCMChannelByApplicationId(ctx, conn, d.Id()) - return sdkdiag.AppendErrorf(diags, "getting Pinpoint GCM Channel for application %s: %s", d.Id(), err) + if !d.IsNewResource() && tfresource.NotFound(err) { + log.Printf("[WARN] Pinpoint GCM Channel (%s) not found, removing from state", d.Id()) + d.SetId("") + return diags } - d.Set(names.AttrApplicationID, output.GCMChannelResponse.ApplicationId) - d.Set(names.AttrEnabled, output.GCMChannelResponse.Enabled) + d.Set(names.AttrApplicationID, output.ApplicationId) + d.Set(names.AttrEnabled, output.Enabled) // api_key is never returned return diags @@ -102,14 +100,14 @@ func resourceGCMChannelRead(ctx context.Context, d *schema.ResourceData, meta in func resourceGCMChannelDelete(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] Deleting Pinpoint GCM Channel for application %s", d.Id()) - _, err := conn.DeleteGcmChannelWithContext(ctx, &pinpoint.DeleteGcmChannelInput{ + _, err := conn.DeleteGcmChannel(ctx, &pinpoint.DeleteGcmChannelInput{ ApplicationId: aws.String(d.Id()), }) - if tfawserr.ErrCodeEquals(err, pinpoint.ErrCodeNotFoundException) { + if errs.IsA[*awstypes.NotFoundException](err) { return diags } @@ -118,3 +116,26 @@ func resourceGCMChannelDelete(ctx context.Context, d *schema.ResourceData, meta } return diags } + +func findGCMChannelByApplicationId(ctx context.Context, conn *pinpoint.Client, applicationId string) (*awstypes.GCMChannelResponse, error) { + input := &pinpoint.GetGcmChannelInput{ + ApplicationId: aws.String(applicationId), + } + + output, err := conn.GetGcmChannel(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.GCMChannelResponse == nil { + return nil, tfresource.NewEmptyResultError(input) + } + + return output.GCMChannelResponse, nil +} diff --git a/internal/service/pinpoint/gcm_channel_test.go b/internal/service/pinpoint/gcm_channel_test.go index 53c182999a9..b02c1c22926 100644 --- a/internal/service/pinpoint/gcm_channel_test.go +++ b/internal/service/pinpoint/gcm_channel_test.go @@ -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" ) @@ -27,7 +27,7 @@ import ( func TestAccPinpointGCMChannel_basic(t *testing.T) { ctx := acctest.Context(t) - var channel pinpoint.GCMChannelResponse + var channel awstypes.GCMChannelResponse resourceName := "aws_pinpoint_gcm_channel.test_gcm_channel" if os.Getenv("GCM_API_KEY") == "" { @@ -66,7 +66,7 @@ func TestAccPinpointGCMChannel_basic(t *testing.T) { }) } -func testAccCheckGCMChannelExists(ctx context.Context, n string, channel *pinpoint.GCMChannelResponse) resource.TestCheckFunc { +func testAccCheckGCMChannelExists(ctx context.Context, n string, channel *awstypes.GCMChannelResponse) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] if !ok { @@ -77,59 +77,54 @@ func testAccCheckGCMChannelExists(ctx context.Context, n string, channel *pinpoi return fmt.Errorf("No Pinpoint GCM 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 app exists - params := &pinpoint.GetGcmChannelInput{ - ApplicationId: aws.String(rs.Primary.ID), - } - output, err := conn.GetGcmChannelWithContext(ctx, params) + output, err := tfpinpoint.FindGCMChannelByApplicationId(ctx, conn, rs.Primary.ID) if err != nil { return err } - *channel = *output.GCMChannelResponse + *channel = *output return nil } } -func testAccGCMChannelConfig_basic(apiKey string) string { - return fmt.Sprintf(` -resource "aws_pinpoint_app" "test_app" {} - -resource "aws_pinpoint_gcm_channel" "test_gcm_channel" { - application_id = aws_pinpoint_app.test_app.application_id - enabled = "false" - api_key = "%s" -} -`, apiKey) -} - func testAccCheckGCMChannelDestroy(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_gcm_channel" { continue } - // Check if the event stream exists - params := &pinpoint.GetGcmChannelInput{ - ApplicationId: aws.String(rs.Primary.ID), + _, err := tfpinpoint.FindGCMChannelByApplicationId(ctx, conn, rs.Primary.ID) + + if tfresource.NotFound(err) { + continue } - _, err := conn.GetGcmChannelWithContext(ctx, params) + if err != nil { - if tfawserr.ErrCodeEquals(err, pinpoint.ErrCodeNotFoundException) { - continue - } return err } - return fmt.Errorf("GCM Channel exists when it should be destroyed!") + + return fmt.Errorf("Pinpoint GCM Channel %s still exists", rs.Primary.ID) } return nil } } + +func testAccGCMChannelConfig_basic(apiKey string) string { + return fmt.Sprintf(` +resource "aws_pinpoint_app" "test_app" {} + +resource "aws_pinpoint_gcm_channel" "test_gcm_channel" { + application_id = aws_pinpoint_app.test_app.application_id + enabled = "false" + api_key = "%s" +} +`, apiKey) +} From 5418094b439f5e6921fade58a5721b666f7c9154 Mon Sep 17 00:00:00 2001 From: Daniel Rieske Date: Wed, 7 Aug 2024 21:12:03 +0200 Subject: [PATCH 13/19] feat: migrate r/sms_channel to SDKv2 --- internal/service/pinpoint/sms_channel.go | 78 ++++++++++++------- internal/service/pinpoint/sms_channel_test.go | 76 +++++++++--------- 2 files changed, 84 insertions(+), 70 deletions(-) diff --git a/internal/service/pinpoint/sms_channel.go b/internal/service/pinpoint/sms_channel.go index 9a25427432f..1048c1d90ec 100644 --- a/internal/service/pinpoint/sms_channel.go +++ b/internal/service/pinpoint/sms_channel.go @@ -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_sms_channel") -func ResourceSMSChannel() *schema.Resource { +// @SDKResource("aws_pinpoint_sms_channel", name="SMS Channel") +func resourceSMSChannel() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourceSMSChannelUpsert, ReadWithoutTimeout: resourceSMSChannelRead, @@ -61,11 +64,11 @@ func ResourceSMSChannel() *schema.Resource { func resourceSMSChannelUpsert(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.SMSChannelRequest{ + params := &awstypes.SMSChannelRequest{ Enabled: aws.Bool(d.Get(names.AttrEnabled).(bool)), } @@ -82,7 +85,7 @@ func resourceSMSChannelUpsert(ctx context.Context, d *schema.ResourceData, meta SMSChannelRequest: params, } - _, err := conn.UpdateSmsChannelWithContext(ctx, &req) + _, err := conn.UpdateSmsChannel(ctx, &req) if err != nil { return sdkdiag.AppendErrorf(diags, "putting Pinpoint SMS Channel for application %s: %s", applicationId, err) } @@ -94,43 +97,37 @@ func resourceSMSChannelUpsert(ctx context.Context, d *schema.ResourceData, meta func resourceSMSChannelRead(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 SMS Channel for application %s", d.Id()) - output, err := conn.GetSmsChannelWithContext(ctx, &pinpoint.GetSmsChannelInput{ - ApplicationId: aws.String(d.Id()), - }) - if err != nil { - if tfawserr.ErrCodeEquals(err, pinpoint.ErrCodeNotFoundException) { - log.Printf("[WARN] Pinpoint SMS Channel for application %s not found, removing from state", d.Id()) - d.SetId("") - return diags - } + output, err := findSMSChannelByApplicationId(ctx, conn, d.Id()) - return sdkdiag.AppendErrorf(diags, "getting Pinpoint SMS Channel for application %s: %s", d.Id(), err) + if !d.IsNewResource() && tfresource.NotFound(err) { + log.Printf("[WARN] Pinpoint SMS Channel (%s) not found, removing from state", d.Id()) + d.SetId("") + return diags } - res := output.SMSChannelResponse - d.Set(names.AttrApplicationID, res.ApplicationId) - d.Set(names.AttrEnabled, res.Enabled) - d.Set("sender_id", res.SenderId) - d.Set("short_code", res.ShortCode) - d.Set("promotional_messages_per_second", res.PromotionalMessagesPerSecond) - d.Set("transactional_messages_per_second", res.TransactionalMessagesPerSecond) + d.Set(names.AttrApplicationID, output.ApplicationId) + d.Set(names.AttrEnabled, output.Enabled) + d.Set("sender_id", output.SenderId) + d.Set("short_code", output.ShortCode) + d.Set("promotional_messages_per_second", output.PromotionalMessagesPerSecond) + d.Set("transactional_messages_per_second", output.TransactionalMessagesPerSecond) return diags } func resourceSMSChannelDelete(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] Deleting Pinpoint SMS Channel for application %s", d.Id()) - _, err := conn.DeleteSmsChannelWithContext(ctx, &pinpoint.DeleteSmsChannelInput{ + _, err := conn.DeleteSmsChannel(ctx, &pinpoint.DeleteSmsChannelInput{ ApplicationId: aws.String(d.Id()), }) - if tfawserr.ErrCodeEquals(err, pinpoint.ErrCodeNotFoundException) { + if errs.IsA[*awstypes.NotFoundException](err) { return diags } @@ -139,3 +136,26 @@ func resourceSMSChannelDelete(ctx context.Context, d *schema.ResourceData, meta } return diags } + +func findSMSChannelByApplicationId(ctx context.Context, conn *pinpoint.Client, applicationId string) (*awstypes.SMSChannelResponse, error) { + input := &pinpoint.GetSmsChannelInput{ + ApplicationId: aws.String(applicationId), + } + + output, err := conn.GetSmsChannel(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.SMSChannelResponse == nil { + return nil, tfresource.NewEmptyResultError(input) + } + + return output.SMSChannelResponse, nil +} diff --git a/internal/service/pinpoint/sms_channel_test.go b/internal/service/pinpoint/sms_channel_test.go index 116fd4eb077..c3b9ed698a8 100644 --- a/internal/service/pinpoint/sms_channel_test.go +++ b/internal/service/pinpoint/sms_channel_test.go @@ -8,20 +8,19 @@ import ( "fmt" "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" ) func TestAccPinpointSMSChannel_basic(t *testing.T) { ctx := acctest.Context(t) - var channel pinpoint.SMSChannelResponse + var channel awstypes.SMSChannelResponse resourceName := "aws_pinpoint_sms_channel.test" resource.ParallelTest(t, resource.TestCase{ @@ -64,7 +63,7 @@ func TestAccPinpointSMSChannel_basic(t *testing.T) { func TestAccPinpointSMSChannel_full(t *testing.T) { ctx := acctest.Context(t) - var channel pinpoint.SMSChannelResponse + var channel awstypes.SMSChannelResponse resourceName := "aws_pinpoint_sms_channel.test" senderId := "1234" shortCode := "5678" @@ -118,7 +117,7 @@ func TestAccPinpointSMSChannel_full(t *testing.T) { func TestAccPinpointSMSChannel_disappears(t *testing.T) { ctx := acctest.Context(t) - var channel pinpoint.SMSChannelResponse + var channel awstypes.SMSChannelResponse resourceName := "aws_pinpoint_sms_channel.test" resource.ParallelTest(t, resource.TestCase{ @@ -139,7 +138,7 @@ func TestAccPinpointSMSChannel_disappears(t *testing.T) { }) } -func testAccCheckSMSChannelExists(ctx context.Context, n string, channel *pinpoint.SMSChannelResponse) resource.TestCheckFunc { +func testAccCheckSMSChannelExists(ctx context.Context, n string, channel *awstypes.SMSChannelResponse) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] if !ok { @@ -150,19 +149,41 @@ func testAccCheckSMSChannelExists(ctx context.Context, n string, channel *pinpoi return fmt.Errorf("No Pinpoint SMS 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 app exists - params := &pinpoint.GetSmsChannelInput{ - ApplicationId: aws.String(rs.Primary.ID), - } - output, err := conn.GetSmsChannelWithContext(ctx, params) + output, err := tfpinpoint.FindSMSChannelByApplicationId(ctx, conn, rs.Primary.ID) if err != nil { return err } - *channel = *output.SMSChannelResponse + *channel = *output + + return nil + } +} + +func testAccCheckSMSChannelDestroy(ctx context.Context) resource.TestCheckFunc { + return func(s *terraform.State) error { + conn := acctest.Provider.Meta().(*conns.AWSClient).PinpointClient(ctx) + + for _, rs := range s.RootModule().Resources { + if rs.Type != "aws_pinpoint_sms_channel" { + continue + } + + _, err := tfpinpoint.FindSMSChannelByApplicationId(ctx, conn, rs.Primary.ID) + + if tfresource.NotFound(err) { + continue + } + + if err != nil { + return err + } + + return fmt.Errorf("Pinpoint SMS Channel %s still exists", rs.Primary.ID) + } return nil } @@ -188,30 +209,3 @@ resource "aws_pinpoint_sms_channel" "test" { } `, senderId, shortCode) } - -func testAccCheckSMSChannelDestroy(ctx context.Context) resource.TestCheckFunc { - return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).PinpointConn(ctx) - - for _, rs := range s.RootModule().Resources { - if rs.Type != "aws_pinpoint_sms_channel" { - continue - } - - // Check if the event stream exists - params := &pinpoint.GetSmsChannelInput{ - ApplicationId: aws.String(rs.Primary.ID), - } - _, err := conn.GetSmsChannelWithContext(ctx, params) - if err != nil { - if tfawserr.ErrCodeEquals(err, pinpoint.ErrCodeNotFoundException) { - continue - } - return err - } - return fmt.Errorf("SMS Channel exists when it should be destroyed!") - } - - return nil - } -} From 9b1c0b8f54040317dd789afb1cb9508b71e05f87 Mon Sep 17 00:00:00 2001 From: Daniel Rieske Date: Wed, 7 Aug 2024 21:12:14 +0200 Subject: [PATCH 14/19] feat: migrate sweepers --- internal/service/pinpoint/sweep.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/internal/service/pinpoint/sweep.go b/internal/service/pinpoint/sweep.go index 9411e513553..37fd1bc0b67 100644 --- a/internal/service/pinpoint/sweep.go +++ b/internal/service/pinpoint/sweep.go @@ -7,11 +7,11 @@ import ( "fmt" "log" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/pinpoint" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/pinpoint" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-provider-aws/internal/sweep" - "github.com/hashicorp/terraform-provider-aws/internal/sweep/awsv1" + "github.com/hashicorp/terraform-provider-aws/internal/sweep/awsv2" ) func RegisterSweepers() { @@ -27,14 +27,14 @@ func sweepApps(region string) error { if err != nil { return fmt.Errorf("error getting client: %s", err) } - conn := client.PinpointConn(ctx) + conn := client.PinpointClient(ctx) input := &pinpoint.GetAppsInput{} for { - output, err := conn.GetAppsWithContext(ctx, input) + output, err := conn.GetApps(ctx, input) if err != nil { - if awsv1.SkipSweepError(err) { + if awsv2.SkipSweepError(err) { log.Printf("[WARN] Skipping Pinpoint app sweep for %s: %s", region, err) return nil } @@ -47,10 +47,10 @@ func sweepApps(region string) error { } for _, item := range output.ApplicationsResponse.Item { - name := aws.StringValue(item.Name) + name := aws.ToString(item.Name) log.Printf("[INFO] Deleting Pinpoint app %s", name) - _, err := conn.DeleteAppWithContext(ctx, &pinpoint.DeleteAppInput{ + _, err := conn.DeleteApp(ctx, &pinpoint.DeleteAppInput{ ApplicationId: item.Id, }) if err != nil { From fb0cecc1e216d220ca393564498c18535256794a Mon Sep 17 00:00:00 2001 From: Daniel Rieske Date: Wed, 7 Aug 2024 21:12:28 +0200 Subject: [PATCH 15/19] feat: fix exports --- internal/service/pinpoint/exports_test.go | 24 +++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 internal/service/pinpoint/exports_test.go diff --git a/internal/service/pinpoint/exports_test.go b/internal/service/pinpoint/exports_test.go new file mode 100644 index 00000000000..4ff4bf79f0c --- /dev/null +++ b/internal/service/pinpoint/exports_test.go @@ -0,0 +1,24 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package pinpoint + +// Exports for use in tests only. +var ( + ResourceApp = resourceApp + ResourceEmailChannel = resourceEmailChannel + ResourceEventStream = resourceEventStream + ResourceSMSChannel = resourceSMSChannel + + FindADMChannelByApplicationId = findADMChannelByApplicationId + FindAPNSChannelByApplicationId = findAPNSChannelByApplicationId + FindAPNSSandboxChannelByApplicationId = findAPNSSandboxChannelByApplicationId + FindAPNSVoIPChannelByApplicationId = findAPNSVoIPChannelByApplicationId + FindAPNSVoIPSandboxChannelByApplicationId = findAPNSVoIPSandboxChannelByApplicationId + FindAppByID = findAppByID + FindBaiduChannelByApplicationId = findBaiduChannelByApplicationId + FindEmailChannelByApplicationId = findEmailChannelByApplicationId + FindEventStreamByApplicationId = findEventStreamByApplicationId + FindGCMChannelByApplicationId = findGCMChannelByApplicationId + FindSMSChannelByApplicationId = findSMSChannelByApplicationId +) From bc36b8f197c184c4f9286715170cd465e1d4d89f Mon Sep 17 00:00:00 2001 From: Daniel Rieske Date: Wed, 7 Aug 2024 21:41:50 +0200 Subject: [PATCH 16/19] feat: added additional error checks --- internal/service/pinpoint/apns_channel.go | 4 ++++ internal/service/pinpoint/apns_sandbox_channel.go | 4 ++++ internal/service/pinpoint/apns_voip_channel.go | 4 ++++ internal/service/pinpoint/apns_voip_sandbox_channel.go | 4 ++++ internal/service/pinpoint/baidu_channel.go | 4 ++++ internal/service/pinpoint/email_channel.go | 4 ++++ internal/service/pinpoint/event_stream.go | 4 ++++ internal/service/pinpoint/gcm_channel.go | 4 ++++ internal/service/pinpoint/sms_channel.go | 4 ++++ 9 files changed, 36 insertions(+) diff --git a/internal/service/pinpoint/apns_channel.go b/internal/service/pinpoint/apns_channel.go index 0850680ab94..a0eed9a9931 100644 --- a/internal/service/pinpoint/apns_channel.go +++ b/internal/service/pinpoint/apns_channel.go @@ -140,6 +140,10 @@ func resourceAPNSChannelRead(ctx context.Context, d *schema.ResourceData, meta i return diags } + if err != nil { + return sdkdiag.AppendErrorf(diags, "reading Pinpoint APNS Channel (%s): %s", d.Id(), err) + } + d.Set(names.AttrApplicationID, output.ApplicationId) d.Set("default_authentication_method", output.DefaultAuthenticationMethod) d.Set(names.AttrEnabled, output.Enabled) diff --git a/internal/service/pinpoint/apns_sandbox_channel.go b/internal/service/pinpoint/apns_sandbox_channel.go index 59c673ed4ae..440753d2ad6 100644 --- a/internal/service/pinpoint/apns_sandbox_channel.go +++ b/internal/service/pinpoint/apns_sandbox_channel.go @@ -140,6 +140,10 @@ func resourceAPNSSandboxChannelRead(ctx context.Context, d *schema.ResourceData, return diags } + if err != nil { + return sdkdiag.AppendErrorf(diags, "reading Pinpoint APNS Sandbox Channel (%s): %s", d.Id(), err) + } + d.Set(names.AttrApplicationID, output.ApplicationId) d.Set("default_authentication_method", output.DefaultAuthenticationMethod) d.Set(names.AttrEnabled, output.Enabled) diff --git a/internal/service/pinpoint/apns_voip_channel.go b/internal/service/pinpoint/apns_voip_channel.go index a25e9f56e1d..c654059597d 100644 --- a/internal/service/pinpoint/apns_voip_channel.go +++ b/internal/service/pinpoint/apns_voip_channel.go @@ -140,6 +140,10 @@ func resourceAPNSVoIPChannelRead(ctx context.Context, d *schema.ResourceData, me return diags } + if err != nil { + return sdkdiag.AppendErrorf(diags, "reading Pinpoint APNS VoIP Channel (%s): %s", d.Id(), err) + } + d.Set(names.AttrApplicationID, output.ApplicationId) d.Set("default_authentication_method", output.DefaultAuthenticationMethod) d.Set(names.AttrEnabled, output.Enabled) diff --git a/internal/service/pinpoint/apns_voip_sandbox_channel.go b/internal/service/pinpoint/apns_voip_sandbox_channel.go index 87cc5315163..deedb68eecc 100644 --- a/internal/service/pinpoint/apns_voip_sandbox_channel.go +++ b/internal/service/pinpoint/apns_voip_sandbox_channel.go @@ -140,6 +140,10 @@ func resourceAPNSVoIPSandboxChannelRead(ctx context.Context, d *schema.ResourceD return diags } + if err != nil { + return sdkdiag.AppendErrorf(diags, "reading Pinpoint APNS VoIP Sandbox Channel (%s): %s", d.Id(), err) + } + d.Set(names.AttrApplicationID, output.ApplicationId) d.Set("default_authentication_method", output.DefaultAuthenticationMethod) d.Set(names.AttrEnabled, output.Enabled) diff --git a/internal/service/pinpoint/baidu_channel.go b/internal/service/pinpoint/baidu_channel.go index 48d1d5f2d07..2de746c9887 100644 --- a/internal/service/pinpoint/baidu_channel.go +++ b/internal/service/pinpoint/baidu_channel.go @@ -97,6 +97,10 @@ func resourceBaiduChannelRead(ctx context.Context, d *schema.ResourceData, meta return diags } + if err != nil { + return sdkdiag.AppendErrorf(diags, "reading Pinpoint Baidu Channel (%s): %s", d.Id(), err) + } + d.Set(names.AttrApplicationID, output.ApplicationId) d.Set(names.AttrEnabled, output.Enabled) // ApiKey and SecretKey are never returned diff --git a/internal/service/pinpoint/email_channel.go b/internal/service/pinpoint/email_channel.go index a0c75a52a37..3af35c4cab7 100644 --- a/internal/service/pinpoint/email_channel.go +++ b/internal/service/pinpoint/email_channel.go @@ -118,6 +118,10 @@ func resourceEmailChannelRead(ctx context.Context, d *schema.ResourceData, meta return diags } + if err != nil { + return sdkdiag.AppendErrorf(diags, "reading Pinpoint Email Channel (%s): %s", d.Id(), err) + } + d.Set(names.AttrApplicationID, output.ApplicationId) d.Set(names.AttrEnabled, output.Enabled) d.Set("from_address", output.FromAddress) diff --git a/internal/service/pinpoint/event_stream.go b/internal/service/pinpoint/event_stream.go index 31049b89595..6f808761d5d 100644 --- a/internal/service/pinpoint/event_stream.go +++ b/internal/service/pinpoint/event_stream.go @@ -100,6 +100,10 @@ func resourceEventStreamRead(ctx context.Context, d *schema.ResourceData, meta i return diags } + if err != nil { + return sdkdiag.AppendErrorf(diags, "reading Pinpoint Event Stream (%s): %s", d.Id(), err) + } + d.Set(names.AttrApplicationID, output.ApplicationId) d.Set("destination_stream_arn", output.DestinationStreamArn) d.Set(names.AttrRoleARN, output.RoleArn) diff --git a/internal/service/pinpoint/gcm_channel.go b/internal/service/pinpoint/gcm_channel.go index dfd9de4c1a9..0b22b0cb960 100644 --- a/internal/service/pinpoint/gcm_channel.go +++ b/internal/service/pinpoint/gcm_channel.go @@ -91,6 +91,10 @@ func resourceGCMChannelRead(ctx context.Context, d *schema.ResourceData, meta in return diags } + if err != nil { + return sdkdiag.AppendErrorf(diags, "reading Pinpoint GCM Channel (%s): %s", d.Id(), err) + } + d.Set(names.AttrApplicationID, output.ApplicationId) d.Set(names.AttrEnabled, output.Enabled) // api_key is never returned diff --git a/internal/service/pinpoint/sms_channel.go b/internal/service/pinpoint/sms_channel.go index 1048c1d90ec..dc718d509f9 100644 --- a/internal/service/pinpoint/sms_channel.go +++ b/internal/service/pinpoint/sms_channel.go @@ -109,6 +109,10 @@ func resourceSMSChannelRead(ctx context.Context, d *schema.ResourceData, meta in return diags } + if err != nil { + return sdkdiag.AppendErrorf(diags, "reading Pinpoint SMS Channel (%s): %s", d.Id(), err) + } + d.Set(names.AttrApplicationID, output.ApplicationId) d.Set(names.AttrEnabled, output.Enabled) d.Set("sender_id", output.SenderId) From 64b712ae449db8541bc73b303782b4f6a429ca9f Mon Sep 17 00:00:00 2001 From: Daniel Rieske Date: Wed, 7 Aug 2024 22:04:35 +0200 Subject: [PATCH 17/19] chore: semgrep finding --- internal/service/pinpoint/event_stream.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/service/pinpoint/event_stream.go b/internal/service/pinpoint/event_stream.go index 6f808761d5d..c219f85b95d 100644 --- a/internal/service/pinpoint/event_stream.go +++ b/internal/service/pinpoint/event_stream.go @@ -73,7 +73,7 @@ func resourceEventStreamUpsert(ctx context.Context, d *schema.ResourceData, meta return conn.PutEventStream(ctx, &req) }, "make sure the IAM Role is configured correctly") - if tfresource.TimedOut(err) { + if tfresource.TimedOut(err) { // nosemgrep:ci.helper-schema-TimeoutError-check-doesnt-return-output _, err = conn.PutEventStream(ctx, &req) } From 220c3be185e054c6a36fff6ebbb0f36f4ebb16af Mon Sep 17 00:00:00 2001 From: Sharon Nam Date: Thu, 8 Aug 2024 22:47:22 -0700 Subject: [PATCH 18/19] Remove role_arn from test and add docs of deprecation --- internal/service/pinpoint/email_channel_test.go | 3 --- website/docs/r/pinpoint_email_channel.html.markdown | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/internal/service/pinpoint/email_channel_test.go b/internal/service/pinpoint/email_channel_test.go index 24c121ee482..caf493246ce 100644 --- a/internal/service/pinpoint/email_channel_test.go +++ b/internal/service/pinpoint/email_channel_test.go @@ -41,7 +41,6 @@ func TestAccPinpointEmailChannel_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, names.AttrEnabled, acctest.CtFalse), resource.TestCheckResourceAttr(resourceName, "from_address", address1), resource.TestCheckResourceAttrSet(resourceName, "messages_per_second"), - resource.TestCheckResourceAttrPair(resourceName, names.AttrRoleARN, "aws_iam_role.test", names.AttrARN), resource.TestCheckResourceAttrPair(resourceName, "identity", "aws_ses_domain_identity.test", names.AttrARN), ), }, @@ -211,7 +210,6 @@ resource "aws_pinpoint_email_channel" "test" { enabled = "false" from_address = %[2]q identity = aws_ses_domain_identity.test.arn - role_arn = aws_iam_role.test.arn } resource "aws_ses_domain_identity" "test" { @@ -272,7 +270,6 @@ resource "aws_pinpoint_email_channel" "test" { enabled = "false" from_address = %[2]q identity = aws_ses_domain_identity.test.arn - role_arn = aws_iam_role.test.arn configuration_set = aws_ses_configuration_set.test.name } diff --git a/website/docs/r/pinpoint_email_channel.html.markdown b/website/docs/r/pinpoint_email_channel.html.markdown index 1a6269d0523..3e0faa36f5f 100644 --- a/website/docs/r/pinpoint_email_channel.html.markdown +++ b/website/docs/r/pinpoint_email_channel.html.markdown @@ -71,7 +71,7 @@ This resource supports the following arguments: * `configuration_set` - (Optional) The ARN of the Amazon SES configuration set that you want to apply to messages that you send through the channel. * `from_address` - (Required) The email address used to send emails from. You can use email only (`user@example.com`) or friendly address (`User `). This field comply with [RFC 5322](https://www.ietf.org/rfc/rfc5322.txt). * `identity` - (Required) The ARN of an identity verified with SES. -* `role_arn` - (Optional) The ARN of an IAM Role used to submit events to Mobile Analytics' event ingestion service. +* `role_arn` - (Optional) *Deprecated* This attribute is no longer supported in SDKv2. The ARN of an IAM Role used to submit events to Mobile Analytics' event ingestion service. ## Attribute Reference From c3ac01355c5cebfd54f8c26f1ef782be7c5f2c1f Mon Sep 17 00:00:00 2001 From: Sharon Nam Date: Thu, 8 Aug 2024 23:09:09 -0700 Subject: [PATCH 19/19] Docs change --- website/docs/r/pinpoint_email_channel.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/docs/r/pinpoint_email_channel.html.markdown b/website/docs/r/pinpoint_email_channel.html.markdown index 3e0faa36f5f..ebc9f18a193 100644 --- a/website/docs/r/pinpoint_email_channel.html.markdown +++ b/website/docs/r/pinpoint_email_channel.html.markdown @@ -71,7 +71,7 @@ This resource supports the following arguments: * `configuration_set` - (Optional) The ARN of the Amazon SES configuration set that you want to apply to messages that you send through the channel. * `from_address` - (Required) The email address used to send emails from. You can use email only (`user@example.com`) or friendly address (`User `). This field comply with [RFC 5322](https://www.ietf.org/rfc/rfc5322.txt). * `identity` - (Required) The ARN of an identity verified with SES. -* `role_arn` - (Optional) *Deprecated* This attribute is no longer supported in SDKv2. The ARN of an IAM Role used to submit events to Mobile Analytics' event ingestion service. +* `role_arn` - (Optional) *Deprecated* The ARN of an IAM Role used to submit events to Mobile Analytics' event ingestion service. ## Attribute Reference