From 54c82a3f59f4f9f38608a57190b31538cd19370f Mon Sep 17 00:00:00 2001 From: Oussama Bounaim <1151352+obounaim@users.noreply.github.com> Date: Fri, 29 Dec 2023 11:53:36 +0100 Subject: [PATCH 01/21] Add support for optional KMS key ID in repository resource --- internal/service/codecommit/repository.go | 31 +++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/internal/service/codecommit/repository.go b/internal/service/codecommit/repository.go index 3196ae60817..fb7676da1c0 100644 --- a/internal/service/codecommit/repository.go +++ b/internal/service/codecommit/repository.go @@ -52,6 +52,12 @@ func ResourceRepository() *schema.Resource { Computed: true, }, + "kms_key_id": { + Type: schema.TypeString, + Optional: true, + ForceNew: true, + }, + "repository_id": { Type: schema.TypeString, Computed: true, @@ -89,6 +95,10 @@ func resourceRepositoryCreate(ctx context.Context, d *schema.ResourceData, meta Tags: getTagsIn(ctx), } + if v, ok := d.GetOk("kms_key_id"); ok { + input.KmsKeyId = aws.String(v.(string)) + } + out, err := conn.CreateRepositoryWithContext(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "creating CodeCommit Repository: %s", err) @@ -130,6 +140,7 @@ func resourceRepositoryRead(ctx context.Context, d *schema.ResourceData, meta in d.Set("repository_id", out.RepositoryMetadata.RepositoryId) d.Set("arn", out.RepositoryMetadata.Arn) + d.Set("kms_key_id", out.RepositoryMetadata.KmsKeyId) d.Set("clone_url_http", out.RepositoryMetadata.CloneUrlHttp) d.Set("clone_url_ssh", out.RepositoryMetadata.CloneUrlSsh) d.Set("description", out.RepositoryMetadata.RepositoryDescription) @@ -168,6 +179,12 @@ func resourceRepositoryUpdate(ctx context.Context, d *schema.ResourceData, meta } } + if d.HasChange("kms_key_id") { + if err := resourceUpdateEncryptionKey(ctx, conn, d); err != nil { + return sdkdiag.AppendErrorf(diags, "updating CodeCommit Repository (%s) encryption key: %s", d.Id(), err) + } + } + return append(diags, resourceRepositoryRead(ctx, d, meta)...) } @@ -245,3 +262,17 @@ func resourceUpdateDefaultBranch(ctx context.Context, conn *codecommit.CodeCommi return nil } + +func resourceUpdateEncryptionKey(ctx context.Context, conn *codecommit.CodeCommit, d *schema.ResourceData) error { + input := &codecommit.UpdateRepositoryEncryptionKeyInput{ + KmsKeyId: aws.String((d.Get("kms_key_id").(string))), + RepositoryName: aws.String(d.Id()), + } + + _, err := conn.UpdateRepositoryEncryptionKeyWithContext(ctx, input) + if err != nil { + return fmt.Errorf("Updating Repository Encryption Key for CodeCommit Repository: %s", err.Error()) + } + + return nil +} From b2871415a7cdd8ed9f147002102bbc1da1b80f43 Mon Sep 17 00:00:00 2001 From: Oussama Bounaim <1151352+obounaim@users.noreply.github.com> Date: Fri, 29 Dec 2023 15:32:07 +0100 Subject: [PATCH 02/21] Remove ForceNew option for kms_key_id --- internal/service/codecommit/repository.go | 1 - 1 file changed, 1 deletion(-) diff --git a/internal/service/codecommit/repository.go b/internal/service/codecommit/repository.go index fb7676da1c0..58a66acfa4b 100644 --- a/internal/service/codecommit/repository.go +++ b/internal/service/codecommit/repository.go @@ -55,7 +55,6 @@ func ResourceRepository() *schema.Resource { "kms_key_id": { Type: schema.TypeString, Optional: true, - ForceNew: true, }, "repository_id": { From ad6d4fc4690cc80d4771b0e2184c4f466006c9a7 Mon Sep 17 00:00:00 2001 From: Oussama Bounaim <1151352+obounaim@users.noreply.github.com> Date: Fri, 29 Dec 2023 15:35:17 +0100 Subject: [PATCH 03/21] Add "kms_key_id" field to repository data source --- internal/service/codecommit/repository_data_source.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/internal/service/codecommit/repository_data_source.go b/internal/service/codecommit/repository_data_source.go index d569bab4696..0fbfda7c4c4 100644 --- a/internal/service/codecommit/repository_data_source.go +++ b/internal/service/codecommit/repository_data_source.go @@ -34,6 +34,11 @@ func DataSourceRepository() *schema.Resource { Computed: true, }, + "kms_key_id": { + Type: schema.TypeString, + Computed: true, + }, + "repository_id": { Type: schema.TypeString, Computed: true, @@ -78,6 +83,7 @@ func dataSourceRepositoryRead(ctx context.Context, d *schema.ResourceData, meta d.SetId(aws.StringValue(out.RepositoryMetadata.RepositoryName)) d.Set("arn", out.RepositoryMetadata.Arn) + d.Set("kms_key_id", out.RepositoryMetadata.KmsKeyId) d.Set("clone_url_http", out.RepositoryMetadata.CloneUrlHttp) d.Set("clone_url_ssh", out.RepositoryMetadata.CloneUrlSsh) d.Set("repository_name", out.RepositoryMetadata.RepositoryName) From 01a648cc25037df29f9b605049f9fab8ff16f110 Mon Sep 17 00:00:00 2001 From: Oussama Bounaim <1151352+obounaim@users.noreply.github.com> Date: Fri, 29 Dec 2023 15:37:08 +0100 Subject: [PATCH 04/21] Add changelog entry --- .changelog/35095.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/35095.txt diff --git a/.changelog/35095.txt b/.changelog/35095.txt new file mode 100644 index 00000000000..50e040f4be2 --- /dev/null +++ b/.changelog/35095.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +resource/aws_codecommit_repository: Add `kms_key_id` argument, enabling encryption at-rest using AWS KMS Customer Managed Keys (CMK) +``` \ No newline at end of file From 93c75ca6011a2b10b3cd101801f81e0e1898b337 Mon Sep 17 00:00:00 2001 From: Oussama Bounaim <1151352+obounaim@users.noreply.github.com> Date: Fri, 29 Dec 2023 16:14:04 +0100 Subject: [PATCH 05/21] Update documentation --- .../docs/d/codecommit_repository.html.markdown | 5 +++-- .../docs/r/codecommit_repository.html.markdown | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/website/docs/d/codecommit_repository.html.markdown b/website/docs/d/codecommit_repository.html.markdown index ad8da358c36..9a2092bec5d 100644 --- a/website/docs/d/codecommit_repository.html.markdown +++ b/website/docs/d/codecommit_repository.html.markdown @@ -28,7 +28,8 @@ This data source supports the following arguments: This data source exports the following attributes in addition to the arguments above: -* `repository_id` - ID of the repository -* `arn` - ARN of the repository +* `repository_id` - ID of the repository. +* `kms_key_id` - The ID of the encryption key. +* `arn` - ARN of the repository. * `clone_url_http` - URL to use for cloning the repository over HTTPS. * `clone_url_ssh` - URL to use for cloning the repository over SSH. diff --git a/website/docs/r/codecommit_repository.html.markdown b/website/docs/r/codecommit_repository.html.markdown index 6b9cbde6aff..51cf7326e86 100644 --- a/website/docs/r/codecommit_repository.html.markdown +++ b/website/docs/r/codecommit_repository.html.markdown @@ -19,11 +19,27 @@ resource "aws_codecommit_repository" "test" { } ``` +### AWS KMS Customer Managed Keys (CMK) + +```terraform +resource "aws_codecommit_repository" "test" { + repository_name = "MyTestRepository" + description = "This is the Sample App Repository" + kms_key_id = aws_kms_key.test.key_id +} + +resource "aws_kms_key" "test" { + description = "test" + deletion_window_in_days = 7 +} +``` + ## Argument Reference This resource supports the following arguments: * `repository_name` - (Required) The name for the repository. This needs to be less than 100 characters. +* `kms_key_id` - (Optional) The ID of the encryption key. You can view the ID of an encryption key in the KMS console, or use the KMS APIs to programmatically retrieve a key ID. For more information about acceptable values for kmsKeyID, see KeyId in the Decrypt API description in the Key Management Service API Reference. If no key is specified, the default aws/codecommit Amazon Web Services managed key is used. * `description` - (Optional) The description of the repository. This needs to be less than 1000 characters * `default_branch` - (Optional) The default branch of the repository. The branch specified here needs to exist. * `tags` - (Optional) Key-value map of resource tags. If configured with a provider [`default_tags` configuration block](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level. From fd0015be01e256118b36e20b4bec9e6ba289ed56 Mon Sep 17 00:00:00 2001 From: Oussama Bounaim <1151352+obounaim@users.noreply.github.com> Date: Fri, 29 Dec 2023 16:16:43 +0100 Subject: [PATCH 06/21] Add Computed field for kms_key_id --- internal/service/codecommit/repository.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/internal/service/codecommit/repository.go b/internal/service/codecommit/repository.go index 58a66acfa4b..02d16a5cd88 100644 --- a/internal/service/codecommit/repository.go +++ b/internal/service/codecommit/repository.go @@ -55,6 +55,7 @@ func ResourceRepository() *schema.Resource { "kms_key_id": { Type: schema.TypeString, Optional: true, + Computed: true, }, "repository_id": { @@ -106,6 +107,7 @@ func resourceRepositoryCreate(ctx context.Context, d *schema.ResourceData, meta d.SetId(d.Get("repository_name").(string)) d.Set("repository_id", out.RepositoryMetadata.RepositoryId) d.Set("arn", out.RepositoryMetadata.Arn) + d.Set("kms_key_id", out.RepositoryMetadata.KmsKeyId) d.Set("clone_url_http", out.RepositoryMetadata.CloneUrlHttp) d.Set("clone_url_ssh", out.RepositoryMetadata.CloneUrlSsh) From c09cf7216a9b499f62fc769b949359b0880acabf Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 19 Jan 2024 16:39:17 -0500 Subject: [PATCH 07/21] codecommit: Use AWS SDK for Go v2. --- names/data/names_data.csv | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/names/data/names_data.csv b/names/data/names_data.csv index cf8e066f21d..5ff74a2101b 100644 --- a/names/data/names_data.csv +++ b/names/data/names_data.csv @@ -76,7 +76,7 @@ rum,rum,cloudwatchrum,rum,,rum,,cloudwatchrum,RUM,CloudWatchRUM,,1,,,aws_rum_,,r synthetics,synthetics,synthetics,synthetics,,synthetics,,,Synthetics,Synthetics,,1,,,aws_synthetics_,,synthetics_,CloudWatch Synthetics,Amazon,,,,,,,synthetics,,, codeartifact,codeartifact,codeartifact,codeartifact,,codeartifact,,,CodeArtifact,CodeArtifact,,1,,,aws_codeartifact_,,codeartifact_,CodeArtifact,AWS,,,,,,,codeartifact,,, codebuild,codebuild,codebuild,codebuild,,codebuild,,,CodeBuild,CodeBuild,,1,,,aws_codebuild_,,codebuild_,CodeBuild,AWS,,,,,,,CodeBuild,,, -codecommit,codecommit,codecommit,codecommit,,codecommit,,,CodeCommit,CodeCommit,,1,,,aws_codecommit_,,codecommit_,CodeCommit,AWS,,,,,,,CodeCommit,,, +codecommit,codecommit,codecommit,codecommit,,codecommit,,,CodeCommit,CodeCommit,,,2,,aws_codecommit_,,codecommit_,CodeCommit,AWS,,,,,,,CodeCommit,,, deploy,deploy,codedeploy,codedeploy,,deploy,,codedeploy,Deploy,CodeDeploy,,,2,aws_codedeploy_,aws_deploy_,,codedeploy_,CodeDeploy,AWS,,,,,,,CodeDeploy,,, codeguruprofiler,codeguruprofiler,codeguruprofiler,codeguruprofiler,,codeguruprofiler,,,CodeGuruProfiler,CodeGuruProfiler,,,2,,aws_codeguruprofiler_,,codeguruprofiler_,CodeGuru Profiler,Amazon,,,,,,,CodeGuruProfiler,ListProfilingGroups,, codeguru-reviewer,codegurureviewer,codegurureviewer,codegurureviewer,,codegurureviewer,,,CodeGuruReviewer,CodeGuruReviewer,,1,,,aws_codegurureviewer_,,codegurureviewer_,CodeGuru Reviewer,Amazon,,,,,,,CodeGuru Reviewer,,, From df8ae2bb4aea7ef2d4017473430b8e1f9c4cfe15 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 19 Jan 2024 16:41:11 -0500 Subject: [PATCH 08/21] Run 'make gen'. --- internal/conns/awsclient_gen.go | 6 ++--- internal/service/codecommit/generate.go | 2 +- .../service/codecommit/service_package_gen.go | 17 ++++++++------ internal/service/codecommit/tags_gen.go | 22 +++++++++---------- 4 files changed, 25 insertions(+), 22 deletions(-) diff --git a/internal/conns/awsclient_gen.go b/internal/conns/awsclient_gen.go index dbe868fd138..e7e2e68f486 100644 --- a/internal/conns/awsclient_gen.go +++ b/internal/conns/awsclient_gen.go @@ -21,6 +21,7 @@ import ( cloudcontrol_sdkv2 "github.com/aws/aws-sdk-go-v2/service/cloudcontrol" cloudwatchlogs_sdkv2 "github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs" codecatalyst_sdkv2 "github.com/aws/aws-sdk-go-v2/service/codecatalyst" + codecommit_sdkv2 "github.com/aws/aws-sdk-go-v2/service/codecommit" codedeploy_sdkv2 "github.com/aws/aws-sdk-go-v2/service/codedeploy" codeguruprofiler_sdkv2 "github.com/aws/aws-sdk-go-v2/service/codeguruprofiler" codepipeline_sdkv2 "github.com/aws/aws-sdk-go-v2/service/codepipeline" @@ -136,7 +137,6 @@ import ( cloudwatchrum_sdkv1 "github.com/aws/aws-sdk-go/service/cloudwatchrum" codeartifact_sdkv1 "github.com/aws/aws-sdk-go/service/codeartifact" codebuild_sdkv1 "github.com/aws/aws-sdk-go/service/codebuild" - codecommit_sdkv1 "github.com/aws/aws-sdk-go/service/codecommit" codegurureviewer_sdkv1 "github.com/aws/aws-sdk-go/service/codegurureviewer" cognitoidentity_sdkv1 "github.com/aws/aws-sdk-go/service/cognitoidentity" cognitoidentityprovider_sdkv1 "github.com/aws/aws-sdk-go/service/cognitoidentityprovider" @@ -415,8 +415,8 @@ func (c *AWSClient) CodeCatalystClient(ctx context.Context) *codecatalyst_sdkv2. return errs.Must(client[*codecatalyst_sdkv2.Client](ctx, c, names.CodeCatalyst, make(map[string]any))) } -func (c *AWSClient) CodeCommitConn(ctx context.Context) *codecommit_sdkv1.CodeCommit { - return errs.Must(conn[*codecommit_sdkv1.CodeCommit](ctx, c, names.CodeCommit, make(map[string]any))) +func (c *AWSClient) CodeCommitClient(ctx context.Context) *codecommit_sdkv2.Client { + return errs.Must(client[*codecommit_sdkv2.Client](ctx, c, names.CodeCommit, make(map[string]any))) } func (c *AWSClient) CodeGuruProfilerClient(ctx context.Context) *codeguruprofiler_sdkv2.Client { diff --git a/internal/service/codecommit/generate.go b/internal/service/codecommit/generate.go index e9349829fa0..1315b05fb4b 100644 --- a/internal/service/codecommit/generate.go +++ b/internal/service/codecommit/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 -ServiceTagsMap -UpdateTags +//go:generate go run ../../generate/tags/main.go -AWSSDKVersion=2 -ListTags -ServiceTagsMap -UpdateTags //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/codecommit/service_package_gen.go b/internal/service/codecommit/service_package_gen.go index e63566ac80e..6e269d29923 100644 --- a/internal/service/codecommit/service_package_gen.go +++ b/internal/service/codecommit/service_package_gen.go @@ -5,9 +5,8 @@ package codecommit import ( "context" - aws_sdkv1 "github.com/aws/aws-sdk-go/aws" - session_sdkv1 "github.com/aws/aws-sdk-go/aws/session" - codecommit_sdkv1 "github.com/aws/aws-sdk-go/service/codecommit" + aws_sdkv2 "github.com/aws/aws-sdk-go-v2/aws" + codecommit_sdkv2 "github.com/aws/aws-sdk-go-v2/service/codecommit" "github.com/hashicorp/terraform-provider-aws/internal/conns" "github.com/hashicorp/terraform-provider-aws/internal/types" "github.com/hashicorp/terraform-provider-aws/names" @@ -65,11 +64,15 @@ func (p *servicePackage) ServicePackageName() string { return names.CodeCommit } -// 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) (*codecommit_sdkv1.CodeCommit, error) { - sess := config["session"].(*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) (*codecommit_sdkv2.Client, error) { + cfg := *(config["aws_sdkv2_config"].(*aws_sdkv2.Config)) - return codecommit_sdkv1.New(sess.Copy(&aws_sdkv1.Config{Endpoint: aws_sdkv1.String(config["endpoint"].(string))})), nil + return codecommit_sdkv2.NewFromConfig(cfg, func(o *codecommit_sdkv2.Options) { + if endpoint := config["endpoint"].(string); endpoint != "" { + o.BaseEndpoint = aws_sdkv2.String(endpoint) + } + }), nil } func ServicePackage(ctx context.Context) conns.ServicePackage { diff --git a/internal/service/codecommit/tags_gen.go b/internal/service/codecommit/tags_gen.go index 09c1bfeb0a7..e58804b255b 100644 --- a/internal/service/codecommit/tags_gen.go +++ b/internal/service/codecommit/tags_gen.go @@ -5,9 +5,9 @@ import ( "context" "fmt" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/codecommit" - "github.com/aws/aws-sdk-go/service/codecommit/codecommitiface" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/codecommit" + awstypes "github.com/aws/aws-sdk-go-v2/service/codecommit/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 codecommit 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 codecommitiface.CodeCommitAPI, identifier string) (tftags.KeyValueTags, error) { +func listTags(ctx context.Context, conn *codecommit.Client, identifier string, optFns ...func(*codecommit.Options)) (tftags.KeyValueTags, error) { input := &codecommit.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 codecommitiface.CodeCommitAPI, identifie // ListTags lists codecommit 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).CodeCommitConn(ctx), identifier) + tags, err := listTags(ctx, meta.(*conns.AWSClient).CodeCommitClient(ctx), identifier) if err != nil { return err @@ -83,7 +83,7 @@ func setTagsOut(ctx context.Context, tags map[string]*string) { // updateTags updates codecommit 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 codecommitiface.CodeCommitAPI, identifier string, oldTagsMap, newTagsMap any) error { +func updateTags(ctx context.Context, conn *codecommit.Client, identifier string, oldTagsMap, newTagsMap any, optFns ...func(*codecommit.Options)) error { oldTags := tftags.New(ctx, oldTagsMap) newTags := tftags.New(ctx, newTagsMap) @@ -94,10 +94,10 @@ func updateTags(ctx context.Context, conn codecommitiface.CodeCommitAPI, identif if len(removedTags) > 0 { input := &codecommit.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) @@ -112,7 +112,7 @@ func updateTags(ctx context.Context, conn codecommitiface.CodeCommitAPI, identif Tags: Tags(updatedTags), } - _, 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 codecommitiface.CodeCommitAPI, identif // UpdateTags updates codecommit 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).CodeCommitConn(ctx), identifier, oldTags, newTags) + return updateTags(ctx, meta.(*conns.AWSClient).CodeCommitClient(ctx), identifier, oldTags, newTags) } From 78405b00337605a72c4978573dafa76e81d949a1 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 19 Jan 2024 16:42:41 -0500 Subject: [PATCH 09/21] Run 'go get github.com/aws/aws-sdk-go-v2/service/codecommit@v1.20.1 && go mod tidy'. --- go.mod | 1 + go.sum | 2 ++ 2 files changed, 3 insertions(+) diff --git a/go.mod b/go.mod index 00852e2ca9d..56f8dcda82b 100644 --- a/go.mod +++ b/go.mod @@ -27,6 +27,7 @@ require ( github.com/aws/aws-sdk-go-v2/service/cloudcontrol v1.15.7 github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs v1.31.0 github.com/aws/aws-sdk-go-v2/service/codecatalyst v1.10.6 + github.com/aws/aws-sdk-go-v2/service/codecommit v1.20.1 github.com/aws/aws-sdk-go-v2/service/codedeploy v1.22.3 github.com/aws/aws-sdk-go-v2/service/codeguruprofiler v1.18.6 github.com/aws/aws-sdk-go-v2/service/codepipeline v1.22.6 diff --git a/go.sum b/go.sum index 57f2bbff360..027e3bbd233 100644 --- a/go.sum +++ b/go.sum @@ -77,6 +77,8 @@ github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs v1.31.0 h1:Rk+Ft0Mu/eiNt2iJ2 github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs v1.31.0/go.mod h1:jZNaJEtn9TLi3pfxycLz79HVkKxP8ZdYm92iaNFgBsA= github.com/aws/aws-sdk-go-v2/service/codecatalyst v1.10.6 h1:WLVD5wFI3yC1u/8L9bNeZ9+VURSdKjGA1Q+n+F1355Y= github.com/aws/aws-sdk-go-v2/service/codecatalyst v1.10.6/go.mod h1:/lHwoB/rkF3eWMJPvm9wXN7y1THwqCLCOrF7xzA2u9E= +github.com/aws/aws-sdk-go-v2/service/codecommit v1.20.1 h1:FJx0hebVW2EfD5eJjRSkQdWsqFTvZSCSitnMHcOxxko= +github.com/aws/aws-sdk-go-v2/service/codecommit v1.20.1/go.mod h1:aNVj38vn3H6ZZTF+I7As5p9ZIOdB5Jhc1TlNLm9E5FA= github.com/aws/aws-sdk-go-v2/service/codedeploy v1.22.3 h1:KUQmoqL05L+fftMgWLVlk15TL005gxC6NTzU5UiW03E= github.com/aws/aws-sdk-go-v2/service/codedeploy v1.22.3/go.mod h1:NqMyFU67rmETeGllV83ilhMC7r+7KnjeEvux4PYakPk= github.com/aws/aws-sdk-go-v2/service/codeguruprofiler v1.18.6 h1:x5j98Y39PwTePUmTdY5XG7OX9+76sKnA9D88xeCtXcc= From 9a18cfc9225519f404c60fadeca4e4eb0c1028f2 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 19 Jan 2024 16:44:36 -0500 Subject: [PATCH 10/21] codecommit: Fix tagging code generation. --- internal/service/codecommit/generate.go | 2 +- internal/service/codecommit/tags_gen.go | 13 ++++++------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/internal/service/codecommit/generate.go b/internal/service/codecommit/generate.go index 1315b05fb4b..46eec9d594b 100644 --- a/internal/service/codecommit/generate.go +++ b/internal/service/codecommit/generate.go @@ -1,7 +1,7 @@ // Copyright (c) HashiCorp, Inc. // SPDX-License-Identifier: MPL-2.0 -//go:generate go run ../../generate/tags/main.go -AWSSDKVersion=2 -ListTags -ServiceTagsMap -UpdateTags +//go:generate go run ../../generate/tags/main.go -AWSSDKVersion=2 -ListTags -ServiceTagsMap -UpdateTags -KVTValues -SkipTypesImp //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/codecommit/tags_gen.go b/internal/service/codecommit/tags_gen.go index e58804b255b..bada530a226 100644 --- a/internal/service/codecommit/tags_gen.go +++ b/internal/service/codecommit/tags_gen.go @@ -7,7 +7,6 @@ import ( "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/codecommit" - awstypes "github.com/aws/aws-sdk-go-v2/service/codecommit/types" "github.com/hashicorp/terraform-plugin-log/tflog" "github.com/hashicorp/terraform-provider-aws/internal/conns" "github.com/hashicorp/terraform-provider-aws/internal/logging" @@ -49,21 +48,21 @@ func (p *servicePackage) ListTags(ctx context.Context, meta any, identifier stri return nil } -// map[string]*string handling +// map[string]string handling // Tags returns codecommit 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 codecommit 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 codecommit 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 +73,7 @@ func getTagsIn(ctx context.Context) map[string]*string { } // setTagsOut sets codecommit 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)) } From 7cce7f1f25daa5848549b9f2bbb1ff683401a5ca Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Fri, 19 Jan 2024 16:55:12 -0500 Subject: [PATCH 11/21] r/aws_codecommit_approval_rule_template: Migrate to AWS SDK for Go v2. --- .../codecommit/approval_rule_template.go | 108 ++++++++++-------- internal/service/codecommit/exports_test.go | 11 ++ .../service/codecommit/service_package_gen.go | 3 +- 3 files changed, 76 insertions(+), 46 deletions(-) create mode 100644 internal/service/codecommit/exports_test.go diff --git a/internal/service/codecommit/approval_rule_template.go b/internal/service/codecommit/approval_rule_template.go index 85c90e6809a..70e4dd1b7c8 100644 --- a/internal/service/codecommit/approval_rule_template.go +++ b/internal/service/codecommit/approval_rule_template.go @@ -8,34 +8,37 @@ import ( "log" "time" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/codecommit" - "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/codecommit" + "github.com/aws/aws-sdk-go-v2/service/codecommit/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/structure" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" "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" ) -// @SDKResource("aws_codecommit_approval_rule_template") -func ResourceApprovalRuleTemplate() *schema.Resource { +// @SDKResource("aws_codecommit_approval_rule_template", name="Approval Rule Template") +func resourceApprovalRuleTemplate() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourceApprovalRuleTemplateCreate, ReadWithoutTimeout: resourceApprovalRuleTemplateRead, UpdateWithoutTimeout: resourceApprovalRuleTemplateUpdate, DeleteWithoutTimeout: resourceApprovalRuleTemplateDelete, + Importer: &schema.ResourceImporter{ StateContext: schema.ImportStatePassthroughContext, }, Schema: map[string]*schema.Schema{ - "name": { - Type: schema.TypeString, - Required: true, - ValidateFunc: validation.StringLenBetween(1, 100), + "approval_rule_template_id": { + Type: schema.TypeString, + Computed: true, }, "content": { Type: schema.TypeString, @@ -50,19 +53,15 @@ func ResourceApprovalRuleTemplate() *schema.Resource { validation.StringLenBetween(1, 3000), ), }, + "creation_date": { + Type: schema.TypeString, + Computed: true, + }, "description": { Type: schema.TypeString, Optional: true, ValidateFunc: validation.StringLenBetween(0, 1000), }, - "approval_rule_template_id": { - Type: schema.TypeString, - Computed: true, - }, - "creation_date": { - Type: schema.TypeString, - Computed: true, - }, "last_modified_date": { Type: schema.TypeString, Computed: true, @@ -71,6 +70,11 @@ func ResourceApprovalRuleTemplate() *schema.Resource { Type: schema.TypeString, Computed: true, }, + "name": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringLenBetween(1, 100), + }, "rule_content_sha256": { Type: schema.TypeString, Computed: true, @@ -81,10 +85,9 @@ func ResourceApprovalRuleTemplate() *schema.Resource { func resourceApprovalRuleTemplateCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).CodeCommitConn(ctx) + conn := meta.(*conns.AWSClient).CodeCommitClient(ctx) name := d.Get("name").(string) - input := &codecommit.CreateApprovalRuleTemplateInput{ ApprovalRuleTemplateName: aws.String(name), ApprovalRuleTemplateContent: aws.String(d.Get("content").(string)), @@ -94,7 +97,8 @@ func resourceApprovalRuleTemplateCreate(ctx context.Context, d *schema.ResourceD input.ApprovalRuleTemplateDescription = aws.String(v.(string)) } - _, err := conn.CreateApprovalRuleTemplateWithContext(ctx, input) + _, err := conn.CreateApprovalRuleTemplate(ctx, input) + if err != nil { return sdkdiag.AppendErrorf(diags, "creating CodeCommit Approval Rule Template (%s): %s", name, err) } @@ -106,16 +110,12 @@ func resourceApprovalRuleTemplateCreate(ctx context.Context, d *schema.ResourceD func resourceApprovalRuleTemplateRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).CodeCommitConn(ctx) + conn := meta.(*conns.AWSClient).CodeCommitClient(ctx) - input := &codecommit.GetApprovalRuleTemplateInput{ - ApprovalRuleTemplateName: aws.String(d.Id()), - } + result, err := findApprovalRuleTemplateByName(ctx, conn, d.Id()) - resp, err := conn.GetApprovalRuleTemplateWithContext(ctx, input) - - if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, codecommit.ErrCodeApprovalRuleTemplateDoesNotExistException) { - log.Printf("[WARN] CodeCommit Approval Rule Template (%s) not found, removing from state", d.Id()) + if !d.IsNewResource() && tfresource.NotFound(err) { + log.Printf("[WARN] CodeCommit Approval Rule Template %s not found, removing from state", d.Id()) d.SetId("") return diags } @@ -124,19 +124,13 @@ func resourceApprovalRuleTemplateRead(ctx context.Context, d *schema.ResourceDat return sdkdiag.AppendErrorf(diags, "reading CodeCommit Approval Rule Template (%s): %s", d.Id(), err) } - if resp == nil || resp.ApprovalRuleTemplate == nil { - return sdkdiag.AppendErrorf(diags, "reading CodeCommit Approval Rule Template (%s): empty output", d.Id()) - } - - result := resp.ApprovalRuleTemplate - - d.Set("name", result.ApprovalRuleTemplateName) d.Set("approval_rule_template_id", result.ApprovalRuleTemplateId) d.Set("description", result.ApprovalRuleTemplateDescription) d.Set("content", result.ApprovalRuleTemplateContent) d.Set("creation_date", result.CreationDate.Format(time.RFC3339)) d.Set("last_modified_date", result.LastModifiedDate.Format(time.RFC3339)) d.Set("last_modified_user", result.LastModifiedUser) + d.Set("name", result.ApprovalRuleTemplateName) d.Set("rule_content_sha256", result.RuleContentSha256) return diags @@ -144,7 +138,7 @@ func resourceApprovalRuleTemplateRead(ctx context.Context, d *schema.ResourceDat func resourceApprovalRuleTemplateUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).CodeCommitConn(ctx) + conn := meta.(*conns.AWSClient).CodeCommitClient(ctx) if d.HasChange("description") { input := &codecommit.UpdateApprovalRuleTemplateDescriptionInput{ @@ -152,7 +146,7 @@ func resourceApprovalRuleTemplateUpdate(ctx context.Context, d *schema.ResourceD ApprovalRuleTemplateName: aws.String(d.Id()), } - _, err := conn.UpdateApprovalRuleTemplateDescriptionWithContext(ctx, input) + _, err := conn.UpdateApprovalRuleTemplateDescription(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "updating CodeCommit Approval Rule Template (%s) description: %s", d.Id(), err) @@ -166,7 +160,7 @@ func resourceApprovalRuleTemplateUpdate(ctx context.Context, d *schema.ResourceD NewRuleContent: aws.String(d.Get("content").(string)), } - _, err := conn.UpdateApprovalRuleTemplateContentWithContext(ctx, input) + _, err := conn.UpdateApprovalRuleTemplateContent(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "updating CodeCommit Approval Rule Template (%s) content: %s", d.Id(), err) @@ -181,7 +175,7 @@ func resourceApprovalRuleTemplateUpdate(ctx context.Context, d *schema.ResourceD OldApprovalRuleTemplateName: aws.String(d.Id()), } - _, err := conn.UpdateApprovalRuleTemplateNameWithContext(ctx, input) + _, err := conn.UpdateApprovalRuleTemplateName(ctx, input) if err != nil { return sdkdiag.AppendErrorf(diags, "updating CodeCommit Approval Rule Template (%s) name: %s", d.Id(), err) @@ -195,15 +189,14 @@ func resourceApprovalRuleTemplateUpdate(ctx context.Context, d *schema.ResourceD func resourceApprovalRuleTemplateDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).CodeCommitConn(ctx) + conn := meta.(*conns.AWSClient).CodeCommitClient(ctx) - input := &codecommit.DeleteApprovalRuleTemplateInput{ + log.Printf("[INFO] Deleting CodeCommit Approval Rule Template: %s", d.Id()) + _, err := conn.DeleteApprovalRuleTemplate(ctx, &codecommit.DeleteApprovalRuleTemplateInput{ ApprovalRuleTemplateName: aws.String(d.Id()), - } - - _, err := conn.DeleteApprovalRuleTemplateWithContext(ctx, input) + }) - if tfawserr.ErrCodeEquals(err, codecommit.ErrCodeApprovalRuleTemplateDoesNotExistException) { + if errs.IsA[*types.ApprovalRuleTemplateDoesNotExistException](err) { return diags } @@ -213,3 +206,28 @@ func resourceApprovalRuleTemplateDelete(ctx context.Context, d *schema.ResourceD return diags } + +func findApprovalRuleTemplateByName(ctx context.Context, conn *codecommit.Client, name string) (*types.ApprovalRuleTemplate, error) { + input := &codecommit.GetApprovalRuleTemplateInput{ + ApprovalRuleTemplateName: aws.String(name), + } + + output, err := conn.GetApprovalRuleTemplate(ctx, input) + + if errs.IsA[*types.ApprovalRuleTemplateDoesNotExistException](err) { + return nil, &retry.NotFoundError{ + LastError: err, + LastRequest: input, + } + } + + if err != nil { + return nil, err + } + + if output == nil || output.ApprovalRuleTemplate == nil { + return nil, tfresource.NewEmptyResultError(input) + } + + return output.ApprovalRuleTemplate, nil +} diff --git a/internal/service/codecommit/exports_test.go b/internal/service/codecommit/exports_test.go new file mode 100644 index 00000000000..108458f679b --- /dev/null +++ b/internal/service/codecommit/exports_test.go @@ -0,0 +1,11 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package codecommit + +// Exports for use in tests only. +var ( + ResourceApprovalRuleTemplate = resourceApprovalRuleTemplate + + FindApprovalRuleTemplateByName = findApprovalRuleTemplateByName +) diff --git a/internal/service/codecommit/service_package_gen.go b/internal/service/codecommit/service_package_gen.go index 6e269d29923..79922854ccb 100644 --- a/internal/service/codecommit/service_package_gen.go +++ b/internal/service/codecommit/service_package_gen.go @@ -38,8 +38,9 @@ func (p *servicePackage) SDKDataSources(ctx context.Context) []*types.ServicePac func (p *servicePackage) SDKResources(ctx context.Context) []*types.ServicePackageSDKResource { return []*types.ServicePackageSDKResource{ { - Factory: ResourceApprovalRuleTemplate, + Factory: resourceApprovalRuleTemplate, TypeName: "aws_codecommit_approval_rule_template", + Name: "Approval Rule Template", }, { Factory: ResourceApprovalRuleTemplateAssociation, From f02d3b40c5e1952da3d994e96082db56fb481451 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Sun, 21 Jan 2024 16:21:12 -0500 Subject: [PATCH 12/21] d/aws_codecommit_approval_rule_template: Migrate to AWS SDK for Go v2. --- .../approval_rule_template_data_source.go | 35 +++++++------------ .../service/codecommit/service_package_gen.go | 3 +- 2 files changed, 14 insertions(+), 24 deletions(-) diff --git a/internal/service/codecommit/approval_rule_template_data_source.go b/internal/service/codecommit/approval_rule_template_data_source.go index ee4f1a419fe..183f20a3d88 100644 --- a/internal/service/codecommit/approval_rule_template_data_source.go +++ b/internal/service/codecommit/approval_rule_template_data_source.go @@ -7,8 +7,7 @@ import ( "context" "time" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/codecommit" + "github.com/aws/aws-sdk-go-v2/aws" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" @@ -16,17 +15,12 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" ) -// @SDKDataSource("aws_codecommit_approval_rule_template") -func DataSourceApprovalRuleTemplate() *schema.Resource { +// @SDKDataSource("aws_codecommit_approval_rule_template", name="Approval Rule Template") +func dataSourceApprovalRuleTemplate() *schema.Resource { return &schema.Resource{ ReadWithoutTimeout: dataSourceApprovalRuleTemplateRead, Schema: map[string]*schema.Schema{ - "name": { - Type: schema.TypeString, - Required: true, - ValidateFunc: validation.StringLenBetween(1, 100), - }, "approval_rule_template_id": { Type: schema.TypeString, Computed: true, @@ -51,6 +45,11 @@ func DataSourceApprovalRuleTemplate() *schema.Resource { Type: schema.TypeString, Computed: true, }, + "name": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringLenBetween(1, 100), + }, "rule_content_sha256": { Type: schema.TypeString, Computed: true, @@ -61,33 +60,23 @@ func DataSourceApprovalRuleTemplate() *schema.Resource { func dataSourceApprovalRuleTemplateRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).CodeCommitConn(ctx) + conn := meta.(*conns.AWSClient).CodeCommitClient(ctx) templateName := d.Get("name").(string) - input := &codecommit.GetApprovalRuleTemplateInput{ - ApprovalRuleTemplateName: aws.String(templateName), - } - - output, err := conn.GetApprovalRuleTemplateWithContext(ctx, input) + result, err := findApprovalRuleTemplateByName(ctx, conn, templateName) if err != nil { return sdkdiag.AppendErrorf(diags, "reading CodeCommit Approval Rule Template (%s): %s", templateName, err) } - if output == nil || output.ApprovalRuleTemplate == nil { - return sdkdiag.AppendErrorf(diags, "reading CodeCommit Approval Rule Template (%s): empty output", templateName) - } - - result := output.ApprovalRuleTemplate - - d.SetId(aws.StringValue(result.ApprovalRuleTemplateName)) - d.Set("name", result.ApprovalRuleTemplateName) + d.SetId(aws.ToString(result.ApprovalRuleTemplateName)) d.Set("approval_rule_template_id", result.ApprovalRuleTemplateId) d.Set("content", result.ApprovalRuleTemplateContent) d.Set("creation_date", result.CreationDate.Format(time.RFC3339)) d.Set("description", result.ApprovalRuleTemplateDescription) d.Set("last_modified_date", result.LastModifiedDate.Format(time.RFC3339)) d.Set("last_modified_user", result.LastModifiedUser) + d.Set("name", result.ApprovalRuleTemplateName) d.Set("rule_content_sha256", result.RuleContentSha256) return diags diff --git a/internal/service/codecommit/service_package_gen.go b/internal/service/codecommit/service_package_gen.go index 79922854ccb..672a6c21c10 100644 --- a/internal/service/codecommit/service_package_gen.go +++ b/internal/service/codecommit/service_package_gen.go @@ -25,8 +25,9 @@ func (p *servicePackage) FrameworkResources(ctx context.Context) []*types.Servic func (p *servicePackage) SDKDataSources(ctx context.Context) []*types.ServicePackageSDKDataSource { return []*types.ServicePackageSDKDataSource{ { - Factory: DataSourceApprovalRuleTemplate, + Factory: dataSourceApprovalRuleTemplate, TypeName: "aws_codecommit_approval_rule_template", + Name: "Approval Rule Template", }, { Factory: DataSourceRepository, From 266cc9eea8779f004ef355f3e6a72ff4f8354b05 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Sun, 21 Jan 2024 16:37:45 -0500 Subject: [PATCH 13/21] r/aws_codecommit_approval_rule_template_association: Migrate to AWS SDK for Go v2. --- .../approval_rule_template_association.go | 109 +++++++++++++----- internal/service/codecommit/exports_test.go | 6 +- internal/service/codecommit/find.go | 58 ---------- .../service/codecommit/service_package_gen.go | 3 +- 4 files changed, 87 insertions(+), 89 deletions(-) delete mode 100644 internal/service/codecommit/find.go diff --git a/internal/service/codecommit/approval_rule_template_association.go b/internal/service/codecommit/approval_rule_template_association.go index 92eed71df5c..483d11934ef 100644 --- a/internal/service/codecommit/approval_rule_template_association.go +++ b/internal/service/codecommit/approval_rule_template_association.go @@ -10,23 +10,27 @@ import ( "strings" "github.com/YakDriver/regexache" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/codecommit" - "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/codecommit" + "github.com/aws/aws-sdk-go-v2/service/codecommit/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/errs" "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" + tfslices "github.com/hashicorp/terraform-provider-aws/internal/slices" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" ) -// @SDKResource("aws_codecommit_approval_rule_template_association") -func ResourceApprovalRuleTemplateAssociation() *schema.Resource { +// @SDKResource("aws_codecommit_approval_rule_template_association", name="Approval Rule Template Association") +func resourceApprovalRuleTemplateAssociation() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourceApprovalRuleTemplateAssociationCreate, ReadWithoutTimeout: resourceApprovalRuleTemplateAssociationRead, DeleteWithoutTimeout: resourceApprovalRuleTemplateAssociationDelete, + Importer: &schema.ResourceImporter{ StateContext: schema.ImportStatePassthroughContext, }, @@ -53,38 +57,37 @@ func ResourceApprovalRuleTemplateAssociation() *schema.Resource { func resourceApprovalRuleTemplateAssociationCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).CodeCommitConn(ctx) + conn := meta.(*conns.AWSClient).CodeCommitClient(ctx) approvalRuleTemplateName := d.Get("approval_rule_template_name").(string) repositoryName := d.Get("repository_name").(string) - + id := approvalRuleTemplateAssociationCreateResourceID(approvalRuleTemplateName, repositoryName) input := &codecommit.AssociateApprovalRuleTemplateWithRepositoryInput{ ApprovalRuleTemplateName: aws.String(approvalRuleTemplateName), RepositoryName: aws.String(repositoryName), } - _, err := conn.AssociateApprovalRuleTemplateWithRepositoryWithContext(ctx, input) + _, err := conn.AssociateApprovalRuleTemplateWithRepository(ctx, input) if err != nil { - return sdkdiag.AppendErrorf(diags, "associating CodeCommit Approval Rule Template (%s) with repository (%s): %s", approvalRuleTemplateName, repositoryName, err) + return sdkdiag.AppendErrorf(diags, "creating CodeCommit Approval Rule Template Association (%s): %s", id, err) } - d.SetId(fmt.Sprintf("%s,%s", approvalRuleTemplateName, repositoryName)) + d.SetId(id) return append(diags, resourceApprovalRuleTemplateAssociationRead(ctx, d, meta)...) } func resourceApprovalRuleTemplateAssociationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).CodeCommitConn(ctx) - - approvalRuleTemplateName, repositoryName, err := ApprovalRuleTemplateAssociationParseID(d.Id()) + conn := meta.(*conns.AWSClient).CodeCommitClient(ctx) + approvalRuleTemplateName, repositoryName, err := approvalRuleTemplateAssociationParseResourceID(d.Id()) if err != nil { - return sdkdiag.AppendErrorf(diags, "reading CodeCommit Approval Rule Template Association (%s): %s", d.Id(), err) + return sdkdiag.AppendFromErr(diags, err) } - err = FindApprovalRuleTemplateAssociation(ctx, conn, approvalRuleTemplateName, repositoryName) + _, err = findApprovalRuleTemplateAssociationByTwoPartKey(ctx, conn, approvalRuleTemplateName, repositoryName) if !d.IsNewResource() && tfresource.NotFound(err) { log.Printf("[WARN] CodeCommit Approval Rule Template Association (%s) not found, removing from state", d.Id()) @@ -104,37 +107,87 @@ func resourceApprovalRuleTemplateAssociationRead(ctx context.Context, d *schema. func resourceApprovalRuleTemplateAssociationDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).CodeCommitConn(ctx) - - approvalRuleTemplateName, repositoryName, err := ApprovalRuleTemplateAssociationParseID(d.Id()) + conn := meta.(*conns.AWSClient).CodeCommitClient(ctx) + approvalRuleTemplateName, repositoryName, err := approvalRuleTemplateAssociationParseResourceID(d.Id()) if err != nil { - return sdkdiag.AppendErrorf(diags, "deleting CodeCommit Approval Rule Template (%s) from repository (%s): %s", approvalRuleTemplateName, repositoryName, err) + return sdkdiag.AppendFromErr(diags, err) } - input := &codecommit.DisassociateApprovalRuleTemplateFromRepositoryInput{ + log.Printf("[INFO] Deleting CodeCommit Approval Rule Template Association: %s", d.Id()) + _, err = conn.DisassociateApprovalRuleTemplateFromRepository(ctx, &codecommit.DisassociateApprovalRuleTemplateFromRepositoryInput{ ApprovalRuleTemplateName: aws.String(approvalRuleTemplateName), RepositoryName: aws.String(repositoryName), - } + }) - _, err = conn.DisassociateApprovalRuleTemplateFromRepositoryWithContext(ctx, input) - - if tfawserr.ErrCodeEquals(err, codecommit.ErrCodeApprovalRuleTemplateDoesNotExistException, codecommit.ErrCodeRepositoryDoesNotExistException) { + if errs.IsA[*types.ApprovalRuleTemplateDoesNotExistException](err) || errs.IsA[*types.RepositoryDoesNotExistException](err) { return diags } if err != nil { - return sdkdiag.AppendErrorf(diags, "deleting CodeCommit Approval Rule Template (%s) from repository (%s): %s", approvalRuleTemplateName, repositoryName, err) + return sdkdiag.AppendErrorf(diags, "deleting CodeCommit Approval Rule Template Association (%s): %s", d.Id(), err) } return diags } -func ApprovalRuleTemplateAssociationParseID(id string) (string, string, error) { - parts := strings.SplitN(id, ",", 2) +const approvalRuleTemplateAssociationResourceIDSeparator = "," + +func approvalRuleTemplateAssociationCreateResourceID(approvalRuleTemplateName, repositoryName string) string { + parts := []string{approvalRuleTemplateName, repositoryName} + id := strings.Join(parts, approvalRuleTemplateAssociationResourceIDSeparator) + + return id +} + +func approvalRuleTemplateAssociationParseResourceID(id string) (string, string, error) { + parts := strings.SplitN(id, approvalRuleTemplateAssociationResourceIDSeparator, 2) + if len(parts) != 2 || parts[0] == "" || parts[1] == "" { - return "", "", fmt.Errorf("unexpected format of ID (%s), expected APPROVAL_RULE_TEMPLATE_NAME,REPOSITORY_NAME", id) + return "", "", fmt.Errorf("unexpected format of ID (%s), expected APPROVAL_RULE_TEMPLATE_NAME%[2]sREPOSITORY_NAME", id, approvalRuleTemplateAssociationResourceIDSeparator) } return parts[0], parts[1], nil } + +func findApprovalRuleTemplateAssociationByTwoPartKey(ctx context.Context, conn *codecommit.Client, approvalRuleTemplateName, repositoryName string) (*string, error) { + input := &codecommit.ListRepositoriesForApprovalRuleTemplateInput{ + ApprovalRuleTemplateName: aws.String(approvalRuleTemplateName), + } + + output, err := findApprovalRuleTemplateRepositories(ctx, conn, input) + + if err != nil { + return nil, err + } + + output = tfslices.Filter(output, func(v string) bool { + return v == repositoryName + }) + + return tfresource.AssertSingleValueResult(output) +} + +func findApprovalRuleTemplateRepositories(ctx context.Context, conn *codecommit.Client, input *codecommit.ListRepositoriesForApprovalRuleTemplateInput) ([]string, error) { + var output []string + + pages := codecommit.NewListRepositoriesForApprovalRuleTemplatePaginator(conn, input) + for pages.HasMorePages() { + page, err := pages.NextPage(ctx) + + if errs.IsA[*types.ApprovalRuleTemplateDoesNotExistException](err) { + return nil, &retry.NotFoundError{ + LastError: err, + LastRequest: input, + } + } + + if err != nil { + return nil, err + } + + output = append(output, page.RepositoryNames...) + } + + return output, nil +} diff --git a/internal/service/codecommit/exports_test.go b/internal/service/codecommit/exports_test.go index 108458f679b..7e0ee183952 100644 --- a/internal/service/codecommit/exports_test.go +++ b/internal/service/codecommit/exports_test.go @@ -5,7 +5,9 @@ package codecommit // Exports for use in tests only. var ( - ResourceApprovalRuleTemplate = resourceApprovalRuleTemplate + ResourceApprovalRuleTemplate = resourceApprovalRuleTemplate + ResourceApprovalRuleTemplateAssociation = resourceApprovalRuleTemplateAssociation - FindApprovalRuleTemplateByName = findApprovalRuleTemplateByName + FindApprovalRuleTemplateAssociationByTwoPartKey = findApprovalRuleTemplateAssociationByTwoPartKey + FindApprovalRuleTemplateByName = findApprovalRuleTemplateByName ) diff --git a/internal/service/codecommit/find.go b/internal/service/codecommit/find.go deleted file mode 100644 index 3f5f68ab4a9..00000000000 --- a/internal/service/codecommit/find.go +++ /dev/null @@ -1,58 +0,0 @@ -// Copyright (c) HashiCorp, Inc. -// SPDX-License-Identifier: MPL-2.0 - -package codecommit - -import ( - "context" - "fmt" - - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/codecommit" - "github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr" - "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry" -) - -// FindApprovalRuleTemplateAssociation validates that an approval rule template has the named associated repository -func FindApprovalRuleTemplateAssociation(ctx context.Context, conn *codecommit.CodeCommit, approvalRuleTemplateName, repositoryName string) error { - input := &codecommit.ListRepositoriesForApprovalRuleTemplateInput{ - ApprovalRuleTemplateName: aws.String(approvalRuleTemplateName), - } - - found := false - - err := conn.ListRepositoriesForApprovalRuleTemplatePagesWithContext(ctx, input, func(page *codecommit.ListRepositoriesForApprovalRuleTemplateOutput, lastPage bool) bool { - if page == nil { - return !lastPage - } - - for _, repoName := range page.RepositoryNames { - if aws.StringValue(repoName) == repositoryName { - found = true - return false - } - } - - return !lastPage - }) - - if tfawserr.ErrCodeEquals(err, codecommit.ErrCodeApprovalRuleTemplateDoesNotExistException) { - return &retry.NotFoundError{ - LastError: err, - LastRequest: input, - } - } - - if err != nil { - return err - } - - if !found { - return &retry.NotFoundError{ - Message: fmt.Sprintf("No approval rule template (%q) associated with repository (%q)", approvalRuleTemplateName, repositoryName), - LastRequest: input, - } - } - - return nil -} diff --git a/internal/service/codecommit/service_package_gen.go b/internal/service/codecommit/service_package_gen.go index 672a6c21c10..b3850c59a1c 100644 --- a/internal/service/codecommit/service_package_gen.go +++ b/internal/service/codecommit/service_package_gen.go @@ -44,8 +44,9 @@ func (p *servicePackage) SDKResources(ctx context.Context) []*types.ServicePacka Name: "Approval Rule Template", }, { - Factory: ResourceApprovalRuleTemplateAssociation, + Factory: resourceApprovalRuleTemplateAssociation, TypeName: "aws_codecommit_approval_rule_template_association", + Name: "Approval Rule Template Association", }, { Factory: ResourceRepository, From 962947c5bc98d7036d35b07f85bd3e118bba0ea7 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Sun, 21 Jan 2024 17:09:26 -0500 Subject: [PATCH 14/21] r/aws_codecommit_trigger: Migrate to AWS SDK for Go v2. --- .changelog/35095.txt | 4 + internal/service/codecommit/exports_test.go | 2 + .../service/codecommit/service_package_gen.go | 3 +- internal/service/codecommit/trigger.go | 230 ++++++++++++------ 4 files changed, 166 insertions(+), 73 deletions(-) diff --git a/.changelog/35095.txt b/.changelog/35095.txt index 50e040f4be2..9a139310b8c 100644 --- a/.changelog/35095.txt +++ b/.changelog/35095.txt @@ -1,3 +1,7 @@ ```release-note:enhancement resource/aws_codecommit_repository: Add `kms_key_id` argument, enabling encryption at-rest using AWS KMS Customer Managed Keys (CMK) +``` + +```release-note:enhancement +resource/aws_codecommit_trigger: Add plan-time validation of `trigger.destination_arn` and `trigger.events` ``` \ No newline at end of file diff --git a/internal/service/codecommit/exports_test.go b/internal/service/codecommit/exports_test.go index 7e0ee183952..e873bcbd88d 100644 --- a/internal/service/codecommit/exports_test.go +++ b/internal/service/codecommit/exports_test.go @@ -7,7 +7,9 @@ package codecommit var ( ResourceApprovalRuleTemplate = resourceApprovalRuleTemplate ResourceApprovalRuleTemplateAssociation = resourceApprovalRuleTemplateAssociation + ResourceTrigger = resourceTrigger FindApprovalRuleTemplateAssociationByTwoPartKey = findApprovalRuleTemplateAssociationByTwoPartKey FindApprovalRuleTemplateByName = findApprovalRuleTemplateByName + FindRepositoryTriggersByName = findRepositoryTriggersByName ) diff --git a/internal/service/codecommit/service_package_gen.go b/internal/service/codecommit/service_package_gen.go index b3850c59a1c..c81764ec931 100644 --- a/internal/service/codecommit/service_package_gen.go +++ b/internal/service/codecommit/service_package_gen.go @@ -57,8 +57,9 @@ func (p *servicePackage) SDKResources(ctx context.Context) []*types.ServicePacka }, }, { - Factory: ResourceTrigger, + Factory: resourceTrigger, TypeName: "aws_codecommit_trigger", + Name: "Trigger", }, } } diff --git a/internal/service/codecommit/trigger.go b/internal/service/codecommit/trigger.go index a0af93e7123..9b9d317b5d0 100644 --- a/internal/service/codecommit/trigger.go +++ b/internal/service/codecommit/trigger.go @@ -7,31 +7,38 @@ import ( "context" "log" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/codecommit" + "github.com/aws/aws-sdk-go-v2/aws" + "github.com/aws/aws-sdk-go-v2/service/codecommit" + "github.com/aws/aws-sdk-go-v2/service/codecommit/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/enum" + "github.com/hashicorp/terraform-provider-aws/internal/errs" "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" + "github.com/hashicorp/terraform-provider-aws/internal/flex" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" + "github.com/hashicorp/terraform-provider-aws/internal/verify" ) -// @SDKResource("aws_codecommit_trigger") -func ResourceTrigger() *schema.Resource { +// @SDKResource("aws_codecommit_trigger", name="Trigger") +func resourceTrigger() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourceTriggerCreate, ReadWithoutTimeout: resourceTriggerRead, DeleteWithoutTimeout: resourceTriggerDelete, Schema: map[string]*schema.Schema{ + "configuration_id": { + Type: schema.TypeString, + Computed: true, + }, "repository_name": { Type: schema.TypeString, ForceNew: true, Required: true, }, - "configuration_id": { - Type: schema.TypeString, - Computed: true, - }, "trigger": { Type: schema.TypeSet, ForceNew: true, @@ -39,36 +46,36 @@ func ResourceTrigger() *schema.Resource { MaxItems: 10, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ - "name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - }, - - "destination_arn": { - Type: schema.TypeString, - Required: true, + "branches": { + Type: schema.TypeList, + Optional: true, ForceNew: true, + Elem: &schema.Schema{Type: schema.TypeString}, }, - "custom_data": { Type: schema.TypeString, Optional: true, ForceNew: true, }, - - "branches": { - Type: schema.TypeList, - Optional: true, - ForceNew: true, - Elem: &schema.Schema{Type: schema.TypeString}, + "destination_arn": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: verify.ValidARN, }, - "events": { Type: schema.TypeList, Required: true, ForceNew: true, - Elem: &schema.Schema{Type: schema.TypeString}, + Elem: &schema.Schema{ + Type: schema.TypeString, + ValidateDiagFunc: enum.Validate[types.RepositoryTriggerEventEnum](), + }, + }, + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, }, }, }, @@ -79,92 +86,171 @@ func ResourceTrigger() *schema.Resource { func resourceTriggerCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).CodeCommitConn(ctx) - - // Expand the "trigger" set to aws-sdk-go compat []*codecommit.RepositoryTrigger - triggers := expandTriggers(d.Get("trigger").(*schema.Set).List()) + conn := meta.(*conns.AWSClient).CodeCommitClient(ctx) + repositoryName := d.Get("repository_name").(string) input := &codecommit.PutRepositoryTriggersInput{ - RepositoryName: aws.String(d.Get("repository_name").(string)), - Triggers: triggers, + RepositoryName: aws.String(repositoryName), + Triggers: expandRepositoryTriggers(d.Get("trigger").(*schema.Set).List()), } - resp, err := conn.PutRepositoryTriggersWithContext(ctx, input) + _, err := conn.PutRepositoryTriggers(ctx, input) + if err != nil { - return sdkdiag.AppendErrorf(diags, "creating CodeCommit Trigger: %s", err) + return sdkdiag.AppendErrorf(diags, "creating CodeCommit Trigger (%s): %s", repositoryName, err) } - log.Printf("[INFO] Code Commit Trigger Created %s input %s", resp, input) - - d.SetId(d.Get("repository_name").(string)) - d.Set("configuration_id", resp.ConfigurationId) + d.SetId(repositoryName) return append(diags, resourceTriggerRead(ctx, d, meta)...) } func resourceTriggerRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).CodeCommitConn(ctx) + conn := meta.(*conns.AWSClient).CodeCommitClient(ctx) - input := &codecommit.GetRepositoryTriggersInput{ - RepositoryName: aws.String(d.Id()), + output, err := findRepositoryTriggersByName(ctx, conn, d.Id()) + + if !d.IsNewResource() && tfresource.NotFound(err) { + log.Printf("[WARN] CodeCommit Trigger %s not found, removing from state", d.Id()) + d.SetId("") + return diags } - resp, err := conn.GetRepositoryTriggersWithContext(ctx, input) if err != nil { - return sdkdiag.AppendErrorf(diags, "reading CodeCommit Trigger: %s", err.Error()) + return sdkdiag.AppendErrorf(diags, "reading CodeCommit Trigger (%s): %s", d.Id(), err) } - log.Printf("[DEBUG] CodeCommit Trigger: %s", resp) + d.Set("configuration_id", output.ConfigurationId) + d.Set("respository_name", d.Id()) + if err := d.Set("trigger", flattenRepositoryTriggers(output.Triggers)); err != nil { + return sdkdiag.AppendErrorf(diags, "setting trigger: %s", err) + } return diags } func resourceTriggerDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).CodeCommitConn(ctx) - - log.Printf("[DEBUG] Deleting Trigger: %q", d.Id()) + conn := meta.(*conns.AWSClient).CodeCommitClient(ctx) + log.Printf("[DEBUG] Deleting CodeCommit Trigger: %s", d.Id()) input := &codecommit.PutRepositoryTriggersInput{ - RepositoryName: aws.String(d.Get("repository_name").(string)), - Triggers: []*codecommit.RepositoryTrigger{}, + RepositoryName: aws.String(d.Id()), + Triggers: []types.RepositoryTrigger{}, + } + + _, err := conn.PutRepositoryTriggers(ctx, input) + + if errs.IsA[*types.RepositoryDoesNotExistException](err) { + return diags } - if _, err := conn.PutRepositoryTriggersWithContext(ctx, input); err != nil { + if err != nil { return sdkdiag.AppendErrorf(diags, "deleting CodeCommit Trigger (%s): %s", d.Id(), err) } return diags } -func expandTriggers(configured []interface{}) []*codecommit.RepositoryTrigger { - triggers := make([]*codecommit.RepositoryTrigger, 0, len(configured)) - // Loop over our configured triggers and create - // an array of aws-sdk-go compatible objects - for _, lRaw := range configured { - data := lRaw.(map[string]interface{}) - t := &codecommit.RepositoryTrigger{ - CustomData: aws.String(data["custom_data"].(string)), - DestinationArn: aws.String(data["destination_arn"].(string)), - Name: aws.String(data["name"].(string)), +func findRepositoryTriggersByName(ctx context.Context, conn *codecommit.Client, repositoryName string) (*codecommit.GetRepositoryTriggersOutput, error) { + input := &codecommit.GetRepositoryTriggersInput{ + RepositoryName: aws.String(repositoryName), + } + + output, err := conn.GetRepositoryTriggers(ctx, input) + + if errs.IsA[*types.RepositoryDoesNotExistException](err) { + return nil, &retry.NotFoundError{ + LastError: err, + LastRequest: input, + } + } + + if err != nil { + return nil, err + } + + if output == nil { + return nil, tfresource.NewEmptyResultError(input) + } + + return output, nil +} + +func expandRepositoryTriggers(tfList []interface{}) []types.RepositoryTrigger { + if len(tfList) == 0 { + return nil + } + + apiObjects := make([]types.RepositoryTrigger, 0, len(tfList)) + + for _, tfMapRaw := range tfList { + tfMap, ok := tfMapRaw.(map[string]interface{}) + if !ok { + continue + } + + apiObject := types.RepositoryTrigger{} + + if v, ok := tfMap["branches"].([]interface{}); ok && len(v) > 0 { + apiObject.Branches = flex.ExpandStringValueList(v) + } + + if v, ok := tfMap["custom_data"].(string); ok && v != "" { + apiObject.CustomData = aws.String(v) + } + + if v, ok := tfMap["destination_arn"].(string); ok && v != "" { + apiObject.DestinationArn = aws.String(v) + } + + if v, ok := tfMap["events"].([]interface{}); ok && len(v) > 0 { + apiObject.Events = flex.ExpandStringyValueList[types.RepositoryTriggerEventEnum](v) + } + + if v, ok := tfMap["name"].(string); ok && v != "" { + apiObject.Name = aws.String(v) + } + + apiObjects = append(apiObjects, apiObject) + } + + return apiObjects +} + +func flattenRepositoryTriggers(apiObjects []types.RepositoryTrigger) []interface{} { + if len(apiObjects) == 0 { + return nil + } + + var tfList []interface{} + + for _, apiObject := range apiObjects { + tfMap := map[string]interface{}{} + + if v := apiObject.Branches; v != nil { + tfMap["branches"] = v } - branches := make([]*string, len(data["branches"].([]interface{}))) - for i, vv := range data["branches"].([]interface{}) { - str := vv.(string) - branches[i] = aws.String(str) + if v := apiObject.CustomData; v != nil { + tfMap["custom_data"] = aws.ToString(v) } - t.Branches = branches - events := make([]*string, len(data["events"].([]interface{}))) - for i, vv := range data["events"].([]interface{}) { - str := vv.(string) - events[i] = aws.String(str) + if v := apiObject.DestinationArn; v != nil { + tfMap["destination_arn"] = aws.ToString(v) } - t.Events = events - triggers = append(triggers, t) + if v := apiObject.Events; v != nil { + tfMap["events"] = v + } + + if v := apiObject.Name; v != nil { + tfMap["name"] = aws.ToString(v) + } + + tfList = append(tfList, tfMap) } - return triggers + + return tfList } From f3b8d3ab3afbef2662f0e23339fefef1e0ad5326 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Sun, 21 Jan 2024 17:28:51 -0500 Subject: [PATCH 15/21] r/aws_codecommit_repository: Migrate to AWS SDK for Go v2. --- .changelog/35095.txt | 2 +- internal/service/codecommit/exports_test.go | 1 + internal/service/codecommit/repository.go | 245 +++++++++--------- .../service/codecommit/service_package_gen.go | 2 +- 4 files changed, 124 insertions(+), 126 deletions(-) diff --git a/.changelog/35095.txt b/.changelog/35095.txt index 9a139310b8c..784a66db36a 100644 --- a/.changelog/35095.txt +++ b/.changelog/35095.txt @@ -1,5 +1,5 @@ ```release-note:enhancement -resource/aws_codecommit_repository: Add `kms_key_id` argument, enabling encryption at-rest using AWS KMS Customer Managed Keys (CMK) +resource/aws_codecommit_repository: Add `kms_key_id` argument ``` ```release-note:enhancement diff --git a/internal/service/codecommit/exports_test.go b/internal/service/codecommit/exports_test.go index e873bcbd88d..1cc39357362 100644 --- a/internal/service/codecommit/exports_test.go +++ b/internal/service/codecommit/exports_test.go @@ -7,6 +7,7 @@ package codecommit var ( ResourceApprovalRuleTemplate = resourceApprovalRuleTemplate ResourceApprovalRuleTemplateAssociation = resourceApprovalRuleTemplateAssociation + ResourceRepository = resourceRepository ResourceTrigger = resourceTrigger FindApprovalRuleTemplateAssociationByTwoPartKey = findApprovalRuleTemplateAssociationByTwoPartKey diff --git a/internal/service/codecommit/repository.go b/internal/service/codecommit/repository.go index 02d16a5cd88..f5c13c60cc6 100644 --- a/internal/service/codecommit/repository.go +++ b/internal/service/codecommit/repository.go @@ -8,74 +8,70 @@ import ( "fmt" "log" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/codecommit" - "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/codecommit" + "github.com/aws/aws-sdk-go-v2/service/codecommit/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/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" "github.com/hashicorp/terraform-provider-aws/internal/verify" "github.com/hashicorp/terraform-provider-aws/names" ) // @SDKResource("aws_codecommit_repository", name="Repository") // @Tags(identifierAttribute="arn") -func ResourceRepository() *schema.Resource { +func resourceRepository() *schema.Resource { return &schema.Resource{ CreateWithoutTimeout: resourceRepositoryCreate, UpdateWithoutTimeout: resourceRepositoryUpdate, ReadWithoutTimeout: resourceRepositoryRead, DeleteWithoutTimeout: resourceRepositoryDelete, + Importer: &schema.ResourceImporter{ StateContext: schema.ImportStatePassthroughContext, }, Schema: map[string]*schema.Schema{ - "repository_name": { - Type: schema.TypeString, - Required: true, - ValidateFunc: validation.StringLenBetween(0, 100), - }, - - "description": { - Type: schema.TypeString, - Optional: true, - ValidateFunc: validation.StringLenBetween(0, 1000), - }, - "arn": { Type: schema.TypeString, Computed: true, }, - - "kms_key_id": { + "clone_url_http": { Type: schema.TypeString, - Optional: true, Computed: true, }, - - "repository_id": { + "clone_url_ssh": { Type: schema.TypeString, Computed: true, }, - - "clone_url_http": { + "default_branch": { Type: schema.TypeString, - Computed: true, + Optional: true, }, - - "clone_url_ssh": { + "description": { + Type: schema.TypeString, + Optional: true, + ValidateFunc: validation.StringLenBetween(0, 1000), + }, + "kms_key_id": { Type: schema.TypeString, + Optional: true, Computed: true, }, - - "default_branch": { + "repository_id": { Type: schema.TypeString, - Optional: true, + Computed: true, + }, + "repository_name": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringLenBetween(0, 100), }, names.AttrTags: tftags.TagsSchema(), names.AttrTagsAll: tftags.TagsSchemaComputed(), @@ -87,33 +83,33 @@ func ResourceRepository() *schema.Resource { func resourceRepositoryCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).CodeCommitConn(ctx) + conn := meta.(*conns.AWSClient).CodeCommitClient(ctx) + name := d.Get("repository_name").(string) input := &codecommit.CreateRepositoryInput{ - RepositoryName: aws.String(d.Get("repository_name").(string)), - RepositoryDescription: aws.String(d.Get("description").(string)), - Tags: getTagsIn(ctx), + RepositoryName: aws.String(name), + Tags: getTagsIn(ctx), } if v, ok := d.GetOk("kms_key_id"); ok { input.KmsKeyId = aws.String(v.(string)) } - out, err := conn.CreateRepositoryWithContext(ctx, input) + if v, ok := d.GetOk("description"); ok { + input.RepositoryDescription = aws.String(v.(string)) + } + + _, err := conn.CreateRepository(ctx, input) + if err != nil { - return sdkdiag.AppendErrorf(diags, "creating CodeCommit Repository: %s", err) + return sdkdiag.AppendErrorf(diags, "creating CodeCommit Repository (%s): %s", name, err) } - d.SetId(d.Get("repository_name").(string)) - d.Set("repository_id", out.RepositoryMetadata.RepositoryId) - d.Set("arn", out.RepositoryMetadata.Arn) - d.Set("kms_key_id", out.RepositoryMetadata.KmsKeyId) - d.Set("clone_url_http", out.RepositoryMetadata.CloneUrlHttp) - d.Set("clone_url_ssh", out.RepositoryMetadata.CloneUrlSsh) + d.SetId(name) - if _, ok := d.GetOk("default_branch"); ok { - if err := resourceUpdateDefaultBranch(ctx, conn, d); err != nil { - return sdkdiag.AppendErrorf(diags, "updating CodeCommit Repository (%s) default branch: %s", d.Id(), err) + if v, ok := d.GetOk("default_branch"); ok { + if err := updateRepositoryDefaultBranch(ctx, conn, d.Id(), v.(string)); err != nil { + return sdkdiag.AppendFromErr(diags, err) } } @@ -122,66 +118,86 @@ func resourceRepositoryCreate(ctx context.Context, d *schema.ResourceData, meta func resourceRepositoryRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).CodeCommitConn(ctx) + conn := meta.(*conns.AWSClient).CodeCommitClient(ctx) - input := &codecommit.GetRepositoryInput{ - RepositoryName: aws.String(d.Id()), - } + repository, err := findRepositoryByName(ctx, conn, d.Id()) - out, err := conn.GetRepositoryWithContext(ctx, input) - if !d.IsNewResource() && tfawserr.ErrCodeEquals(err, codecommit.ErrCodeRepositoryDoesNotExistException) { - create.LogNotFoundRemoveState(names.CodeCommit, create.ErrActionReading, ResNameRepository, d.Id()) + if !d.IsNewResource() && tfresource.NotFound(err) { + log.Printf("[WARN] CodeCommit Repository %s not found, removing from state", d.Id()) d.SetId("") return diags } if err != nil { - return create.AppendDiagError(diags, names.CodeCommit, create.ErrActionReading, ResNameRepository, d.Id(), err) + return sdkdiag.AppendErrorf(diags, "reading CodeCommit Repository (%s): %s", d.Id(), err) } - d.Set("repository_id", out.RepositoryMetadata.RepositoryId) - d.Set("arn", out.RepositoryMetadata.Arn) - d.Set("kms_key_id", out.RepositoryMetadata.KmsKeyId) - d.Set("clone_url_http", out.RepositoryMetadata.CloneUrlHttp) - d.Set("clone_url_ssh", out.RepositoryMetadata.CloneUrlSsh) - d.Set("description", out.RepositoryMetadata.RepositoryDescription) - d.Set("repository_name", out.RepositoryMetadata.RepositoryName) - + d.Set("arn", repository.Arn) + d.Set("clone_url_http", repository.CloneUrlHttp) + d.Set("clone_url_ssh", repository.CloneUrlSsh) if _, ok := d.GetOk("default_branch"); ok { - // The default branch can only be set when there is code in the repository - // Preserve the configured value - if out.RepositoryMetadata.DefaultBranch != nil { // nosemgrep:ci.helper-schema-ResourceData-Set-extraneous-nil-check - d.Set("default_branch", out.RepositoryMetadata.DefaultBranch) + // The default branch can only be set when there is code in the repository. + // Preserve the configured value. + if v := repository.DefaultBranch; v != nil { // nosemgrep:ci.helper-schema-ResourceData-Set-extraneous-nil-check + d.Set("default_branch", v) } } + d.Set("description", repository.RepositoryDescription) + d.Set("kms_key_id", repository.KmsKeyId) + d.Set("repository_id", repository.RepositoryId) + d.Set("repository_name", repository.RepositoryName) return diags } func resourceRepositoryUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).CodeCommitConn(ctx) + conn := meta.(*conns.AWSClient).CodeCommitClient(ctx) if d.HasChange("repository_name") { - if err := resourceUpdateRepositoryName(ctx, conn, d); err != nil { + newName := d.Get("repository_name").(string) + input := &codecommit.UpdateRepositoryNameInput{ + NewName: aws.String(newName), + OldName: aws.String(d.Id()), + } + + _, err := conn.UpdateRepositoryName(ctx, input) + + if err != nil { return sdkdiag.AppendErrorf(diags, "updating CodeCommit Repository (%s) name: %s", d.Id(), err) } + + d.SetId(newName) } if d.HasChange("default_branch") { - if err := resourceUpdateDefaultBranch(ctx, conn, d); err != nil { - return sdkdiag.AppendErrorf(diags, "updating CodeCommit Repository (%s) default branch: %s", d.Id(), err) + if err := updateRepositoryDefaultBranch(ctx, conn, d.Id(), d.Get("default_branch").(string)); err != nil { + return sdkdiag.AppendFromErr(diags, err) } } if d.HasChange("description") { - if err := resourceUpdateDescription(ctx, conn, d); err != nil { + input := &codecommit.UpdateRepositoryDescriptionInput{ + RepositoryDescription: aws.String(d.Get("description").(string)), + RepositoryName: aws.String(d.Id()), + } + + _, err := conn.UpdateRepositoryDescription(ctx, input) + + if err != nil { return sdkdiag.AppendErrorf(diags, "updating CodeCommit Repository (%s) description: %s", d.Id(), err) } } if d.HasChange("kms_key_id") { - if err := resourceUpdateEncryptionKey(ctx, conn, d); err != nil { + input := &codecommit.UpdateRepositoryEncryptionKeyInput{ + KmsKeyId: aws.String((d.Get("kms_key_id").(string))), + RepositoryName: aws.String(d.Id()), + } + + _, err := conn.UpdateRepositoryEncryptionKey(ctx, input) + + if err != nil { return sdkdiag.AppendErrorf(diags, "updating CodeCommit Repository (%s) encryption key: %s", d.Id(), err) } } @@ -191,89 +207,70 @@ func resourceRepositoryUpdate(ctx context.Context, d *schema.ResourceData, meta func resourceRepositoryDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).CodeCommitConn(ctx) + conn := meta.(*conns.AWSClient).CodeCommitClient(ctx) - log.Printf("[DEBUG] CodeCommit Delete Repository: %s", d.Id()) - _, err := conn.DeleteRepositoryWithContext(ctx, &codecommit.DeleteRepositoryInput{ + log.Printf("[INFO] Deleting CodeCommit Repository: %s", d.Id()) + _, err := conn.DeleteRepository(ctx, &codecommit.DeleteRepositoryInput{ RepositoryName: aws.String(d.Id()), }) + if err != nil { - return sdkdiag.AppendErrorf(diags, "deleting CodeCommit Repository: %s", err.Error()) + return sdkdiag.AppendErrorf(diags, "deleting CodeCommit Repository (%s): %s", d.Id(), err) } return diags } -func resourceUpdateRepositoryName(ctx context.Context, conn *codecommit.CodeCommit, d *schema.ResourceData) error { - newName := d.Get("repository_name").(string) - - branchInput := &codecommit.UpdateRepositoryNameInput{ - OldName: aws.String(d.Id()), - NewName: aws.String(newName), +func updateRepositoryDefaultBranch(ctx context.Context, conn *codecommit.Client, name, defaultBranch string) error { + inputL := &codecommit.ListBranchesInput{ + RepositoryName: aws.String(name), } - _, err := conn.UpdateRepositoryNameWithContext(ctx, branchInput) + output, err := conn.ListBranches(ctx, inputL) + if err != nil { - return fmt.Errorf("Updating Repository Name for CodeCommit Repository: %s", err.Error()) + return fmt.Errorf("listing CodeCommit Repository (%s) branches: %s", name, err) } - // The Id is the name - d.SetId(newName) - - return nil -} + if len(output.Branches) == 0 { + return nil + } -func resourceUpdateDescription(ctx context.Context, conn *codecommit.CodeCommit, d *schema.ResourceData) error { - branchInput := &codecommit.UpdateRepositoryDescriptionInput{ - RepositoryName: aws.String(d.Id()), - RepositoryDescription: aws.String(d.Get("description").(string)), + inputU := &codecommit.UpdateDefaultBranchInput{ + DefaultBranchName: aws.String(defaultBranch), + RepositoryName: aws.String(name), } - _, err := conn.UpdateRepositoryDescriptionWithContext(ctx, branchInput) + _, err = conn.UpdateDefaultBranch(ctx, inputU) + if err != nil { - return fmt.Errorf("Updating Repository Description for CodeCommit Repository: %s", err.Error()) + return fmt.Errorf("updating CodeCommit Repository (%s) default branch: %w", name, err) } return nil } -func resourceUpdateDefaultBranch(ctx context.Context, conn *codecommit.CodeCommit, d *schema.ResourceData) error { - input := &codecommit.ListBranchesInput{ - RepositoryName: aws.String(d.Id()), - } - - out, err := conn.ListBranchesWithContext(ctx, input) - if err != nil { - return fmt.Errorf("reading CodeCommit Repository branches: %s", err.Error()) - } - - if len(out.Branches) == 0 { - log.Printf("[WARN] Not setting Default Branch CodeCommit Repository that has no branches: %s", d.Id()) - return nil +func findRepositoryByName(ctx context.Context, conn *codecommit.Client, name string) (*types.RepositoryMetadata, error) { + input := &codecommit.GetRepositoryInput{ + RepositoryName: aws.String(name), } - branchInput := &codecommit.UpdateDefaultBranchInput{ - RepositoryName: aws.String(d.Id()), - DefaultBranchName: aws.String(d.Get("default_branch").(string)), - } + output, err := conn.GetRepository(ctx, input) - if _, err := conn.UpdateDefaultBranchWithContext(ctx, branchInput); err != nil { - return fmt.Errorf("Updating Default Branch for CodeCommit Repository: %s", err.Error()) + if errs.IsA[*types.RepositoryDoesNotExistException](err) { + return nil, &retry.NotFoundError{ + LastError: err, + LastRequest: input, + } } - return nil -} - -func resourceUpdateEncryptionKey(ctx context.Context, conn *codecommit.CodeCommit, d *schema.ResourceData) error { - input := &codecommit.UpdateRepositoryEncryptionKeyInput{ - KmsKeyId: aws.String((d.Get("kms_key_id").(string))), - RepositoryName: aws.String(d.Id()), + if err != nil { + return nil, err } - _, err := conn.UpdateRepositoryEncryptionKeyWithContext(ctx, input) - if err != nil { - return fmt.Errorf("Updating Repository Encryption Key for CodeCommit Repository: %s", err.Error()) + if output == nil || output.RepositoryMetadata == nil { + return nil, tfresource.NewEmptyResultError(input) } - return nil + return output.RepositoryMetadata, nil } diff --git a/internal/service/codecommit/service_package_gen.go b/internal/service/codecommit/service_package_gen.go index c81764ec931..f66bef860e3 100644 --- a/internal/service/codecommit/service_package_gen.go +++ b/internal/service/codecommit/service_package_gen.go @@ -49,7 +49,7 @@ func (p *servicePackage) SDKResources(ctx context.Context) []*types.ServicePacka Name: "Approval Rule Template Association", }, { - Factory: ResourceRepository, + Factory: resourceRepository, TypeName: "aws_codecommit_repository", Name: "Repository", Tags: &types.ServicePackageResourceTags{ From 3a47e423c6668e66e26ec286d70d9ee5add1fb9e Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Sun, 21 Jan 2024 17:34:05 -0500 Subject: [PATCH 16/21] d/aws_codecommit_repository: Migrate to AWS SDK for Go v2. --- .changelog/35095.txt | 4 ++ .../codecommit/repository_data_source.go | 67 +++++++------------ .../service/codecommit/service_package_gen.go | 3 +- 3 files changed, 29 insertions(+), 45 deletions(-) diff --git a/.changelog/35095.txt b/.changelog/35095.txt index 784a66db36a..bf7e0484b4c 100644 --- a/.changelog/35095.txt +++ b/.changelog/35095.txt @@ -2,6 +2,10 @@ resource/aws_codecommit_repository: Add `kms_key_id` argument ``` +```release-note:enhancement +data-source/aws_codecommit_repository: Add `kms_key_id` attribute +``` + ```release-note:enhancement resource/aws_codecommit_trigger: Add plan-time validation of `trigger.destination_arn` and `trigger.events` ``` \ No newline at end of file diff --git a/internal/service/codecommit/repository_data_source.go b/internal/service/codecommit/repository_data_source.go index 0fbfda7c4c4..9c506706198 100644 --- a/internal/service/codecommit/repository_data_source.go +++ b/internal/service/codecommit/repository_data_source.go @@ -5,11 +5,8 @@ package codecommit import ( "context" - "log" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/codecommit" - "github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr" + "github.com/aws/aws-sdk-go-v2/aws" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" @@ -17,77 +14,59 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/errs/sdkdiag" ) -// @SDKDataSource("aws_codecommit_repository") -func DataSourceRepository() *schema.Resource { +// @SDKDataSource("aws_codecommit_repository", name="Repository") +func dataSourceRepository() *schema.Resource { return &schema.Resource{ ReadWithoutTimeout: dataSourceRepositoryRead, Schema: map[string]*schema.Schema{ - "repository_name": { - Type: schema.TypeString, - Required: true, - ValidateFunc: validation.StringLenBetween(0, 100), - }, - "arn": { Type: schema.TypeString, Computed: true, }, - - "kms_key_id": { + "clone_url_http": { Type: schema.TypeString, Computed: true, }, - - "repository_id": { + "clone_url_ssh": { Type: schema.TypeString, Computed: true, }, - - "clone_url_http": { + "kms_key_id": { Type: schema.TypeString, Computed: true, }, - - "clone_url_ssh": { + "repository_id": { Type: schema.TypeString, Computed: true, }, + "repository_name": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringLenBetween(0, 100), + }, }, } } func dataSourceRepositoryRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { var diags diag.Diagnostics - conn := meta.(*conns.AWSClient).CodeCommitConn(ctx) + conn := meta.(*conns.AWSClient).CodeCommitClient(ctx) - repositoryName := d.Get("repository_name").(string) - input := &codecommit.GetRepositoryInput{ - RepositoryName: aws.String(repositoryName), - } + name := d.Get("repository_name").(string) + repository, err := findRepositoryByName(ctx, conn, name) - out, err := conn.GetRepositoryWithContext(ctx, input) if err != nil { - if tfawserr.ErrCodeEquals(err, codecommit.ErrCodeRepositoryDoesNotExistException) { - log.Printf("[WARN] CodeCommit Repository (%s) not found, removing from state", d.Id()) - d.SetId("") - return sdkdiag.AppendErrorf(diags, "Resource codecommit repository not found for %s", repositoryName) - } else { - return sdkdiag.AppendErrorf(diags, "reading CodeCommit Repository: %s", err) - } - } - - if out.RepositoryMetadata == nil { - return sdkdiag.AppendErrorf(diags, "no matches found for repository name: %s", repositoryName) + return sdkdiag.AppendErrorf(diags, "reading CodeCommit Repository (%s): %s", name, err) } - d.SetId(aws.StringValue(out.RepositoryMetadata.RepositoryName)) - d.Set("arn", out.RepositoryMetadata.Arn) - d.Set("kms_key_id", out.RepositoryMetadata.KmsKeyId) - d.Set("clone_url_http", out.RepositoryMetadata.CloneUrlHttp) - d.Set("clone_url_ssh", out.RepositoryMetadata.CloneUrlSsh) - d.Set("repository_name", out.RepositoryMetadata.RepositoryName) - d.Set("repository_id", out.RepositoryMetadata.RepositoryId) + d.SetId(aws.ToString(repository.RepositoryName)) + d.Set("arn", repository.Arn) + d.Set("clone_url_http", repository.CloneUrlHttp) + d.Set("clone_url_ssh", repository.CloneUrlSsh) + d.Set("kms_key_id", repository.KmsKeyId) + d.Set("repository_id", repository.RepositoryId) + d.Set("repository_name", repository.RepositoryName) return diags } diff --git a/internal/service/codecommit/service_package_gen.go b/internal/service/codecommit/service_package_gen.go index f66bef860e3..8f1e375d18b 100644 --- a/internal/service/codecommit/service_package_gen.go +++ b/internal/service/codecommit/service_package_gen.go @@ -30,8 +30,9 @@ func (p *servicePackage) SDKDataSources(ctx context.Context) []*types.ServicePac Name: "Approval Rule Template", }, { - Factory: DataSourceRepository, + Factory: dataSourceRepository, TypeName: "aws_codecommit_repository", + Name: "Repository", }, } } From 11c9ec866402ea0ec45acf1804a0c3ac3349f632 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Sun, 21 Jan 2024 17:35:23 -0500 Subject: [PATCH 17/21] Add 'names.CodeCommitEndpointID'. --- names/names.go | 1 + 1 file changed, 1 insertion(+) diff --git a/names/names.go b/names/names.go index 6f6a652ebf0..4f2f4ea6d19 100644 --- a/names/names.go +++ b/names/names.go @@ -39,6 +39,7 @@ const ( ChimeSDKMediaPipelinesEndpointID = "media-pipelines-chime" CleanRoomsEndpointID = "cleanrooms" CloudWatchLogsEndpointID = "logs" + CodeCommitEndpointID = "codecommit" CodeDeployEndpointID = "codedeploy" CodeGuruProfilerEndpointID = "codeguru-profiler" CodePipelineEndpointID = "codepipeline" From e4e899c8642d566a3931747470c8dfd4c94671cc Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Mon, 22 Jan 2024 07:46:52 -0500 Subject: [PATCH 18/21] codecommit: Migrate acceptance tests to AWS SDK for Go v2. --- ...approval_rule_template_association_test.go | 38 +++------ ...approval_rule_template_data_source_test.go | 4 +- .../codecommit/approval_rule_template_test.go | 37 ++++----- internal/service/codecommit/exports_test.go | 1 + .../codecommit/repository_data_source_test.go | 4 +- .../service/codecommit/repository_test.go | 77 ++++++++----------- internal/service/codecommit/trigger.go | 2 +- internal/service/codecommit/trigger_test.go | 48 ++++-------- 8 files changed, 81 insertions(+), 130 deletions(-) diff --git a/internal/service/codecommit/approval_rule_template_association_test.go b/internal/service/codecommit/approval_rule_template_association_test.go index dad29db8600..9d8baad8c40 100644 --- a/internal/service/codecommit/approval_rule_template_association_test.go +++ b/internal/service/codecommit/approval_rule_template_association_test.go @@ -8,7 +8,6 @@ import ( "fmt" "testing" - "github.com/aws/aws-sdk-go/service/codecommit" sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-plugin-testing/terraform" @@ -16,6 +15,7 @@ import ( "github.com/hashicorp/terraform-provider-aws/internal/conns" tfcodecommit "github.com/hashicorp/terraform-provider-aws/internal/service/codecommit" "github.com/hashicorp/terraform-provider-aws/internal/tfresource" + "github.com/hashicorp/terraform-provider-aws/names" ) func TestAccCodeCommitApprovalRuleTemplateAssociation_basic(t *testing.T) { @@ -27,7 +27,7 @@ func TestAccCodeCommitApprovalRuleTemplateAssociation_basic(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, codecommit.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.CodeCommitEndpointID), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: testAccCheckApprovalRuleTemplateAssociationDestroy(ctx), Steps: []resource.TestStep{ @@ -55,7 +55,7 @@ func TestAccCodeCommitApprovalRuleTemplateAssociation_disappears(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, codecommit.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.CodeCommitEndpointID), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: testAccCheckApprovalRuleTemplateAssociationDestroy(ctx), Steps: []resource.TestStep{ @@ -79,7 +79,7 @@ func TestAccCodeCommitApprovalRuleTemplateAssociation_Disappears_repository(t *t resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, codecommit.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.CodeCommitEndpointID), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: testAccCheckApprovalRuleTemplateAssociationDestroy(ctx), Steps: []resource.TestStep{ @@ -95,45 +95,31 @@ func TestAccCodeCommitApprovalRuleTemplateAssociation_Disappears_repository(t *t }) } -func testAccCheckApprovalRuleTemplateAssociationExists(ctx context.Context, name string) resource.TestCheckFunc { +func testAccCheckApprovalRuleTemplateAssociationExists(ctx context.Context, n string) resource.TestCheckFunc { return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[name] + rs, ok := s.RootModule().Resources[n] if !ok { - return fmt.Errorf("Not found: %s", name) + return fmt.Errorf("Not found: %s", n) } - if rs.Primary.ID == "" { - return fmt.Errorf("No ID is set") - } - - conn := acctest.Provider.Meta().(*conns.AWSClient).CodeCommitConn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).CodeCommitClient(ctx) - approvalTemplateName, repositoryName, err := tfcodecommit.ApprovalRuleTemplateAssociationParseID(rs.Primary.ID) + _, err := tfcodecommit.FindApprovalRuleTemplateAssociationByTwoPartKey(ctx, conn, rs.Primary.Attributes["approval_rule_template_name"], rs.Primary.Attributes["repository_name"]) - if err != nil { - return err - } - - return tfcodecommit.FindApprovalRuleTemplateAssociation(ctx, conn, approvalTemplateName, repositoryName) + return err } } func testAccCheckApprovalRuleTemplateAssociationDestroy(ctx context.Context) resource.TestCheckFunc { return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).CodeCommitConn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).CodeCommitClient(ctx) for _, rs := range s.RootModule().Resources { if rs.Type != "aws_codecommit_approval_rule_template_association" { continue } - approvalTemplateName, repositoryName, err := tfcodecommit.ApprovalRuleTemplateAssociationParseID(rs.Primary.ID) - - if err != nil { - return err - } - - err = tfcodecommit.FindApprovalRuleTemplateAssociation(ctx, conn, approvalTemplateName, repositoryName) + _, err := tfcodecommit.FindApprovalRuleTemplateAssociationByTwoPartKey(ctx, conn, rs.Primary.Attributes["approval_rule_template_name"], rs.Primary.Attributes["repository_name"]) if tfresource.NotFound(err) { continue diff --git a/internal/service/codecommit/approval_rule_template_data_source_test.go b/internal/service/codecommit/approval_rule_template_data_source_test.go index 30d9d19aa63..4a1b21c41c4 100644 --- a/internal/service/codecommit/approval_rule_template_data_source_test.go +++ b/internal/service/codecommit/approval_rule_template_data_source_test.go @@ -7,10 +7,10 @@ import ( "fmt" "testing" - "github.com/aws/aws-sdk-go/service/codecommit" sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-provider-aws/internal/acctest" + "github.com/hashicorp/terraform-provider-aws/names" ) func TestAccCodeCommitApprovalRuleTemplateDataSource_basic(t *testing.T) { @@ -21,7 +21,7 @@ func TestAccCodeCommitApprovalRuleTemplateDataSource_basic(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, codecommit.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.CodeCommitEndpointID), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, Steps: []resource.TestStep{ { diff --git a/internal/service/codecommit/approval_rule_template_test.go b/internal/service/codecommit/approval_rule_template_test.go index 7e9b76682e8..88f2ab9029c 100644 --- a/internal/service/codecommit/approval_rule_template_test.go +++ b/internal/service/codecommit/approval_rule_template_test.go @@ -8,15 +8,14 @@ import ( "fmt" "testing" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/codecommit" - "github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr" 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" tfcodecommit "github.com/hashicorp/terraform-provider-aws/internal/service/codecommit" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" + "github.com/hashicorp/terraform-provider-aws/names" ) func TestAccCodeCommitApprovalRuleTemplate_basic(t *testing.T) { @@ -26,7 +25,7 @@ func TestAccCodeCommitApprovalRuleTemplate_basic(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, codecommit.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.CodeCommitEndpointID), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: testAccCheckApprovalRuleTemplateDestroy(ctx), Steps: []resource.TestStep{ @@ -59,7 +58,7 @@ func TestAccCodeCommitApprovalRuleTemplate_disappears(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, codecommit.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.CodeCommitEndpointID), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: testAccCheckApprovalRuleTemplateDestroy(ctx), Steps: []resource.TestStep{ @@ -82,7 +81,7 @@ func TestAccCodeCommitApprovalRuleTemplate_updateContentAndDescription(t *testin resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, codecommit.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.CodeCommitEndpointID), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: testAccCheckApprovalRuleTemplateDestroy(ctx), Steps: []resource.TestStep{ @@ -119,7 +118,7 @@ func TestAccCodeCommitApprovalRuleTemplate_updateName(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, codecommit.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.CodeCommitEndpointID), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: testAccCheckApprovalRuleTemplateDestroy(ctx), Steps: []resource.TestStep{ @@ -155,22 +154,16 @@ func testAccCheckApprovalRuleTemplateContent(resourceName string, numApprovals i } } -func testAccCheckApprovalRuleTemplateExists(ctx context.Context, name string) resource.TestCheckFunc { +func testAccCheckApprovalRuleTemplateExists(ctx context.Context, n string) resource.TestCheckFunc { return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[name] + rs, ok := s.RootModule().Resources[n] if !ok { - return fmt.Errorf("Not found: %s", name) + return fmt.Errorf("Not found: %s", n) } - if rs.Primary.ID == "" { - return fmt.Errorf("No ID is set") - } - - conn := acctest.Provider.Meta().(*conns.AWSClient).CodeCommitConn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).CodeCommitClient(ctx) - _, err := conn.GetApprovalRuleTemplateWithContext(ctx, &codecommit.GetApprovalRuleTemplateInput{ - ApprovalRuleTemplateName: aws.String(rs.Primary.ID), - }) + _, err := tfcodecommit.FindApprovalRuleTemplateByName(ctx, conn, rs.Primary.ID) return err } @@ -178,18 +171,16 @@ func testAccCheckApprovalRuleTemplateExists(ctx context.Context, name string) re func testAccCheckApprovalRuleTemplateDestroy(ctx context.Context) resource.TestCheckFunc { return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).CodeCommitConn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).CodeCommitClient(ctx) for _, rs := range s.RootModule().Resources { if rs.Type != "aws_codecommit_approval_rule_template" { continue } - _, err := conn.GetApprovalRuleTemplateWithContext(ctx, &codecommit.GetApprovalRuleTemplateInput{ - ApprovalRuleTemplateName: aws.String(rs.Primary.ID), - }) + _, err := tfcodecommit.FindApprovalRuleTemplateByName(ctx, conn, rs.Primary.ID) - if tfawserr.ErrCodeEquals(err, codecommit.ErrCodeApprovalRuleTemplateDoesNotExistException) { + if tfresource.NotFound(err) { continue } diff --git a/internal/service/codecommit/exports_test.go b/internal/service/codecommit/exports_test.go index 1cc39357362..123f13a9e3f 100644 --- a/internal/service/codecommit/exports_test.go +++ b/internal/service/codecommit/exports_test.go @@ -12,5 +12,6 @@ var ( FindApprovalRuleTemplateAssociationByTwoPartKey = findApprovalRuleTemplateAssociationByTwoPartKey FindApprovalRuleTemplateByName = findApprovalRuleTemplateByName + FindRepositoryByName = findRepositoryByName FindRepositoryTriggersByName = findRepositoryTriggersByName ) diff --git a/internal/service/codecommit/repository_data_source_test.go b/internal/service/codecommit/repository_data_source_test.go index 6aebe2d562d..e5db0964f45 100644 --- a/internal/service/codecommit/repository_data_source_test.go +++ b/internal/service/codecommit/repository_data_source_test.go @@ -7,10 +7,10 @@ import ( "fmt" "testing" - "github.com/aws/aws-sdk-go/service/codecommit" sdkacctest "github.com/hashicorp/terraform-plugin-testing/helper/acctest" "github.com/hashicorp/terraform-plugin-testing/helper/resource" "github.com/hashicorp/terraform-provider-aws/internal/acctest" + "github.com/hashicorp/terraform-provider-aws/names" ) func TestAccCodeCommitRepositoryDataSource_basic(t *testing.T) { @@ -21,7 +21,7 @@ func TestAccCodeCommitRepositoryDataSource_basic(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, codecommit.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.CodeCommitEndpointID), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, Steps: []resource.TestStep{ { diff --git a/internal/service/codecommit/repository_test.go b/internal/service/codecommit/repository_test.go index 1623b01855a..6dce6002e2b 100644 --- a/internal/service/codecommit/repository_test.go +++ b/internal/service/codecommit/repository_test.go @@ -8,25 +8,27 @@ import ( "fmt" "testing" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/codecommit" - "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/codecommit/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" + tfcodecommit "github.com/hashicorp/terraform-provider-aws/internal/service/codecommit" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" + "github.com/hashicorp/terraform-provider-aws/names" ) func TestAccCodeCommitRepository_basic(t *testing.T) { ctx := acctest.Context(t) rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) resourceName := "aws_codecommit_repository.test" - var v codecommit.RepositoryMetadata + var v types.RepositoryMetadata resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, codecommit.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.CodeCommitEndpointID), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: testAccCheckRepositoryDestroy(ctx), Steps: []resource.TestStep{ @@ -58,11 +60,11 @@ func TestAccCodeCommitRepository_withChanges(t *testing.T) { rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) rNameUpdated := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) resourceName := "aws_codecommit_repository.test" - var v1, v2 codecommit.RepositoryMetadata + var v1, v2 types.RepositoryMetadata resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, codecommit.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.CodeCommitEndpointID), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: testAccCheckRepositoryDestroy(ctx), Steps: []resource.TestStep{ @@ -95,11 +97,11 @@ func TestAccCodeCommitRepository_CreateDefault_branch(t *testing.T) { ctx := acctest.Context(t) rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) resourceName := "aws_codecommit_repository.test" - var v codecommit.RepositoryMetadata + var v types.RepositoryMetadata resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, codecommit.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.CodeCommitEndpointID), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: testAccCheckRepositoryDestroy(ctx), Steps: []resource.TestStep{ @@ -124,11 +126,11 @@ func TestAccCodeCommitRepository_CreateAndUpdateDefault_branch(t *testing.T) { ctx := acctest.Context(t) rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) resourceName := "aws_codecommit_repository.test" - var v1, v2 codecommit.RepositoryMetadata + var v1, v2 types.RepositoryMetadata resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, codecommit.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.CodeCommitEndpointID), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: testAccCheckRepositoryDestroy(ctx), Steps: []resource.TestStep{ @@ -160,11 +162,11 @@ func TestAccCodeCommitRepository_tags(t *testing.T) { ctx := acctest.Context(t) rName := sdkacctest.RandString(10) resourceName := "aws_codecommit_repository.test" - var v1, v2, v3 codecommit.RepositoryMetadata + var v1, v2, v3 types.RepositoryMetadata resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, codecommit.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.CodeCommitEndpointID), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: testAccCheckRepositoryDestroy(ctx), Steps: []resource.TestStep{ @@ -217,11 +219,11 @@ func TestAccCodeCommitRepository_UpdateNameAndTags(t *testing.T) { rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) rNameUpdated := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) resourceName := "aws_codecommit_repository.test" - var v1, v2 codecommit.RepositoryMetadata + var v1, v2 types.RepositoryMetadata resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, codecommit.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.CodeCommitEndpointID), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: testAccCheckRepositoryDestroy(ctx), Steps: []resource.TestStep{ @@ -258,36 +260,22 @@ func TestAccCodeCommitRepository_UpdateNameAndTags(t *testing.T) { }) } -func testAccCheckRepositoryExists(ctx context.Context, name string, v *codecommit.RepositoryMetadata) resource.TestCheckFunc { +func testAccCheckRepositoryExists(ctx context.Context, n string, v *types.RepositoryMetadata) resource.TestCheckFunc { return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[name] + rs, ok := s.RootModule().Resources[n] if !ok { - return fmt.Errorf("Not found: %s", name) + return fmt.Errorf("Not found: %s", n) } - if rs.Primary.ID == "" { - return fmt.Errorf("No ID is set") - } + conn := acctest.Provider.Meta().(*conns.AWSClient).CodeCommitClient(ctx) - conn := acctest.Provider.Meta().(*conns.AWSClient).CodeCommitConn(ctx) - out, err := conn.GetRepositoryWithContext(ctx, &codecommit.GetRepositoryInput{ - RepositoryName: aws.String(rs.Primary.ID), - }) + output, err := tfcodecommit.FindRepositoryByName(ctx, conn, rs.Primary.ID) if err != nil { return err } - if out.RepositoryMetadata.Arn == nil { - return fmt.Errorf("No CodeCommit Repository Vault Found") - } - - if *out.RepositoryMetadata.RepositoryName != rs.Primary.ID { - return fmt.Errorf("CodeCommit Repository Mismatch - existing: %q, state: %q", - *out.RepositoryMetadata.RepositoryName, rs.Primary.ID) - } - - *v = *out.RepositoryMetadata + *v = *output return nil } @@ -295,34 +283,33 @@ func testAccCheckRepositoryExists(ctx context.Context, name string, v *codecommi func testAccCheckRepositoryDestroy(ctx context.Context) resource.TestCheckFunc { return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).CodeCommitConn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).CodeCommitClient(ctx) for _, rs := range s.RootModule().Resources { if rs.Type != "aws_codecommit_repository" { continue } - _, err := conn.GetRepositoryWithContext(ctx, &codecommit.GetRepositoryInput{ - RepositoryName: aws.String(rs.Primary.ID), - }) + _, err := tfcodecommit.FindRepositoryByName(ctx, conn, rs.Primary.ID) - if tfawserr.ErrCodeEquals(err, codecommit.ErrCodeRepositoryDoesNotExistException) { + if tfresource.NotFound(err) { continue } - if err == nil { - return fmt.Errorf("Repository still exists: %s", rs.Primary.ID) + if err != nil { + return err } - return err + + return fmt.Errorf("CodeCommit Repository (%s) still exists", rs.Primary.ID) } return nil } } -func testAccCheckRepositoryNotRecreated(v1, v2 *codecommit.RepositoryMetadata) resource.TestCheckFunc { +func testAccCheckRepositoryNotRecreated(v1, v2 *types.RepositoryMetadata) resource.TestCheckFunc { return func(s *terraform.State) error { - if aws.StringValue(v1.RepositoryId) != aws.StringValue(v2.RepositoryId) { + if aws.ToString(v1.RepositoryId) != aws.ToString(v2.RepositoryId) { return fmt.Errorf("CodeCommit Repository recreated") } return nil diff --git a/internal/service/codecommit/trigger.go b/internal/service/codecommit/trigger.go index 9b9d317b5d0..230c7097796 100644 --- a/internal/service/codecommit/trigger.go +++ b/internal/service/codecommit/trigger.go @@ -171,7 +171,7 @@ func findRepositoryTriggersByName(ctx context.Context, conn *codecommit.Client, return nil, err } - if output == nil { + if output == nil || len(output.Triggers) == 0 { return nil, tfresource.NewEmptyResultError(input) } diff --git a/internal/service/codecommit/trigger_test.go b/internal/service/codecommit/trigger_test.go index fbcdd7cb026..7ff68c4e83a 100644 --- a/internal/service/codecommit/trigger_test.go +++ b/internal/service/codecommit/trigger_test.go @@ -8,14 +8,14 @@ import ( "fmt" "testing" - "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/service/codecommit" - "github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr" 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" + tfcodecommit "github.com/hashicorp/terraform-provider-aws/internal/service/codecommit" + "github.com/hashicorp/terraform-provider-aws/internal/tfresource" + "github.com/hashicorp/terraform-provider-aws/names" ) func TestAccCodeCommitTrigger_basic(t *testing.T) { @@ -25,7 +25,7 @@ func TestAccCodeCommitTrigger_basic(t *testing.T) { resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { acctest.PreCheck(ctx, t) }, - ErrorCheck: acctest.ErrorCheck(t, codecommit.EndpointsID), + ErrorCheck: acctest.ErrorCheck(t, names.CodeCommitEndpointID), ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, CheckDestroy: testAccCheckTriggerDestroy(ctx), Steps: []resource.TestStep{ @@ -42,56 +42,42 @@ func TestAccCodeCommitTrigger_basic(t *testing.T) { func testAccCheckTriggerDestroy(ctx context.Context) resource.TestCheckFunc { return func(s *terraform.State) error { - conn := acctest.Provider.Meta().(*conns.AWSClient).CodeCommitConn(ctx) + conn := acctest.Provider.Meta().(*conns.AWSClient).CodeCommitClient(ctx) for _, rs := range s.RootModule().Resources { if rs.Type != "aws_codecommit_trigger" { continue } - _, err := conn.GetRepositoryTriggersWithContext(ctx, &codecommit.GetRepositoryTriggersInput{ - RepositoryName: aws.String(rs.Primary.ID), - }) + _, err := tfcodecommit.FindRepositoryTriggersByName(ctx, conn, rs.Primary.ID) - if tfawserr.ErrCodeEquals(err, codecommit.ErrCodeRepositoryDoesNotExistException) { + if tfresource.NotFound(err) { continue } - if err == nil { - return fmt.Errorf("Trigger still exists: %s", rs.Primary.ID) + if err != nil { + return err } - return err + + return fmt.Errorf("CodeCommit Trigger (%s) still exists", rs.Primary.ID) } return nil } } -func testAccCheckTriggerExists(ctx context.Context, name string) resource.TestCheckFunc { +func testAccCheckTriggerExists(ctx context.Context, n string) resource.TestCheckFunc { return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[name] + rs, ok := s.RootModule().Resources[n] if !ok { - return fmt.Errorf("Not found: %s", name) - } - - if rs.Primary.ID == "" { - return fmt.Errorf("No ID is set") + return fmt.Errorf("Not found: %s", n) } - conn := acctest.Provider.Meta().(*conns.AWSClient).CodeCommitConn(ctx) - out, err := conn.GetRepositoryTriggersWithContext(ctx, &codecommit.GetRepositoryTriggersInput{ - RepositoryName: aws.String(rs.Primary.ID), - }) + conn := acctest.Provider.Meta().(*conns.AWSClient).CodeCommitClient(ctx) - if err != nil { - return err - } + _, err := tfcodecommit.FindRepositoryTriggersByName(ctx, conn, rs.Primary.ID) - if len(out.Triggers) == 0 { - return fmt.Errorf("CodeCommit Trigger Failed: %q", out) - } - - return nil + return err } } From db96a506d36b3ccd5bb11389543b6e0632ee9f65 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Mon, 22 Jan 2024 08:09:47 -0500 Subject: [PATCH 19/21] Add 'TestAccCodeCommitRepository_kmsKey'. --- internal/service/codecommit/repository.go | 7 ++- .../service/codecommit/repository_test.go | 61 +++++++++++++++++-- .../r/codecommit_repository.html.markdown | 4 +- 3 files changed, 63 insertions(+), 9 deletions(-) diff --git a/internal/service/codecommit/repository.go b/internal/service/codecommit/repository.go index f5c13c60cc6..d5f7c2b19f2 100644 --- a/internal/service/codecommit/repository.go +++ b/internal/service/codecommit/repository.go @@ -60,9 +60,10 @@ func resourceRepository() *schema.Resource { ValidateFunc: validation.StringLenBetween(0, 1000), }, "kms_key_id": { - Type: schema.TypeString, - Optional: true, - Computed: true, + Type: schema.TypeString, + Optional: true, + Computed: true, + ValidateFunc: verify.ValidARN, }, "repository_id": { Type: schema.TypeString, diff --git a/internal/service/codecommit/repository_test.go b/internal/service/codecommit/repository_test.go index 6dce6002e2b..08dbe4a5b5b 100644 --- a/internal/service/codecommit/repository_test.go +++ b/internal/service/codecommit/repository_test.go @@ -36,13 +36,14 @@ func TestAccCodeCommitRepository_basic(t *testing.T) { Config: testAccRepositoryConfig_basic(rName), Check: resource.ComposeAggregateTestCheckFunc( testAccCheckRepositoryExists(ctx, resourceName, &v), - resource.TestCheckResourceAttr(resourceName, "repository_name", rName), - resource.TestCheckNoResourceAttr(resourceName, "default_branch"), - resource.TestCheckResourceAttr(resourceName, "description", "This is a test description"), acctest.CheckResourceAttrRegionalARN(resourceName, "arn", "codecommit", rName), - resource.TestCheckResourceAttrSet(resourceName, "repository_id"), resource.TestCheckResourceAttrSet(resourceName, "clone_url_http"), resource.TestCheckResourceAttrSet(resourceName, "clone_url_ssh"), + resource.TestCheckNoResourceAttr(resourceName, "default_branch"), + resource.TestCheckResourceAttr(resourceName, "description", "This is a test description"), + resource.TestCheckResourceAttrSet(resourceName, "kms_key_id"), + resource.TestCheckResourceAttrSet(resourceName, "repository_id"), + resource.TestCheckResourceAttr(resourceName, "repository_name", rName), resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), ), }, @@ -260,6 +261,42 @@ func TestAccCodeCommitRepository_UpdateNameAndTags(t *testing.T) { }) } +func TestAccCodeCommitRepository_kmsKey(t *testing.T) { + ctx := acctest.Context(t) + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + resourceName := "aws_codecommit_repository.test" + var v1, v2 types.RepositoryMetadata + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(ctx, t) }, + ErrorCheck: acctest.ErrorCheck(t, names.CodeCommitEndpointID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckRepositoryDestroy(ctx), + Steps: []resource.TestStep{ + { + Config: testAccRepositoryConfig_kmsKey(rName, 0), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckRepositoryExists(ctx, resourceName, &v1), + resource.TestCheckResourceAttrPair(resourceName, "kms_key_id", "aws_kms_key.test.0", "arn"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccRepositoryConfig_kmsKey(rName, 1), + Check: resource.ComposeAggregateTestCheckFunc( + testAccCheckRepositoryExists(ctx, resourceName, &v2), + testAccCheckRepositoryNotRecreated(&v1, &v2), + resource.TestCheckResourceAttrPair(resourceName, "kms_key_id", "aws_kms_key.test.1", "arn"), + ), + }, + }, + }) +} + func testAccCheckRepositoryExists(ctx context.Context, n string, v *types.RepositoryMetadata) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] @@ -368,3 +405,19 @@ resource "aws_codecommit_repository" "test" { } `, rName, tag1Key, tag1Value, tag2Key, tag2Value) } + +func testAccRepositoryConfig_kmsKey(rName string, idx int) string { + return fmt.Sprintf(` +resource "aws_kms_key" "test" { + count = 2 + + description = %[1]q + deletion_window_in_days = 7 +} + +resource "aws_codecommit_repository" "test" { + repository_name = %[1]q + kms_key_id = aws_kms_key.test[%[2]d].arn +} +`, rName, idx) +} diff --git a/website/docs/r/codecommit_repository.html.markdown b/website/docs/r/codecommit_repository.html.markdown index 51cf7326e86..2bdea448866 100644 --- a/website/docs/r/codecommit_repository.html.markdown +++ b/website/docs/r/codecommit_repository.html.markdown @@ -25,7 +25,7 @@ resource "aws_codecommit_repository" "test" { resource "aws_codecommit_repository" "test" { repository_name = "MyTestRepository" description = "This is the Sample App Repository" - kms_key_id = aws_kms_key.test.key_id + kms_key_id = aws_kms_key.test.arn } resource "aws_kms_key" "test" { @@ -39,9 +39,9 @@ resource "aws_kms_key" "test" { This resource supports the following arguments: * `repository_name` - (Required) The name for the repository. This needs to be less than 100 characters. -* `kms_key_id` - (Optional) The ID of the encryption key. You can view the ID of an encryption key in the KMS console, or use the KMS APIs to programmatically retrieve a key ID. For more information about acceptable values for kmsKeyID, see KeyId in the Decrypt API description in the Key Management Service API Reference. If no key is specified, the default aws/codecommit Amazon Web Services managed key is used. * `description` - (Optional) The description of the repository. This needs to be less than 1000 characters * `default_branch` - (Optional) The default branch of the repository. The branch specified here needs to exist. +* `kms_key_id` - (Optional) The ARN of the encryption key. If no key is specified, the default `aws/codecommit`` Amazon Web Services managed key is used. * `tags` - (Optional) Key-value map of resource tags. If configured with a provider [`default_tags` configuration block](https://registry.terraform.io/providers/hashicorp/aws/latest/docs#default_tags-configuration-block) present, tags with matching keys will overwrite those defined at the provider-level. ## Attribute Reference From c9b758c1f3bf20385032fcfdd641df3c0735dd24 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Mon, 22 Jan 2024 08:10:07 -0500 Subject: [PATCH 20/21] Acceptance test output: % make testacc TESTARGS='-run=TestAccCodeCommitRepository_kmsKey' PKG=codecommit ==> Checking that code complies with gofmt requirements... TF_ACC=1 go test ./internal/service/codecommit/... -v -count 1 -parallel 20 -run=TestAccCodeCommitRepository_kmsKey -timeout 360m === RUN TestAccCodeCommitRepository_kmsKey === PAUSE TestAccCodeCommitRepository_kmsKey === CONT TestAccCodeCommitRepository_kmsKey --- PASS: TestAccCodeCommitRepository_kmsKey (33.42s) PASS ok github.com/hashicorp/terraform-provider-aws/internal/service/codecommit 44.236s ` From 05d982c5b7ec818de9f685ed11a72f226111e6e9 Mon Sep 17 00:00:00 2001 From: Kit Ewbank Date: Mon, 22 Jan 2024 08:29:38 -0500 Subject: [PATCH 21/21] r/aws_codecommit_trigger: Fix 'RepositoryTriggerBranchNameListRequiredException: Repository trigger branch name list cannot be null' error. --- internal/service/codecommit/trigger.go | 5 +- internal/service/codecommit/trigger_test.go | 80 ++++++++++++++++++++- 2 files changed, 82 insertions(+), 3 deletions(-) diff --git a/internal/service/codecommit/trigger.go b/internal/service/codecommit/trigger.go index 230c7097796..1b7e2cf1190 100644 --- a/internal/service/codecommit/trigger.go +++ b/internal/service/codecommit/trigger.go @@ -122,7 +122,7 @@ func resourceTriggerRead(ctx context.Context, d *schema.ResourceData, meta inter } d.Set("configuration_id", output.ConfigurationId) - d.Set("respository_name", d.Id()) + d.Set("repository_name", d.Id()) if err := d.Set("trigger", flattenRepositoryTriggers(output.Triggers)); err != nil { return sdkdiag.AppendErrorf(diags, "setting trigger: %s", err) } @@ -193,7 +193,8 @@ func expandRepositoryTriggers(tfList []interface{}) []types.RepositoryTrigger { apiObject := types.RepositoryTrigger{} - if v, ok := tfMap["branches"].([]interface{}); ok && len(v) > 0 { + // "RepositoryTriggerBranchNameListRequiredException: Repository trigger branch name list cannot be null". + if v, ok := tfMap["branches"].([]interface{}); ok { apiObject.Branches = flex.ExpandStringValueList(v) } diff --git a/internal/service/codecommit/trigger_test.go b/internal/service/codecommit/trigger_test.go index 7ff68c4e83a..47621750de7 100644 --- a/internal/service/codecommit/trigger_test.go +++ b/internal/service/codecommit/trigger_test.go @@ -31,9 +31,64 @@ func TestAccCodeCommitTrigger_basic(t *testing.T) { Steps: []resource.TestStep{ { Config: testAccTriggerConfig_basic(rName), - Check: resource.ComposeTestCheckFunc( + Check: resource.ComposeAggregateTestCheckFunc( testAccCheckTriggerExists(ctx, resourceName), resource.TestCheckResourceAttr(resourceName, "trigger.#", "1"), + resource.TestCheckResourceAttr(resourceName, "trigger.0.branches.#", "0"), + resource.TestCheckResourceAttr(resourceName, "trigger.0.events.#", "1"), + resource.TestCheckResourceAttr(resourceName, "trigger.0.events.0", "all"), + resource.TestCheckResourceAttr(resourceName, "trigger.0.name", rName), + ), + }, + }, + }) +} + +func TestAccCodeCommitTrigger_disappears(t *testing.T) { + ctx := acctest.Context(t) + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + resourceName := "aws_codecommit_trigger.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(ctx, t) }, + ErrorCheck: acctest.ErrorCheck(t, names.CodeCommitEndpointID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckTriggerDestroy(ctx), + Steps: []resource.TestStep{ + { + Config: testAccTriggerConfig_basic(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckTriggerExists(ctx, resourceName), + acctest.CheckResourceDisappears(ctx, acctest.Provider, tfcodecommit.ResourceTrigger(), resourceName), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + +func TestAccCodeCommitTrigger_branches(t *testing.T) { + ctx := acctest.Context(t) + rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix) + resourceName := "aws_codecommit_trigger.test" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acctest.PreCheck(ctx, t) }, + ErrorCheck: acctest.ErrorCheck(t, names.CodeCommitEndpointID), + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories, + CheckDestroy: testAccCheckTriggerDestroy(ctx), + Steps: []resource.TestStep{ + { + Config: testAccTriggerConfig_branches(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckTriggerExists(ctx, resourceName), + resource.TestCheckResourceAttr(resourceName, "trigger.0.branches.#", "2"), + resource.TestCheckResourceAttr(resourceName, "trigger.0.branches.0", "main"), + resource.TestCheckResourceAttr(resourceName, "trigger.0.branches.1", "develop"), + resource.TestCheckResourceAttr(resourceName, "trigger.0.events.#", "2"), + resource.TestCheckResourceAttr(resourceName, "trigger.0.events.0", "updateReference"), + resource.TestCheckResourceAttr(resourceName, "trigger.0.events.1", "createReference"), + resource.TestCheckResourceAttr(resourceName, "trigger.0.name", rName), ), }, }, @@ -102,3 +157,26 @@ resource "aws_codecommit_trigger" "test" { } `, rName) } + +func testAccTriggerConfig_branches(rName string) string { + return fmt.Sprintf(` +resource "aws_sns_topic" "test" { + name = %[1]q +} + +resource "aws_codecommit_repository" "test" { + repository_name = %[1]q +} + +resource "aws_codecommit_trigger" "test" { + repository_name = aws_codecommit_repository.test.id + + trigger { + name = %[1]q + events = ["updateReference", "createReference"] + destination_arn = aws_sns_topic.test.arn + branches = ["main", "develop"] + } +} +`, rName) +}