forked from GoogleCloudPlatform/magic-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make notebooks wait for active state and enable stopping/starting ins… (
GoogleCloudPlatform#9971) * Make notebooks wait for active state and enable stopping/starting instances using the desired_state field * ignore update_time
- Loading branch information
1 parent
15a57d6
commit d327ecf
Showing
7 changed files
with
205 additions
and
0 deletions.
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
10 changes: 10 additions & 0 deletions
10
mmv1/templates/terraform/examples/notebook_instance_basic_stopped.tf.erb
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,10 @@ | ||
resource "google_notebooks_instance" "<%= ctx[:primary_resource_id] %>" { | ||
name = "<%= ctx[:vars]["instance_name"] %>" | ||
location = "us-west1-a" | ||
machine_type = "e2-medium" | ||
vm_image { | ||
project = "deeplearning-platform-release" | ||
image_family = "tf-latest-cpu" | ||
} | ||
desired_state = "STOPPED" | ||
} |
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
13 changes: 13 additions & 0 deletions
13
mmv1/templates/terraform/post_create/notebooks_instance.go.erb
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,13 @@ | ||
if err := waitForNotebooksInstanceActive(d, config, d.Timeout(schema.TimeoutCreate) - time.Minute); err != nil { | ||
return fmt.Errorf("Notebook instance %q did not reach ACTIVE state: %q", d.Get("name").(string), err) | ||
} | ||
|
||
if p, ok := d.GetOk("desired_state"); ok && p.(string) == "STOPPED" { | ||
dRes, err := modifyNotebooksInstanceState(config, d, project, billingProject, userAgent, "stop") | ||
if err != nil { | ||
return err | ||
} | ||
if err := waitForNotebooksOperation(config, d, project, billingProject, userAgent, dRes); err != nil { | ||
return fmt.Errorf("Error stopping Notebook Instance: %s", err) | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
mmv1/templates/terraform/post_update/notebooks_instance.go.erb
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,21 @@ | ||
name := d.Get("name").(string) | ||
state := d.Get("state").(string) | ||
desired_state := d.Get("desired_state").(string) | ||
|
||
if state != desired_state { | ||
verb := "start" | ||
if desired_state == "STOPPED" { | ||
verb = "stop" | ||
} | ||
pRes, err := modifyNotebooksInstanceState(config, d, project, billingProject, userAgent, verb) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := waitForNotebooksOperation(config, d, project, billingProject, userAgent, pRes); err != nil { | ||
return fmt.Errorf("Error waiting to modify Notebook Instance state: %s", err) | ||
} | ||
|
||
} else { | ||
log.Printf("[DEBUG] Notebook Instance %q has state %q.", name, state) | ||
} |
85 changes: 85 additions & 0 deletions
85
mmv1/third_party/terraform/services/notebooks/resource_notebooks_instance_state_test.go.erb
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,85 @@ | ||
<% autogen_exception -%> | ||
package notebooks_test | ||
|
||
<% unless version == 'ga' %> | ||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-provider-google/google/acctest" | ||
) | ||
|
||
func TestAccNotebooksInstance_state(t *testing.T) { | ||
t.Parallel() | ||
|
||
prefix := fmt.Sprintf("%d", acctest.RandInt(t)) | ||
name := fmt.Sprintf("tf-%s", prefix) | ||
|
||
acctest.VcrTest(t, resource.TestCase{ | ||
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccNotebooksInstance_basic_active(name), | ||
}, | ||
{ | ||
ResourceName: "google_notebooks_instance.test", | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
ExpectNonEmptyPlan: true, | ||
ImportStateVerifyIgnore: []string{"container_image", "metadata", "vm_image","desired_state", "update_time"}, | ||
}, | ||
{ | ||
Config: testAccNotebooksInstance_basic_stopped(name), | ||
}, | ||
{ | ||
ResourceName: "google_notebooks_instance.test", | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
ExpectNonEmptyPlan: true, | ||
ImportStateVerifyIgnore: []string{"container_image", "metadata", "vm_image","desired_state", "update_time"}, | ||
}, | ||
{ | ||
Config: testAccNotebooksInstance_basic_active(name), | ||
}, | ||
{ | ||
ResourceName: "google_notebooks_instance.test", | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
ExpectNonEmptyPlan: true, | ||
ImportStateVerifyIgnore: []string{"container_image", "metadata", "vm_image","desired_state", "update_time"}, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccNotebooksInstance_basic_active(name string) string { | ||
return fmt.Sprintf(` | ||
resource "google_notebooks_instance" "test" { | ||
name = "%s" | ||
location = "us-west1-a" | ||
machine_type = "e2-medium" | ||
vm_image { | ||
project = "deeplearning-platform-release" | ||
image_family = "tf-latest-cpu" | ||
} | ||
desired_state = "ACTIVE" | ||
} | ||
`, name) | ||
} | ||
|
||
func testAccNotebooksInstance_basic_stopped(name string) string { | ||
return fmt.Sprintf(` | ||
resource "google_notebooks_instance" "test" { | ||
name = "%s" | ||
location = "us-west1-a" | ||
machine_type = "e2-medium" | ||
vm_image { | ||
project = "deeplearning-platform-release" | ||
image_family = "tf-latest-cpu" | ||
} | ||
desired_state = "STOPPED" | ||
} | ||
`, name) | ||
} | ||
<% end -%> |