Skip to content

Commit

Permalink
Correct a bad assumption about versions made in instance templa… (#4699)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored and rileykarson committed Oct 17, 2019
1 parent 22f774c commit 7ba8fef
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 21 deletions.
28 changes: 15 additions & 13 deletions google/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,38 +197,40 @@ func resolveImage(c *Config, project, name string) (string, error) {
return "", fmt.Errorf("Could not find image or family %s", name)
}

// resolvedImageSelfLink takes the output of resolveImage and coerces it into a self_link.
// In the event that a global/images/IMAGE or global/images/family/FAMILY reference is
// returned from resolveImage, providerProject will be used as the project for the self_link.
func resolvedImageSelfLink(providerProject, name string) (string, error) {
// resolveImageRefToRelativeURI takes the output of resolveImage and coerces it
// into a relative URI. In the event that a global/images/IMAGE or
// global/images/family/FAMILY reference is returned from resolveImage,
// providerProject will be used as the project for the self_link.
func resolveImageRefToRelativeURI(providerProject, name string) (string, error) {
switch {
case resolveImageLink.MatchString(name): // https://www.googleapis.com/compute/v1/projects/xyz/global/images/xyz
return name, nil
case resolveImageProjectImage.MatchString(name): // projects/xyz/global/images/xyz
res := resolveImageProjectImage.FindStringSubmatch(name)
if err := sanityTestRegexMatches(2, res, "project image", name); err != nil {
namePath, err := getRelativePath(name)
if err != nil {
return "", err
}
return fmt.Sprintf("https://www.googleapis.com/compute/v1/projects/%s/global/images/%s", res[1], res[2]), nil

return namePath, nil
case resolveImageProjectImage.MatchString(name): // projects/xyz/global/images/xyz
return name, nil
case resolveImageProjectFamily.MatchString(name): // projects/xyz/global/images/family/xyz
res := resolveImageProjectFamily.FindStringSubmatch(name)
if err := sanityTestRegexMatches(2, res, "project family", name); err != nil {
return "", err
}
return fmt.Sprintf("https://www.googleapis.com/compute/v1/projects/%s/global/images/family/%s", res[1], res[2]), nil
return fmt.Sprintf("projects/%s/global/images/family/%s", res[1], res[2]), nil
case resolveImageGlobalImage.MatchString(name): // global/images/xyz
res := resolveImageGlobalImage.FindStringSubmatch(name)
if err := sanityTestRegexMatches(1, res, "global image", name); err != nil {
return "", err
}
return fmt.Sprintf("https://www.googleapis.com/compute/v1/projects/%s/global/images/%s", providerProject, res[1]), nil
return fmt.Sprintf("projects/%s/global/images/%s", providerProject, res[1]), nil
case resolveImageGlobalFamily.MatchString(name): // global/images/family/xyz
res := resolveImageGlobalFamily.FindStringSubmatch(name)
if err := sanityTestRegexMatches(1, res, "global family", name); err != nil {
return "", err
}
return fmt.Sprintf("https://www.googleapis.com/compute/v1/projects/%s/global/images/family/%s", providerProject, res[1]), nil
return fmt.Sprintf("projects/%s/global/images/family/%s", providerProject, res[1]), nil
}
return "", fmt.Errorf("Could not expand image or family %q into a self_link", name)
return "", fmt.Errorf("Could not expand image or family %q into a relative URI", name)

}
12 changes: 4 additions & 8 deletions google/resource_compute_instance_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,15 +514,15 @@ func resourceComputeInstanceTemplateSourceImageCustomizeDiff(diff *schema.Resour
if err != nil {
return err
}
oldResolved, err = resolvedImageSelfLink(project, oldResolved)
oldResolved, err = resolveImageRefToRelativeURI(project, oldResolved)
if err != nil {
return err
}
newResolved, err := resolveImage(config, project, new.(string))
if err != nil {
return err
}
newResolved, err = resolvedImageSelfLink(project, newResolved)
newResolved, err = resolveImageRefToRelativeURI(project, newResolved)
if err != nil {
return err
}
Expand Down Expand Up @@ -781,13 +781,9 @@ func flattenDisk(disk *computeBeta.AttachedDisk, defaultProject string) (map[str
diskMap := make(map[string]interface{})
if disk.InitializeParams != nil {
if disk.InitializeParams.SourceImage != "" {
selfLink, err := resolvedImageSelfLink(defaultProject, disk.InitializeParams.SourceImage)
path, err := resolveImageRefToRelativeURI(defaultProject, disk.InitializeParams.SourceImage)
if err != nil {
return nil, errwrap.Wrapf("Error expanding source image input to self_link: {{err}}", err)
}
path, err := getRelativePath(selfLink)
if err != nil {
return nil, errwrap.Wrapf("Error getting relative path for source image: {{err}}", err)
return nil, errwrap.Wrapf("Error expanding source image input to relative URI: {{err}}", err)
}
diskMap["source_image"] = path
} else {
Expand Down

0 comments on commit 7ba8fef

Please sign in to comment.