From 0c49b17f729eeeafbbf74a81a2c14b161e697751 Mon Sep 17 00:00:00 2001 From: Hector Rivas Gandara Date: Wed, 30 Mar 2016 15:23:32 +0100 Subject: [PATCH] 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. --- .../aws/resource_aws_codecommit_repository.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/builtin/providers/aws/resource_aws_codecommit_repository.go b/builtin/providers/aws/resource_aws_codecommit_repository.go index 48803dca0..42691a6d8 100644 --- a/builtin/providers/aws/resource_aws_codecommit_repository.go +++ b/builtin/providers/aws/resource_aws_codecommit_repository.go @@ -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()) }