Skip to content

Commit

Permalink
allow empty metadata.startup-script (#3732)
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 danawillow committed May 29, 2019
1 parent a28fcec commit f070c28
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 2 deletions.
3 changes: 2 additions & 1 deletion google/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package google
import (
"errors"
"fmt"

computeBeta "google.golang.org/api/compute/v0.beta"
"google.golang.org/api/compute/v1"
)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion google/resource_compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
65 changes: 65 additions & 0 deletions google/resource_compute_instance_from_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)
}

0 comments on commit f070c28

Please sign in to comment.