From 899d6f71a5d771fbe8a031002058bd8fd1c6de29 Mon Sep 17 00:00:00 2001 From: Brian Flad Date: Wed, 11 Jul 2018 11:13:01 -0400 Subject: [PATCH] resource/aws_codebuild_project: Support report build status for GitHub source type --- aws/resource_aws_codebuild_project.go | 54 ++-- aws/resource_aws_codebuild_project_test.go | 302 +++++++++++++++++- .../docs/r/codebuild_project.html.markdown | 1 + 3 files changed, 317 insertions(+), 40 deletions(-) diff --git a/aws/resource_aws_codebuild_project.go b/aws/resource_aws_codebuild_project.go index 0ff25f5663e2..275c04ce822c 100644 --- a/aws/resource_aws_codebuild_project.go +++ b/aws/resource_aws_codebuild_project.go @@ -236,6 +236,10 @@ func resourceAwsCodeBuildProject() *schema.Resource { Type: schema.TypeBool, Optional: true, }, + "report_build_status": { + Type: schema.TypeBool, + Optional: true, + }, }, }, Required: true, @@ -508,17 +512,19 @@ func expandProjectSource(d *schema.ResourceData) codebuild.ProjectSource { data := configRaw.(map[string]interface{}) sourceType := data["type"].(string) - location := data["location"].(string) - buildspec := data["buildspec"].(string) - gitCloneDepth := aws.Int64(int64(data["git_clone_depth"].(int))) - insecureSsl := aws.Bool(data["insecure_ssl"].(bool)) projectSource = codebuild.ProjectSource{ - Type: &sourceType, - Location: &location, - Buildspec: &buildspec, - GitCloneDepth: gitCloneDepth, - InsecureSsl: insecureSsl, + Buildspec: aws.String(data["buildspec"].(string)), + GitCloneDepth: aws.Int64(int64(data["git_clone_depth"].(int))), + InsecureSsl: aws.Bool(data["insecure_ssl"].(bool)), + Location: aws.String(data["location"].(string)), + Type: aws.String(sourceType), + } + + // Only valid for GITHUB source type, e.g. + // InvalidInputException: Source type GITHUB_ENTERPRISE does not support ReportBuildStatus + if sourceType == codebuild.SourceTypeGithub { + projectSource.ReportBuildStatus = aws.Bool(data["report_build_status"].(bool)) } if v, ok := data["auth"]; ok { @@ -764,30 +770,19 @@ func flattenAwsCodeBuildProjectEnvironment(environment *codebuild.ProjectEnviron func flattenAwsCodeBuildProjectSource(source *codebuild.ProjectSource) []interface{} { l := make([]interface{}, 1) - m := map[string]interface{}{} - - m["type"] = *source.Type + m := map[string]interface{}{ + "buildspec": aws.StringValue(source.Buildspec), + "location": aws.StringValue(source.Location), + "git_clone_depth": int(aws.Int64Value(source.GitCloneDepth)), + "insecure_ssl": aws.BoolValue(source.InsecureSsl), + "report_build_status": aws.BoolValue(source.ReportBuildStatus), + "type": aws.StringValue(source.Type), + } if source.Auth != nil { m["auth"] = schema.NewSet(resourceAwsCodeBuildProjectSourceAuthHash, []interface{}{sourceAuthToMap(source.Auth)}) } - if source.Buildspec != nil { - m["buildspec"] = *source.Buildspec - } - - if source.Location != nil { - m["location"] = *source.Location - } - - if source.GitCloneDepth != nil { - m["git_clone_depth"] = *source.GitCloneDepth - } - - if source.InsecureSsl != nil { - m["insecure_ssl"] = *source.InsecureSsl - } - l[0] = m return l @@ -862,6 +857,9 @@ func resourceAwsCodeBuildProjectSourceHash(v interface{}) int { if v, ok := m["insecure_ssl"]; ok { buf.WriteString(fmt.Sprintf("%s-", strconv.FormatBool(v.(bool)))) } + if v, ok := m["report_build_status"]; ok { + buf.WriteString(fmt.Sprintf("%s-", strconv.FormatBool(v.(bool)))) + } return hashcode.String(buf.String()) } diff --git a/aws/resource_aws_codebuild_project_test.go b/aws/resource_aws_codebuild_project_test.go index e5b1a3b1165c..60e5b07c2981 100644 --- a/aws/resource_aws_codebuild_project_test.go +++ b/aws/resource_aws_codebuild_project_test.go @@ -60,11 +60,12 @@ func TestAccAWSCodeBuildProject_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "environment.1974383098.type", "LINUX_CONTAINER"), resource.TestMatchResourceAttr(resourceName, "service_role", regexp.MustCompile(`^arn:[^:]+:iam::[^:]+:role/tf-acc-test-[0-9]+$`)), resource.TestCheckResourceAttr(resourceName, "source.#", "1"), - resource.TestCheckResourceAttr(resourceName, "source.3661695266.auth.#", "0"), - resource.TestCheckResourceAttr(resourceName, "source.3661695266.git_clone_depth", "0"), - resource.TestCheckResourceAttr(resourceName, "source.3661695266.insecure_ssl", "false"), - resource.TestCheckResourceAttr(resourceName, "source.3661695266.location", "https://github.com/hashibot-test/aws-test.git"), - resource.TestCheckResourceAttr(resourceName, "source.3661695266.type", "GITHUB"), + resource.TestCheckResourceAttr(resourceName, "source.1441597390.auth.#", "0"), + resource.TestCheckResourceAttr(resourceName, "source.1441597390.git_clone_depth", "0"), + resource.TestCheckResourceAttr(resourceName, "source.1441597390.insecure_ssl", "false"), + resource.TestCheckResourceAttr(resourceName, "source.1441597390.location", "https://github.com/hashibot-test/aws-test.git"), + resource.TestCheckResourceAttr(resourceName, "source.1441597390.report_build_status", "false"), + resource.TestCheckResourceAttr(resourceName, "source.1441597390.type", "GITHUB"), resource.TestCheckResourceAttr(resourceName, "vpc_config.#", "0"), resource.TestCheckResourceAttr(resourceName, "tags.%", "0"), ), @@ -280,9 +281,9 @@ func TestAccAWSCodeBuildProject_Source_Auth(t *testing.T) { Config: testAccAWSCodeBuildProjectConfig_Source_Auth(rName, "FAKERESOURCE1", "OAUTH"), Check: resource.ComposeTestCheckFunc( testAccCheckAWSCodeBuildProjectExists(resourceName, &project), - resource.TestCheckResourceAttr(resourceName, "source.3680370172.auth.#", "1"), - resource.TestCheckResourceAttr(resourceName, "source.3680370172.auth.2706882902.resource", "FAKERESOURCE1"), - resource.TestCheckResourceAttr(resourceName, "source.3680370172.auth.2706882902.type", "OAUTH"), + resource.TestCheckResourceAttr(resourceName, "source.3680505372.auth.#", "1"), + resource.TestCheckResourceAttr(resourceName, "source.3680505372.auth.2706882902.resource", "FAKERESOURCE1"), + resource.TestCheckResourceAttr(resourceName, "source.3680505372.auth.2706882902.type", "OAUTH"), ), }, }, @@ -303,14 +304,14 @@ func TestAccAWSCodeBuildProject_Source_GitCloneDepth(t *testing.T) { Config: testAccAWSCodeBuildProjectConfig_Source_GitCloneDepth(rName, 1), Check: resource.ComposeTestCheckFunc( testAccCheckAWSCodeBuildProjectExists(resourceName, &project), - resource.TestCheckResourceAttr(resourceName, "source.402128226.git_clone_depth", "1"), + resource.TestCheckResourceAttr(resourceName, "source.1181740906.git_clone_depth", "1"), ), }, { Config: testAccAWSCodeBuildProjectConfig_Source_GitCloneDepth(rName, 2), Check: resource.ComposeTestCheckFunc( testAccCheckAWSCodeBuildProjectExists(resourceName, &project), - resource.TestCheckResourceAttr(resourceName, "source.2574842497.git_clone_depth", "2"), + resource.TestCheckResourceAttr(resourceName, "source.974047921.git_clone_depth", "2"), ), }, }, @@ -331,14 +332,147 @@ func TestAccAWSCodeBuildProject_Source_InsecureSSL(t *testing.T) { Config: testAccAWSCodeBuildProjectConfig_Source_InsecureSSL(rName, true), Check: resource.ComposeTestCheckFunc( testAccCheckAWSCodeBuildProjectExists(resourceName, &project), - resource.TestCheckResourceAttr(resourceName, "source.3334111800.insecure_ssl", "true"), + resource.TestCheckResourceAttr(resourceName, "source.1976396802.insecure_ssl", "true"), ), }, { Config: testAccAWSCodeBuildProjectConfig_Source_InsecureSSL(rName, false), Check: resource.ComposeTestCheckFunc( testAccCheckAWSCodeBuildProjectExists(resourceName, &project), - resource.TestCheckResourceAttr(resourceName, "source.3680370172.insecure_ssl", "false"), + resource.TestCheckResourceAttr(resourceName, "source.3680505372.insecure_ssl", "false"), + ), + }, + }, + }) +} + +func TestAccAWSCodeBuildProject_Source_ReportBuildStatus(t *testing.T) { + var project codebuild.Project + rName := acctest.RandomWithPrefix("tf-acc-test") + resourceName := "aws_codebuild_project.test" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSCodeBuildProjectDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSCodeBuildProjectConfig_Source_ReportBuildStatus(rName, true), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSCodeBuildProjectExists(resourceName, &project), + resource.TestCheckResourceAttr(resourceName, "source.4215890488.report_build_status", "true"), + ), + }, + { + Config: testAccAWSCodeBuildProjectConfig_Source_ReportBuildStatus(rName, false), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSCodeBuildProjectExists(resourceName, &project), + resource.TestCheckResourceAttr(resourceName, "source.3680505372.report_build_status", "false"), + ), + }, + }, + }) +} + +func TestAccAWSCodeBuildProject_Source_Type_Bitbucket(t *testing.T) { + var project codebuild.Project + rName := acctest.RandomWithPrefix("tf-acc-test") + resourceName := "aws_codebuild_project.test" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSCodeBuildProjectDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSCodeBuildProjectConfig_Source_Type_Bitbucket(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSCodeBuildProjectExists(resourceName, &project), + resource.TestCheckResourceAttr(resourceName, "source.2806293607.type", "BITBUCKET"), + ), + }, + }, + }) +} + +func TestAccAWSCodeBuildProject_Source_Type_CodeCommit(t *testing.T) { + var project codebuild.Project + rName := acctest.RandomWithPrefix("tf-acc-test") + resourceName := "aws_codebuild_project.test" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSCodeBuildProjectDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSCodeBuildProjectConfig_Source_Type_CodeCommit(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSCodeBuildProjectExists(resourceName, &project), + resource.TestCheckResourceAttr(resourceName, "source.3715340088.type", "CODECOMMIT"), + ), + }, + }, + }) +} + +func TestAccAWSCodeBuildProject_Source_Type_CodePipeline(t *testing.T) { + var project codebuild.Project + rName := acctest.RandomWithPrefix("tf-acc-test") + resourceName := "aws_codebuild_project.test" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSCodeBuildProjectDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSCodeBuildProjectConfig_Source_Type_CodePipeline(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSCodeBuildProjectExists(resourceName, &project), + resource.TestCheckResourceAttr(resourceName, "source.2280907000.type", "CODEPIPELINE"), + ), + }, + }, + }) +} + +func TestAccAWSCodeBuildProject_Source_Type_GitHubEnterprise(t *testing.T) { + var project codebuild.Project + rName := acctest.RandomWithPrefix("tf-acc-test") + resourceName := "aws_codebuild_project.test" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSCodeBuildProjectDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSCodeBuildProjectConfig_Source_Type_GitHubEnterprise(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSCodeBuildProjectExists(resourceName, &project), + resource.TestCheckResourceAttr(resourceName, "source.553628638.type", "GITHUB_ENTERPRISE"), + ), + }, + }, + }) +} + +func TestAccAWSCodeBuildProject_Source_Type_S3(t *testing.T) { + var project codebuild.Project + rName := acctest.RandomWithPrefix("tf-acc-test") + resourceName := "aws_codebuild_project.test" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSCodeBuildProjectDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSCodeBuildProjectConfig_Source_Type_S3(rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSCodeBuildProjectExists(resourceName, &project), + resource.TestCheckResourceAttr(resourceName, "source.2751363124.type", "S3"), ), }, }, @@ -861,6 +995,150 @@ resource "aws_codebuild_project" "test" { `, rName, insecureSSL) } +func testAccAWSCodeBuildProjectConfig_Source_ReportBuildStatus(rName string, reportBuildStatus bool) string { + return testAccAWSCodeBuildProjectConfig_Base_ServiceRole(rName) + fmt.Sprintf(` +resource "aws_codebuild_project" "test" { + name = %q + service_role = "${aws_iam_role.test.arn}" + + artifacts { + type = "NO_ARTIFACTS" + } + + environment { + compute_type = "BUILD_GENERAL1_SMALL" + image = "2" + type = "LINUX_CONTAINER" + } + + source { + location = "https://github.com/hashicorp/packer.git" + report_build_status = %t + type = "GITHUB" + } +} +`, rName, reportBuildStatus) +} + +func testAccAWSCodeBuildProjectConfig_Source_Type_Bitbucket(rName string) string { + return testAccAWSCodeBuildProjectConfig_Base_ServiceRole(rName) + fmt.Sprintf(` +resource "aws_codebuild_project" "test" { + name = %q + service_role = "${aws_iam_role.test.arn}" + + artifacts { + type = "NO_ARTIFACTS" + } + + environment { + compute_type = "BUILD_GENERAL1_SMALL" + image = "2" + type = "LINUX_CONTAINER" + } + + source { + location = "https://bitbucket.org/organization/repository.git" + type = "BITBUCKET" + } +} +`, rName) +} + +func testAccAWSCodeBuildProjectConfig_Source_Type_CodeCommit(rName string) string { + return testAccAWSCodeBuildProjectConfig_Base_ServiceRole(rName) + fmt.Sprintf(` +resource "aws_codebuild_project" "test" { + name = %q + service_role = "${aws_iam_role.test.arn}" + + artifacts { + type = "NO_ARTIFACTS" + } + + environment { + compute_type = "BUILD_GENERAL1_SMALL" + image = "2" + type = "LINUX_CONTAINER" + } + + source { + location = "https://git-codecommit.region-id.amazonaws.com/v1/repos/repo-name" + type = "CODECOMMIT" + } +} +`, rName) +} + +func testAccAWSCodeBuildProjectConfig_Source_Type_CodePipeline(rName string) string { + return testAccAWSCodeBuildProjectConfig_Base_ServiceRole(rName) + fmt.Sprintf(` +resource "aws_codebuild_project" "test" { + name = %q + service_role = "${aws_iam_role.test.arn}" + + artifacts { + type = "CODEPIPELINE" + } + + environment { + compute_type = "BUILD_GENERAL1_SMALL" + image = "2" + type = "LINUX_CONTAINER" + } + + source { + type = "CODEPIPELINE" + } +} +`, rName) +} + +func testAccAWSCodeBuildProjectConfig_Source_Type_GitHubEnterprise(rName string) string { + return testAccAWSCodeBuildProjectConfig_Base_ServiceRole(rName) + fmt.Sprintf(` +resource "aws_codebuild_project" "test" { + name = %q + service_role = "${aws_iam_role.test.arn}" + + artifacts { + type = "NO_ARTIFACTS" + } + + environment { + compute_type = "BUILD_GENERAL1_SMALL" + image = "2" + type = "LINUX_CONTAINER" + } + + source { + location = "https://example.com/organization/repository.git" + type = "GITHUB_ENTERPRISE" + } +} +`, rName) +} + +func testAccAWSCodeBuildProjectConfig_Source_Type_S3(rName string) string { + return testAccAWSCodeBuildProjectConfig_Base_ServiceRole(rName) + fmt.Sprintf(` +resource "aws_codebuild_project" "test" { + name = %q + service_role = "${aws_iam_role.test.arn}" + + artifacts { + type = "NO_ARTIFACTS" + } + + environment { + compute_type = "BUILD_GENERAL1_SMALL" + image = "2" + type = "LINUX_CONTAINER" + } + + source { + location = "bucket-name/object-name.zip" + type = "S3" + } +} +`, rName) +} + func testAccAWSCodeBuildProjectConfig_Tags(rName, tagKey, tagValue string) string { return testAccAWSCodeBuildProjectConfig_Base_ServiceRole(rName) + fmt.Sprintf(` resource "aws_codebuild_project" "test" { diff --git a/website/docs/r/codebuild_project.html.markdown b/website/docs/r/codebuild_project.html.markdown index a8512c6a37e4..35ac817a59d3 100644 --- a/website/docs/r/codebuild_project.html.markdown +++ b/website/docs/r/codebuild_project.html.markdown @@ -194,6 +194,7 @@ The following arguments are supported: * `git_clone_depth` - (Optional) Truncate git history to this many commits. * `insecure_ssl` - (Optional) Ignore SSL warnings when connecting to source control. * `location` - (Optional) The location of the source code from git or s3. +* `report_build_status` - (Optional) Set to `true` to report the status of a build's start and finish to your source provider. This option is only valid when your source provider is GitHub. `auth` supports the following: