Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly handle renaming of CodeCommit repository #32207

Merged
merged 6 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/32207.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
resource/aws_codecommit_repository: rename repository when repository_name is changed, don't force replacement, causing data loss
```
35 changes: 31 additions & 4 deletions 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 @@ -137,7 +136,11 @@ func resourceRepositoryRead(ctx context.Context, d *schema.ResourceData, meta in
d.Set("repository_name", out.RepositoryMetadata.RepositoryName)

if _, ok := d.GetOk("default_branch"); ok {
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 out.RepositoryMetadata.DefaultBranch != nil { // nosemgrep:ci.helper-schema-ResourceData-Set-extraneous-nil-check
d.Set("default_branch", out.RepositoryMetadata.DefaultBranch)
}
}

return diags
Expand All @@ -147,6 +150,12 @@ func resourceRepositoryUpdate(ctx context.Context, d *schema.ResourceData, meta
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 {
return sdkdiag.AppendErrorf(diags, "updating CodeCommit Repository (%s) default branch: %s", d.Id(), err)
Expand Down Expand Up @@ -177,6 +186,25 @@ func resourceRepositoryDelete(ctx context.Context, d *schema.ResourceData, meta
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),
}

_, 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
}

func resourceUpdateDescription(ctx context.Context, conn *codecommit.CodeCommit, d *schema.ResourceData) error {
branchInput := &codecommit.UpdateRepositoryDescriptionInput{
RepositoryName: aws.String(d.Id()),
Expand Down Expand Up @@ -211,8 +239,7 @@ func resourceUpdateDefaultBranch(ctx context.Context, conn *codecommit.CodeCommi
DefaultBranchName: aws.String(d.Get("default_branch").(string)),
}

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

Expand Down
Loading
Loading