Skip to content

Commit

Permalink
Colab runtime (#12715)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcreddy-gcp authored Jan 16, 2025
1 parent a575eb9 commit 2351a26
Show file tree
Hide file tree
Showing 6 changed files with 269 additions and 2 deletions.
81 changes: 81 additions & 0 deletions mmv1/products/colab/Runtime.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# Copyright 2025 Google Inc.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

---

name: 'Runtime'
description: |
'A runtime is a Google-provisioned virtual machine (VM) that can run the code in your notebook (IPYNB file).'
references:
guides:
'Create a runtime': 'https://cloud.google.com/colab/docs/create-runtime'
api: 'https://cloud.google.com/vertex-ai/docs/reference/rest/v1/projects.locations.notebookRuntimes'
base_url: 'projects/{{project}}/locations/{{location}}/notebookRuntimes'
self_link: 'projects/{{project}}/locations/{{location}}/notebookRuntimes/{{name}}'
create_url: 'projects/{{project}}/locations/{{location}}/notebookRuntimes:assign?notebook_runtime_id={{name}}'
async:
type: 'OpAsync'
operation:
full_url: 'https://{{location}}-aiplatform.googleapis.com/v1/{{op_id}}'
custom_code:
encoder: 'templates/terraform/encoders/colab_runtime.go.tmpl'
examples:
- name: 'colab_runtime_basic'
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'
- name: 'colab_runtime_full'
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'
key_name: 'my-crypto-key'
test_vars_overrides:
key_name: 'acctest.BootstrapKMSKeyInLocation(t, "us-central1").CryptoKey.Name'
parameters:
- name: 'location'
type: String
required: true
url_param_only: true
description: 'The location for the resource: https://cloud.google.com/colab/docs/locations'
- name: 'name'
type: String
url_param_only: true
description: 'The resource name of the Runtime'
properties:
- name: notebookRuntimeTemplateRef
type: NestedObject
description: |
'Runtime specific information used for NotebookRuntime creation.'
properties:
- name: 'notebookRuntimeTemplate'
type: String
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
description: 'The user email of the NotebookRuntime.'
- name: 'displayName'
type: String
description:
Required. The display name of the Runtime.
required: true
- name: description
type: String
description: 'The description of the Runtime.'
4 changes: 2 additions & 2 deletions mmv1/products/colab/RuntimeTemplate.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ base_url: 'projects/{{project}}/locations/{{location}}/notebookRuntimeTemplates'
self_link: 'projects/{{project}}/locations/{{location}}/notebookRuntimeTemplates/{{name}}'
immutable: true
create_url: 'projects/{{project}}/locations/{{location}}/notebookRuntimeTemplates?notebook_runtime_template_id={{name}}'
autogen_async: true
async:
type: 'OpAsync'
operation:
base_url: '{{op_id}}'
full_url: 'https://{{location}}-aiplatform.googleapis.com/v1/{{op_id}}'
custom_code:
post_create: 'templates/terraform/post_create/colab_runtime_template.tmpl'
examples:
Expand Down
20 changes: 20 additions & 0 deletions mmv1/templates/terraform/encoders/colab_runtime.go.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{{/*
The license inside this block applies to this file
Copyright 2025 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/ -}}

newObj := make(map[string]interface{})
newObj["notebookRuntimeTemplate"], _ = d.GetOk("notebook_runtime_template_ref.0.notebook_runtime_template")

delete(obj, "notebookRuntimeTemplateRef")

newObj["notebookRuntime"] = obj
return newObj, nil
29 changes: 29 additions & 0 deletions mmv1/templates/terraform/examples/colab_runtime_basic.tf.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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
}

display_name = "Runtime basic"
runtime_user = "[email protected]"

depends_on = [
google_colab_runtime_template.my_template,
]
}
59 changes: 59 additions & 0 deletions mmv1/templates/terraform/examples/colab_runtime_full.tf.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
resource "google_colab_runtime_template" "my_template" {
name = "{{index $.Vars "runtime_name"}}"
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 = "{{index $.Vars "key_name"}}"
}
}

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
}

display_name = "Runtime full"
runtime_user = "[email protected]"
description = "Full runtime"

depends_on = [
google_colab_runtime_template.my_template
]
}
78 changes: 78 additions & 0 deletions mmv1/third_party/terraform/services/colab/colab_operation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package colab

import (
"encoding/json"
"errors"
"fmt"
"time"

"github.com/hashicorp/terraform-provider-google/google/tpgresource"
transport_tpg "github.com/hashicorp/terraform-provider-google/google/transport"
)

type ColabOperationWaiter struct {
Config *transport_tpg.Config
UserAgent string
Project string
tpgresource.CommonOperationWaiter
}

func (w *ColabOperationWaiter) QueryOp() (interface{}, error) {
if w == nil {
return nil, fmt.Errorf("Cannot query operation, it's unset or nil.")
}

region := tpgresource.GetRegionFromRegionalSelfLink(w.CommonOperationWaiter.Op.Name)

// Returns the proper get.
url := fmt.Sprintf("https://%s-aiplatform.googleapis.com/v1/%s", region, w.CommonOperationWaiter.Op.Name)

return transport_tpg.SendRequest(transport_tpg.SendRequestOptions{
Config: w.Config,
Method: "GET",
Project: w.Project,
RawURL: url,
UserAgent: w.UserAgent,
})
}

func createColabWaiter(config *transport_tpg.Config, op map[string]interface{}, project, activity, userAgent string) (*ColabOperationWaiter, error) {
w := &ColabOperationWaiter{
Config: config,
UserAgent: userAgent,
Project: project,
}
if err := w.CommonOperationWaiter.SetOp(op); err != nil {
return nil, err
}
return w, nil
}

// nolint: deadcode,unused
func ColabOperationWaitTimeWithResponse(config *transport_tpg.Config, op map[string]interface{}, response *map[string]interface{}, project, activity, userAgent string, timeout time.Duration) error {
w, err := createColabWaiter(config, op, project, activity, userAgent)
if err != nil {
return err
}
if err := tpgresource.OperationWait(w, activity, timeout, config.PollInterval); err != nil {
return err
}
rawResponse := []byte(w.CommonOperationWaiter.Op.Response)
if len(rawResponse) == 0 {
return errors.New("`resource` not set in operation response")
}
return json.Unmarshal(rawResponse, response)
}

func ColabOperationWaitTime(config *transport_tpg.Config, op map[string]interface{}, project, activity, userAgent string, timeout time.Duration) error {
if val, ok := op["name"]; !ok || val == "" {
// This was a synchronous call - there is no operation to wait for.
return nil
}
w, err := createColabWaiter(config, op, project, activity, userAgent)
if err != nil {
// If w is nil, the op was synchronous.
return err
}
return tpgresource.OperationWait(w, activity, timeout, config.PollInterval)
}

0 comments on commit 2351a26

Please sign in to comment.