From d1fceacc05f08ae1c676c80644b23dd0310ffdc0 Mon Sep 17 00:00:00 2001 From: The Magician Date: Mon, 3 Feb 2025 13:55:37 -0800 Subject: [PATCH] Auto-upgrade for colab runtimes (#12931) (#21214) [upstream:b79356dcaaa3925403eade2599558778d385097f] Signed-off-by: Modular Magician --- .changelog/12931.txt | 3 + .../services/colab/resource_colab_runtime.go | 62 +++++++++++++++++++ ...resource_colab_runtime_generated_meta.yaml | 5 ++ .../resource_colab_runtime_generated_test.go | 4 +- .../colab/resource_colab_runtime_test.go | 24 +++++-- website/docs/r/colab_runtime.html.markdown | 13 ++++ 6 files changed, 106 insertions(+), 5 deletions(-) create mode 100644 .changelog/12931.txt diff --git a/.changelog/12931.txt b/.changelog/12931.txt new file mode 100644 index 00000000000..b83c721ad76 --- /dev/null +++ b/.changelog/12931.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +colab: Auto-upgrade for colab runtimes +``` \ No newline at end of file diff --git a/google/services/colab/resource_colab_runtime.go b/google/services/colab/resource_colab_runtime.go index 81c36b8440c..ba92de16779 100644 --- a/google/services/colab/resource_colab_runtime.go +++ b/google/services/colab/resource_colab_runtime.go @@ -140,6 +140,21 @@ func ResourceColabRuntime() *schema.Resource { }, }, }, + "expiration_time": { + Type: schema.TypeString, + Computed: true, + Description: `Output only. Timestamp when this NotebookRuntime will be expired.`, + }, + "is_upgradable": { + Type: schema.TypeBool, + Computed: true, + Description: `Output only. Checks if the NotebookRuntime is upgradable.`, + }, + "notebook_runtime_type": { + Type: schema.TypeString, + Computed: true, + Description: `Output only. The type of the notebook runtime.`, + }, "state": { Type: schema.TypeString, Computed: true, @@ -151,6 +166,11 @@ func ResourceColabRuntime() *schema.Resource { Description: `Desired state of the Colab Runtime. Set this field to 'RUNNING' to start the runtime, and 'STOPPED' to stop it.`, Default: "RUNNING", }, + "auto_upgrade": { + Type: schema.TypeBool, + Optional: true, + Description: `Triggers an upgrade anytime the runtime is started if it is upgradable.`, + }, "project": { Type: schema.TypeString, Optional: true, @@ -325,6 +345,15 @@ func resourceColabRuntimeRead(d *schema.ResourceData, meta interface{}) error { if err := d.Set("state", flattenColabRuntimeState(res["state"], d, config)); err != nil { return fmt.Errorf("Error reading Runtime: %s", err) } + if err := d.Set("is_upgradable", flattenColabRuntimeIsUpgradable(res["isUpgradable"], d, config)); err != nil { + return fmt.Errorf("Error reading Runtime: %s", err) + } + if err := d.Set("expiration_time", flattenColabRuntimeExpirationTime(res["expirationTime"], d, config)); err != nil { + return fmt.Errorf("Error reading Runtime: %s", err) + } + if err := d.Set("notebook_runtime_type", flattenColabRuntimeNotebookRuntimeType(res["notebookRuntimeType"], d, config)); err != nil { + return fmt.Errorf("Error reading Runtime: %s", err) + } return nil } @@ -370,6 +399,27 @@ func resourceColabRuntimeUpdate(d *schema.ResourceData, meta interface{}) error 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 + } + } + return nil } @@ -487,6 +537,18 @@ func flattenColabRuntimeState(v interface{}, d *schema.ResourceData, config *tra return v } +func flattenColabRuntimeIsUpgradable(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func flattenColabRuntimeExpirationTime(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + +func flattenColabRuntimeNotebookRuntimeType(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} { + return v +} + func expandColabRuntimeNotebookRuntimeTemplateRef(v interface{}, d tpgresource.TerraformResourceData, config *transport_tpg.Config) (interface{}, error) { l := v.([]interface{}) if len(l) == 0 || l[0] == nil { diff --git a/google/services/colab/resource_colab_runtime_generated_meta.yaml b/google/services/colab/resource_colab_runtime_generated_meta.yaml index b0d1f16f072..3383a9e858c 100644 --- a/google/services/colab/resource_colab_runtime_generated_meta.yaml +++ b/google/services/colab/resource_colab_runtime_generated_meta.yaml @@ -5,14 +5,19 @@ api_service_name: 'aiplatform.googleapis.com' api_version: 'v1' api_resource_type_kind: 'NotebookRuntime' fields: + - field: 'auto_upgrade' + provider_only: true - field: 'description' - field: 'desired_state' provider_only: true - field: 'display_name' + - field: 'expiration_time' + - field: 'is_upgradable' - field: 'location' provider_only: true - field: 'name' provider_only: true - field: 'notebook_runtime_template_ref.notebook_runtime_template' + - field: 'notebook_runtime_type' - field: 'runtime_user' - field: 'state' diff --git a/google/services/colab/resource_colab_runtime_generated_test.go b/google/services/colab/resource_colab_runtime_generated_test.go index c9d246ad006..3e9dda40bb9 100644 --- a/google/services/colab/resource_colab_runtime_generated_test.go +++ b/google/services/colab/resource_colab_runtime_generated_test.go @@ -170,7 +170,7 @@ func TestAccColabRuntime_colabRuntimeFullExample(t *testing.T) { ResourceName: "google_colab_runtime.runtime", ImportState: true, ImportStateVerify: true, - ImportStateVerifyIgnore: []string{"desired_state", "location", "name"}, + ImportStateVerifyIgnore: []string{"auto_upgrade", "desired_state", "location", "name"}, }, }, }) @@ -235,6 +235,8 @@ resource "google_colab_runtime" "runtime" { desired_state = "ACTIVE" + auto_upgrade = true + depends_on = [ google_colab_runtime_template.my_template ] diff --git a/google/services/colab/resource_colab_runtime_test.go b/google/services/colab/resource_colab_runtime_test.go index ac6d3185687..86c4059b6d6 100644 --- a/google/services/colab/resource_colab_runtime_test.go +++ b/google/services/colab/resource_colab_runtime_test.go @@ -31,7 +31,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), @@ -40,7 +40,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), @@ -54,7 +54,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"}, }, }, }) @@ -117,7 +131,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 diff --git a/website/docs/r/colab_runtime.html.markdown b/website/docs/r/colab_runtime.html.markdown index cdc05a057b5..3957d33dbd1 100644 --- a/website/docs/r/colab_runtime.html.markdown +++ b/website/docs/r/colab_runtime.html.markdown @@ -174,6 +174,8 @@ resource "google_colab_runtime" "runtime" { desired_state = "ACTIVE" + auto_upgrade = true + depends_on = [ google_colab_runtime_template.my_template ] @@ -219,6 +221,8 @@ The following arguments are supported: * `desired_state` - (Optional) Desired state of the Colab Runtime. Set this field to `RUNNING` to start the runtime, and `STOPPED` to stop it. +* `auto_upgrade` - (Optional) Triggers an upgrade anytime the runtime is started if it is upgradable. + The `notebook_runtime_template_ref` block supports: @@ -235,6 +239,15 @@ In addition to the arguments listed above, the following computed attributes are * `state` - Output only. The state of the runtime. +* `is_upgradable` - + Output only. Checks if the NotebookRuntime is upgradable. + +* `expiration_time` - + Output only. Timestamp when this NotebookRuntime will be expired. + +* `notebook_runtime_type` - + Output only. The type of the notebook runtime. + ## Timeouts