From 57403cde86b6d9afcbe692d7402de248307dc9e7 Mon Sep 17 00:00:00 2001 From: Nathan McKinley Date: Tue, 24 Jul 2018 14:49:40 -0700 Subject: [PATCH 1/3] Add Substitutions to cloud build build triggers. --- google/resource_cloudbuild_build_trigger.go | 24 ++++++++++++++++++- .../resource_cloudbuild_build_trigger_test.go | 10 ++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/google/resource_cloudbuild_build_trigger.go b/google/resource_cloudbuild_build_trigger.go index 278e8039cc0..8009a687e0c 100644 --- a/google/resource_cloudbuild_build_trigger.go +++ b/google/resource_cloudbuild_build_trigger.go @@ -19,7 +19,7 @@ func resourceCloudBuildTrigger() *schema.Resource { Read: resourceCloudbuildBuildTriggerRead, Delete: resourceCloudbuildBuildTriggerDelete, Importer: &schema.ResourceImporter{ - State: schema.ImportStatePassthrough, + State: resourceCloudBuildTriggerImportState, }, Timeouts: &schema.ResourceTimeout{ @@ -89,6 +89,12 @@ func resourceCloudBuildTrigger() *schema.Resource { Optional: true, ForceNew: true, }, + "substitutions": &schema.Schema{ + Optional: true, + Type: schema.TypeMap, + ForceNew: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, "trigger_template": &schema.Schema{ Optional: true, Type: schema.TypeList, @@ -155,6 +161,7 @@ func resourceCloudbuildBuildTriggerCreate(d *schema.ResourceData, meta interface } buildTrigger.TriggerTemplate = expandCloudbuildBuildTriggerTemplate(d, project) + buildTrigger.Substitutions = expandStringMap(d, "substitutions") tstr, err := json.Marshal(buildTrigger) if err != nil { @@ -186,6 +193,7 @@ func resourceCloudbuildBuildTriggerRead(d *schema.ResourceData, meta interface{} } d.Set("description", buildTrigger.Description) + d.Set("substitutions", buildTrigger.Substitutions) if buildTrigger.TriggerTemplate != nil { d.Set("trigger_template", flattenCloudbuildBuildTriggerTemplate(d, config, buildTrigger.TriggerTemplate)) @@ -312,3 +320,17 @@ func resourceCloudbuildBuildTriggerDelete(d *schema.ResourceData, meta interface d.SetId("") return nil } + +func resourceCloudBuildTriggerImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + parts := strings.Split(d.Id(), "/") + + if len(parts) == 1 { + return []*schema.ResourceData{d}, nil + } else if len(parts) == 2 { + d.Set("project", parts[0]) + d.SetId(parts[1]) + return []*schema.ResourceData{d}, nil + } else { + return nil, fmt.Errorf("Invalid import id %q. Expecting {trigger_name} or {project}/{trigger_name}", d.Id()) + } +} diff --git a/google/resource_cloudbuild_build_trigger_test.go b/google/resource_cloudbuild_build_trigger_test.go index c9d9bdf0e25..68f37f2d440 100644 --- a/google/resource_cloudbuild_build_trigger_test.go +++ b/google/resource_cloudbuild_build_trigger_test.go @@ -28,6 +28,12 @@ func TestAccCloudBuildTrigger_basic(t *testing.T) { testAccCheckGoogleCloudBuildTriggerExists("google_cloudbuild_trigger.build_trigger"), ), }, + resource.TestStep{ + ResourceName: "google_cloudbuild_trigger.build_trigger", + ImportState: true, + ImportStateVerify: true, + ImportStateIdPrefix: fmt.Sprintf("%s/", projectID), + }, resource.TestStep{ Config: testGoogleCloudBuildTrigger_removed(projectID, projectOrg, projectBillingAccount), Check: resource.ComposeTestCheckFunc( @@ -233,6 +239,10 @@ resource "google_cloudbuild_trigger" "filename_build_trigger" { project = "${google_project_services.acceptance.project}" repo_name = "some-repo" } + substitutions { + foo = "bar" + baz = "qux" + } filename = "cloudbuild.yaml" } `, projectID, projectID, projectOrg, projectBillingAccount) From 2540637b20b8220f4fb6d59bd820f33b9e1fd95f Mon Sep 17 00:00:00 2001 From: Nathan McKinley Date: Tue, 24 Jul 2018 14:57:15 -0700 Subject: [PATCH 2/3] Add docs for substitutions. --- website/docs/r/cloudbuild_trigger.html.markdown | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/website/docs/r/cloudbuild_trigger.html.markdown b/website/docs/r/cloudbuild_trigger.html.markdown index 05d3ab4e19f..3fc7a9e26f8 100644 --- a/website/docs/r/cloudbuild_trigger.html.markdown +++ b/website/docs/r/cloudbuild_trigger.html.markdown @@ -78,6 +78,22 @@ will be expanded when the build is created: in the Git repo. This is mutually exclusive with `build`. This is typically `cloudbuild.yaml` however it can be specified by the user. +* `substitutions`: (Optional) User-defined substitutions. +User-defined substitutions must conform to the following rules: + * Substitutions must begin with an underscore (`_`) and use only + uppercase-letters and numbers (respecting the regular expression + `_[A-Z0-9_]+`). This prevents conflicts with built-in substitutions. + * Unmatched keys in the template will cause an error (for example, if a build + request includes `$_FOO` and the substitutions map doesn’t define `_FOO`). + * Unmatched keys in the parameters list will result in an error (for example, + if a substitutions map defines `_FOO` but the build request doesn't include `$_FOO`). + * To include a literal `$_VARIABLE` in the template, you must escape with `$$`. + * You can explicitly denote variable expansion using the `${_VAR}` syntax. This prevents + ambiguity in cases like `${_FOO}BAR`, where `$_FOO` is a variable. + * The number of parameters is limited to 100 parameters. + * The length of a parameter key and the length of a parameter value + are limited to 100 characters. + --- The `trigger_template` block supports: From 99f07f22445156e1fceba1e4520c00922fec1d68 Mon Sep 17 00:00:00 2001 From: Nathan McKinley Date: Wed, 25 Jul 2018 13:20:35 -0700 Subject: [PATCH 3/3] Update substitutions to be correct. --- google/resource_cloudbuild_build_trigger_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/google/resource_cloudbuild_build_trigger_test.go b/google/resource_cloudbuild_build_trigger_test.go index 68f37f2d440..bc36c2a4c24 100644 --- a/google/resource_cloudbuild_build_trigger_test.go +++ b/google/resource_cloudbuild_build_trigger_test.go @@ -239,10 +239,10 @@ resource "google_cloudbuild_trigger" "filename_build_trigger" { project = "${google_project_services.acceptance.project}" repo_name = "some-repo" } - substitutions { - foo = "bar" - baz = "qux" - } + substitutions { + _FOO = "bar" + _BAZ = "qux" + } filename = "cloudbuild.yaml" } `, projectID, projectID, projectOrg, projectBillingAccount)