|
1 | 1 | package google
|
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "strconv" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 9 | +) |
| 10 | + |
| 11 | +func TestAccNotebooksInstance_create_vm_image(t *testing.T) { |
| 12 | + t.Parallel() |
| 13 | + |
| 14 | + prefix := fmt.Sprintf("%d", RandInt(t)) |
| 15 | + name := fmt.Sprintf("tf-%s", prefix) |
| 16 | + |
| 17 | + VcrTest(t, resource.TestCase{ |
| 18 | + ProtoV5ProviderFactories: ProtoV5ProviderFactories(t), |
| 19 | + Steps: []resource.TestStep{ |
| 20 | + { |
| 21 | + Config: testAccNotebooksInstance_create_vm_image(name), |
| 22 | + }, |
| 23 | + { |
| 24 | + ResourceName: "google_notebooks_instance.test", |
| 25 | + ImportState: true, |
| 26 | + ImportStateVerify: true, |
| 27 | + ImportStateVerifyIgnore: []string{"vm_image", "metadata"}, |
| 28 | + }, |
| 29 | + }, |
| 30 | + }) |
| 31 | +} |
| 32 | + |
| 33 | +func TestAccNotebooksInstance_update(t *testing.T) { |
| 34 | + context := map[string]interface{}{ |
| 35 | + "random_suffix": RandString(t, 10), |
| 36 | + } |
| 37 | + |
| 38 | + VcrTest(t, resource.TestCase{ |
| 39 | + ProtoV5ProviderFactories: ProtoV5ProviderFactories(t), |
| 40 | + Steps: []resource.TestStep{ |
| 41 | + { |
| 42 | + Config: testAccNotebooksInstance_basic(context), |
| 43 | + }, |
| 44 | + { |
| 45 | + ResourceName: "google_notebooks_instance.instance", |
| 46 | + ImportState: true, |
| 47 | + ImportStateVerify: true, |
| 48 | + ImportStateVerifyIgnore: []string{"vm_image", "metadata"}, |
| 49 | + }, |
| 50 | + { |
| 51 | + Config: testAccNotebooksInstance_update(context, true), |
| 52 | + }, |
| 53 | + { |
| 54 | + ResourceName: "google_notebooks_instance.instance", |
| 55 | + ImportState: true, |
| 56 | + ImportStateVerify: true, |
| 57 | + ImportStateVerifyIgnore: []string{"vm_image", "metadata"}, |
| 58 | + }, |
| 59 | + { |
| 60 | + Config: testAccNotebooksInstance_update(context, false), |
| 61 | + }, |
| 62 | + { |
| 63 | + ResourceName: "google_notebooks_instance.instance", |
| 64 | + ImportState: true, |
| 65 | + ImportStateVerify: true, |
| 66 | + ImportStateVerifyIgnore: []string{"vm_image", "metadata"}, |
| 67 | + }, |
| 68 | + }, |
| 69 | + }) |
| 70 | +} |
| 71 | + |
| 72 | +func testAccNotebooksInstance_create_vm_image(name string) string { |
| 73 | + return fmt.Sprintf(` |
| 74 | +
|
| 75 | +resource "google_notebooks_instance" "test" { |
| 76 | + name = "%s" |
| 77 | + location = "us-west1-a" |
| 78 | + machine_type = "e2-medium" |
| 79 | + metadata = { |
| 80 | + proxy-mode = "service_account" |
| 81 | + terraform = "true" |
| 82 | + } |
| 83 | +
|
| 84 | + nic_type = "VIRTIO_NET" |
| 85 | +
|
| 86 | + reservation_affinity { |
| 87 | + consume_reservation_type = "NO_RESERVATION" |
| 88 | + } |
| 89 | +
|
| 90 | + vm_image { |
| 91 | + project = "deeplearning-platform-release" |
| 92 | + image_family = "tf-latest-cpu" |
| 93 | + } |
| 94 | +} |
| 95 | +`, name) |
| 96 | +} |
| 97 | + |
| 98 | +func testAccNotebooksInstance_basic(context map[string]interface{}) string { |
| 99 | + return Nprintf(` |
| 100 | +resource "google_notebooks_instance" "instance" { |
| 101 | + name = "tf-test-notebooks-instance%{random_suffix}" |
| 102 | + location = "us-central1-a" |
| 103 | + machine_type = "e2-medium" |
| 104 | +
|
| 105 | + vm_image { |
| 106 | + project = "deeplearning-platform-release" |
| 107 | + image_family = "tf-latest-cpu" |
| 108 | + } |
| 109 | +
|
| 110 | + metadata = { |
| 111 | + proxy-mode = "service_account" |
| 112 | + terraform = "true" |
| 113 | + } |
| 114 | +
|
| 115 | + lifecycle { |
| 116 | + prevent_destroy = true |
| 117 | + } |
| 118 | +} |
| 119 | +`, context) |
| 120 | +} |
| 121 | + |
| 122 | +func testAccNotebooksInstance_update(context map[string]interface{}, preventDestroy bool) string { |
| 123 | + context["prevent_destroy"] = strconv.FormatBool(preventDestroy) |
| 124 | + |
| 125 | + return Nprintf(` |
| 126 | +resource "google_notebooks_instance" "instance" { |
| 127 | + name = "tf-test-notebooks-instance%{random_suffix}" |
| 128 | + location = "us-central1-a" |
| 129 | + machine_type = "e2-medium" |
| 130 | +
|
| 131 | + vm_image { |
| 132 | + project = "deeplearning-platform-release" |
| 133 | + image_family = "tf-latest-cpu" |
| 134 | + } |
| 135 | +
|
| 136 | + metadata = { |
| 137 | + proxy-mode = "service_account" |
| 138 | + terraform = "true" |
| 139 | + notebook-upgrade-schedule = "0 * * * *" |
| 140 | + } |
| 141 | +
|
| 142 | + labels = { |
| 143 | + key = "value" |
| 144 | + } |
| 145 | +
|
| 146 | + lifecycle { |
| 147 | + prevent_destroy = %{prevent_destroy} |
| 148 | + } |
| 149 | +} |
| 150 | +`, context) |
| 151 | +} |
0 commit comments