Skip to content
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

Better DiffSuppressFunc for disk image field #884

Merged
merged 2 commits into from
Dec 21, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
21 changes: 20 additions & 1 deletion google/resource_compute_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func resourceComputeDisk() *schema.Resource {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
DiffSuppressFunc: linkDiffSuppress,
DiffSuppressFunc: diskImageDiffSuppress,
},

"project": &schema.Schema{
Expand Down Expand Up @@ -407,3 +407,22 @@ func resourceComputeDiskDelete(d *schema.ResourceData, meta interface{}) error {
d.SetId("")
return nil
}

func diskImageDiffSuppress(_, old, new string, _ *schema.ResourceData) bool {
parts := strings.Split(old, "/")

if parts[len(parts)-1] == new {
return true
}

// Handle the "latest" image short hand case:
// "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-8-jessie-v20171213" => "debian-cloud/debian-8"
newParts := strings.Split(new, "/")
if len(newParts) == 2 {
if strings.Contains(old, newParts[0]) && strings.Contains(old, newParts[1]) {
return true
}
}

return false
}
39 changes: 39 additions & 0 deletions google/resource_compute_disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,45 @@ import (
"google.golang.org/api/compute/v1"
)

func TestDiskImageDiffSuppress(t *testing.T) {
cases := map[string]struct {
Old, New string
ExpectDiffSuppress bool
}{
"new is a matching short hand": {
Old: "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-8-jessie-v20171213",
New: "debian-cloud/debian-8-jessie-v20171213",
ExpectDiffSuppress: true,
},
"new is a matching latest short hand": {
Old: "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-8-jessie-v20171213",
New: "debian-cloud/debian-8",
ExpectDiffSuppress: true,
},
"new is a different self_link": {
Old: "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-8-jessie-v20171213",
New: "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-7-jessie-v20171213",
ExpectDiffSuppress: false,
},
"new is a different short hand": {
Old: "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-8-jessie-v20171213",
New: "debian-cloud/debian-7-jessie-v20171213",
ExpectDiffSuppress: false,
},
"new is a different latest short hand": {
Old: "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-8-jessie-v20171213",
New: "debian-cloud/debian-7",
ExpectDiffSuppress: false,
},
}

for tn, tc := range cases {
if diskImageDiffSuppress("image", tc.Old, tc.New, nil) != tc.ExpectDiffSuppress {
t.Errorf("bad: %s, %q => %q expect DiffSuppress to return %t", tn, tc.Old, tc.New, tc.ExpectDiffSuppress)
}
}
}

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

Expand Down