Skip to content

Commit

Permalink
Add cloudbuild_trigger build timeout and CustomizeDiff
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
c2thorn authored and modular-magician committed Nov 18, 2019
1 parent b486de7 commit 27e1d13
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 0 deletions.
78 changes: 78 additions & 0 deletions google/resource_cloud_build_trigger.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,63 @@ import (
"fmt"
"log"
"reflect"
"regexp"
"strconv"
"time"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

var (
cloudBuildTimeoutRegexp = regexp.MustCompile("\\d+s$")
)

func stepTimeoutCustomizeDiff(diff *schema.ResourceDiff, v interface{}) error {
buildList := diff.Get("build").([]interface{})
if len(buildList) == 0 || buildList[0] == nil {
return nil
}
build := buildList[0].(map[string]interface{})
buildTimeoutString := build["timeout"].(string)

matched := cloudBuildTimeoutRegexp.MatchString(buildTimeoutString)
if !matched {
return fmt.Errorf("Cloud build timeout is not in duration format: %s", buildTimeoutString)
}

buildTimeout, err := strconv.Atoi(buildTimeoutString[0 : len(buildTimeoutString)-1])
if err != nil {
return fmt.Errorf("Error parsing build timeout : %s", err)
}

stepTimeoutSum := 0
steps := build["step"].([]interface{})
for _, rawstep := range steps {
if rawstep == nil {
continue
}
step := rawstep.(map[string]interface{})
timeoutString := step["timeout"].(string)
if len(timeoutString) == 0 {
continue
}

matched := cloudBuildTimeoutRegexp.MatchString(timeoutString)
if !matched {
return fmt.Errorf("Cloud build step timeout is not in duration format: %s", timeoutString)
}
timeout, err := strconv.Atoi(timeoutString[0 : len(timeoutString)-1])
if err != nil {
return fmt.Errorf("Error parsing build step timeout: %s", err)
}
stepTimeoutSum += timeout
}
if stepTimeoutSum > buildTimeout {
return fmt.Errorf("Step timeout sum (%v) cannot be greater than build timeout (%v)", stepTimeoutSum, buildTimeout)
}
return nil
}

func resourceCloudBuildTrigger() *schema.Resource {
return &schema.Resource{
Create: resourceCloudBuildTriggerCreate,
Expand All @@ -41,6 +93,7 @@ func resourceCloudBuildTrigger() *schema.Resource {
},

SchemaVersion: 1,
CustomizeDiff: stepTimeoutCustomizeDiff,

Schema: map[string]*schema.Schema{
"trigger_template": {
Expand Down Expand Up @@ -276,6 +329,14 @@ If any of the images fail to be pushed, the build status is marked FAILURE.`,
Type: schema.TypeString,
},
},
"timeout": {
Type: schema.TypeString,
Optional: true,
Description: `Amount of time that this build should be allowed to run, to second granularity.
If this amount of time elapses, work on the build will cease and the build status will be TIMEOUT.
Default time is ten minutes.`,
Default: "600s",
},
},
},
ExactlyOneOf: []string{"filename", "build"},
Expand Down Expand Up @@ -742,6 +803,8 @@ func flattenCloudBuildTriggerBuild(v interface{}, d *schema.ResourceData) interf
flattenCloudBuildTriggerBuildTags(original["tags"], d)
transformed["images"] =
flattenCloudBuildTriggerBuildImages(original["images"], d)
transformed["timeout"] =
flattenCloudBuildTriggerBuildTimeout(original["timeout"], d)
transformed["step"] =
flattenCloudBuildTriggerBuildStep(original["steps"], d)
return []interface{}{transformed}
Expand All @@ -754,6 +817,10 @@ func flattenCloudBuildTriggerBuildImages(v interface{}, d *schema.ResourceData)
return v
}

func flattenCloudBuildTriggerBuildTimeout(v interface{}, d *schema.ResourceData) interface{} {
return v
}

func flattenCloudBuildTriggerBuildStep(v interface{}, d *schema.ResourceData) interface{} {
if v == nil {
return v
Expand Down Expand Up @@ -985,6 +1052,13 @@ func expandCloudBuildTriggerBuild(v interface{}, d TerraformResourceData, config
transformed["images"] = transformedImages
}

transformedTimeout, err := expandCloudBuildTriggerBuildTimeout(original["timeout"], d, config)
if err != nil {
return nil, err
} else if val := reflect.ValueOf(transformedTimeout); val.IsValid() && !isEmptyValue(val) {
transformed["timeout"] = transformedTimeout
}

transformedStep, err := expandCloudBuildTriggerBuildStep(original["step"], d, config)
if err != nil {
return nil, err
Expand All @@ -1003,6 +1077,10 @@ func expandCloudBuildTriggerBuildImages(v interface{}, d TerraformResourceData,
return v, nil
}

func expandCloudBuildTriggerBuildTimeout(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}

func expandCloudBuildTriggerBuildStep(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
l := v.([]interface{})
req := make([]interface{}, 0, len(l))
Expand Down
103 changes: 103 additions & 0 deletions google/resource_cloudbuild_trigger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package google

import (
"fmt"
"regexp"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
Expand Down Expand Up @@ -37,6 +38,42 @@ func TestAccCloudBuildTrigger_basic(t *testing.T) {
})
}

func TestAccCloudBuildTrigger_customizeDiffTimeoutSum(t *testing.T) {
t.Parallel()

name := acctest.RandomWithPrefix("tf-test")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCloudBuildTriggerDestroy,
Steps: []resource.TestStep{
{
Config: testAccCloudBuildTrigger_customizeDiffTimeoutSum(name),
ExpectError: regexp.MustCompile("cannot be greater than build timeout"),
},
},
})
}

func TestAccCloudBuildTrigger_customizeDiffTimeoutFormat(t *testing.T) {
t.Parallel()

name := acctest.RandomWithPrefix("tf-test")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCloudBuildTriggerDestroy,
Steps: []resource.TestStep{
{
Config: testAccCloudBuildTrigger_customizeDiffTimeoutFormat(name),
ExpectError: regexp.MustCompile("Cloud build timeout is not in duration format"),
},
},
})
}

func TestAccCloudBuildTrigger_disable(t *testing.T) {
t.Parallel()
name := acctest.RandomWithPrefix("tf-test")
Expand Down Expand Up @@ -98,18 +135,22 @@ resource "google_cloudbuild_trigger" "build_trigger" {
build {
images = ["gcr.io/$PROJECT_ID/$REPO_NAME:$COMMIT_SHA"]
tags = ["team-a", "service-b"]
timeout = "1800s"
step {
name = "gcr.io/cloud-builders/gsutil"
args = ["cp", "gs://mybucket/remotefile.zip", "localfile.zip"]
timeout = "300s"
}
step {
name = "gcr.io/cloud-builders/go"
args = ["build", "my_package"]
env = ["env1=two"]
timeout = "300s"
}
step {
name = "gcr.io/cloud-builders/docker"
args = ["build", "-t", "gcr.io/$PROJECT_ID/$REPO_NAME:$COMMIT_SHA", "-f", "Dockerfile", "."]
timeout = "300s"
}
}
}
Expand Down Expand Up @@ -185,21 +226,83 @@ resource "google_cloudbuild_trigger" "build_trigger" {
build {
images = ["gcr.io/$PROJECT_ID/$REPO_NAME:$SHORT_SHA"]
tags = ["team-a", "service-b", "updated"]
timeout = "2100s"
step {
name = "gcr.io/cloud-builders/gsutil"
args = ["cp", "gs://mybucket/remotefile.zip", "localfile-updated.zip"]
timeout = "300s"
}
step {
name = "gcr.io/cloud-builders/go"
args = ["build", "my_package_updated"]
timeout = "300s"
}
step {
name = "gcr.io/cloud-builders/docker"
args = ["build", "-t", "gcr.io/$PROJECT_ID/$REPO_NAME:$SHORT_SHA", "-f", "Dockerfile", "."]
timeout = "300s"
}
step {
name = "gcr.io/$PROJECT_ID/$REPO_NAME:$SHORT_SHA"
args = ["test"]
timeout = "300s"
}
}
}
`, name)
}

func testAccCloudBuildTrigger_customizeDiffTimeoutSum(name string) string {
return fmt.Sprintf(`
resource "google_cloudbuild_trigger" "build_trigger" {
name = "%s"
description = "acceptance test build trigger"
trigger_template {
branch_name = "master"
repo_name = "some-repo"
}
build {
images = ["gcr.io/$PROJECT_ID/$REPO_NAME:$COMMIT_SHA"]
tags = ["team-a", "service-b"]
timeout = "900s"
step {
name = "gcr.io/cloud-builders/gsutil"
args = ["cp", "gs://mybucket/remotefile.zip", "localfile.zip"]
timeout = "500s"
}
step {
name = "gcr.io/cloud-builders/go"
args = ["build", "my_package"]
env = ["env1=two"]
timeout = "500s"
}
step {
name = "gcr.io/cloud-builders/docker"
args = ["build", "-t", "gcr.io/$PROJECT_ID/$REPO_NAME:$COMMIT_SHA", "-f", "Dockerfile", "."]
timeout = "500s"
}
}
}
`, name)
}

func testAccCloudBuildTrigger_customizeDiffTimeoutFormat(name string) string {
return fmt.Sprintf(`
resource "google_cloudbuild_trigger" "build_trigger" {
name = "%s"
description = "acceptance test build trigger"
trigger_template {
branch_name = "master"
repo_name = "some-repo"
}
build {
images = ["gcr.io/$PROJECT_ID/$REPO_NAME:$COMMIT_SHA"]
tags = ["team-a", "service-b"]
timeout = "1200"
step {
name = "gcr.io/cloud-builders/gsutil"
args = ["cp", "gs://mybucket/remotefile.zip", "localfile.zip"]
timeout = "500s"
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions website/docs/r/cloudbuild_trigger.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,12 @@ The `build` block supports:
The digests of the pushed images will be stored in the Build resource's results field.
If any of the images fail to be pushed, the build status is marked FAILURE.

* `timeout` -
(Optional)
Amount of time that this build should be allowed to run, to second granularity.
If this amount of time elapses, work on the build will cease and the build status will be TIMEOUT.
Default time is ten minutes.

* `step` -
(Required)
The operations to be performed on the workspace. Structure is documented below.
Expand Down

0 comments on commit 27e1d13

Please sign in to comment.