-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make colab runtime startable/stoppable (#12904)
- Loading branch information
1 parent
bb83af8
commit 30fcb3c
Showing
9 changed files
with
406 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
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 | ||
} | ||
|
||
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", method, 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, "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 }} |
40 changes: 40 additions & 0 deletions
40
mmv1/templates/terraform/custom_update/colab_runtime.go.tmpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
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 "RUNNING": | ||
verb = "start" | ||
default: | ||
return fmt.Errorf("desired_state has to be RUNNING or STOPPED") | ||
} | ||
|
||
if err := ModifyColabRuntime(config, d, project, billingProject, userAgent, verb); err != nil { | ||
return err | ||
} | ||
|
||
} else { | ||
log.Printf("[DEBUG] Colab runtime %q has state %q.", name, state) | ||
} | ||
|
||
return nil |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
mmv1/templates/terraform/examples/colab_runtime_stopped.tf.tmpl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
if p, ok := d.GetOk("desired_state"); ok && p.(string) == "STOPPED" { | ||
if err := ModifyColabRuntime(config, d, project, billingProject, userAgent, "stop"); err != nil { | ||
return err | ||
} | ||
} |
File renamed without changes.
Oops, something went wrong.