From f070c281f90a2a0a9c2423d5d9329bd43312d3ff Mon Sep 17 00:00:00 2001 From: The Magician Date: Tue, 28 May 2019 18:46:31 -0700 Subject: [PATCH] allow empty metadata.startup-script (#3732) Signed-off-by: Modular Magician --- google/metadata.go | 3 +- google/resource_compute_instance.go | 2 +- ...rce_compute_instance_from_template_test.go | 65 +++++++++++++++++++ 3 files changed, 68 insertions(+), 2 deletions(-) diff --git a/google/metadata.go b/google/metadata.go index 37117c84ba9..7a9fa61cabe 100644 --- a/google/metadata.go +++ b/google/metadata.go @@ -3,6 +3,7 @@ package google import ( "errors" "fmt" + computeBeta "google.golang.org/api/compute/v0.beta" "google.golang.org/api/compute/v1" ) @@ -135,7 +136,7 @@ func resourceInstanceMetadata(d TerraformResourceData) (*computeBeta.Metadata, e m := &computeBeta.Metadata{} mdMap := d.Get("metadata").(map[string]interface{}) if v, ok := d.GetOk("metadata_startup_script"); ok && v.(string) != "" { - if ss, ok := mdMap["startup-script"]; ok && ss != "" { + if _, ok := mdMap["startup-script"]; ok { return nil, errors.New("Cannot provide both metadata_startup_script and metadata.startup-script.") } mdMap["startup-script"] = v diff --git a/google/resource_compute_instance.go b/google/resource_compute_instance.go index 9c5707b3685..989bcbe611d 100644 --- a/google/resource_compute_instance.go +++ b/google/resource_compute_instance.go @@ -787,7 +787,7 @@ func resourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) error // If the existing config specifies "metadata.startup-script" instead of "metadata_startup_script", // we shouldn't move the remote metadata.startup-script to metadata_startup_script. Otherwise, // we should. - if ss, ok := existingMetadata["startup-script"]; !ok || ss == "" { + if _, ok := existingMetadata["startup-script"]; !ok { d.Set("metadata_startup_script", md["startup-script"]) // Note that here we delete startup-script from our metadata list. This is to prevent storing the startup-script // as a value in the metadata since the config specifically tracks it under 'metadata_startup_script' diff --git a/google/resource_compute_instance_from_template_test.go b/google/resource_compute_instance_from_template_test.go index 0c84e4388e5..fa3f31fde36 100644 --- a/google/resource_compute_instance_from_template_test.go +++ b/google/resource_compute_instance_from_template_test.go @@ -168,6 +168,29 @@ func TestAccComputeInstanceFromTemplate_012_removableFields(t *testing.T) { }) } +func TestAccComputeInstanceFromTemplate_overrideMetadataDotStartupScript(t *testing.T) { + var instance compute.Instance + instanceName := fmt.Sprintf("terraform-test-%s", acctest.RandString(10)) + templateName := fmt.Sprintf("terraform-test-%s", acctest.RandString(10)) + resourceName := "google_compute_instance_from_template.inst" + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckComputeInstanceFromTemplateDestroy, + Steps: []resource.TestStep{ + { + Config: testAccComputeInstanceFromTemplate_overrideMetadataDotStartupScript(instanceName, templateName), + Check: resource.ComposeTestCheckFunc( + testAccCheckComputeInstanceExists(resourceName, &instance), + resource.TestCheckResourceAttr(resourceName, "metadata.startup-script", ""), + ), + }, + }, + }) + +} + func testAccCheckComputeInstanceFromTemplateDestroy(s *terraform.State) error { config := testAccProvider.Meta().(*Config) @@ -511,3 +534,45 @@ resource "google_compute_instance_from_template" "inst" { } `, instance) } + +func testAccComputeInstanceFromTemplate_overrideMetadataDotStartupScript(instance, template string) string { + return fmt.Sprintf(` +data "google_compute_image" "my_image" { + family = "debian-9" + project = "debian-cloud" +} + +resource "google_compute_instance_template" "foobar" { + name = "%s" + machine_type = "n1-standard-1" + + disk { + source_image = "${data.google_compute_image.my_image.self_link}" + auto_delete = true + boot = true + } + + network_interface { + network = "default" + } + + metadata = { + startup-script = "#!/bin/bash\necho Hello" + } + + can_ip_forward = true +} + +resource "google_compute_instance_from_template" "inst" { + name = "%s" + zone = "us-central1-a" + + source_instance_template = "${google_compute_instance_template.foobar.self_link}" + + // Overrides + metadata = { + startup-script = "" + } +} +`, template, instance) +}