Skip to content

Commit

Permalink
Remove latest image from diskImageDiffSuppress (#1021)
Browse files Browse the repository at this point in the history
  • Loading branch information
rosbo authored Jan 30, 2018
1 parent cc5f77e commit 429c8e8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 31 deletions.
19 changes: 9 additions & 10 deletions google/resource_compute_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,18 +426,17 @@ func diskImageDiffSuppress(_, old, new string, _ *schema.ResourceData) bool {
newProject := matches[1]
newFamilyName := matches[2]

return diskImageProjectNameEquals(oldProject, oldName, newProject, newFamilyName)
return diskImageProjectNameEquals(oldProject, newProject) && strings.Contains(oldName, newFamilyName)
}

// Partial or full self link image
if resolveImageProjectImage.MatchString(new) {
// Value matches pattern "projects/{project}/global/images/{image-name}$"
// or "projects/{project}/global/images/{image-name-latest}$"
matches := resolveImageProjectImage.FindStringSubmatch(new)
newProject := matches[1]
newImageName := matches[2]

return diskImageProjectNameEquals(oldProject, oldName, newProject, newImageName)
return diskImageProjectNameEquals(oldProject, newProject) && strings.Contains(oldName, newImageName)
}

// Partial link without project family
Expand All @@ -451,7 +450,7 @@ func diskImageDiffSuppress(_, old, new string, _ *schema.ResourceData) bool {

// Partial link without project image
if resolveImageGlobalImage.MatchString(new) {
// Value is "global/images/family/{image-name}" or "global/images/family/{image-name-latest}"
// Value is "global/images/{image-name}"
matches := resolveImageGlobalImage.FindStringSubmatch(new)
imageName := matches[1]

Expand All @@ -467,32 +466,32 @@ func diskImageDiffSuppress(_, old, new string, _ *schema.ResourceData) bool {
return strings.Contains(oldName, familyName)
}

// Shorthand for image
// Shorthand for image or family
if resolveImageProjectImageShorthand.MatchString(new) {
// Value is "{project}/{image-name}" or "{project}/{image-name-latest}"
// Value is "{project}/{image-name}" or "{project}/{family-name}"
matches := resolveImageProjectImageShorthand.FindStringSubmatch(new)
newProject := matches[1]
newName := matches[2]

return diskImageProjectNameEquals(oldProject, oldName, newProject, newName)
return diskImageProjectNameEquals(oldProject, newProject) && strings.Contains(oldName, newName)
}

// Image or family only
if strings.Contains(oldName, new) {
// Value is "{image-name}" or "{family-name}" or "{image-name-latest}"
// Value is "{image-name}" or "{family-name}"
return true
}

return false
}

func diskImageProjectNameEquals(project1, name1, project2, name2 string) bool {
func diskImageProjectNameEquals(project1, project2 string) bool {
// Convert short project name to full name
// For instance, centos => centos-cloud
fullProjectName, ok := imageMap[project2]
if ok {
project2 = fullProjectName
}

return project1 == project2 && strings.Contains(name1, name2)
return project1 == project2
}
41 changes: 20 additions & 21 deletions google/resource_compute_disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,27 +75,6 @@ func TestDiskImageDiffSuppress(t *testing.T) {
New: "debian-cloud/debian-7-jessie-v20171213",
ExpectDiffSuppress: false,
},
// Latest image short hand
"matching latest image 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,
},
"matching latest image short hand with project short name": {
Old: "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-8-jessie-v20171213",
New: "debian/debian-8",
ExpectDiffSuppress: true,
},
"matching latest image short hand but different project": {
Old: "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-8-jessie-v20171213",
New: "different-cloud/debian-8",
ExpectDiffSuppress: false,
},
"different latest image 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,
},
// Image Family
"matching image family": {
Old: "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-8-jessie-v20171213",
Expand All @@ -117,6 +96,16 @@ func TestDiskImageDiffSuppress(t *testing.T) {
New: "global/images/family/debian-8",
ExpectDiffSuppress: true,
},
"matching image family 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,
},
"matching image family short hand with project short name": {
Old: "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-8-jessie-v20171213",
New: "debian/debian-8",
ExpectDiffSuppress: true,
},
"different image family": {
Old: "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-8-jessie-v20171213",
New: "family/debian-7",
Expand Down Expand Up @@ -147,6 +136,16 @@ func TestDiskImageDiffSuppress(t *testing.T) {
New: "projects/other-cloud/global/images/family/debian-8",
ExpectDiffSuppress: false,
},
"different image family 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,
},
"matching image family shorthand but different project": {
Old: "https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/debian-8-jessie-v20171213",
New: "different-cloud/debian-8",
ExpectDiffSuppress: false,
},
}

for tn, tc := range cases {
Expand Down

0 comments on commit 429c8e8

Please sign in to comment.