Skip to content

Commit 45b053a

Browse files
authored
Remove post-startup-script key from protected metadata for EUC (#15998)
1 parent 463571b commit 45b053a

File tree

2 files changed

+74
-4
lines changed

2 files changed

+74
-4
lines changed

mmv1/templates/terraform/constants/workbench_instance.go.tmpl

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ var WorkbenchInstanceSettableUnmodifiableDefaultMetadata = []string{
44
"report-notebook-metrics",
55
}
66

7+
var WorkbenchInstanceEUCSettableUnmodifiableDefaultMetadata = []string{
8+
"post-startup-script",
9+
"post-startup-script-behavior",
10+
}
11+
712
var WorkbenchInstanceEUCProvidedAdditionalMetadata = []string{
813
"enable-oslogin",
914
"disable-ssh",
1015
"ssh-keys",
1116
"block-project-ssh-keys",
12-
"post-startup-script",
13-
"post-startup-script-behavior",
1417
"startup-script",
1518
"startup-script-url",
1619
"gce-container-declaration",
@@ -95,6 +98,12 @@ func WorkbenchInstanceMetadataDiffSuppress(k, old, new string, d *schema.Resourc
9598
return true
9699
}
97100
}
101+
102+
for _, metadata := range WorkbenchInstanceEUCSettableUnmodifiableDefaultMetadata {
103+
if key == metadata && new == "" {
104+
return true
105+
}
106+
}
98107
}
99108

100109
for _, metadata := range WorkbenchInstanceSettableUnmodifiableDefaultMetadata {
@@ -284,7 +293,12 @@ func workbenchMetadataCustomizeDiff(_ context.Context, diff *schema.ResourceDiff
284293
oldMetadata := o.(map[string]interface{})
285294
newMetadata := n.(map[string]interface{})
286295

287-
for _, key := range WorkbenchInstanceSettableUnmodifiableDefaultMetadata {
296+
unmodifiableKeys := append([]string{}, WorkbenchInstanceSettableUnmodifiableDefaultMetadata...)
297+
if v, ok := diff.GetOk("enable_managed_euc"); ok && v.(bool) {
298+
unmodifiableKeys = append(unmodifiableKeys, WorkbenchInstanceEUCSettableUnmodifiableDefaultMetadata...)
299+
}
300+
301+
for _, key := range unmodifiableKeys {
288302
oldValue, oldOk := oldMetadata[key]
289303
newValue, newOk := newMetadata[key]
290304

@@ -304,4 +318,4 @@ func workbenchMetadataCustomizeDiff(_ context.Context, diff *schema.ResourceDiff
304318
}
305319
}
306320
return nil
307-
}
321+
}

mmv1/third_party/terraform/services/workbench/resource_workbench_instance_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
77

88
"github.com/hashicorp/terraform-provider-google/google/acctest"
9+
10+
"github.com/hashicorp/terraform-plugin-testing/plancheck"
911
)
1012

1113
func TestAccWorkbenchInstance_update(t *testing.T) {
@@ -877,3 +879,57 @@ resource "google_workbench_instance" "instance" {
877879
}
878880
`, context)
879881
}
882+
883+
func TestAccWorkbenchInstance_metadataEUCForceNew(t *testing.T) {
884+
t.Skip("Skipping until backend rollout completes which throws an error for non-allowlisted users, rather than silently dropping the key.")
885+
t.Parallel()
886+
887+
context := map[string]interface{}{
888+
"random_suffix": acctest.RandString(t, 10),
889+
}
890+
891+
acctest.VcrTest(t, resource.TestCase{
892+
PreCheck: func() { acctest.AccTestPreCheck(t) },
893+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
894+
CheckDestroy: testAccCheckWorkbenchInstanceDestroyProducer(t),
895+
Steps: []resource.TestStep{
896+
{
897+
Config: testAccWorkbenchInstance_metadataEUC(context, "old-script.sh"),
898+
},
899+
{
900+
Config: testAccWorkbenchInstance_metadataEUC(context, "new-script.sh"),
901+
ConfigPlanChecks: resource.ConfigPlanChecks{
902+
PreApply: []plancheck.PlanCheck{
903+
plancheck.ExpectResourceAction("google_workbench_instance.instance", plancheck.ResourceActionReplace),
904+
},
905+
},
906+
},
907+
},
908+
})
909+
}
910+
911+
func testAccWorkbenchInstance_metadataEUC(context map[string]interface{}, scriptName string) string {
912+
context["script_name"] = scriptName
913+
return acctest.Nprintf(`
914+
resource "google_workbench_instance" "instance" {
915+
name = "tf-test-workbench-%{random_suffix}"
916+
location = "us-central1-a"
917+
instance_owners = ["workbenche2etestota@gmail.com"]
918+
919+
gce_setup {
920+
machine_type = "n1-standard-1"
921+
vm_image {
922+
project = "cloud-notebooks-managed"
923+
family = "workbench-instances"
924+
}
925+
926+
metadata = {
927+
post-startup-script = "%{script_name}"
928+
post-startup-script-behavior = "run_once"
929+
}
930+
}
931+
932+
enable_managed_euc = true
933+
}
934+
`, context)
935+
}

0 commit comments

Comments
 (0)