Skip to content

Commit

Permalink
Fixes update name errors
Browse files Browse the repository at this point in the history
  • Loading branch information
gdavison committed Oct 13, 2023
1 parent eba739b commit 7e2f369
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
7 changes: 6 additions & 1 deletion internal/service/codecommit/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,16 +187,21 @@ func resourceRepositoryDelete(ctx context.Context, d *schema.ResourceData, meta
}

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(d.Get("repository_name").(string)),
NewName: aws.String(newName),
}

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

// The Id is the name
d.SetId(newName)

return nil
}

Expand Down
56 changes: 56 additions & 0 deletions internal/service/codecommit/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ func TestAccCodeCommitRepository_withChanges(t *testing.T) {
Config: testAccRepositoryConfig_changes(rNameUpdated),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckRepositoryExists(ctx, resourceName, &v2),
testAccCheckRepositoryNotRecreated(&v1, &v2),
resource.TestCheckResourceAttr(resourceName, "description", "This is a test description - with changes"),
resource.TestCheckResourceAttr(resourceName, "repository_name", rNameUpdated),
),
Expand Down Expand Up @@ -211,6 +212,52 @@ func TestAccCodeCommitRepository_tags(t *testing.T) {
})
}

func TestAccCodeCommitRepository_UpdateNameAndTags(t *testing.T) {
ctx := acctest.Context(t)
rName := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
rNameUpdated := sdkacctest.RandomWithPrefix(acctest.ResourcePrefix)
resourceName := "aws_codecommit_repository.test"
var v1, v2 codecommit.RepositoryMetadata

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acctest.PreCheck(ctx, t) },
ErrorCheck: acctest.ErrorCheck(t, codecommit.EndpointsID),
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories,
CheckDestroy: testAccCheckRepositoryDestroy(ctx),
Steps: []resource.TestStep{
{
Config: testAccRepositoryConfig_tags1(rName, "key1", "value1"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckRepositoryExists(ctx, resourceName, &v1),
resource.TestCheckResourceAttr(resourceName, "repository_name", rName),
resource.TestCheckResourceAttr(resourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccRepositoryConfig_tags2(rNameUpdated, "key1", "value1updated", "key2", "value2"),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckRepositoryExists(ctx, resourceName, &v2),
resource.TestCheckResourceAttr(resourceName, "repository_name", rNameUpdated),
resource.TestCheckResourceAttr(resourceName, "tags.%", "2"),
resource.TestCheckResourceAttr(resourceName, "tags.key1", "value1updated"),
resource.TestCheckResourceAttr(resourceName, "tags.key2", "value2"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func testAccCheckRepositoryExists(ctx context.Context, name string, v *codecommit.RepositoryMetadata) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
Expand Down Expand Up @@ -273,6 +320,15 @@ func testAccCheckRepositoryDestroy(ctx context.Context) resource.TestCheckFunc {
}
}

func testAccCheckRepositoryNotRecreated(v1, v2 *codecommit.RepositoryMetadata) resource.TestCheckFunc {
return func(s *terraform.State) error {
if aws.StringValue(v1.RepositoryId) != aws.StringValue(v2.RepositoryId) {
return fmt.Errorf("CodeCommit Repository recreated")
}
return nil
}
}

func testAccRepositoryConfig_basic(rName string) string {
return fmt.Sprintf(`
resource "aws_codecommit_repository" "test" {
Expand Down

0 comments on commit 7e2f369

Please sign in to comment.