From 93af03cd6cbf8b06f0c289d8a7207c610bd4dbc2 Mon Sep 17 00:00:00 2001 From: Chris Stephens Date: Wed, 5 Jun 2019 17:26:38 +0000 Subject: [PATCH] Retrieve project after detecting changes Signed-off-by: Modular Magician --- google/resource_compute_instance_template.go | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/google/resource_compute_instance_template.go b/google/resource_compute_instance_template.go index 873e15968b1..215389766af 100644 --- a/google/resource_compute_instance_template.go +++ b/google/resource_compute_instance_template.go @@ -477,14 +477,18 @@ 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) { + // project must be retrieved once we know there is a diff to resolve, otherwise it will + // attempt to retrieve project during `plan` before all calculated fields are ready + // see https://github.com/terraform-providers/terraform-provider-google/issues/2878 + project, err := getProjectFromDiff(diff, config) + if err != nil { + return err + } old, new := diff.GetChange(key) if old == "" || new == "" { // no sense in resolving empty strings