Skip to content

Commit 02eb26b

Browse files
nownaberileykarson
authored andcommitted
Update cloudbuild trigger in place (#2121)
Hi! Now any changes of cloud build trigger create new one, this PR makes triggers modifiable in place. I referred to [bigquery dataset](https://github.com/terraform-providers/terraform-provider-google/blob/9f0b792f091ce1a27f718d23184a481d6b20ec05/google/resource_bigquery_dataset.go#L353). If theare are better examples, I'd like to know them.
1 parent 8a62911 commit 02eb26b

File tree

2 files changed

+116
-30
lines changed

2 files changed

+116
-30
lines changed

google/resource_cloudbuild_build_trigger.go

+54-30
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ func resourceCloudBuildTrigger() *schema.Resource {
1717
return &schema.Resource{
1818
Create: resourceCloudbuildBuildTriggerCreate,
1919
Read: resourceCloudbuildBuildTriggerRead,
20+
Update: resourceCloudbuildBuildTriggerUpdate,
2021
Delete: resourceCloudbuildBuildTriggerDelete,
2122
Importer: &schema.ResourceImporter{
2223
State: resourceCloudBuildTriggerImportState,
@@ -39,46 +40,39 @@ func resourceCloudBuildTrigger() *schema.Resource {
3940
"filename": &schema.Schema{
4041
Type: schema.TypeString,
4142
Optional: true,
42-
ForceNew: true,
4343
ConflictsWith: []string{"build"},
4444
},
4545
"build": {
4646
Type: schema.TypeList,
4747
Description: "Contents of the build template.",
4848
Optional: true,
49-
ForceNew: true,
5049
MaxItems: 1,
5150
Elem: &schema.Resource{
5251
Schema: map[string]*schema.Schema{
5352
"images": &schema.Schema{
5453
Type: schema.TypeList,
5554
Optional: true,
56-
ForceNew: true,
5755
Elem: &schema.Schema{Type: schema.TypeString},
5856
},
5957
"step": &schema.Schema{
6058
Type: schema.TypeList,
6159
Optional: true,
62-
ForceNew: true,
6360
Elem: &schema.Resource{
6461
Schema: map[string]*schema.Schema{
6562
"name": &schema.Schema{
6663
Type: schema.TypeString,
6764
Optional: true,
68-
ForceNew: true,
6965
},
7066
"args": &schema.Schema{
7167
Type: schema.TypeString,
7268
Optional: true,
73-
ForceNew: true,
7469
},
7570
},
7671
},
7772
},
7873
"tags": &schema.Schema{
7974
Type: schema.TypeList,
8075
Optional: true,
81-
ForceNew: true,
8276
Elem: &schema.Schema{Type: schema.TypeString},
8377
},
8478
},
@@ -87,50 +81,41 @@ func resourceCloudBuildTrigger() *schema.Resource {
8781
"description": &schema.Schema{
8882
Type: schema.TypeString,
8983
Optional: true,
90-
ForceNew: true,
9184
},
9285
"substitutions": &schema.Schema{
9386
Optional: true,
9487
Type: schema.TypeMap,
95-
ForceNew: true,
9688
Elem: &schema.Schema{Type: schema.TypeString},
9789
},
9890
"trigger_template": &schema.Schema{
9991
Optional: true,
10092
Type: schema.TypeList,
10193
MaxItems: 1,
102-
ForceNew: true,
10394
Elem: &schema.Resource{
10495
Schema: map[string]*schema.Schema{
10596
"branch_name": &schema.Schema{
10697
Type: schema.TypeString,
10798
Optional: true,
108-
ForceNew: true,
10999
},
110100
"commit_sha": &schema.Schema{
111101
Type: schema.TypeString,
112102
Optional: true,
113-
ForceNew: true,
114103
},
115104
"dir": &schema.Schema{
116105
Type: schema.TypeString,
117106
Optional: true,
118-
ForceNew: true,
119107
},
120108
"project": &schema.Schema{
121109
Type: schema.TypeString,
122110
Optional: true,
123-
ForceNew: true,
124111
},
125112
"repo_name": &schema.Schema{
126113
Type: schema.TypeString,
127114
Optional: true,
128-
ForceNew: true,
129115
},
130116
"tag_name": &schema.Schema{
131117
Type: schema.TypeString,
132118
Optional: true,
133-
ForceNew: true,
134119
},
135120
},
136121
},
@@ -147,22 +132,11 @@ func resourceCloudbuildBuildTriggerCreate(d *schema.ResourceData, meta interface
147132
return err
148133
}
149134

150-
// Build the address parameter
151-
buildTrigger := &cloudbuild.BuildTrigger{}
152-
153-
if v, ok := d.GetOk("description"); ok {
154-
buildTrigger.Description = v.(string)
155-
}
156-
157-
if v, ok := d.GetOk("filename"); ok {
158-
buildTrigger.Filename = v.(string)
159-
} else {
160-
buildTrigger.Build = expandCloudbuildBuildTriggerBuild(d)
135+
buildTrigger, err := expandCloudbuildBuildTrigger(d, meta)
136+
if err != nil {
137+
return err
161138
}
162139

163-
buildTrigger.TriggerTemplate = expandCloudbuildBuildTriggerTemplate(d, project)
164-
buildTrigger.Substitutions = expandStringMap(d, "substitutions")
165-
166140
tstr, err := json.Marshal(buildTrigger)
167141
if err != nil {
168142
return err
@@ -208,6 +182,56 @@ func resourceCloudbuildBuildTriggerRead(d *schema.ResourceData, meta interface{}
208182
return nil
209183
}
210184

185+
func resourceCloudbuildBuildTriggerUpdate(d *schema.ResourceData, meta interface{}) error {
186+
config := meta.(*Config)
187+
188+
project, err := getProject(d, config)
189+
if err != nil {
190+
return err
191+
}
192+
193+
buildTrigger, err := expandCloudbuildBuildTrigger(d, meta)
194+
if err != nil {
195+
return err
196+
}
197+
198+
id := d.Id()
199+
200+
log.Printf("[INFO] Updating Cloud Build Trigger: %s", id)
201+
202+
if _, err = config.clientBuild.Projects.Triggers.Patch(project, id, buildTrigger).Do(); err != nil {
203+
return err
204+
}
205+
206+
return resourceCloudbuildBuildTriggerRead(d, meta)
207+
}
208+
209+
func expandCloudbuildBuildTrigger(d *schema.ResourceData, meta interface{}) (*cloudbuild.BuildTrigger, error) {
210+
config := meta.(*Config)
211+
212+
project, err := getProject(d, config)
213+
if err != nil {
214+
return nil, err
215+
}
216+
217+
t := &cloudbuild.BuildTrigger{}
218+
219+
if v, ok := d.GetOk("description"); ok {
220+
t.Description = v.(string)
221+
}
222+
223+
if v, ok := d.GetOk("filename"); ok {
224+
t.Filename = v.(string)
225+
} else {
226+
t.Build = expandCloudbuildBuildTriggerBuild(d)
227+
}
228+
229+
t.Substitutions = expandStringMap(d, "substitutions")
230+
t.TriggerTemplate = expandCloudbuildBuildTriggerTemplate(d, project)
231+
232+
return t, nil
233+
}
234+
211235
func expandCloudbuildBuildTriggerTemplate(d *schema.ResourceData, project string) *cloudbuild.RepoSource {
212236
if d.Get("trigger_template.#").(int) == 0 {
213237
return nil

google/resource_cloudbuild_build_trigger_test.go

+62
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ func TestAccCloudBuildTrigger_basic(t *testing.T) {
3434
ImportStateVerify: true,
3535
ImportStateIdPrefix: fmt.Sprintf("%s/", projectID),
3636
},
37+
resource.TestStep{
38+
Config: testGoogleCloudBuildTrigger_updated(projectID, projectOrg, projectBillingAccount),
39+
},
40+
resource.TestStep{
41+
ResourceName: "google_cloudbuild_trigger.build_trigger",
42+
ImportState: true,
43+
ImportStateVerify: true,
44+
ImportStateIdPrefix: fmt.Sprintf("%s/", projectID),
45+
},
3746
resource.TestStep{
3847
Config: testGoogleCloudBuildTrigger_removed(projectID, projectOrg, projectBillingAccount),
3948
Check: resource.ComposeTestCheckFunc(
@@ -210,6 +219,59 @@ resource "google_cloudbuild_trigger" "build_trigger" {
210219
`, projectID, projectID, projectOrg, projectBillingAccount)
211220
}
212221

222+
func testGoogleCloudBuildTrigger_updated(projectID, projectOrg, projectBillingAccount string) string {
223+
return fmt.Sprintf(`
224+
resource "google_project" "acceptance" {
225+
name = "%s"
226+
project_id = "%s"
227+
org_id = "%s"
228+
billing_account = "%s"
229+
}
230+
231+
resource "google_project_services" "acceptance" {
232+
project = "${google_project.acceptance.project_id}"
233+
234+
services = [
235+
"cloudbuild.googleapis.com",
236+
"containerregistry.googleapis.com",
237+
"logging.googleapis.com",
238+
"pubsub.googleapis.com",
239+
"storage-api.googleapis.com",
240+
]
241+
}
242+
243+
resource "google_cloudbuild_trigger" "build_trigger" {
244+
project = "${google_project_services.acceptance.project}"
245+
description = "acceptance test build trigger updated"
246+
trigger_template {
247+
branch_name = "master-updated"
248+
project = "${google_project_services.acceptance.project}"
249+
repo_name = "some-repo-updated"
250+
}
251+
build {
252+
images = ["gcr.io/$PROJECT_ID/$REPO_NAME:$SHORT_SHA"]
253+
tags = ["team-a", "service-b", "updated"]
254+
step {
255+
name = "gcr.io/cloud-builders/gsutil"
256+
args = "cp gs://mybucket/remotefile.zip localfile-updated.zip "
257+
}
258+
step {
259+
name = "gcr.io/cloud-builders/go"
260+
args = "build my_package_updated"
261+
}
262+
step {
263+
name = "gcr.io/cloud-builders/docker"
264+
args = "build -t gcr.io/$PROJECT_ID/$REPO_NAME:$SHORT_SHA -f Dockerfile ."
265+
}
266+
step {
267+
name = "gcr.io/$PROJECT_ID/$REPO_NAME:$SHORT_SHA"
268+
args = "test"
269+
}
270+
}
271+
}
272+
`, projectID, projectID, projectOrg, projectBillingAccount)
273+
}
274+
213275
func testGoogleCloudBuildTrigger_filename(projectID, projectOrg, projectBillingAccount string) string {
214276
return fmt.Sprintf(`
215277
resource "google_project" "acceptance" {

0 commit comments

Comments
 (0)