Skip to content

Commit

Permalink
Merge pull request #20480 from tatsuya-ogawa/f-codebuild-webhook-buil…
Browse files Browse the repository at this point in the history
…dtype

Add parameter build_type to codebuild_webhook
  • Loading branch information
ewbankkit authored Aug 11, 2021
2 parents ac8882a + 3ad193c commit 6e7d495
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 21 deletions.
3 changes: 3 additions & 0 deletions .changelog/20480.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
resource/aws_codebuild_webhook: Add support for `build_type`
```
25 changes: 15 additions & 10 deletions aws/resource_aws_codebuild_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ func resourceAwsCodeBuildWebhook() *schema.Resource {
Required: true,
ForceNew: true,
},
"build_type": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice(codebuild.WebhookBuildType_Values(), false),
},
"branch_filter": {
Type: schema.TypeString,
Optional: true,
Expand All @@ -45,16 +50,9 @@ func resourceAwsCodeBuildWebhook() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
codebuild.WebhookFilterTypeEvent,
codebuild.WebhookFilterTypeActorAccountId,
codebuild.WebhookFilterTypeBaseRef,
codebuild.WebhookFilterTypeFilePath,
codebuild.WebhookFilterTypeHeadRef,
codebuild.WebhookFilterTypeCommitMessage,
}, false),
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice(codebuild.WebhookFilterType_Values(), false),
},
"exclude_matched_pattern": {
Type: schema.TypeBool,
Expand Down Expand Up @@ -98,6 +96,10 @@ func resourceAwsCodeBuildWebhookCreate(d *schema.ResourceData, meta interface{})
FilterGroups: expandWebhookFilterGroups(d),
}

if v, ok := d.GetOk("build_type"); ok {
input.BuildType = aws.String(v.(string))
}

// The CodeBuild API requires this to be non-empty if defined
if v, ok := d.GetOk("branch_filter"); ok {
input.BranchFilter = aws.String(v.(string))
Expand Down Expand Up @@ -180,6 +182,7 @@ func resourceAwsCodeBuildWebhookRead(d *schema.ResourceData, meta interface{}) e
return nil
}

d.Set("build_type", project.Webhook.BuildType)
d.Set("branch_filter", project.Webhook.BranchFilter)
d.Set("filter_group", flattenAwsCodeBuildWebhookFilterGroups(project.Webhook.FilterGroups))
d.Set("payload_url", project.Webhook.PayloadUrl)
Expand All @@ -199,12 +202,14 @@ func resourceAwsCodeBuildWebhookUpdate(d *schema.ResourceData, meta interface{})
if len(filterGroups) >= 1 {
_, err = conn.UpdateWebhook(&codebuild.UpdateWebhookInput{
ProjectName: aws.String(d.Id()),
BuildType: aws.String(d.Get("build_type").(string)),
FilterGroups: filterGroups,
RotateSecret: aws.Bool(false),
})
} else {
_, err = conn.UpdateWebhook(&codebuild.UpdateWebhookInput{
ProjectName: aws.String(d.Id()),
BuildType: aws.String(d.Get("build_type").(string)),
BranchFilter: aws.String(d.Get("branch_filter").(string)),
RotateSecret: aws.Bool(false),
})
Expand Down
69 changes: 59 additions & 10 deletions aws/resource_aws_codebuild_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,41 @@ func TestAccAWSCodeBuildWebhook_GitHubEnterprise(t *testing.T) {
})
}

func TestAccAWSCodeBuildWebhook_BuildType(t *testing.T) {
var webhook codebuild.Webhook
rName := acctest.RandomWithPrefix("tf-acc-test")
resourceName := "aws_codebuild_webhook.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t); testAccPreCheckAWSCodeBuild(t) },
ErrorCheck: testAccErrorCheck(t, codebuild.EndpointsID),
Providers: testAccProviders,
CheckDestroy: testAccCheckAWSCodeBuildWebhookDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSCodeBuildWebhookConfig_BuildType(rName, "BUILD"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeBuildWebhookExists(resourceName, &webhook),
resource.TestCheckResourceAttr(resourceName, "build_type", "BUILD"),
),
},
{
Config: testAccAWSCodeBuildWebhookConfig_BuildType(rName, "BUILD_BATCH"),
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSCodeBuildWebhookExists(resourceName, &webhook),
resource.TestCheckResourceAttr(resourceName, "build_type", "BUILD_BATCH"),
),
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"secret"},
},
},
})
}

func TestAccAWSCodeBuildWebhook_BranchFilter(t *testing.T) {
var webhook codebuild.Webhook
rName := acctest.RandomWithPrefix("tf-acc-test")
Expand Down Expand Up @@ -291,25 +326,28 @@ func testAccCheckAWSCodeBuildWebhookExists(name string, webhook *codebuild.Webho

func testAccAWSCodeBuildWebhookConfig_Bitbucket(rName, sourceLocation string) string {
return composeConfig(
testAccAWSCodeBuildProjectConfig_Source_Type_Bitbucket(rName, sourceLocation), `
testAccAWSCodeBuildProjectConfig_Source_Type_Bitbucket(rName, sourceLocation),
`
resource "aws_codebuild_webhook" "test" {
project_name = aws_codebuild_project.test.name
}
`)
}

