Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add build_status_config to codebuild source object #15442

Merged
merged 4 commits into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/15442.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_codebuild_project: Add `build_status_config` attribute to `source` and `secondary_sources` configuration blocks
```
67 changes: 67 additions & 0 deletions aws/resource_aws_codebuild_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,23 @@ func resourceAwsCodeBuildProject() *schema.Resource {
Type: schema.TypeString,
Required: true,
},
"build_status_config": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"context": {
Type: schema.TypeString,
Optional: true,
},
"target_url": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
},
},
},
Expand Down Expand Up @@ -554,6 +571,23 @@ func resourceAwsCodeBuildProject() *schema.Resource {
Type: schema.TypeBool,
Optional: true,
},
"build_status_config": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"context": {
Type: schema.TypeString,
Optional: true,
},
"target_url": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
},
},
},
Expand Down Expand Up @@ -1072,6 +1106,24 @@ func expandProjectSourceData(data map[string]interface{}) codebuild.ProjectSourc
}
}

// Only valid for BITBUCKET, GITHUB, GITHUB_ENTERPRISE source types.
if sourceType == codebuild.SourceTypeBitbucket || sourceType == codebuild.SourceTypeGithub || sourceType == codebuild.SourceTypeGithubEnterprise {
if v, ok := data["build_status_config"]; ok && len(v.([]interface{})) > 0 {
config := v.([]interface{})[0].(map[string]interface{})

buildStatusConfig := &codebuild.BuildStatusConfig{}

if v, ok := config["context"]; ok {
buildStatusConfig.Context = aws.String(v.(string))
}
if v, ok := config["target_url"]; ok {
buildStatusConfig.TargetUrl = aws.String(v.(string))
}

projectSource.BuildStatusConfig = buildStatusConfig
}
}

return projectSource
}

Expand Down Expand Up @@ -1461,6 +1513,8 @@ func flattenAwsCodeBuildProjectSourceData(source *codebuild.ProjectSource) inter

m["git_submodules_config"] = flattenAwsCodebuildProjectGitSubmodulesConfig(source.GitSubmodulesConfig)

m["build_status_config"] = flattenAwsCodebuildProjectBuildStatusConfig(source.BuildStatusConfig)

if source.Auth != nil {
m["auth"] = []interface{}{sourceAuthToMap(source.Auth)}
}
Expand All @@ -1483,6 +1537,19 @@ func flattenAwsCodebuildProjectGitSubmodulesConfig(config *codebuild.GitSubmodul
return []interface{}{values}
}

func flattenAwsCodebuildProjectBuildStatusConfig(config *codebuild.BuildStatusConfig) []interface{} {
if config == nil {
return []interface{}{}
}

values := map[string]interface{}{
"context": aws.StringValue(config.Context),
"target_url": aws.StringValue(config.TargetUrl),
}

return []interface{}{values}
}

func flattenAwsCodeBuildVpcConfig(vpcConfig *codebuild.VpcConfig) []interface{} {
if vpcConfig != nil {
values := map[string]interface{}{}
Expand Down
69 changes: 69 additions & 0 deletions aws/resource_aws_codebuild_project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,16 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
)

func init() {
RegisterServiceErrorCheckFunc(codebuild.EndpointsID, testAccErrorCheckSkipCodebuild)
}

func testAccErrorCheckSkipCodebuild(t *testing.T) resource.ErrorCheckFunc {
return testAccErrorCheckSkipMessagesContaining(t,
"InvalidInputException: Region",
)
}

// This is used for testing aws_codebuild_webhook as well as aws_codebuild_project.
// The Terraform AWS user must have done the manual Bitbucket OAuth dance for this
// functionality to work. Additionally, the Bitbucket user that the Terraform AWS
Expand Down Expand Up @@ -858,6 +868,36 @@ func TestAccAWSCodeBuildProject_SecondarySources_GitSubmodulesConfig_GitHubEnter
})
}

func TestAccAWSCodeBuildProject_Source_BuildStatusConfig_GitHubEnterprise(t *testing.T) {
var project codebuild.Project
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_codebuild_project.test"

if testAccGetPartition() == "aws-us-gov" {
t.Skip("CodeBuild Project build status config is not supported in GovCloud partition")
}

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSCodeBuild(t) },
ErrorCheck: testAccErrorCheck(t, codebuild.EndpointsID),
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCodeBuildProjectDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCodeBuildProjectConfig_Source_BuildStatusConfig_GitHubEnterprise(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeBuildProjectExists(resourceName, &project),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAWSCodeBuildProject_Source_InsecureSSL(t *testing.T) {
var project codebuild.Project
rName := acctest.RandomWithPrefix("tf-acc-test")
Expand Down Expand Up @@ -4197,3 +4237,32 @@ resource "aws_codebuild_project" "test" {
}
`, rName))
}

func testAccAWSCodeBuildProjectConfig_Source_BuildStatusConfig_GitHubEnterprise(rName string) string {
return testAccAWSCodeBuildProjectConfig_Base_ServiceRole(rName) + fmt.Sprintf(`
resource "aws_codebuild_project" "test" {
name = "%s"
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"

build_status_config {
context = "codebuild"
target_url = "https://example.com/$${CODEBUILD_BUILD_ID}"
}
}
}
`, rName)
}
8 changes: 7 additions & 1 deletion website/docs/r/codebuild_project.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,7 @@ Credentials for access to a private Docker registry.
* `insecure_ssl` - (Optional) Ignore SSL warnings when connecting to source control.
* `location` - (Optional) Location of the source code from git or s3.
* `report_build_status` - (Optional) Whether 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`, `BITBUCKET`, or `GITHUB_ENTERPRISE`.
* `build_status_config` - (Optional) Contains information that defines how the build project reports the build status to the source provider. This option is only used when the source provider is `GITHUB`, `GITHUB_ENTERPRISE`, or `BITBUCKET`.
* `source_identifier` - (Required) Source identifier. Source data will be put inside a folder named as this parameter inside AWS CodeBuild source directory
* `type` - (Required) Type of repository that contains the source code to be built. Valid values: `CODECOMMIT`, `CODEPIPELINE`, `GITHUB`, `GITHUB_ENTERPRISE`, `BITBUCKET` or `S3`.

Expand All @@ -337,7 +338,12 @@ This block is only valid when the `type` is `CODECOMMIT`, `GITHUB` or `GITHUB_EN

* `fetch_submodules` - (Required) Whether to fetch Git submodules for the AWS CodeBuild build project.

### source
`build_status_config` supports the following:

* `context` - (Optional) Specifies the context of the build status CodeBuild sends to the source provider. The usage of this parameter depends on the source provider.
* `target_url` - (Optional) Specifies the target url of the build status CodeBuild sends to the source provider. The usage of this parameter depends on the source provider.

`vpc_config` supports the following:

* `auth` - (Optional, **Deprecated**) Configuration block with the authorization settings for AWS CodeBuild to access the source code to be built. This information is for the AWS CodeBuild console's use only. Use the [`aws_codebuild_source_credential` resource](codebuild_source_credential.html) instead. Auth blocks are documented below.
* `buildspec` - (Optional) Build specification to use for this build project's related builds. This must be set when `type` is `NO_SOURCE`.
Expand Down