Skip to content

Commit e7b72b5

Browse files
Change the import behaviour of GCE instance metadata startup-script (#5329) (#10392)
Signed-off-by: Modular Magician <[email protected]>
1 parent 9c894a0 commit e7b72b5

6 files changed

+44
-21
lines changed

.changelog/5329.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:breaking-change
2+
compute: changed the import / drift detection behaviours for `metadata_startup_script`, `metadata.startup-script` in `google_compute_instance`. Now, `metadata.startup-script` will be set by default, and `metadata_startup_script` will only be set if present.
3+
```

google/resource_compute_instance.go

+5-9
Original file line numberDiff line numberDiff line change
@@ -1118,19 +1118,15 @@ func resourceComputeInstanceRead(d *schema.ResourceData, meta interface{}) error
11181118
}
11191119

11201120
md := flattenMetadataBeta(instance.Metadata)
1121-
existingMetadata := d.Get("metadata").(map[string]interface{})
11221121

1123-
// If the existing config specifies "metadata.startup-script" instead of "metadata_startup_script",
1124-
// we shouldn't move the remote metadata.startup-script to metadata_startup_script. Otherwise,
1125-
// we should.
1126-
if _, ok := existingMetadata["startup-script"]; !ok {
1122+
// If the existing state contains "metadata_startup_script" instead of "metadata.startup-script",
1123+
// we should move the remote metadata.startup-script to metadata_startup_script to avoid
1124+
// specifying it in two places.
1125+
if _, ok := d.GetOk("metadata_startup_script"); ok {
11271126
if err := d.Set("metadata_startup_script", md["startup-script"]); err != nil {
11281127
return fmt.Errorf("Error setting metadata_startup_script: %s", err)
11291128
}
1130-
// Note that here we delete startup-script from our metadata list. This is to prevent storing the startup-script
1131-
// as a value in the metadata since the config specifically tracks it under 'metadata_startup_script'
1132-
delete(md, "startup-script")
1133-
} else if _, ok := d.GetOk("metadata_startup_script"); ok {
1129+
11341130
delete(md, "startup-script")
11351131
}
11361132

google/resource_compute_instance_template.go

+2
Original file line numberDiff line numberDiff line change
@@ -1291,8 +1291,10 @@ func resourceComputeInstanceTemplateRead(d *schema.ResourceData, meta interface{
12911291
if err = d.Set("metadata_startup_script", script); err != nil {
12921292
return fmt.Errorf("Error setting metadata_startup_script: %s", err)
12931293
}
1294+
12941295
delete(_md, "startup-script")
12951296
}
1297+
12961298
if err = d.Set("metadata", _md); err != nil {
12971299
return fmt.Errorf("Error setting metadata: %s", err)
12981300
}

google/resource_compute_instance_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ func testSweepComputeInstance(region string) error {
7676

7777
func computeInstanceImportStep(zone, instanceName string, additionalImportIgnores []string) resource.TestStep {
7878
// metadata is only read into state if set in the config
79-
// since importing doesn't know whether metadata.startup_script vs metadata_startup_script is set in the config,
80-
// it guesses metadata_startup_script
79+
// importing doesn't know whether metadata.startup_script vs metadata_startup_script is set in the config,
80+
// it always takes metadata.startup-script
8181
ignores := []string{"metadata.%", "metadata.startup-script", "metadata_startup_script"}
8282

8383
return resource.TestStep{

website/docs/guides/version_4_upgrade.html.markdown

+22
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,28 @@ The provider will now enforce at plan time that one of these fields be set.
277277

278278
Previously, if all of these fields were left empty, the firewall defaulted to allowing traffic from 0.0.0.0/0, which is a suboptimal default.
279279

280+
## Resource: `google_compute_instance`
281+
282+
### `metadata_startup_script` is no longer set on import
283+
284+
Earlier versions of the provider set the `metadata_startup_script` value on
285+
import, omitting the value of `metadata.startup-script` for historical backwards
286+
compatibility. This was dangerous in practice, as `metadata_startup_script`
287+
would flag an instance for recreation if the values differed rather than for
288+
just an update.
289+
290+
In `4.0.0` the behaviour has been flipped, and `metadata.startup-script` is the
291+
default value that gets written. Users who want `metadata_startup_script` set
292+
on an imported instance will need to modify their state manually. This is more
293+
consistent with our expectations for the field, that a user who manages an
294+
instance **only** through Terraform uses it but that most users should prefer
295+
the `metadata` block.
296+
297+
No action is required for user configs with instances already imported. If you
298+
have a config or module where neither is specified- where `import` will be run,
299+
or an old config that is not reconciled with the API- the value that gets set
300+
will change.
301+
280302
## Resource: `google_compute_instance_group_manager`
281303

282304
### `update_policy.min_ready_sec` is removed from the GA provider

website/docs/r/compute_instance.html.markdown

+10-10
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,16 @@ The following arguments are supported:
132132
we provide a special attribute, `metadata_startup_script`, which is documented below.
133133

134134
* `metadata_startup_script` - (Optional) An alternative to using the
135-
startup-script metadata key, except this one forces the instance to be
136-
recreated (thus re-running the script) if it is changed. This replaces the
137-
startup-script metadata key on the created instance and thus the two
138-
mechanisms are not allowed to be used simultaneously. Users are free to use
139-
either mechanism - the only distinction is that this separate attribute
140-
will cause a recreate on modification. On import, `metadata_startup_script`
141-
will be set, but `metadata.startup-script` will not - if you choose to use the
142-
other mechanism, you will see a diff immediately after import, which will cause a
143-
destroy/recreate operation. You may want to modify your state file manually
144-
using `terraform state` commands, depending on your use case.
135+
startup-script metadata key, except this one forces the instance to be recreated
136+
(thus re-running the script) if it is changed. This replaces the startup-script
137+
metadata key on the created instance and thus the two mechanisms are not
138+
allowed to be used simultaneously. Users are free to use either mechanism - the
139+
only distinction is that this separate attribute will cause a recreate on
140+
modification. On import, `metadata_startup_script` will not be set - if you
141+
choose to specify it you will see a diff immediately after import causing a
142+
destroy/recreate operation. If importing an instance and specifying this value
143+
is desired, you will need to modify your state file manually using
144+
`terraform state` commands.
145145

146146
* `min_cpu_platform` - (Optional) Specifies a minimum CPU platform for the VM instance. Applicable values are the friendly names of CPU platforms, such as
147147
`Intel Haswell` or `Intel Skylake`. See the complete list [here](https://cloud.google.com/compute/docs/instances/specify-min-cpu-platform).

0 commit comments

Comments
 (0)