provider/aws: codecommit check default_branch before update

In AWS codecommit the default branch must have a value unless there are
no branches created, in which case it is not possible to set it to any value.

We query the existing branches and do not update the default branch
if there are none defined remotely.

This solves the issue of the initial creation of the repository with a
resource with `default_branch` defined.
This commit is contained in:
Hector Rivas Gandara 2016-03-30 15:23:32 +01:00
parent 1afd3a53b2
commit 0c49b17f72
1 changed files with 15 additions and 1 deletions

View File

@ -176,12 +176,26 @@ func resourceAwsCodeCommitUpdateDescription(conn *codecommit.CodeCommit, d *sche
}
func resourceAwsCodeCommitUpdateDefaultBranch(conn *codecommit.CodeCommit, d *schema.ResourceData) error {
input := &codecommit.ListBranchesInput{
RepositoryName: aws.String(d.Id()),
}
out, err := conn.ListBranches(input)
if err != nil {
return fmt.Errorf("Error reading CodeCommit Repository branches: %s", err.Error())
}
if len(out.Branches) == 0 {
log.Printf("[WARN] Not setting Default Branch CodeCommit Repository that has no branches: %s", d.Id())
return nil
}
branchInput := &codecommit.UpdateDefaultBranchInput{
RepositoryName: aws.String(d.Id()),
DefaultBranchName: aws.String(d.Get("default_branch").(string)),
}
_, err := conn.UpdateDefaultBranch(branchInput)
_, err = conn.UpdateDefaultBranch(branchInput)
if err != nil {
return fmt.Errorf("Error Updating Default Branch for CodeCommit Repository: %s", err.Error())
}