Skip to content

Commit c35a20b

Browse files
modular-magicianemilymye
authored andcommitted
Add diff suppress to dataproc image version (#4088)
Signed-off-by: Modular Magician <[email protected]>
1 parent d790acb commit c35a20b

File tree

2 files changed

+137
-7
lines changed

2 files changed

+137
-7
lines changed

google/resource_dataproc_cluster.go

+55-4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
dataproc "google.golang.org/api/dataproc/v1beta2"
1616
)
1717

18+
var resolveDataprocImageVersion = regexp.MustCompile(`(?P<Major>[^\s.-]+)\.(?P<Minor>[^\s.-]+)(?:\.(?P<Subminor>[^\s.-]+))?(?:\-(?P<Distr>[^\s.-]+))?`)
19+
1820
func resourceDataprocCluster() *schema.Resource {
1921
return &schema.Resource{
2022
Create: resourceDataprocClusterCreate,
@@ -258,10 +260,11 @@ func resourceDataprocCluster() *schema.Resource {
258260
Elem: &schema.Resource{
259261
Schema: map[string]*schema.Schema{
260262
"image_version": {
261-
Type: schema.TypeString,
262-
Optional: true,
263-
Computed: true,
264-
ForceNew: true,
263+
Type: schema.TypeString,
264+
Optional: true,
265+
Computed: true,
266+
ForceNew: true,
267+
DiffSuppressFunc: dataprocImageVersionDiffSuppress,
265268
},
266269

267270
"override_properties": {
@@ -1005,3 +1008,51 @@ func configOptions(d *schema.ResourceData, option string) (map[string]interface{
10051008
}
10061009
return nil, false
10071010
}
1011+
1012+
func dataprocImageVersionDiffSuppress(_, old, new string, _ *schema.ResourceData) bool {
1013+
oldV, err := parseDataprocImageVersion(old)
1014+
if err != nil {
1015+
return false
1016+
}
1017+
newV, err := parseDataprocImageVersion(new)
1018+
if err != nil {
1019+
return false
1020+
}
1021+
1022+
if newV.major != oldV.major {
1023+
return false
1024+
}
1025+
if newV.minor != oldV.minor {
1026+
return false
1027+
}
1028+
// Only compare subminor version if set in config version.
1029+
if newV.subminor != "" && newV.subminor != oldV.subminor {
1030+
return false
1031+
}
1032+
// Only compare os if it is set in config version.
1033+
if newV.osName != "" && newV.osName != oldV.osName {
1034+
return false
1035+
}
1036+
return true
1037+
}
1038+
1039+
type dataprocImageVersion struct {
1040+
major string
1041+
minor string
1042+
subminor string
1043+
osName string
1044+
}
1045+
1046+
func parseDataprocImageVersion(version string) (*dataprocImageVersion, error) {
1047+
matches := resolveDataprocImageVersion.FindStringSubmatch(version)
1048+
if len(matches) != 5 {
1049+
return nil, fmt.Errorf("invalid image version %q", version)
1050+
}
1051+
1052+
return &dataprocImageVersion{
1053+
major: matches[1],
1054+
minor: matches[2],
1055+
subminor: matches[3],
1056+
osName: matches[4],
1057+
}, nil
1058+
}

google/resource_dataproc_cluster_test.go

+82-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
"google.golang.org/api/googleapi"
1717
)
1818

19-
func TestExtractInitTimeout(t *testing.T) {
19+
func TestDataprocExtractInitTimeout(t *testing.T) {
2020
t.Parallel()
2121

2222
actual, err := extractInitTimeout("500s")
@@ -29,7 +29,7 @@ func TestExtractInitTimeout(t *testing.T) {
2929
}
3030
}
3131

32-
func TestExtractInitTimeout_nonSeconds(t *testing.T) {
32+
func TestDataprocExtractInitTimeout_nonSeconds(t *testing.T) {
3333
t.Parallel()
3434

3535
actual, err := extractInitTimeout("5m")
@@ -42,7 +42,7 @@ func TestExtractInitTimeout_nonSeconds(t *testing.T) {
4242
}
4343
}
4444

45-
func TestExtractInitTimeout_empty(t *testing.T) {
45+
func TestDataprocExtractInitTimeout_empty(t *testing.T) {
4646
t.Parallel()
4747

4848
_, err := extractInitTimeout("")
@@ -53,6 +53,85 @@ func TestExtractInitTimeout_empty(t *testing.T) {
5353
t.Fatalf("Expected an error with message '%s', but got %v", expected, err.Error())
5454
}
5555

56+
func TestDataprocParseImageVersion(t *testing.T) {
57+
t.Parallel()
58+
59+
testCases := map[string]dataprocImageVersion{
60+
"1.2": {"1", "2", "", ""},
61+
"1.2.3": {"1", "2", "3", ""},
62+
"1.2.3rc": {"1", "2", "3rc", ""},
63+
"1.2-debian9": {"1", "2", "", "debian9"},
64+
"1.2.3-debian9": {"1", "2", "3", "debian9"},
65+
"1.2.3rc-debian9": {"1", "2", "3rc", "debian9"},
66+
}
67+
68+
for v, expected := range testCases {
69+
actual, err := parseDataprocImageVersion(v)
70+
if actual.major != expected.major {
71+
t.Errorf("parsing version %q returned error: %v", v, err)
72+
}
73+
if err != nil {
74+
t.Errorf("parsing version %q returned error: %v", v, err)
75+
}
76+
if actual.minor != expected.minor {
77+
t.Errorf("parsing version %q returned error: %v", v, err)
78+
}
79+
if actual.subminor != expected.subminor {
80+
t.Errorf("parsing version %q returned error: %v", v, err)
81+
}
82+
if actual.osName != expected.osName {
83+
t.Errorf("parsing version %q returned error: %v", v, err)
84+
}
85+
}
86+
87+
errorTestCases := []string{
88+
"",
89+
"1",
90+
"notaversion",
91+
"1-debian",
92+
}
93+
for _, v := range errorTestCases {
94+
if _, err := parseDataprocImageVersion(v); err == nil {
95+
t.Errorf("expected parsing invalid version %q to return error", v)
96+
}
97+
}
98+
}
99+
100+
func TestDataprocDiffSuppress(t *testing.T) {
101+
t.Parallel()
102+
103+
doSuppress := [][]string{
104+
{"1.3.10-debian9", "1.3"},
105+
{"1.3.10-debian9", "1.3-debian9"},
106+
{"1.3.10", "1.3"},
107+
{"1.3-debian9", "1.3"},
108+
}
109+
110+
noSuppress := [][]string{
111+
{"1.3.10-debian9", "1.3.10-ubuntu"},
112+
{"1.3.10-debian9", "1.3.9-debian9"},
113+
{"1.3.10-debian9", "1.3-ubuntu"},
114+
{"1.3.10-debian9", "1.3.9"},
115+
{"1.3.10-debian9", "1.4"},
116+
{"1.3.10-debian9", "2.3"},
117+
{"1.3.10", "1.3.10-debian9"},
118+
{"1.3", "1.3.10"},
119+
{"1.3", "1.3.10-debian9"},
120+
{"1.3", "1.3-debian9"},
121+
}
122+
123+
for _, tup := range doSuppress {
124+
if !dataprocImageVersionDiffSuppress("", tup[0], tup[1], nil) {
125+
t.Errorf("expected (old: %q, new: %q) to be suppressed", tup[0], tup[1])
126+
}
127+
}
128+
for _, tup := range noSuppress {
129+
if dataprocImageVersionDiffSuppress("", tup[0], tup[1], nil) {
130+
t.Errorf("expected (old: %q, new: %q) to not be suppressed", tup[0], tup[1])
131+
}
132+
}
133+
}
134+
56135
func TestAccDataprocCluster_missingZoneGlobalRegion1(t *testing.T) {
57136
t.Parallel()
58137

0 commit comments

Comments
 (0)