Skip to content

Commit 6a1d0a4

Browse files
modular-magicianEdward Sun
and
Edward Sun
authored
fix metadata_startup_script (#6822) (#13077)
Co-authored-by: Edward Sun <[email protected]> Signed-off-by: Modular Magician <[email protected]> Signed-off-by: Modular Magician <[email protected]> Co-authored-by: Edward Sun <[email protected]>
1 parent 6c62647 commit 6a1d0a4

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

.changelog/6822.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
compute: fixed the error when `metadata` and `machine_type` are updated while `metadata_startup_script` was already provided on `google_compute_instance`
3+
```

google/metadata.go

+5-2
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,11 @@ func resourceInstanceMetadata(d TerraformResourceData) (*compute.Metadata, error
142142
m := &compute.Metadata{}
143143
mdMap := d.Get("metadata").(map[string]interface{})
144144
if v, ok := d.GetOk("metadata_startup_script"); ok && v.(string) != "" {
145-
if _, ok := mdMap["startup-script"]; ok {
146-
return nil, errors.New("Cannot provide both metadata_startup_script and metadata.startup-script.")
145+
if w, ok := mdMap["startup-script"]; ok {
146+
// metadata.startup-script could be from metadata_startup_script in the first place
147+
if v != w {
148+
return nil, errors.New("Cannot provide both metadata_startup_script and metadata.startup-script.")
149+
}
147150
}
148151
mdMap["startup-script"] = v
149152
}

google/resource_compute_instance_test.go

+62
Original file line numberDiff line numberDiff line change
@@ -2370,6 +2370,35 @@ func TestComputeInstance_networkIPCustomizedDiff(t *testing.T) {
23702370
}
23712371
}
23722372

2373+
func TestAccComputeInstance_metadataStartupScript_update(t *testing.T) {
2374+
t.Parallel()
2375+
2376+
var instance compute.Instance
2377+
var instanceName = fmt.Sprintf("tf-test-%s", randString(t, 10))
2378+
2379+
vcrTest(t, resource.TestCase{
2380+
PreCheck: func() { testAccPreCheck(t) },
2381+
Providers: testAccProviders,
2382+
CheckDestroy: testAccCheckComputeInstanceDestroyProducer(t),
2383+
Steps: []resource.TestStep{
2384+
{
2385+
Config: testAccComputeInstance_metadataStartupScript(instanceName, "e2-medium", "abc"),
2386+
Check: resource.ComposeTestCheckFunc(
2387+
testAccCheckComputeInstanceExists(
2388+
t, "google_compute_instance.foobar", &instance),
2389+
),
2390+
},
2391+
{
2392+
Config: testAccComputeInstance_metadataStartupScript(instanceName, "e2-standard-4", "xyz"),
2393+
Check: resource.ComposeTestCheckFunc(
2394+
testAccCheckComputeInstanceExists(
2395+
t, "google_compute_instance.foobar", &instance),
2396+
),
2397+
},
2398+
},
2399+
})
2400+
}
2401+
23732402
func testAccCheckComputeInstanceUpdateMachineType(t *testing.T, n string) resource.TestCheckFunc {
23742403
return func(s *terraform.State) error {
23752404
rs, ok := s.RootModule().Resources[n]
@@ -6205,3 +6234,36 @@ resource "google_compute_instance" "foobar" {
62056234
}
62066235
`, instance)
62076236
}
6237+
6238+
func testAccComputeInstance_metadataStartupScript(instance, machineType, metadata string) string {
6239+
return fmt.Sprintf(`
6240+
data "google_compute_image" "my_image" {
6241+
family = "debian-11"
6242+
project = "debian-cloud"
6243+
}
6244+
6245+
resource "google_compute_instance" "foobar" {
6246+
name = "%s"
6247+
machine_type = "%s"
6248+
zone = "us-central1-a"
6249+
can_ip_forward = false
6250+
tags = ["foo", "bar"]
6251+
6252+
boot_disk {
6253+
initialize_params {
6254+
image = data.google_compute_image.my_image.self_link
6255+
}
6256+
}
6257+
6258+
network_interface {
6259+
network = "default"
6260+
}
6261+
6262+
metadata = {
6263+
foo = "%s"
6264+
}
6265+
metadata_startup_script = "echo hi > /test.txt"
6266+
allow_stopping_for_update = true
6267+
}
6268+
`, instance, machineType, metadata)
6269+
}

0 commit comments

Comments
 (0)