From a966f6ceec33434af0b0650470f2afdb8c22bab4 Mon Sep 17 00:00:00 2001 From: Bhargav Chereddy Date: Thu, 31 Oct 2024 21:26:11 +0000 Subject: [PATCH 01/16] update documentation for instance_owners field --- mmv1/products/workbench/Instance.yaml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/mmv1/products/workbench/Instance.yaml b/mmv1/products/workbench/Instance.yaml index 06b7f5329bb9..6c453f60a76e 100644 --- a/mmv1/products/workbench/Instance.yaml +++ b/mmv1/products/workbench/Instance.yaml @@ -449,7 +449,9 @@ properties: description: | 'Optional. Input only. The owner of this instance after creation. Format: `alias@example.com` Currently supports one owner only. If not specified, all of - the service account users of your VM instance''s service account can use the instance.' + the service account users of your VM instance''s service account can use the instance. + Sets the access mode to `Single user`. For more details, see + https://cloud.google.com/vertex-ai/docs/workbench/instances/manage-access-jupyterlab' immutable: true ignore_read: true item_type: From 71e2553bb30862cd36f65391561efbdab996377f Mon Sep 17 00:00:00 2001 From: Bhargav Chereddy Date: Thu, 31 Oct 2024 21:30:33 +0000 Subject: [PATCH 02/16] update documentation for instance_owners field --- mmv1/products/workbench/Instance.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mmv1/products/workbench/Instance.yaml b/mmv1/products/workbench/Instance.yaml index 6c453f60a76e..0e26ad1412ae 100644 --- a/mmv1/products/workbench/Instance.yaml +++ b/mmv1/products/workbench/Instance.yaml @@ -450,7 +450,7 @@ properties: 'Optional. Input only. The owner of this instance after creation. Format: `alias@example.com` Currently supports one owner only. If not specified, all of the service account users of your VM instance''s service account can use the instance. - Sets the access mode to `Single user`. For more details, see + If specified, sets the access mode to `Single user`. For more details, see https://cloud.google.com/vertex-ai/docs/workbench/instances/manage-access-jupyterlab' immutable: true ignore_read: true From 6a7ddbb6136edde6cc58d74b66823b29cdcc2765 Mon Sep 17 00:00:00 2001 From: Bhargav Chereddy Date: Thu, 31 Oct 2024 21:34:55 +0000 Subject: [PATCH 03/16] lint fix --- mmv1/products/workbench/Instance.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mmv1/products/workbench/Instance.yaml b/mmv1/products/workbench/Instance.yaml index 0e26ad1412ae..846936c4235e 100644 --- a/mmv1/products/workbench/Instance.yaml +++ b/mmv1/products/workbench/Instance.yaml @@ -450,7 +450,7 @@ properties: 'Optional. Input only. The owner of this instance after creation. Format: `alias@example.com` Currently supports one owner only. If not specified, all of the service account users of your VM instance''s service account can use the instance. - If specified, sets the access mode to `Single user`. For more details, see + If specified, sets the access mode to `Single user`. For more details, see https://cloud.google.com/vertex-ai/docs/workbench/instances/manage-access-jupyterlab' immutable: true ignore_read: true From 268e2ae980e9dee415ef5b4ffddc534297eda350 Mon Sep 17 00:00:00 2001 From: Bhargav Chereddy Date: Thu, 5 Dec 2024 21:50:43 +0000 Subject: [PATCH 04/16] gcs-data-bucket is now modifiable by the customer --- mmv1/templates/terraform/constants/workbench_instance.go.tmpl | 1 - 1 file changed, 1 deletion(-) diff --git a/mmv1/templates/terraform/constants/workbench_instance.go.tmpl b/mmv1/templates/terraform/constants/workbench_instance.go.tmpl index 19a572094435..b9091e398358 100644 --- a/mmv1/templates/terraform/constants/workbench_instance.go.tmpl +++ b/mmv1/templates/terraform/constants/workbench_instance.go.tmpl @@ -42,7 +42,6 @@ var WorkbenchInstanceProvidedMetadata = []string{ "dataproc-service-account", "disable-check-xsrf", "framework", - "gcs-data-bucket", "generate-diagnostics-bucket", "generate-diagnostics-file", "generate-diagnostics-options", From c9e07e691abb1d0ea011a2cb5b676120bf2995da Mon Sep 17 00:00:00 2001 From: Bhargav Chereddy Date: Wed, 18 Dec 2024 19:02:19 +0000 Subject: [PATCH 05/16] Add suport for Third Party Identity --- mmv1/products/workbench/Instance.yaml | 5 +++++ .../terraform/examples/workbench_instance_full.tf.tmpl | 2 ++ 2 files changed, 7 insertions(+) diff --git a/mmv1/products/workbench/Instance.yaml b/mmv1/products/workbench/Instance.yaml index 846936c4235e..f70caa8cefca 100644 --- a/mmv1/products/workbench/Instance.yaml +++ b/mmv1/products/workbench/Instance.yaml @@ -549,3 +549,8 @@ properties: Optional. Labels to apply to this instance. These can be later modified by the UpdateInstance method. diff_suppress_func: 'WorkbenchInstanceLabelsDiffSuppress' + - name: 'enableThirdPartyIdentity' + type: Boolean + description: | + Flag that specifies that a notebook can be accessed with third party + identity provider. diff --git a/mmv1/templates/terraform/examples/workbench_instance_full.tf.tmpl b/mmv1/templates/terraform/examples/workbench_instance_full.tf.tmpl index 2252af59b8e1..97f8ee97306e 100644 --- a/mmv1/templates/terraform/examples/workbench_instance_full.tf.tmpl +++ b/mmv1/templates/terraform/examples/workbench_instance_full.tf.tmpl @@ -88,4 +88,6 @@ resource "google_workbench_instance" "{{$.PrimaryResourceId}}" { desired_state = "ACTIVE" + enable_third_party_identity = "true" + } From e21b14e4b0456cf622412ce5635fca12ca180a75 Mon Sep 17 00:00:00 2001 From: Bhargav Chereddy Date: Wed, 29 Jan 2025 05:01:43 +0000 Subject: [PATCH 06/16] draft --- mmv1/products/colab/Runtime.yaml | 22 +++++++++++++ .../examples/colab_runtime_full.tf.tmpl | 2 ++ .../examples/colab_runtime_stopped.tf.tmpl | 31 +++++++++++++++++++ .../terraform/post_create/colab_runtime.tmpl | 6 ++++ 4 files changed, 61 insertions(+) create mode 100644 mmv1/templates/terraform/examples/colab_runtime_stopped.tf.tmpl create mode 100644 mmv1/templates/terraform/post_create/colab_runtime.tmpl diff --git a/mmv1/products/colab/Runtime.yaml b/mmv1/products/colab/Runtime.yaml index e44e0cb41d15..cac98ca41ff7 100644 --- a/mmv1/products/colab/Runtime.yaml +++ b/mmv1/products/colab/Runtime.yaml @@ -30,6 +30,7 @@ async: full_url: 'https://{{location}}-aiplatform.googleapis.com/v1/{{op_id}}' custom_code: encoder: 'templates/terraform/encoders/colab_runtime.go.tmpl' + post_create: 'templates/terraform/post_create/colab_runtime.go.tmpl' examples: - name: 'colab_runtime_basic' primary_resource_id: 'runtime' @@ -37,6 +38,14 @@ examples: region_override: 'us-central1' vars: runtime_name: 'colab-runtime' + - name: 'colab_runtime_stopped' + primary_resource_id: 'runtime' + primary_resource_name: 'fmt.Sprintf("tf-test-colab-runtime%s", context["random_suffix"])' + region_override: 'us-central1' + vars: + runtime_name: 'colab-runtime' + ignore_read_extra: + - 'desired_state' - name: 'colab_runtime_full' primary_resource_id: 'runtime' primary_resource_name: 'fmt.Sprintf("tf-test-colab-runtime%s", context["random_suffix"])' @@ -46,6 +55,14 @@ examples: key_name: 'my-crypto-key' test_vars_overrides: key_name: 'acctest.BootstrapKMSKeyInLocation(t, "us-central1").CryptoKey.Name' + ignore_read_extra: + - 'desired_state' +virtual_fields: + - name: 'desired_state' + description: | + Desired state of the Colab Runtime. Set this field to `ACTIVE` to start the runtime, and `STOPPED` to stop it. + type: String + default_from_api: true parameters: - name: 'location' type: String @@ -79,3 +96,8 @@ properties: - name: description type: String description: 'The description of the Runtime.' + - name: state + type: String + description: | + Output only. The state of the runtime. + output: true diff --git a/mmv1/templates/terraform/examples/colab_runtime_full.tf.tmpl b/mmv1/templates/terraform/examples/colab_runtime_full.tf.tmpl index 661364d9e4eb..54859ecd1314 100644 --- a/mmv1/templates/terraform/examples/colab_runtime_full.tf.tmpl +++ b/mmv1/templates/terraform/examples/colab_runtime_full.tf.tmpl @@ -53,6 +53,8 @@ resource "google_colab_runtime" "{{$.PrimaryResourceId}}" { runtime_user = "gterraformtestuser@gmail.com" description = "Full runtime" + desired_state = "ACTIVE" + depends_on = [ google_colab_runtime_template.my_template ] diff --git a/mmv1/templates/terraform/examples/colab_runtime_stopped.tf.tmpl b/mmv1/templates/terraform/examples/colab_runtime_stopped.tf.tmpl new file mode 100644 index 000000000000..e7d9fa469f1b --- /dev/null +++ b/mmv1/templates/terraform/examples/colab_runtime_stopped.tf.tmpl @@ -0,0 +1,31 @@ +resource "google_colab_runtime_template" "my_template" { + name = "{{index $.Vars "runtime_name"}}" + display_name = "Runtime template basic" + location = "us-central1" + + machine_spec { + machine_type = "e2-standard-4" + } + + network_spec { + enable_internet_access = true + } +} + +resource "google_colab_runtime" "{{$.PrimaryResourceId}}" { + name = "{{index $.Vars "runtime_name"}}" + location = "us-central1" + + notebook_runtime_template_ref { + notebook_runtime_template = google_colab_runtime_template.my_template.id + } + + desired_state = "STOPPED" + + display_name = "Runtime stopped" + runtime_user = "gterraformtestuser@gmail.com" + + depends_on = [ + google_colab_runtime_template.my_template, + ] +} diff --git a/mmv1/templates/terraform/post_create/colab_runtime.tmpl b/mmv1/templates/terraform/post_create/colab_runtime.tmpl new file mode 100644 index 000000000000..0ba4b91d1675 --- /dev/null +++ b/mmv1/templates/terraform/post_create/colab_runtime.tmpl @@ -0,0 +1,6 @@ +if p, ok := d.GetOk("desired_state"); ok && p.(string) == "PAUSED" { + _, err := modifyColabRuntimeState(config, d, project, billingProject, userAgent, "pause") + if err != nil { + return err + } +} From 54fcc2558f6a51c72f08488ff3e265112c11a093 Mon Sep 17 00:00:00 2001 From: Bhargav Chereddy Date: Wed, 29 Jan 2025 06:07:10 +0000 Subject: [PATCH 07/16] remove workbench changes --- mmv1/products/workbench/Instance.yaml | 5 ----- .../terraform/examples/workbench_instance_full.tf.tmpl | 2 -- 2 files changed, 7 deletions(-) diff --git a/mmv1/products/workbench/Instance.yaml b/mmv1/products/workbench/Instance.yaml index b6e18b678194..94d727e1e0e9 100644 --- a/mmv1/products/workbench/Instance.yaml +++ b/mmv1/products/workbench/Instance.yaml @@ -543,8 +543,3 @@ properties: Optional. Labels to apply to this instance. These can be later modified by the UpdateInstance method. diff_suppress_func: 'WorkbenchInstanceLabelsDiffSuppress' - - name: 'enableThirdPartyIdentity' - type: Boolean - description: | - Flag that specifies that a notebook can be accessed with third party - identity provider. diff --git a/mmv1/templates/terraform/examples/workbench_instance_full.tf.tmpl b/mmv1/templates/terraform/examples/workbench_instance_full.tf.tmpl index 97f8ee97306e..2252af59b8e1 100644 --- a/mmv1/templates/terraform/examples/workbench_instance_full.tf.tmpl +++ b/mmv1/templates/terraform/examples/workbench_instance_full.tf.tmpl @@ -88,6 +88,4 @@ resource "google_workbench_instance" "{{$.PrimaryResourceId}}" { desired_state = "ACTIVE" - enable_third_party_identity = "true" - } From 1e9729e38c669e5739076cf7273f3250a2543b7a Mon Sep 17 00:00:00 2001 From: Bhargav Chereddy Date: Wed, 29 Jan 2025 17:34:41 +0000 Subject: [PATCH 08/16] draft --- mmv1/templates/terraform/post_create/colab_runtime.go.tmpl | 6 ++++++ mmv1/templates/terraform/post_create/colab_runtime.tmpl | 4 ++-- ...runtime_template.tmpl => colab_runtime_template.go.tmpl} | 0 3 files changed, 8 insertions(+), 2 deletions(-) create mode 100644 mmv1/templates/terraform/post_create/colab_runtime.go.tmpl rename mmv1/templates/terraform/post_create/{colab_runtime_template.tmpl => colab_runtime_template.go.tmpl} (100%) diff --git a/mmv1/templates/terraform/post_create/colab_runtime.go.tmpl b/mmv1/templates/terraform/post_create/colab_runtime.go.tmpl new file mode 100644 index 000000000000..6294586e63d3 --- /dev/null +++ b/mmv1/templates/terraform/post_create/colab_runtime.go.tmpl @@ -0,0 +1,6 @@ +if p, ok := d.GetOk("desired_state"); ok && p.(string) == "STOPPED" { + _, err := modifyColabRuntimeState(config, d, project, billingProject, userAgent, "pause") + if err != nil { + return err + } +} diff --git a/mmv1/templates/terraform/post_create/colab_runtime.tmpl b/mmv1/templates/terraform/post_create/colab_runtime.tmpl index 0ba4b91d1675..20d530557719 100644 --- a/mmv1/templates/terraform/post_create/colab_runtime.tmpl +++ b/mmv1/templates/terraform/post_create/colab_runtime.tmpl @@ -1,5 +1,5 @@ -if p, ok := d.GetOk("desired_state"); ok && p.(string) == "PAUSED" { - _, err := modifyColabRuntimeState(config, d, project, billingProject, userAgent, "pause") +if p, ok := d.GetOk("desired_state"); ok && p.(string) == "STOPPED" { + _, err := modifyColabRuntimeState(config, d, project, billingProject, userAgent, "stop") if err != nil { return err } diff --git a/mmv1/templates/terraform/post_create/colab_runtime_template.tmpl b/mmv1/templates/terraform/post_create/colab_runtime_template.go.tmpl similarity index 100% rename from mmv1/templates/terraform/post_create/colab_runtime_template.tmpl rename to mmv1/templates/terraform/post_create/colab_runtime_template.go.tmpl From a1e848bc4abeb7de48109c9cf56e63fc7b491bf2 Mon Sep 17 00:00:00 2001 From: Bhargav Chereddy Date: Thu, 30 Jan 2025 07:46:45 +0000 Subject: [PATCH 09/16] draft --- mmv1/products/colab/Runtime.yaml | 1 + mmv1/products/colab/RuntimeTemplate.yaml | 2 +- .../terraform/constants/colab_runtime.go.tmpl | 31 +++++++++++++++++++ .../post_create/colab_runtime.go.tmpl | 5 ++- .../terraform/post_create/colab_runtime.tmpl | 6 ---- 5 files changed, 37 insertions(+), 8 deletions(-) create mode 100644 mmv1/templates/terraform/constants/colab_runtime.go.tmpl delete mode 100644 mmv1/templates/terraform/post_create/colab_runtime.tmpl diff --git a/mmv1/products/colab/Runtime.yaml b/mmv1/products/colab/Runtime.yaml index cac98ca41ff7..61ebbd813eda 100644 --- a/mmv1/products/colab/Runtime.yaml +++ b/mmv1/products/colab/Runtime.yaml @@ -31,6 +31,7 @@ async: custom_code: encoder: 'templates/terraform/encoders/colab_runtime.go.tmpl' post_create: 'templates/terraform/post_create/colab_runtime.go.tmpl' + constants: 'templates/terraform/constants/colab_runtime.go.tmpl' examples: - name: 'colab_runtime_basic' primary_resource_id: 'runtime' diff --git a/mmv1/products/colab/RuntimeTemplate.yaml b/mmv1/products/colab/RuntimeTemplate.yaml index 01528df0facd..8ac1699825c1 100644 --- a/mmv1/products/colab/RuntimeTemplate.yaml +++ b/mmv1/products/colab/RuntimeTemplate.yaml @@ -41,7 +41,7 @@ iam_policy: - 'projects/{{project}}/locations/{{location}}/notebookRuntimeTemplates/{{runtime_template}}' - '{{runtime_template}}' custom_code: - post_create: 'templates/terraform/post_create/colab_runtime_template.tmpl' + post_create: 'templates/terraform/post_create/colab_runtime_template.go.tmpl' examples: - name: 'colab_runtime_template_basic' primary_resource_id: 'runtime-template' diff --git a/mmv1/templates/terraform/constants/colab_runtime.go.tmpl b/mmv1/templates/terraform/constants/colab_runtime.go.tmpl new file mode 100644 index 000000000000..423f2df216fa --- /dev/null +++ b/mmv1/templates/terraform/constants/colab_runtime.go.tmpl @@ -0,0 +1,31 @@ +func modifyColabRuntimeState(config *transport_tpg.Config, d *schema.ResourceData, project string, billingProject string, userAgent string, state string) (map[string]interface{}, error) { + url, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}ColabBasePath{{"}}"}}projects/{{"{{"}}project{{"}}"}}/locations/{{"{{"}}location{{"}}"}}/notebookRuntimes/{{"{{"}}name{{"}}"}}:"+state) + if err != nil { + return nil, err + } + + res, err := transport_tpg.SendRequest(transport_tpg.SendRequestOptions{ + Config: config, + Method: "POST", + Project: billingProject, + RawURL: url, + UserAgent: userAgent, + }) + if err != nil { + return nil, fmt.Errorf("Unable to %q google_colab_runtime %q: %s", state, d.Id(), err) + } + return res, nil +} + +{{- if ne $.Compiler "terraformgoogleconversion-codegen" }} +func waitForColabOperation(config *transport_tpg.Config, d *schema.ResourceData, project string, billingProject string, userAgent string, response map[string]interface{}) error { + var opRes map[string]interface{} + err := ColabOperationWaitTimeWithResponse( + config, response, &opRes, project, "Modifying Colab Runtime state", userAgent, + d.Timeout(schema.TimeoutUpdate)) + if err != nil { + return err + } + return nil +} +{{- end }} diff --git a/mmv1/templates/terraform/post_create/colab_runtime.go.tmpl b/mmv1/templates/terraform/post_create/colab_runtime.go.tmpl index 6294586e63d3..7e5de36aa81f 100644 --- a/mmv1/templates/terraform/post_create/colab_runtime.go.tmpl +++ b/mmv1/templates/terraform/post_create/colab_runtime.go.tmpl @@ -1,6 +1,9 @@ if p, ok := d.GetOk("desired_state"); ok && p.(string) == "STOPPED" { - _, err := modifyColabRuntimeState(config, d, project, billingProject, userAgent, "pause") + dRes, err := modifyColabRuntimeState(config, d, project, billingProject, userAgent, "stop") if err != nil { return err } + if err := waitForColabOperation(config, d, project, billingProject, userAgent, dRes); err != nil { + return fmt.Errorf("Error stopping Colab runtime: %s", err) + } } diff --git a/mmv1/templates/terraform/post_create/colab_runtime.tmpl b/mmv1/templates/terraform/post_create/colab_runtime.tmpl deleted file mode 100644 index 20d530557719..000000000000 --- a/mmv1/templates/terraform/post_create/colab_runtime.tmpl +++ /dev/null @@ -1,6 +0,0 @@ -if p, ok := d.GetOk("desired_state"); ok && p.(string) == "STOPPED" { - _, err := modifyColabRuntimeState(config, d, project, billingProject, userAgent, "stop") - if err != nil { - return err - } -} From 9e3cfd9e04205aff9b41f45a3e36e58c55e651ed Mon Sep 17 00:00:00 2001 From: Bhargav Chereddy Date: Thu, 30 Jan 2025 08:28:20 +0000 Subject: [PATCH 10/16] Add update test --- mmv1/products/colab/Runtime.yaml | 1 + .../pre_update/colab_runtime.go.tmpl | 27 ++ .../colab/resource_colab_runtime_test.go | 249 ++++++++++++++++++ 3 files changed, 277 insertions(+) create mode 100644 mmv1/templates/terraform/pre_update/colab_runtime.go.tmpl create mode 100644 mmv1/third_party/terraform/services/colab/resource_colab_runtime_test.go diff --git a/mmv1/products/colab/Runtime.yaml b/mmv1/products/colab/Runtime.yaml index 61ebbd813eda..0396e6ce79bf 100644 --- a/mmv1/products/colab/Runtime.yaml +++ b/mmv1/products/colab/Runtime.yaml @@ -31,6 +31,7 @@ async: custom_code: encoder: 'templates/terraform/encoders/colab_runtime.go.tmpl' post_create: 'templates/terraform/post_create/colab_runtime.go.tmpl' + pre_update: 'templates/terraform/pre_update/colab_runtime.go.tmpl' constants: 'templates/terraform/constants/colab_runtime.go.tmpl' examples: - name: 'colab_runtime_basic' diff --git a/mmv1/templates/terraform/pre_update/colab_runtime.go.tmpl b/mmv1/templates/terraform/pre_update/colab_runtime.go.tmpl new file mode 100644 index 000000000000..84e84ecbc7ec --- /dev/null +++ b/mmv1/templates/terraform/pre_update/colab_runtime.go.tmpl @@ -0,0 +1,27 @@ +name := d.Get("name").(string) +state := d.Get("state").(string) +desired_state := d.Get("desired_state").(string) + +if desired_state != "" && state != desired_state { + var verb string + + switch desired_state { + case "STOPPED": + verb = "stop" + case "ACTIVE": + verb = "start" + default: + return fmt.Errorf("desired_state has to be ACTIVE or STOPPED") + } + + dRes, err := modifyColabRuntimeState(config, d, project, billingProject, userAgent, verb) + if err != nil { + return err + } + if err := waitForColabOperation(config, d, project, billingProject, userAgent, dRes); err != nil { + return fmt.Errorf("Error stopping Colab runtime: %s", err) + } + +} else { + log.Printf("[DEBUG] Colab runtime %q has state %q.", name, state) +} diff --git a/mmv1/third_party/terraform/services/colab/resource_colab_runtime_test.go b/mmv1/third_party/terraform/services/colab/resource_colab_runtime_test.go new file mode 100644 index 000000000000..eee948b800ed --- /dev/null +++ b/mmv1/third_party/terraform/services/colab/resource_colab_runtime_test.go @@ -0,0 +1,249 @@ +package colab_test + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-testing/helper/resource" + + "github.com/hashicorp/terraform-provider-google/google/acctest" +) + +func TestAccColabRuntime_update(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "key_name": acctest.BootstrapKMSKeyInLocation(t, "us-central1").CryptoKey.Name, + "random_suffix": acctest.RandString(t, 10), + } + + acctest.VcrTest(t, resource.TestCase{ + PreCheck: func() { acctest.AccTestPreCheck(t) }, + ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), + CheckDestroy: testAccCheckColabRuntimeDestroyProducer(t), + Steps: []resource.TestStep{ + { + Config: testAccColabRuntime_full(context), + }, + { + ResourceName: "google_colab_runtime.runtime", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"desired_state", "location", "name"}, + }, + { + Config: testAccColabRuntime_no_state(context), + }, + { + ResourceName: "google_colab_runtime.runtime", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"desired_state", "location", "name"}, + }, + { + Config: testAccColabRuntime_stopped(context), + }, + { + ResourceName: "google_colab_runtime.runtime", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"desired_state", "location", "name"}, + }, + }, + }) +} + +func testAccColabRuntime_full(context map[string]interface{}) string { + return acctest.Nprintf(` +resource "google_colab_runtime_template" "my_template" { + name = "tf-test-colab-runtime%{random_suffix}" + display_name = "Runtime template full" + location = "us-central1" + description = "Full runtime template" + machine_spec { + machine_type = "n1-standard-2" + accelerator_type = "NVIDIA_TESLA_T4" + accelerator_count = "1" + } + + data_persistent_disk_spec { + disk_type = "pd-standard" + disk_size_gb = 200 + } + + network_spec { + enable_internet_access = true + } + + labels = { + k = "val" + } + + idle_shutdown_config { + idle_timeout = "3600s" + } + + euc_config { + euc_disabled = true + } + + shielded_vm_config { + enable_secure_boot = true + } + + network_tags = ["abc", "def"] + + encryption_spec { + kms_key_name = "%{key_name}" + } +} + +resource "google_colab_runtime" "runtime" { + name = "tf-test-colab-runtime%{random_suffix}" + location = "us-central1" + + notebook_runtime_template_ref { + notebook_runtime_template = google_colab_runtime_template.my_template.id + } + + display_name = "Runtime full" + runtime_user = "gterraformtestuser@gmail.com" + description = "Full runtime" + + desired_state = "ACTIVE" + + depends_on = [ + google_colab_runtime_template.my_template + ] +} +`, context) +} + +func testAccColabRuntime_stopped(context map[string]interface{}) string { + return acctest.Nprintf(` +resource "google_colab_runtime_template" "my_template" { + name = "tf-test-colab-runtime%{random_suffix}" + display_name = "Runtime template full" + location = "us-central1" + description = "Full runtime template" + machine_spec { + machine_type = "n1-standard-2" + accelerator_type = "NVIDIA_TESLA_T4" + accelerator_count = "1" + } + + data_persistent_disk_spec { + disk_type = "pd-standard" + disk_size_gb = 200 + } + + network_spec { + enable_internet_access = true + } + + labels = { + k = "val" + } + + idle_shutdown_config { + idle_timeout = "3600s" + } + + euc_config { + euc_disabled = true + } + + shielded_vm_config { + enable_secure_boot = true + } + + network_tags = ["abc", "def"] + + encryption_spec { + kms_key_name = "%{key_name}" + } +} + +resource "google_colab_runtime" "runtime" { + name = "tf-test-colab-runtime%{random_suffix}" + location = "us-central1" + + notebook_runtime_template_ref { + notebook_runtime_template = google_colab_runtime_template.my_template.id + } + + display_name = "Runtime full" + runtime_user = "gterraformtestuser@gmail.com" + description = "Full runtime" + + desired_state = "STOPPED" + + depends_on = [ + google_colab_runtime_template.my_template + ] +} +`, context) +} + +func testAccColabRuntime_no_state(context map[string]interface{}) string { + return acctest.Nprintf(` +resource "google_colab_runtime_template" "my_template" { + name = "tf-test-colab-runtime%{random_suffix}" + display_name = "Runtime template full" + location = "us-central1" + description = "Full runtime template" + machine_spec { + machine_type = "n1-standard-2" + accelerator_type = "NVIDIA_TESLA_T4" + accelerator_count = "1" + } + + data_persistent_disk_spec { + disk_type = "pd-standard" + disk_size_gb = 200 + } + + network_spec { + enable_internet_access = true + } + + labels = { + k = "val" + } + + idle_shutdown_config { + idle_timeout = "3600s" + } + + euc_config { + euc_disabled = true + } + + shielded_vm_config { + enable_secure_boot = true + } + + network_tags = ["abc", "def"] + + encryption_spec { + kms_key_name = "%{key_name}" + } +} + +resource "google_colab_runtime" "runtime" { + name = "tf-test-colab-runtime%{random_suffix}" + location = "us-central1" + + notebook_runtime_template_ref { + notebook_runtime_template = google_colab_runtime_template.my_template.id + } + + display_name = "Runtime full" + runtime_user = "gterraformtestuser@gmail.com" + description = "Full runtime" + + depends_on = [ + google_colab_runtime_template.my_template + ] +} +`, context) +} From ee87f59b28920a11c93da5f6dcb9e6e3df6d290d Mon Sep 17 00:00:00 2001 From: Bhargav Chereddy Date: Thu, 30 Jan 2025 10:50:55 +0000 Subject: [PATCH 11/16] working example --- mmv1/products/colab/Runtime.yaml | 11 +++++++--- .../colab_runtime.go.tmpl | 20 +++++++++++++++++-- .../colab/resource_colab_runtime_test.go | 6 ++++++ 3 files changed, 32 insertions(+), 5 deletions(-) rename mmv1/templates/terraform/{pre_update => custom_update}/colab_runtime.go.tmpl (62%) diff --git a/mmv1/products/colab/Runtime.yaml b/mmv1/products/colab/Runtime.yaml index 0396e6ce79bf..30ddf74a2add 100644 --- a/mmv1/products/colab/Runtime.yaml +++ b/mmv1/products/colab/Runtime.yaml @@ -31,7 +31,7 @@ async: custom_code: encoder: 'templates/terraform/encoders/colab_runtime.go.tmpl' post_create: 'templates/terraform/post_create/colab_runtime.go.tmpl' - pre_update: 'templates/terraform/pre_update/colab_runtime.go.tmpl' + custom_update: 'templates/terraform/custom_update/colab_runtime.go.tmpl' constants: 'templates/terraform/constants/colab_runtime.go.tmpl' examples: - name: 'colab_runtime_basic' @@ -62,9 +62,9 @@ examples: virtual_fields: - name: 'desired_state' description: | - Desired state of the Colab Runtime. Set this field to `ACTIVE` to start the runtime, and `STOPPED` to stop it. + Desired state of the Colab Runtime. Set this field to `RUNNING` to start the runtime, and `STOPPED` to stop it. type: String - default_from_api: true + default_value: "RUNNING" parameters: - name: 'location' type: String @@ -75,6 +75,7 @@ parameters: type: String url_param_only: true description: 'The resource name of the Runtime' + immutable: true properties: - name: notebookRuntimeTemplateRef type: NestedObject @@ -83,20 +84,24 @@ properties: properties: - name: 'notebookRuntimeTemplate' type: String + immutable: true required: true description: 'The resource name of the NotebookRuntimeTemplate based on which a NotebookRuntime will be created.' diff_suppress_func: 'tpgresource.ProjectNumberDiffSuppress' - name: 'runtimeUser' type: String required: true + immutable: true description: 'The user email of the NotebookRuntime.' - name: 'displayName' type: String + immutable: true description: Required. The display name of the Runtime. required: true - name: description type: String + immutable: true description: 'The description of the Runtime.' - name: state type: String diff --git a/mmv1/templates/terraform/pre_update/colab_runtime.go.tmpl b/mmv1/templates/terraform/custom_update/colab_runtime.go.tmpl similarity index 62% rename from mmv1/templates/terraform/pre_update/colab_runtime.go.tmpl rename to mmv1/templates/terraform/custom_update/colab_runtime.go.tmpl index 84e84ecbc7ec..f6680d3dc624 100644 --- a/mmv1/templates/terraform/pre_update/colab_runtime.go.tmpl +++ b/mmv1/templates/terraform/custom_update/colab_runtime.go.tmpl @@ -2,16 +2,31 @@ name := d.Get("name").(string) state := d.Get("state").(string) desired_state := d.Get("desired_state").(string) +project, err := tpgresource.GetProject(d, config) +if err != nil { + return err +} + +userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent) +if err != nil { + return err +} + +billingProject := "" +if bp, err := tpgresource.GetBillingProject(d, config); err == nil { + billingProject = bp +} + if desired_state != "" && state != desired_state { var verb string switch desired_state { case "STOPPED": verb = "stop" - case "ACTIVE": + case "RUNNING": verb = "start" default: - return fmt.Errorf("desired_state has to be ACTIVE or STOPPED") + return fmt.Errorf("desired_state has to be RUNNING or STOPPED") } dRes, err := modifyColabRuntimeState(config, d, project, billingProject, userAgent, verb) @@ -25,3 +40,4 @@ if desired_state != "" && state != desired_state { } else { log.Printf("[DEBUG] Colab runtime %q has state %q.", name, state) } +return nil \ No newline at end of file diff --git a/mmv1/third_party/terraform/services/colab/resource_colab_runtime_test.go b/mmv1/third_party/terraform/services/colab/resource_colab_runtime_test.go index eee948b800ed..a1c06a7bc097 100644 --- a/mmv1/third_party/terraform/services/colab/resource_colab_runtime_test.go +++ b/mmv1/third_party/terraform/services/colab/resource_colab_runtime_test.go @@ -4,6 +4,7 @@ import ( "testing" "github.com/hashicorp/terraform-plugin-testing/helper/resource" + "github.com/hashicorp/terraform-plugin-testing/plancheck" "github.com/hashicorp/terraform-provider-google/google/acctest" ) @@ -41,6 +42,11 @@ func TestAccColabRuntime_update(t *testing.T) { }, { Config: testAccColabRuntime_stopped(context), + ConfigPlanChecks: resource.ConfigPlanChecks{ + PreApply: []plancheck.PlanCheck{ + plancheck.ExpectResourceAction("google_colab_runtime.runtime", plancheck.ResourceActionUpdate), + }, + }, }, { ResourceName: "google_colab_runtime.runtime", From 884938cfd91545cb689aed46850a8d2b9dada09d Mon Sep 17 00:00:00 2001 From: Bhargav Chereddy Date: Thu, 30 Jan 2025 11:50:24 +0000 Subject: [PATCH 12/16] Also make the runtime upgradable --- mmv1/products/colab/Runtime.yaml | 4 ++++ .../terraform/constants/colab_runtime.go.tmpl | 19 +++++++++++++++---- .../custom_update/colab_runtime.go.tmpl | 14 ++++++++------ .../post_create/colab_runtime.go.tmpl | 6 +----- 4 files changed, 28 insertions(+), 15 deletions(-) diff --git a/mmv1/products/colab/Runtime.yaml b/mmv1/products/colab/Runtime.yaml index 30ddf74a2add..09dea299be1c 100644 --- a/mmv1/products/colab/Runtime.yaml +++ b/mmv1/products/colab/Runtime.yaml @@ -65,6 +65,10 @@ virtual_fields: Desired state of the Colab Runtime. Set this field to `RUNNING` to start the runtime, and `STOPPED` to stop it. type: String default_value: "RUNNING" + - name: 'upgrade_runtime' + description: | + Triggers an upgrade when flipped from false to true + type: Boolean parameters: - name: 'location' type: String diff --git a/mmv1/templates/terraform/constants/colab_runtime.go.tmpl b/mmv1/templates/terraform/constants/colab_runtime.go.tmpl index 423f2df216fa..f9edbbb009cc 100644 --- a/mmv1/templates/terraform/constants/colab_runtime.go.tmpl +++ b/mmv1/templates/terraform/constants/colab_runtime.go.tmpl @@ -1,5 +1,5 @@ -func modifyColabRuntimeState(config *transport_tpg.Config, d *schema.ResourceData, project string, billingProject string, userAgent string, state string) (map[string]interface{}, error) { - url, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}ColabBasePath{{"}}"}}projects/{{"{{"}}project{{"}}"}}/locations/{{"{{"}}location{{"}}"}}/notebookRuntimes/{{"{{"}}name{{"}}"}}:"+state) +func ModifyColabRuntimeOperation(config *transport_tpg.Config, d *schema.ResourceData, project string, billingProject string, userAgent string, method string) (map[string]interface{}, error) { + url, err := tpgresource.ReplaceVars(d, config, "{{"{{"}}ColabBasePath{{"}}"}}projects/{{"{{"}}project{{"}}"}}/locations/{{"{{"}}location{{"}}"}}/notebookRuntimes/{{"{{"}}name{{"}}"}}:"+method) if err != nil { return nil, err } @@ -12,7 +12,7 @@ func modifyColabRuntimeState(config *transport_tpg.Config, d *schema.ResourceDat UserAgent: userAgent, }) if err != nil { - return nil, fmt.Errorf("Unable to %q google_colab_runtime %q: %s", state, d.Id(), err) + return nil, fmt.Errorf("Unable to %q google_colab_runtime %q: %s", method, d.Id(), err) } return res, nil } @@ -21,11 +21,22 @@ func modifyColabRuntimeState(config *transport_tpg.Config, d *schema.ResourceDat func waitForColabOperation(config *transport_tpg.Config, d *schema.ResourceData, project string, billingProject string, userAgent string, response map[string]interface{}) error { var opRes map[string]interface{} err := ColabOperationWaitTimeWithResponse( - config, response, &opRes, project, "Modifying Colab Runtime state", userAgent, + config, response, &opRes, project, "Waiting for Colab Runtime Operation", userAgent, d.Timeout(schema.TimeoutUpdate)) if err != nil { return err } return nil } + +func ModifyColabRuntime(config *transport_tpg.Config, d *schema.ResourceData, project string, billingProject string, userAgent string, method string) error { + dRes, err := ModifyColabRuntimeOperation(config, d, project, billingProject, userAgent, method) + if err != nil { + return err + } + if err := waitForColabOperation(config, d, project, billingProject, userAgent, dRes); err != nil { + return fmt.Errorf("Error with Colab runtime method: %s", err) + } + return nil +} {{- end }} diff --git a/mmv1/templates/terraform/custom_update/colab_runtime.go.tmpl b/mmv1/templates/terraform/custom_update/colab_runtime.go.tmpl index f6680d3dc624..7bcf066a3b79 100644 --- a/mmv1/templates/terraform/custom_update/colab_runtime.go.tmpl +++ b/mmv1/templates/terraform/custom_update/colab_runtime.go.tmpl @@ -29,15 +29,17 @@ if desired_state != "" && state != desired_state { return fmt.Errorf("desired_state has to be RUNNING or STOPPED") } - dRes, err := modifyColabRuntimeState(config, d, project, billingProject, userAgent, verb) - if err != nil { + if err := ModifyColabRuntime(config, d, project, billingProject, userAgent, verb); err != nil { return err } - if err := waitForColabOperation(config, d, project, billingProject, userAgent, dRes); err != nil { - return fmt.Errorf("Error stopping Colab runtime: %s", err) - } } else { log.Printf("[DEBUG] Colab runtime %q has state %q.", name, state) } -return nil \ No newline at end of file + +if d.Get("upgrade_runtime").(bool) { + if err := ModifyColabRuntime(config, d, project, billingProject, userAgent, "upgrade"); err != nil { + return err + } +} +return nil diff --git a/mmv1/templates/terraform/post_create/colab_runtime.go.tmpl b/mmv1/templates/terraform/post_create/colab_runtime.go.tmpl index 7e5de36aa81f..d6675db2b3ef 100644 --- a/mmv1/templates/terraform/post_create/colab_runtime.go.tmpl +++ b/mmv1/templates/terraform/post_create/colab_runtime.go.tmpl @@ -1,9 +1,5 @@ if p, ok := d.GetOk("desired_state"); ok && p.(string) == "STOPPED" { - dRes, err := modifyColabRuntimeState(config, d, project, billingProject, userAgent, "stop") - if err != nil { + if err := ModifyColabRuntime(config, d, project, billingProject, userAgent, "stop"); err != nil { return err } - if err := waitForColabOperation(config, d, project, billingProject, userAgent, dRes); err != nil { - return fmt.Errorf("Error stopping Colab runtime: %s", err) - } } From f343fc7fe36108f1c22ada40fd46d295fea12122 Mon Sep 17 00:00:00 2001 From: Bhargav Chereddy Date: Thu, 30 Jan 2025 22:28:26 +0000 Subject: [PATCH 13/16] remove upgrade changes --- mmv1/products/colab/Runtime.yaml | 4 ---- .../terraform/custom_update/colab_runtime.go.tmpl | 5 ----- .../terraform/post_create/colab_runtime_template.tmpl | 10 ++++++++++ 3 files changed, 10 insertions(+), 9 deletions(-) create mode 100644 mmv1/templates/terraform/post_create/colab_runtime_template.tmpl diff --git a/mmv1/products/colab/Runtime.yaml b/mmv1/products/colab/Runtime.yaml index 09dea299be1c..30ddf74a2add 100644 --- a/mmv1/products/colab/Runtime.yaml +++ b/mmv1/products/colab/Runtime.yaml @@ -65,10 +65,6 @@ virtual_fields: Desired state of the Colab Runtime. Set this field to `RUNNING` to start the runtime, and `STOPPED` to stop it. type: String default_value: "RUNNING" - - name: 'upgrade_runtime' - description: | - Triggers an upgrade when flipped from false to true - type: Boolean parameters: - name: 'location' type: String diff --git a/mmv1/templates/terraform/custom_update/colab_runtime.go.tmpl b/mmv1/templates/terraform/custom_update/colab_runtime.go.tmpl index 7bcf066a3b79..6e91487af8f8 100644 --- a/mmv1/templates/terraform/custom_update/colab_runtime.go.tmpl +++ b/mmv1/templates/terraform/custom_update/colab_runtime.go.tmpl @@ -37,9 +37,4 @@ if desired_state != "" && state != desired_state { log.Printf("[DEBUG] Colab runtime %q has state %q.", name, state) } -if d.Get("upgrade_runtime").(bool) { - if err := ModifyColabRuntime(config, d, project, billingProject, userAgent, "upgrade"); err != nil { - return err - } -} return nil diff --git a/mmv1/templates/terraform/post_create/colab_runtime_template.tmpl b/mmv1/templates/terraform/post_create/colab_runtime_template.tmpl new file mode 100644 index 000000000000..c959ee745e0b --- /dev/null +++ b/mmv1/templates/terraform/post_create/colab_runtime_template.tmpl @@ -0,0 +1,10 @@ +// The operation for this resource contains the generated name that we need +// in order to perform a READ. We need to access the object inside of it as +// a map[string]interface, so let's do that. + +resp := res["response"].(map[string]interface{}) +name := tpgresource.GetResourceNameFromSelfLink(resp["name"].(string)) +log.Printf("[DEBUG] Setting resource name, id to %s", name) +if err := d.Set("name", name); err != nil { + return fmt.Errorf("Error setting name: %s", err) +} From d196576bac274bfcabb63f45c1b455aaea47fd49 Mon Sep 17 00:00:00 2001 From: Bhargav Chereddy Date: Thu, 30 Jan 2025 22:36:19 +0000 Subject: [PATCH 14/16] resolve merge --- .../post_create/colab_runtime_template.go.tmpl | 2 +- .../terraform/post_create/colab_runtime_template.tmpl | 10 ---------- 2 files changed, 1 insertion(+), 11 deletions(-) delete mode 100644 mmv1/templates/terraform/post_create/colab_runtime_template.tmpl diff --git a/mmv1/templates/terraform/post_create/colab_runtime_template.go.tmpl b/mmv1/templates/terraform/post_create/colab_runtime_template.go.tmpl index c959ee745e0b..322c8de2d4eb 100644 --- a/mmv1/templates/terraform/post_create/colab_runtime_template.go.tmpl +++ b/mmv1/templates/terraform/post_create/colab_runtime_template.go.tmpl @@ -4,7 +4,7 @@ resp := res["response"].(map[string]interface{}) name := tpgresource.GetResourceNameFromSelfLink(resp["name"].(string)) -log.Printf("[DEBUG] Setting resource name, id to %s", name) +log.Printf("[DEBUG] Setting resource name to %s", name) if err := d.Set("name", name); err != nil { return fmt.Errorf("Error setting name: %s", err) } diff --git a/mmv1/templates/terraform/post_create/colab_runtime_template.tmpl b/mmv1/templates/terraform/post_create/colab_runtime_template.tmpl deleted file mode 100644 index 322c8de2d4eb..000000000000 --- a/mmv1/templates/terraform/post_create/colab_runtime_template.tmpl +++ /dev/null @@ -1,10 +0,0 @@ -// The operation for this resource contains the generated name that we need -// in order to perform a READ. We need to access the object inside of it as -// a map[string]interface, so let's do that. - -resp := res["response"].(map[string]interface{}) -name := tpgresource.GetResourceNameFromSelfLink(resp["name"].(string)) -log.Printf("[DEBUG] Setting resource name to %s", name) -if err := d.Set("name", name); err != nil { - return fmt.Errorf("Error setting name: %s", err) -} From 67c1cfa8e857240689908bc3c84747dfc99cb2b1 Mon Sep 17 00:00:00 2001 From: Bhargav Chereddy Date: Fri, 31 Jan 2025 06:47:41 +0000 Subject: [PATCH 15/16] Enable auto-upgrade for runtimes --- mmv1/products/colab/Runtime.yaml | 10 ++++++++ .../custom_update/colab_runtime.go.tmpl | 6 +++++ .../examples/colab_runtime_full.tf.tmpl | 2 ++ .../colab/resource_colab_runtime_test.go | 24 +++++++++++++++---- 4 files changed, 38 insertions(+), 4 deletions(-) diff --git a/mmv1/products/colab/Runtime.yaml b/mmv1/products/colab/Runtime.yaml index a7afef828e67..d2867711ae70 100644 --- a/mmv1/products/colab/Runtime.yaml +++ b/mmv1/products/colab/Runtime.yaml @@ -60,12 +60,17 @@ examples: key_name: 'acctest.BootstrapKMSKeyInLocation(t, "us-central1").CryptoKey.Name' ignore_read_extra: - 'desired_state' + - 'auto_upgrade' virtual_fields: - name: 'desired_state' description: | Desired state of the Colab Runtime. Set this field to `RUNNING` to start the runtime, and `STOPPED` to stop it. type: String default_value: "RUNNING" + - name: 'auto_upgrade' + description: | + Triggers an upgrade anytime the runtime is started if it is upgradable. + type: Boolean parameters: - name: 'location' type: String @@ -109,3 +114,8 @@ properties: description: | Output only. The state of the runtime. output: true + - name: isUpgradable + type: Boolean + description: | + Output only. Checks if the NotebookRuntime is upgradable. + output: true diff --git a/mmv1/templates/terraform/custom_update/colab_runtime.go.tmpl b/mmv1/templates/terraform/custom_update/colab_runtime.go.tmpl index 6e91487af8f8..97a5514ac32c 100644 --- a/mmv1/templates/terraform/custom_update/colab_runtime.go.tmpl +++ b/mmv1/templates/terraform/custom_update/colab_runtime.go.tmpl @@ -37,4 +37,10 @@ if desired_state != "" && state != desired_state { log.Printf("[DEBUG] Colab runtime %q has state %q.", name, state) } +if d.Get("auto_upgrade").(bool) && d.Get("is_upgradable").(bool) { + if err := ModifyColabRuntime(config, d, project, billingProject, userAgent, "upgrade"); err != nil { + return err + } +} + return nil diff --git a/mmv1/templates/terraform/examples/colab_runtime_full.tf.tmpl b/mmv1/templates/terraform/examples/colab_runtime_full.tf.tmpl index 54859ecd1314..970ccc096246 100644 --- a/mmv1/templates/terraform/examples/colab_runtime_full.tf.tmpl +++ b/mmv1/templates/terraform/examples/colab_runtime_full.tf.tmpl @@ -55,6 +55,8 @@ resource "google_colab_runtime" "{{$.PrimaryResourceId}}" { desired_state = "ACTIVE" + auto_upgrade = true + depends_on = [ google_colab_runtime_template.my_template ] diff --git a/mmv1/third_party/terraform/services/colab/resource_colab_runtime_test.go b/mmv1/third_party/terraform/services/colab/resource_colab_runtime_test.go index a1c06a7bc097..50a2c0a2c7fc 100644 --- a/mmv1/third_party/terraform/services/colab/resource_colab_runtime_test.go +++ b/mmv1/third_party/terraform/services/colab/resource_colab_runtime_test.go @@ -29,7 +29,7 @@ func TestAccColabRuntime_update(t *testing.T) { ResourceName: "google_colab_runtime.runtime", ImportState: true, ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"desired_state", "location", "name"}, + ImportStateVerifyIgnore: []string{"desired_state", "location", "name", "auto_upgrade"}, }, { Config: testAccColabRuntime_no_state(context), @@ -38,7 +38,7 @@ func TestAccColabRuntime_update(t *testing.T) { ResourceName: "google_colab_runtime.runtime", ImportState: true, ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"desired_state", "location", "name"}, + ImportStateVerifyIgnore: []string{"desired_state", "location", "name", "auto_upgrade"}, }, { Config: testAccColabRuntime_stopped(context), @@ -52,7 +52,21 @@ func TestAccColabRuntime_update(t *testing.T) { ResourceName: "google_colab_runtime.runtime", ImportState: true, ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"desired_state", "location", "name"}, + ImportStateVerifyIgnore: []string{"desired_state", "location", "name", "auto_upgrade"}, + }, + { + Config: testAccColabRuntime_full(context), + ConfigPlanChecks: resource.ConfigPlanChecks{ + PreApply: []plancheck.PlanCheck{ + plancheck.ExpectResourceAction("google_colab_runtime.runtime", plancheck.ResourceActionUpdate), + }, + }, + }, + { + ResourceName: "google_colab_runtime.runtime", + ImportState: true, + ImportStateVerify: true, + ImportStateVerifyIgnore: []string{"desired_state", "location", "name", "auto_upgrade"}, }, }, }) @@ -115,7 +129,9 @@ resource "google_colab_runtime" "runtime" { runtime_user = "gterraformtestuser@gmail.com" description = "Full runtime" - desired_state = "ACTIVE" + desired_state = "RUNNING" + + auto_upgrade = true depends_on = [ google_colab_runtime_template.my_template From 0f3e6bbe32f8ec0715484a970731e19b1a7695ea Mon Sep 17 00:00:00 2001 From: Bhargav Chereddy Date: Mon, 3 Feb 2025 16:33:25 +0000 Subject: [PATCH 16/16] Upgrade runtime automatically if a USER_DEFINED runtime is expired --- mmv1/products/colab/Runtime.yaml | 10 ++++++++++ .../terraform/custom_update/colab_runtime.go.tmpl | 15 +++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/mmv1/products/colab/Runtime.yaml b/mmv1/products/colab/Runtime.yaml index d2867711ae70..4ea0d956261b 100644 --- a/mmv1/products/colab/Runtime.yaml +++ b/mmv1/products/colab/Runtime.yaml @@ -119,3 +119,13 @@ properties: description: | Output only. Checks if the NotebookRuntime is upgradable. output: true + - name: expirationTime + type: String + description: | + Output only. Timestamp when this NotebookRuntime will be expired. + output: true + - name: notebookRuntimeType + type: String + description: | + Output only. The type of the notebook runtime. + output: true diff --git a/mmv1/templates/terraform/custom_update/colab_runtime.go.tmpl b/mmv1/templates/terraform/custom_update/colab_runtime.go.tmpl index 97a5514ac32c..8cc234a988c9 100644 --- a/mmv1/templates/terraform/custom_update/colab_runtime.go.tmpl +++ b/mmv1/templates/terraform/custom_update/colab_runtime.go.tmpl @@ -37,7 +37,22 @@ if desired_state != "" && state != desired_state { log.Printf("[DEBUG] Colab runtime %q has state %q.", name, state) } +var upgrade_runtime bool if d.Get("auto_upgrade").(bool) && d.Get("is_upgradable").(bool) { + upgrade_runtime = true +} + +expiration_time_string := d.Get("expiration_time").(string) +expiration_time, err := time.Parse(time.RFC3339Nano, expiration_time_string) +if err != nil { + return err +} + +if expiration_time.Before(time.Now()) && d.Get("notebook_runtime_type").(string) == "USER_DEFINED" { + upgrade_runtime = true +} + +if upgrade_runtime { if err := ModifyColabRuntime(config, d, project, billingProject, userAgent, "upgrade"); err != nil { return err }