func testAccAWSCodeBuildWebhookConfig_GitHub(rName string) string {
return fmt.Sprintf(testAccAWSCodeBuildProjectConfig_basic(rName) + `
return composeConfig(
testAccAWSCodeBuildProjectConfig_basic(rName),
`
resource "aws_codebuild_webhook" "test" {
project_name = aws_codebuild_project.test.name
}
`)
}

func testAccAWSCodeBuildWebhookConfig_GitHubEnterprise(rName string, branchFilter string) string {
return testAccAWSCodeBuildProjectConfig_Base_ServiceRole(rName) + fmt.Sprintf(`
return composeConfig(testAccAWSCodeBuildProjectConfig_Base_ServiceRole(rName), fmt.Sprintf(`
resource "aws_codebuild_project" "test" {
name = "%s"
name = %[1]q
service_role = aws_iam_role.test.arn
artifacts {
Expand All @@ -330,22 +368,33 @@ resource "aws_codebuild_project" "test" {
resource "aws_codebuild_webhook" "test" {
project_name = aws_codebuild_project.test.name
branch_filter = "%s"
branch_filter = %[2]q
}
`, rName, branchFilter)
`, rName, branchFilter))
}

func testAccAWSCodeBuildWebhookConfig_BuildType(rName, branchFilter string) string {
return composeConfig(testAccAWSCodeBuildProjectConfig_basic(rName), fmt.Sprintf(`
resource "aws_codebuild_webhook" "test" {
build_type = %[1]q
project_name = aws_codebuild_project.test.name
}
`, branchFilter))
}

func testAccAWSCodeBuildWebhookConfig_BranchFilter(rName, branchFilter string) string {
return fmt.Sprintf(testAccAWSCodeBuildProjectConfig_basic(rName)+`
return composeConfig(testAccAWSCodeBuildProjectConfig_basic(rName), fmt.Sprintf(`
resource "aws_codebuild_webhook" "test" {
branch_filter = "%s"
branch_filter = %[1]q
project_name = aws_codebuild_project.test.name
}
`, branchFilter)
`, branchFilter))
}

func testAccAWSCodeBuildWebhookConfig_FilterGroup(rName string) string {
return fmt.Sprintf(testAccAWSCodeBuildProjectConfig_basic(rName) + `
return composeConfig(
testAccAWSCodeBuildProjectConfig_basic(rName),
`
resource "aws_codebuild_webhook" "test" {
project_name = aws_codebuild_project.test.name
Expand Down
3 changes: 2 additions & 1 deletion website/docs/r/codebuild_webhook.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ When working with [Bitbucket](https://bitbucket.org) and [GitHub](https://github
```terraform
resource "aws_codebuild_webhook" "example" {
project_name = aws_codebuild_project.example.name
build_type = "BUILD"
filter_group {
filter {
type = "EVENT"
Expand Down Expand Up @@ -69,6 +69,7 @@ resource "github_repository_webhook" "example" {
The following arguments are supported:

* `project_name` - (Required) The name of the build project.
* `build_type` - (Optional) The type of build this webhook will trigger. Valid values for this parameter are: `BUILD`, `BUILD_BATCH`.
* `branch_filter` - (Optional) A regular expression used to determine which branches get built. Default is all branches are built. It is recommended to use `filter_group` over `branch_filter`.
* `filter_group` - (Optional) Information about the webhook's trigger. Filter group blocks are documented below.

Expand Down

0 comments on commit 6e7d495

Please sign in to comment.