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

Fix perma-diff on instance templates. #1995

Merged
merged 4 commits into from
Oct 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
36 changes: 36 additions & 0 deletions google/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,39 @@ 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) {
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 {
return "", err
}
return fmt.Sprintf("https://www.googleapis.com/compute/v1/projects/%s/global/images/%s", res[1], res[2]), 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
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
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.Errorf("Could not expand image or family %q into a self_link", name)

}
59 changes: 55 additions & 4 deletions google/resource_compute_instance_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ func resourceComputeInstanceTemplate() *schema.Resource {
State: schema.ImportStatePassthrough,
},
SchemaVersion: 1,
CustomizeDiff: resourceComputeInstanceTemplateSourceImageCustomizeDiff,
MigrateState: resourceComputeInstanceTemplateMigrateState,

// A compute instance template is more or less a subset of a compute
Expand Down Expand Up @@ -99,10 +100,9 @@ func resourceComputeInstanceTemplate() *schema.Resource {
},

"source_image": &schema.Schema{
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: compareSelfLinkRelativePaths,
ForceNew: true,
Type: schema.TypeString,
Optional: true,
Computed: true,
},

"interface": &schema.Schema{
Expand Down Expand Up @@ -420,6 +420,57 @@ func resourceComputeInstanceTemplate() *schema.Resource {
}
}

func resourceComputeInstanceTemplateSourceImageCustomizeDiff(diff *schema.ResourceDiff, meta interface{}) error {
config := meta.(*Config)
project, err := getProjectFromDiff(diff, config)
if err != nil {
return err
}
numDisks := diff.Get("disk.#").(int)
for i := 0; i < numDisks; i++ {
key := fmt.Sprintf("disk.%d.source_image", i)
if diff.HasChange(key) {
old, new := diff.GetChange(key)
if old == "" || new == "" {
// no sense in resolving empty strings
err = diff.ForceNew(key)
if err != nil {
return err
}
continue
}
oldResolved, err := resolveImage(config, project, old.(string))
if err != nil {
return err
}
oldResolved, err = resolvedImageSelfLink(project, oldResolved)
if err != nil {
return err
}
newResolved, err := resolveImage(config, project, new.(string))
if err != nil {
return err
}
newResolved, err = resolvedImageSelfLink(project, newResolved)
if err != nil {
return err
}
if oldResolved != newResolved {
err = diff.ForceNew(key)
if err != nil {
return err
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't there be a continue here? otherwise the diff will get cleared on line 456

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch.

continue
}
err = diff.Clear(key)
if err != nil {
return err
}
}
}
return nil
}

func buildDisks(d *schema.ResourceData, config *Config) ([]*computeBeta.AttachedDisk, error) {
project, err := getProject(d, config)
if err != nil {
Expand Down
14 changes: 14 additions & 0 deletions google/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,20 @@ func getProject(d TerraformResourceData, config *Config) (string, error) {
return getProjectFromSchema("project", d, config)
}

// getProjectFromDiff reads the "project" field from the given diff and falls
// back to the provider's value if not given. If the provider's value is not
// given, an error is returned.
func getProjectFromDiff(d *schema.ResourceDiff, config *Config) (string, error) {
res, ok := d.GetOk("project")
if ok {
return res.(string), nil
}
if config.Project != "" {
return config.Project, nil
}
return "", fmt.Errorf("%s: required field is not set", "project")
}

func getProjectFromInstanceState(is *terraform.InstanceState, config *Config) (string, error) {
res, ok := is.Attributes["project"]

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions vendor/github.com/hashicorp/terraform/helper/schema/set.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 3 additions & 5 deletions vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -665,12 +665,10 @@
"versionExact": "v0.11.2"
},
{
"checksumSHA1": "JHxGzmxcIS8NyLX9pGhK5beIra4=",
"checksumSHA1": "OOwTGBTHcUmQTPBdyscTMkjApbI=",
"path": "github.com/hashicorp/terraform/helper/schema",
"revision": "41e50bd32a8825a84535e353c3674af8ce799161",
"revisionTime": "2018-04-10T16:50:42Z",
"version": "v0.11.2",
"versionExact": "v0.11.2"
"revision": "35d82b055591e9d47a254e68754216d8849ba67a",
"revisionTime": "2018-09-26T21:21:28Z"
},
{
"checksumSHA1": "Fzbv+N7hFXOtrR6E7ZcHT3jEE9s=",
Expand Down