Skip to content

Commit 152d3a0

Browse files
harrisonlimhshuyama1
authored andcommitted
Add a diffSupressFunc() for template_gcs_path. (GoogleCloudPlatform#13207)
Co-authored-by: Shuya Ma <[email protected]>
1 parent 26dc7d1 commit 152d3a0

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

mmv1/third_party/terraform/services/dataflow/resource_dataflow_job.go.tmpl

+39-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"fmt"
66
"log"
7+
"regexp"
78
"strings"
89
"time"
910

@@ -51,6 +52,35 @@ func resourceDataflowJobLabelDiffSuppress(k, old, new string, d *schema.Resource
5152
return false
5253
}
5354

55+
// ResourceDataflowJobTemplateGcsPathDiffSuppress suppresses diffs on the template_gcs_path field
56+
// when the only diff is the region name in the bucket.
57+
// `old` is the value set by service and `new` is what is stored in the config. When using a public
58+
// template bucket, it will be automatically turned to use a regional bucket path. In this case,
59+
// we want to supress the diff.
60+
// Example:
61+
// - old: gs://template-bucket-us-central1/path/to/file.txt
62+
// - new: gs://template-bucket/path/to/file.txt
63+
func ResourceDataflowJobTemplateGcsPathDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
64+
if old == new {
65+
return true
66+
}
67+
68+
return isRegionSuffixedPathMatch(old, new)
69+
}
70+
71+
72+
func isRegionSuffixedPathMatch(old, new string) bool {
73+
re := regexp.MustCompile(`gs://([a-z0-9\-]+)-[a-z0-9]+-[a-z0-9]+(/.*)?`)
74+
matches := re.FindStringSubmatch(old)
75+
76+
if len(matches) == 3 && matches[2] != "" {
77+
modifiedOld := "gs://" + matches[1] + matches[2]
78+
return modifiedOld == new
79+
}
80+
81+
return false
82+
}
83+
5484
func ResourceDataflowJob() *schema.Resource {
5585
return &schema.Resource{
5686
Create: resourceDataflowJobCreate,
@@ -87,6 +117,7 @@ func ResourceDataflowJob() *schema.Resource {
87117
"template_gcs_path": {
88118
Type: schema.TypeString,
89119
Required: true,
120+
DiffSuppressFunc: ResourceDataflowJobTemplateGcsPathDiffSuppress,
90121
Description: `The Google Cloud Storage path to the Dataflow job template.`,
91122
},
92123

@@ -262,13 +293,20 @@ func resourceDataflowJobTypeCustomizeDiff(_ context.Context, d *schema.ResourceD
262293
}
263294

264295
if field != "terraform_labels" && d.HasChange(field) {
296+
if field == "template_gcs_path" {
297+
old, new := d.GetChange("template_gcs_path")
298+
299+
if isRegionSuffixedPathMatch(old.(string), new.(string)) {
300+
continue
301+
}
302+
}
303+
265304
if err := d.ForceNew(field); err != nil {
266305
return err
267306
}
268307
}
269308
}
270309
}
271-
272310
return nil
273311
}
274312

mmv1/third_party/terraform/services/dataflow/resource_dataflow_job_test.go.tmpl

+38-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"testing"
1010
"time"
1111

12+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1213
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
1314
"github.com/hashicorp/terraform-plugin-testing/terraform"
1415
"github.com/hashicorp/terraform-provider-google/google/services/dataflow"
@@ -25,7 +26,7 @@ import (
2526
)
2627

2728
const (
28-
testDataflowJobTemplateWordCountUrl = "gs://dataflow-templates-us-central1/latest/Word_Count"
29+
testDataflowJobTemplateWordCountUrl = "gs://dataflow-templates/latest/Word_Count"
2930
testDataflowJobSampleFileUrl = "gs://dataflow-samples/shakespeare/various.txt"
3031
testDataflowJobTemplateTextToPubsub = "gs://dataflow-templates-us-central1/latest/Stream_GCS_Text_to_Cloud_PubSub"
3132
testDataflowJobRegion = "us-central1"
@@ -1371,3 +1372,39 @@ resource "google_dataflow_job" "pubsub_stream" {
13711372
}
13721373
`, suffix, suffix, suffix, testDataflowJobTemplateTextToPubsub, onDelete)
13731374
}
1375+
1376+
func TestResourceDataflowJobTemplateGcsPathDiffSuppress(t *testing.T) {
1377+
cases := map[string]struct {
1378+
Old, New string
1379+
ExpectDiffSuppress bool
1380+
}{
1381+
"same bucket": {
1382+
Old: "gs://template-bucket/path/to/file",
1383+
New: "gs://template-bucket/path/to/file",
1384+
ExpectDiffSuppress: true,
1385+
},
1386+
"different bucket": {
1387+
Old: "gs://template-bucket-foo/path/to/file1",
1388+
New: "gs://template-bucket/path/to/file1",
1389+
ExpectDiffSuppress: false,
1390+
},
1391+
"different object": {
1392+
Old: "gs://template-bucket-foo/path/to/file2",
1393+
New: "gs://template-bucket/path/to/file",
1394+
ExpectDiffSuppress: false,
1395+
},
1396+
"regional bucket name change is okay": {
1397+
Old: "gs://template-bucket-us-central1/path/to/file1",
1398+
New: "gs://template-bucket/path/to/file1",
1399+
ExpectDiffSuppress: true,
1400+
},
1401+
}
1402+
rd := schema.TestResourceDataRaw(t, dataflow.ResourceDataflowJob().Schema, nil)
1403+
rd.Set("region", "us-central1")
1404+
1405+
for tn, tc := range cases {
1406+
if dataflow.ResourceDataflowJobTemplateGcsPathDiffSuppress("template_gcs_path", tc.Old, tc.New, rd) != tc.ExpectDiffSuppress {
1407+
t.Errorf("bad: %s, '%s' => '%s' expect DiffSuppress to return %t", tn, tc.Old, tc.New, tc.ExpectDiffSuppress)
1408+
}
1409+
}
1410+
}

0 commit comments

Comments
 (0)