Skip to content

Commit a98d2f0

Browse files
committed
compute: Implement graceful switch for metadata_startup_script
This patch checks whether a graceful switch (without ForceNew) is available between `metadata_startup_script` and `metadata.startup-script`. Graceful switch can be executed in two situations: 1. When `metadata_startup_script` is created with the old value of `metadata.startup-script`. 2. When `metadata_startup_script` is deleted and the old value remains in `metadata.startup-script` For all other changes in `metadata_startup_script`, function sets ForceNew. Closes: hashicorp/terraform-provider-google#9459 Signed-off-by: Norbert Kamiński <[email protected]>
1 parent 5be6d05 commit a98d2f0

File tree

1 file changed

+49
-1
lines changed

1 file changed

+49
-1
lines changed

mmv1/third_party/terraform/services/compute/resource_compute_instance.go.tmpl

+49-1
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,6 @@ func ResourceComputeInstance() *schema.Resource {
816816
"metadata_startup_script": {
817817
Type: schema.TypeString,
818818
Optional: true,
819-
ForceNew: true,
820819
Description: `Metadata startup scripts made available within the instance.`,
821820
},
822821

@@ -1316,6 +1315,9 @@ be from 0 to 999,999,999 inclusive.`,
13161315
},
13171316
suppressEmptyGuestAcceleratorDiff,
13181317
),
1318+
customdiff.ForceNewIf("metadata_startup_script", func(_ context.Context, d *schema.ResourceDiff, meta interface{}) bool {
1319+
return isGracefulMetadataStartupSwitch(d)
1320+
}),
13191321
validateSubnetworkProject,
13201322
forceNewIfNetworkIPNotUpdatable,
13211323
tpgresource.SetLabelsDiff,
@@ -2978,6 +2980,52 @@ func suppressEmptyGuestAcceleratorDiff(_ context.Context, d *schema.ResourceDiff
29782980
return nil
29792981
}
29802982

2983+
// Function checks whether a graceful switch (without ForceNew) is available
2984+
// between `metadata_startup_script` and `metadata.startup-script`.
2985+
// Graceful switch can be executed in two situations:
2986+
// 1. When `metadata_startup_script` is created with the old value of
2987+
// `metadata.startup-script`.
2988+
// 2. When `metadata_startup_script` is deleted and the old value remains in
2989+
// `metadata.startup-script`
2990+
// For all other changes in `metadata_startup_script`, function sets ForceNew.
2991+
func isGracefulMetadataStartupSwitch(d *schema.ResourceDiff) bool {
2992+
oldMd, newMd := d.GetChange("metadata")
2993+
oldMdMap := oldMd.(map[string]interface{})
2994+
newMdMap := newMd.(map[string]interface{})
2995+
2996+
//No new and old metadata
2997+
if len(oldMdMap) == 0 && len(newMdMap) == 0 {
2998+
return true
2999+
}
3000+
3001+
oldMds, newMds := d.GetChange("metadata_startup_script")
3002+
vMdOld, okOld := oldMdMap["startup-script"]
3003+
vMdNew, okNew := newMdMap["startup-script"]
3004+
3005+
// metadata_startup_script is created
3006+
if oldMds == "" {
3007+
if !okOld {
3008+
return true
3009+
} else if newMds == vMdOld {
3010+
return false
3011+
} else {
3012+
return true
3013+
}
3014+
}
3015+
// metadata_startup_script is deleted
3016+
if newMds == "" {
3017+
if !okNew {
3018+
return true
3019+
} else if oldMds == vMdNew {
3020+
return false
3021+
} else {
3022+
return true
3023+
}
3024+
}
3025+
3026+
return true
3027+
}
3028+
29813029
func resourceComputeInstanceDelete(d *schema.ResourceData, meta interface{}) error {
29823030
config := meta.(*transport_tpg.Config)
29833031
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)

0 commit comments

Comments
 (0)