diff --git a/.changelog/35095.txt b/.changelog/35095.txt new file mode 100644 index 00000000000..bf7e0484b4c --- /dev/null +++ b/.changelog/35095.txt @@ -0,0 +1,11 @@ +```release-note:enhancement +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/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= 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/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/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/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.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/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 new file mode 100644 index 00000000000..123f13a9e3f --- /dev/null +++ b/internal/service/codecommit/exports_test.go @@ -0,0 +1,17 @@ +// Copyright (c) HashiCorp, Inc. +// SPDX-License-Identifier: MPL-2.0 + +package codecommit + +// Exports for use in tests only. +var ( + ResourceApprovalRuleTemplate = resourceApprovalRuleTemplate + ResourceApprovalRuleTemplateAssociation = resourceApprovalRuleTemplateAssociation + ResourceRepository = resourceRepository + ResourceTrigger = resourceTrigger + + FindApprovalRuleTemplateAssociationByTwoPartKey = findApprovalRuleTemplateAssociationByTwoPartKey + FindApprovalRuleTemplateByName = findApprovalRuleTemplateByName + FindRepositoryByName = findRepositoryByName + FindRepositoryTriggersByName = findRepositoryTriggersByName +) 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/generate.go b/internal/service/codecommit/generate.go index e9349829fa0..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 -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/repository.go b/internal/service/codecommit/repository.go index 3196ae60817..d5f7c2b19f2 100644 --- a/internal/service/codecommit/repository.go +++ b/internal/service/codecommit/repository.go @@ -8,69 +8,72 @@ 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, }, - - "repository_id": { - Type: schema.TypeString, - Computed: true, - }, - "clone_url_http": { Type: schema.TypeString, Computed: true, }, - "clone_url_ssh": { Type: schema.TypeString, Computed: true, }, - "default_branch": { Type: schema.TypeString, Optional: true, }, + "description": { + Type: schema.TypeString, + Optional: true, + ValidateFunc: validation.StringLenBetween(0, 1000), + }, + "kms_key_id": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ValidateFunc: verify.ValidARN, + }, + "repository_id": { + Type: schema.TypeString, + Computed: true, + }, + "repository_name": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validation.StringLenBetween(0, 100), + }, names.AttrTags: tftags.TagsSchema(), names.AttrTagsAll: tftags.TagsSchemaComputed(), }, @@ -81,28 +84,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)) + } + + if v, ok := d.GetOk("description"); ok { + input.RepositoryDescription = aws.String(v.(string)) } - out, err := conn.CreateRepositoryWithContext(ctx, input) + _, 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("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) } } @@ -111,137 +119,159 @@ 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("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") { + 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) + } + } + return append(diags, resourceRepositoryRead(ctx, d, 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()), +func findRepositoryByName(ctx context.Context, conn *codecommit.Client, name string) (*types.RepositoryMetadata, error) { + input := &codecommit.GetRepositoryInput{ + RepositoryName: aws.String(name), } - out, err := conn.ListBranchesWithContext(ctx, input) - if err != nil { - return fmt.Errorf("reading CodeCommit Repository branches: %s", err.Error()) - } + output, err := conn.GetRepository(ctx, input) - if len(out.Branches) == 0 { - log.Printf("[WARN] Not setting Default Branch CodeCommit Repository that has no branches: %s", d.Id()) - return nil + if errs.IsA[*types.RepositoryDoesNotExistException](err) { + return nil, &retry.NotFoundError{ + LastError: err, + LastRequest: input, + } } - branchInput := &codecommit.UpdateDefaultBranchInput{ - RepositoryName: aws.String(d.Id()), - DefaultBranchName: aws.String(d.Get("default_branch").(string)), + if err != nil { + return nil, err } - if _, err := conn.UpdateDefaultBranchWithContext(ctx, branchInput); err != nil { - return fmt.Errorf("Updating Default Branch 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/repository_data_source.go b/internal/service/codecommit/repository_data_source.go index d569bab4696..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,71 +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, }, - - "repository_id": { + "clone_url_http": { Type: schema.TypeString, Computed: true, }, - - "clone_url_http": { + "clone_url_ssh": { Type: schema.TypeString, Computed: true, }, - - "clone_url_ssh": { + "kms_key_id": { Type: schema.TypeString, Computed: true, }, + "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("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/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..08dbe4a5b5b 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{ @@ -34,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"), ), }, @@ -58,11 +61,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 +98,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 +127,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 +163,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 +220,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 +261,58 @@ func TestAccCodeCommitRepository_UpdateNameAndTags(t *testing.T) { }) } -func testAccCheckRepositoryExists(ctx context.Context, name string, v *codecommit.RepositoryMetadata) resource.TestCheckFunc { +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[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 +320,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 @@ -381,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/internal/service/codecommit/service_package_gen.go b/internal/service/codecommit/service_package_gen.go index e63566ac80e..8f1e375d18b 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" @@ -26,12 +25,14 @@ 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, + Factory: dataSourceRepository, TypeName: "aws_codecommit_repository", + Name: "Repository", }, } } @@ -39,15 +40,17 @@ 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, + Factory: resourceApprovalRuleTemplateAssociation, TypeName: "aws_codecommit_approval_rule_template_association", + Name: "Approval Rule Template Association", }, { - Factory: ResourceRepository, + Factory: resourceRepository, TypeName: "aws_codecommit_repository", Name: "Repository", Tags: &types.ServicePackageResourceTags{ @@ -55,8 +58,9 @@ func (p *servicePackage) SDKResources(ctx context.Context) []*types.ServicePacka }, }, { - Factory: ResourceTrigger, + Factory: resourceTrigger, TypeName: "aws_codecommit_trigger", + Name: "Trigger", }, } } @@ -65,11 +69,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..bada530a226 100644 --- a/internal/service/codecommit/tags_gen.go +++ b/internal/service/codecommit/tags_gen.go @@ -5,9 +5,8 @@ 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" "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 +18,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 +35,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 @@ -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)) } @@ -83,7 +82,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 +93,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 +111,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 +124,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) } diff --git a/internal/service/codecommit/trigger.go b/internal/service/codecommit/trigger.go index a0af93e7123..1b7e2cf1190 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,172 @@ 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("repository_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 || len(output.Triggers) == 0 { + 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{} + + // "RepositoryTriggerBranchNameListRequiredException: Repository trigger branch name list cannot be null". + if v, ok := tfMap["branches"].([]interface{}); ok { + 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 } diff --git a/internal/service/codecommit/trigger_test.go b/internal/service/codecommit/trigger_test.go index fbcdd7cb026..47621750de7 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,15 +25,70 @@ 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{ { 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), ), }, }, @@ -42,56 +97,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 - } - - if len(out.Triggers) == 0 { - return fmt.Errorf("CodeCommit Trigger Failed: %q", out) - } + _, err := tfcodecommit.FindRepositoryTriggersByName(ctx, conn, rs.Primary.ID) - return nil + return err } } @@ -116,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) +} 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,,, 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" 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..2bdea448866 100644 --- a/website/docs/r/codecommit_repository.html.markdown +++ b/website/docs/r/codecommit_repository.html.markdown @@ -19,6 +19,21 @@ 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.arn +} + +resource "aws_kms_key" "test" { + description = "test" + deletion_window_in_days = 7 +} +``` + ## Argument Reference This resource supports the following arguments: @@ -26,6 +41,7 @@ This resource supports the following arguments: * `repository_name` - (Required) The name for the repository. This needs to be less than 100 characters. * `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