Skip to content

Commit

Permalink
Add diff suppress to dataproc image version
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
emilymye authored and modular-magician committed Jul 24, 2019
1 parent bcdb422 commit e81212a
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 7 deletions.
59 changes: 55 additions & 4 deletions google-beta/resource_dataproc_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
dataproc "google.golang.org/api/dataproc/v1beta2"
)

var resolveDataprocImageVersion = regexp.MustCompile(`(?P<Major>[^\s.-]+)\.(?P<Minor>[^\s.-]+)(?:\.(?P<Subminor>[^\s.-]+))?(?:\-(?P<Distr>[^\s.-]+))?`)

func resourceDataprocCluster() *schema.Resource {
return &schema.Resource{
Create: resourceDataprocClusterCreate,
Expand Down Expand Up @@ -259,10 +261,11 @@ func resourceDataprocCluster() *schema.Resource {
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"image_version": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
DiffSuppressFunc: dataprocImageVersionDiffSuppress,
},

"override_properties": {
Expand Down Expand Up @@ -1016,3 +1019,51 @@ func configOptions(d *schema.ResourceData, option string) (map[string]interface{
}
return nil, false
}

func dataprocImageVersionDiffSuppress(_, old, new string, _ *schema.ResourceData) bool {
oldV, err := parseDataprocImageVersion(old)
if err != nil {
return false
}
newV, err := parseDataprocImageVersion(new)
if err != nil {
return false
}

if newV.major != oldV.major {
return false
}
if newV.minor != oldV.minor {
return false
}
// Only compare subminor version if set in config version.
if newV.subminor != "" && newV.subminor != oldV.subminor {
return false
}
// Only compare os if it is set in config version.
if newV.osName != "" && newV.osName != oldV.osName {
return false
}
return true
}

type dataprocImageVersion struct {
major string
minor string
subminor string
osName string
}

func parseDataprocImageVersion(version string) (*dataprocImageVersion, error) {
matches := resolveDataprocImageVersion.FindStringSubmatch(version)
if len(matches) != 5 {
return nil, fmt.Errorf("invalid image version %q", version)
}

return &dataprocImageVersion{
major: matches[1],
minor: matches[2],
subminor: matches[3],
osName: matches[4],
}, nil
}
85 changes: 82 additions & 3 deletions google-beta/resource_dataproc_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"google.golang.org/api/googleapi"
)

func TestExtractInitTimeout(t *testing.T) {
func TestDataprocExtractInitTimeout(t *testing.T) {
t.Parallel()

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

func TestExtractInitTimeout_nonSeconds(t *testing.T) {
func TestDataprocExtractInitTimeout_nonSeconds(t *testing.T) {
t.Parallel()

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

func TestExtractInitTimeout_empty(t *testing.T) {
func TestDataprocExtractInitTimeout_empty(t *testing.T) {
t.Parallel()

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

func TestDataprocParseImageVersion(t *testing.T) {
t.Parallel()

testCases := map[string]dataprocImageVersion{
"1.2": {"1", "2", "", ""},
"1.2.3": {"1", "2", "3", ""},
"1.2.3rc": {"1", "2", "3rc", ""},
"1.2-debian9": {"1", "2", "", "debian9"},
"1.2.3-debian9": {"1", "2", "3", "debian9"},
"1.2.3rc-debian9": {"1", "2", "3rc", "debian9"},
}

for v, expected := range testCases {
actual, err := parseDataprocImageVersion(v)
if actual.major != expected.major {
t.Errorf("parsing version %q returned error: %v", v, err)
}
if err != nil {
t.Errorf("parsing version %q returned error: %v", v, err)
}
if actual.minor != expected.minor {
t.Errorf("parsing version %q returned error: %v", v, err)
}
if actual.subminor != expected.subminor {
t.Errorf("parsing version %q returned error: %v", v, err)
}
if actual.osName != expected.osName {
t.Errorf("parsing version %q returned error: %v", v, err)
}
}

errorTestCases := []string{
"",
"1",
"notaversion",
"1-debian",
}
for _, v := range errorTestCases {
if _, err := parseDataprocImageVersion(v); err == nil {
t.Errorf("expected parsing invalid version %q to return error", v)
}
}
}

func TestDataprocDiffSuppress(t *testing.T) {
t.Parallel()

doSuppress := [][]string{
{"1.3.10-debian9", "1.3"},
{"1.3.10-debian9", "1.3-debian9"},
{"1.3.10", "1.3"},
{"1.3-debian9", "1.3"},
}

noSuppress := [][]string{
{"1.3.10-debian9", "1.3.10-ubuntu"},
{"1.3.10-debian9", "1.3.9-debian9"},
{"1.3.10-debian9", "1.3-ubuntu"},
{"1.3.10-debian9", "1.3.9"},
{"1.3.10-debian9", "1.4"},
{"1.3.10-debian9", "2.3"},
{"1.3.10", "1.3.10-debian9"},
{"1.3", "1.3.10"},
{"1.3", "1.3.10-debian9"},
{"1.3", "1.3-debian9"},
}

for _, tup := range doSuppress {
if !dataprocImageVersionDiffSuppress("", tup[0], tup[1], nil) {
t.Errorf("expected (old: %q, new: %q) to be suppressed", tup[0], tup[1])
}
}
for _, tup := range noSuppress {
if dataprocImageVersionDiffSuppress("", tup[0], tup[1], nil) {
t.Errorf("expected (old: %q, new: %q) to not be suppressed", tup[0], tup[1])
}
}
}

func TestAccDataprocCluster_missingZoneGlobalRegion1(t *testing.T) {
t.Parallel()

Expand Down

0 comments on commit e81212a

Please sign in to comment.