Skip to content

Commit

Permalink
Merge pull request #32207 from Michagogo/b-aws_codecommit_repository-…
Browse files Browse the repository at this point in the history
…handle-renaming

Properly handle renaming of CodeCommit repository
  • Loading branch information
gdavison committed Oct 13, 2023
2 parents 936bc1a + 7e9ad1d commit a53e897
Show file tree
Hide file tree
Showing 3 changed files with 178 additions and 69 deletions.
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: Doesn't force replacement when renaming
```
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

0 comments on commit a53e897

Please sign in to comment.