Skip to content

Commit

Permalink
Properly handle renaming of CodeCommit repository
Browse files Browse the repository at this point in the history
Resolve #32161
  • Loading branch information
Michagogo authored and gdavison committed Oct 13, 2023
1 parent 4a19834 commit a568ac6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
21 changes: 20 additions & 1 deletion internal/service/codecommit/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ func ResourceRepository() *schema.Resource {
"repository_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringLenBetween(0, 100),
},

Expand Down Expand Up @@ -146,6 +145,12 @@ func resourceRepositoryRead(ctx context.Context, d *schema.ResourceData, meta in
func resourceRepositoryUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
conn := meta.(*conns.AWSClient).CodeCommitConn(ctx)

if d.HasChange("repository_name") {
if err := resourceUpdateRepositoryName(ctx, conn, d); err != nil {
return sdkdiag.AppendErrorf(diags, "updating CodeCommit Repository (%s) name: %s", d.Id(), err)
}
}

if d.HasChange("default_branch") {
if err := resourceUpdateDefaultBranch(ctx, conn, d); err != nil {
Expand Down Expand Up @@ -177,6 +182,20 @@ func resourceRepositoryDelete(ctx context.Context, d *schema.ResourceData, meta
return diags
}

func resourceUpdateRepositoryName(ctx context.Context, conn *codecommit.CodeCommit, d *schema.ResourceData) error {
branchInput := &codecommit.UpdateRepositoryNameInput{
OldName: aws.String(d.Id()),
NewName: aws.String(d.Get("repository_name").(string)),
}

_, err := conn.UpdateRepositoryNameWithContext(ctx, branchInput)
if err != nil {
return fmt.Errorf("Updating Repository Name for CodeCommit Repository: %s", err.Error())
}

return nil
}

func resourceUpdateDescription(ctx context.Context, conn *codecommit.CodeCommit, d *schema.ResourceData) error {
branchInput := &codecommit.UpdateRepositoryDescriptionInput{
RepositoryName: aws.String(d.Id()),
Expand Down
6 changes: 4 additions & 2 deletions internal/service/codecommit/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ func TestAccCodeCommitRepository_withChanges(t *testing.T) {
testAccCheckRepositoryExists(ctx, "aws_codecommit_repository.test"),
resource.TestCheckResourceAttr(
"aws_codecommit_repository.test", "description", "This is a test description - with changes"),
),
resource.TestCheckResourceAttr(
"aws_codecommit_repository.test", "repository_name", "renamed_test_repository"),
),
},
},
})
Expand Down Expand Up @@ -262,7 +264,7 @@ resource "aws_codecommit_repository" "test" {
func testAccRepositoryConfig_changes(rInt int) string {
return fmt.Sprintf(`
resource "aws_codecommit_repository" "test" {
repository_name = "test_repository_%d"
repository_name = "renamed_test_repository_%d"
description = "This is a test description - with changes"
}
`, rInt)
Expand Down

0 comments on commit a568ac6

Please sign in to comment.