Skip to content

Commit

Permalink
resource/aws_codebuild_project: Additional handling for environment c…
Browse files Browse the repository at this point in the history
…ertificate

* Add certificate to resourceAwsCodeBuildProjectEnvironmentHash
* Add ValidateFunc for .pem and .zip ending
* Only send certificate when non-empty
* Fix acceptance testing for changing TypeSet hash
  • Loading branch information
bflad committed Oct 9, 2018
1 parent ca8a2c2 commit c5fe073
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
10 changes: 7 additions & 3 deletions aws/resource_aws_codebuild_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,9 @@ func resourceAwsCodeBuildProject() *schema.Resource {
Default: false,
},
"certificate": {
Type: schema.TypeString,
Optional: true,
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringMatch(regexp.MustCompile(`\.(pem|zip)$`), "must end in .pem or .zip"),
},
},
},
Expand Down Expand Up @@ -623,7 +624,7 @@ func expandProjectEnvironment(d *schema.ResourceData) *codebuild.ProjectEnvironm
projectEnv.Type = aws.String(v.(string))
}

if v := envConfig["certificate"]; v != nil {
if v, ok := envConfig["certificate"]; ok && v.(string) != "" {
projectEnv.Certificate = aws.String(v.(string))
}

Expand Down Expand Up @@ -1080,6 +1081,9 @@ func resourceAwsCodeBuildProjectEnvironmentHash(v interface{}) int {
buf.WriteString(fmt.Sprintf("%s-", computeType))
buf.WriteString(fmt.Sprintf("%s-", image))
buf.WriteString(fmt.Sprintf("%t-", privilegedMode))
if v, ok := m["certificate"]; ok && v.(string) != "" {
buf.WriteString(fmt.Sprintf("%s-", v.(string)))
}
for _, e := range environmentVariables {
if e != nil { // Old statefiles might have nil values in them
ev := e.(map[string]interface{})
Expand Down
11 changes: 10 additions & 1 deletion aws/resource_aws_codebuild_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ func TestAccAWSCodeBuildProject_Environment_Certificate(t *testing.T) {
Config: testAccAWSCodeBuildProjectConfig_Environment_Certificate(rName, bName, oName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeBuildProjectExists(resourceName, &project),
resource.TestCheckResourceAttr(resourceName, "environment.1974383098.certificate", fmt.Sprintf("%s/%s", bName, oName)),
testAccCheckAWSCodeBuildProjectCertificate(&project, fmt.Sprintf("%s/%s", bName, oName)),
),
},
},
Expand Down Expand Up @@ -776,6 +776,15 @@ func testAccCheckAWSCodeBuildProjectDestroy(s *terraform.State) error {
return nil
}

func testAccCheckAWSCodeBuildProjectCertificate(project *codebuild.Project, expectedCertificate string) resource.TestCheckFunc {
return func(s *terraform.State) error {
if aws.StringValue(project.Environment.Certificate) != expectedCertificate {
return fmt.Errorf("CodeBuild Project certificate (%s) did not match: %s", aws.StringValue(project.Environment.Certificate), expectedCertificate)
}
return nil
}
}

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

0 comments on commit c5fe073

Please sign in to comment.