Skip to content

Commit 439926b

Browse files
Add a diffSupressFunc() for template_gcs_path. (#13207) (#9564)
[upstream:c7fe3e3d0dae74979e5f9a10e620e89a53b9ba4e] Signed-off-by: Modular Magician <[email protected]>
1 parent 4a776cc commit 439926b

File tree

3 files changed

+82
-5
lines changed

3 files changed

+82
-5
lines changed

.changelog/13207.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
dataflow: fixed a permadiff on `template_gcs_path` in `google_dataflow_job` resource
3+
```

google-beta/services/dataflow/resource_dataflow_job.go

+41-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"context"
77
"fmt"
88
"log"
9+
"regexp"
910
"strings"
1011
"time"
1112

@@ -53,6 +54,34 @@ func resourceDataflowJobLabelDiffSuppress(k, old, new string, d *schema.Resource
5354
return false
5455
}
5556

57+
// ResourceDataflowJobTemplateGcsPathDiffSuppress suppresses diffs on the template_gcs_path field
58+
// when the only diff is the region name in the bucket.
59+
// `old` is the value set by service and `new` is what is stored in the config. When using a public
60+
// template bucket, it will be automatically turned to use a regional bucket path. In this case,
61+
// we want to supress the diff.
62+
// Example:
63+
// - old: gs://template-bucket-us-central1/path/to/file.txt
64+
// - new: gs://template-bucket/path/to/file.txt
65+
func ResourceDataflowJobTemplateGcsPathDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
66+
if old == new {
67+
return true
68+
}
69+
70+
return isRegionSuffixedPathMatch(old, new)
71+
}
72+
73+
func isRegionSuffixedPathMatch(old, new string) bool {
74+
re := regexp.MustCompile(`gs://([a-z0-9\-]+)-[a-z0-9]+-[a-z0-9]+(/.*)?`)
75+
matches := re.FindStringSubmatch(old)
76+
77+
if len(matches) == 3 && matches[2] != "" {
78+
modifiedOld := "gs://" + matches[1] + matches[2]
79+
return modifiedOld == new
80+
}
81+
82+
return false
83+
}
84+
5685
func ResourceDataflowJob() *schema.Resource {
5786
return &schema.Resource{
5887
Create: resourceDataflowJobCreate,
@@ -87,9 +116,10 @@ func ResourceDataflowJob() *schema.Resource {
87116
},
88117

89118
"template_gcs_path": {
90-
Type: schema.TypeString,
91-
Required: true,
92-
Description: `The Google Cloud Storage path to the Dataflow job template.`,
119+
Type: schema.TypeString,
120+
Required: true,
121+
DiffSuppressFunc: ResourceDataflowJobTemplateGcsPathDiffSuppress,
122+
Description: `The Google Cloud Storage path to the Dataflow job template.`,
93123
},
94124

95125
"temp_gcs_location": {
@@ -264,13 +294,20 @@ func resourceDataflowJobTypeCustomizeDiff(_ context.Context, d *schema.ResourceD
264294
}
265295

266296
if field != "terraform_labels" && d.HasChange(field) {
297+
if field == "template_gcs_path" {
298+
old, new := d.GetChange("template_gcs_path")
299+
300+
if isRegionSuffixedPathMatch(old.(string), new.(string)) {
301+
continue
302+
}
303+
}
304+
267305
if err := d.ForceNew(field); err != nil {
268306
return err
269307
}
270308
}
271309
}
272310
}
273-
274311
return nil
275312
}
276313

google-beta/services/dataflow/resource_dataflow_job_test.go

+38-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"testing"
1212
"time"
1313

14+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1415
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
1516
"github.com/hashicorp/terraform-plugin-testing/terraform"
1617
"github.com/hashicorp/terraform-provider-google-beta/google-beta/services/dataflow"
@@ -23,7 +24,7 @@ import (
2324
)
2425

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

0 commit comments

Comments
 (0)