Skip to content

Add a diffSupressFunc() for template_gcs_path. #13207

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
317edc8
Add a diffSupressFunc() for template_gcs_path.
harrisonlimh Feb 26, 2025
a2c98b3
Update resource_dataflow_job.go.tmpl
harrisonlimh Feb 27, 2025
8ea2447
Update resource_dataflow_job.go.tmpl
harrisonlimh Feb 27, 2025
1ecb12d
Add tests
harrisonlimh Feb 27, 2025
cc0a1f4
Update resource_dataflow_job_test.go.tmpl
harrisonlimh Feb 28, 2025
50506bb
Update resource_dataflow_job_test.go.tmpl
harrisonlimh Feb 28, 2025
3344612
Update resource_dataflow_job_test.go.tmpl
harrisonlimh Feb 28, 2025
202b1c5
Update resource_dataflow_job_test.go.tmpl
harrisonlimh Feb 28, 2025
f340dee
Update resource_dataflow_job.go.tmpl
harrisonlimh Feb 28, 2025
bcc00e5
Update resource_dataflow_job_test.go.tmpl
harrisonlimh Mar 5, 2025
2bec9cc
Update resource_dataflow_job.go.tmpl
harrisonlimh Mar 13, 2025
6bb4c47
Update resource_dataflow_job.go.tmpl
harrisonlimh Mar 14, 2025
c138dcb
Update resource_dataflow_job.go.tmpl
harrisonlimh Mar 14, 2025
1a5d25f
Update resource_dataflow_job.go.tmpl
harrisonlimh Mar 14, 2025
5c03374
Update resource_dataflow_job.go.tmpl
harrisonlimh Mar 14, 2025
b561e15
Update resource_dataflow_job.go.tmpl
harrisonlimh Mar 14, 2025
d571478
Update resource_dataflow_job.go.tmpl
harrisonlimh Mar 14, 2025
1cef567
Update resource_dataflow_job.go.tmpl
harrisonlimh Mar 14, 2025
c21473f
Update resource_dataflow_job.go.tmpl
harrisonlimh Mar 14, 2025
b7fd375
Update mmv1/third_party/terraform/services/dataflow/resource_dataflow…
harrisonlimh Mar 14, 2025
088f432
Update mmv1/third_party/terraform/services/dataflow/resource_dataflow…
harrisonlimh Mar 14, 2025
e6815ad
Update mmv1/third_party/terraform/services/dataflow/resource_dataflow…
shuyama1 Mar 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
"regexp"
"strings"
"time"

Expand Down Expand Up @@ -51,6 +52,35 @@ func resourceDataflowJobLabelDiffSuppress(k, old, new string, d *schema.Resource
return false
}

// ResourceDataflowJobTemplateGcsPathDiffSuppress suppresses diffs on the template_gcs_path field
// when the only diff is the region name in the bucket.
// `old` is the value set by service and `new` is what is stored in the config. When using a public
// template bucket, it will be automatically turned to use a regional bucket path. In this case,
// we want to supress the diff.
// Example:
// - old: gs://template-bucket-us-central1/path/to/file.txt
// - new: gs://template-bucket/path/to/file.txt
func ResourceDataflowJobTemplateGcsPathDiffSuppress(k, old, new string, d *schema.ResourceData) bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resourceDataflowJobTypeCustomizeDiff forces the resource recreation (makes every field forcenew) if any non-virtual fields (except terraform_labels field) changed for batch jobs, we need to extend it with this diff suppress logic.

A working example is available in #13351, but the code can be optimized. Hope this helps

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

if old == new {
return true
}

return isRegionSuffixedPathMatch(old, new)
}


func isRegionSuffixedPathMatch(old, new string) bool {
re := regexp.MustCompile(`gs://([a-z0-9\-]+)-[a-z0-9]+-[a-z0-9]+(/.*)?`)
matches := re.FindStringSubmatch(old)

if len(matches) == 3 && matches[2] != "" {
modifiedOld := "gs://" + matches[1] + matches[2]
return modifiedOld == new
}

return false
}

func ResourceDataflowJob() *schema.Resource {
return &schema.Resource{
Create: resourceDataflowJobCreate,
Expand Down Expand Up @@ -87,6 +117,7 @@ func ResourceDataflowJob() *schema.Resource {
"template_gcs_path": {
Type: schema.TypeString,
Required: true,
DiffSuppressFunc: ResourceDataflowJobTemplateGcsPathDiffSuppress,
Description: `The Google Cloud Storage path to the Dataflow job template.`,
},

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

if field != "terraform_labels" && d.HasChange(field) {
if field == "template_gcs_path" {
old, new := d.GetChange("template_gcs_path")

if isRegionSuffixedPathMatch(old.(string), new.(string)) {
continue
}
}

if err := d.ForceNew(field); err != nil {
return err
}
}
}
}

return nil
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
"github.com/hashicorp/terraform-plugin-testing/terraform"
"github.com/hashicorp/terraform-provider-google/google/services/dataflow"
Expand All @@ -25,7 +26,7 @@ import (
)

const (
testDataflowJobTemplateWordCountUrl = "gs://dataflow-templates-us-central1/latest/Word_Count"
testDataflowJobTemplateWordCountUrl = "gs://dataflow-templates/latest/Word_Count"
testDataflowJobSampleFileUrl = "gs://dataflow-samples/shakespeare/various.txt"
testDataflowJobTemplateTextToPubsub = "gs://dataflow-templates-us-central1/latest/Stream_GCS_Text_to_Cloud_PubSub"
testDataflowJobRegion = "us-central1"
Expand Down Expand Up @@ -1371,3 +1372,39 @@ resource "google_dataflow_job" "pubsub_stream" {
}
`, suffix, suffix, suffix, testDataflowJobTemplateTextToPubsub, onDelete)
}

func TestResourceDataflowJobTemplateGcsPathDiffSuppress(t *testing.T) {
cases := map[string]struct {
Old, New string
ExpectDiffSuppress bool
}{
"same bucket": {
Old: "gs://template-bucket/path/to/file",
New: "gs://template-bucket/path/to/file",
ExpectDiffSuppress: true,
},
"different bucket": {
Old: "gs://template-bucket-foo/path/to/file1",
New: "gs://template-bucket/path/to/file1",
ExpectDiffSuppress: false,
},
"different object": {
Old: "gs://template-bucket-foo/path/to/file2",
New: "gs://template-bucket/path/to/file",
ExpectDiffSuppress: false,
},
"regional bucket name change is okay": {
Old: "gs://template-bucket-us-central1/path/to/file1",
New: "gs://template-bucket/path/to/file1",
ExpectDiffSuppress: true,
},
}
rd := schema.TestResourceDataRaw(t, dataflow.ResourceDataflowJob().Schema, nil)
rd.Set("region", "us-central1")

for tn, tc := range cases {
if dataflow.ResourceDataflowJobTemplateGcsPathDiffSuppress("template_gcs_path", tc.Old, tc.New, rd) != tc.ExpectDiffSuppress {
t.Errorf("bad: %s, '%s' => '%s' expect DiffSuppress to return %t", tn, tc.Old, tc.New, tc.ExpectDiffSuppress)
}
}
}
Loading