Skip to content

Commit f19e5ba

Browse files
Add max_run_duration to instance and instance template on Beta (#6812) (#13489)
Signed-off-by: Modular Magician <[email protected]> Signed-off-by: Modular Magician <[email protected]>
1 parent 81d5560 commit f19e5ba

5 files changed

+104
-6
lines changed

.changelog/6812.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
compute: added `max_run_duration` field to `google_compute_instance` and `google_compute_instance_template` resource (beta)
3+
```

google/resource_compute_instance_template_test.go

+42-2
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,6 @@ func TestAccComputeInstanceTemplate_spot(t *testing.T) {
10821082
t.Parallel()
10831083

10841084
var instanceTemplate compute.InstanceTemplate
1085-
10861085
vcrTest(t, resource.TestCase{
10871086
PreCheck: func() { testAccPreCheck(t) },
10881087
Providers: testAccProviders,
@@ -1096,7 +1095,6 @@ func TestAccComputeInstanceTemplate_spot(t *testing.T) {
10961095
testAccCheckComputeInstanceTemplateAutomaticRestart(&instanceTemplate, false),
10971096
testAccCheckComputeInstanceTemplatePreemptible(&instanceTemplate, true),
10981097
testAccCheckComputeInstanceTemplateProvisioningModel(&instanceTemplate, "SPOT"),
1099-
testAccCheckComputeInstanceTemplateInstanceTerminationAction(&instanceTemplate, "STOP"),
11001098
),
11011099
},
11021100
{
@@ -2890,6 +2888,48 @@ resource "google_compute_instance_template" "foobar" {
28902888
`, suffix)
28912889
}
28922890

2891+
func testAccComputeInstanceTemplate_spot_maxRunDuration(suffix string) string {
2892+
return fmt.Sprintf(`
2893+
data "google_compute_image" "my_image" {
2894+
family = "debian-11"
2895+
project = "debian-cloud"
2896+
}
2897+
2898+
resource "google_compute_instance_template" "foobar" {
2899+
name = "tf-test-instance-template-%s"
2900+
machine_type = "e2-medium"
2901+
can_ip_forward = false
2902+
tags = ["foo", "bar"]
2903+
2904+
disk {
2905+
source_image = data.google_compute_image.my_image.self_link
2906+
auto_delete = true
2907+
boot = true
2908+
}
2909+
2910+
network_interface {
2911+
network = "default"
2912+
}
2913+
2914+
scheduling {
2915+
preemptible = true
2916+
automatic_restart = false
2917+
provisioning_model = "SPOT"
2918+
instance_termination_action = "DELETE"
2919+
2920+
}
2921+
2922+
metadata = {
2923+
foo = "bar"
2924+
}
2925+
2926+
service_account {
2927+
scopes = ["userinfo-email", "compute-ro", "storage-ro"]
2928+
}
2929+
}
2930+
`, suffix)
2931+
}
2932+
28932933
func testAccComputeInstanceTemplate_sourceSnapshotEncryptionKey(kmsRingName, kmsKeyName, suffix string) string {
28942934
return fmt.Sprintf(`
28952935
data "google_kms_key_ring" "ring" {

google/resource_compute_instance_test.go

+31-3
Original file line numberDiff line numberDiff line change
@@ -2192,7 +2192,6 @@ func TestAccComputeInstance_spotVM(t *testing.T) {
21922192

21932193
var instance compute.Instance
21942194
var instanceName = fmt.Sprintf("tf-test-%s", randString(t, 10))
2195-
21962195
vcrTest(t, resource.TestCase{
21972196
PreCheck: func() { testAccPreCheck(t) },
21982197
Providers: testAccProviders,
@@ -2203,7 +2202,6 @@ func TestAccComputeInstance_spotVM(t *testing.T) {
22032202
Check: resource.ComposeTestCheckFunc(
22042203
testAccCheckComputeInstanceExists(
22052204
t, "google_compute_instance.foobar", &instance),
2206-
testAccCheckComputeInstanceTerminationAction(&instance, "STOP"),
22072205
),
22082206
},
22092207
computeInstanceImportStep("us-central1-a", instanceName, []string{}),
@@ -2216,7 +2214,6 @@ func TestAccComputeInstance_spotVM_update(t *testing.T) {
22162214

22172215
var instance compute.Instance
22182216
var instanceName = fmt.Sprintf("tf-test-%s", randString(t, 10))
2219-
22202217
vcrTest(t, resource.TestCase{
22212218
PreCheck: func() { testAccPreCheck(t) },
22222219
Providers: testAccProviders,
@@ -6230,7 +6227,38 @@ resource "google_compute_instance" "foobar" {
62306227
preemptible = true
62316228
instance_termination_action = "STOP"
62326229
}
6230+
}
6231+
`, instance)
6232+
}
6233+
6234+
func testAccComputeInstance_spotVM_maxRunDuration(instance string) string {
6235+
return fmt.Sprintf(`
6236+
data "google_compute_image" "my_image" {
6237+
family = "ubuntu-2004-lts"
6238+
project = "ubuntu-os-cloud"
6239+
}
6240+
6241+
resource "google_compute_instance" "foobar" {
6242+
name = "%s"
6243+
machine_type = "e2-medium"
6244+
zone = "us-central1-a"
62336245
6246+
boot_disk {
6247+
initialize_params {
6248+
image = data.google_compute_image.my_image.self_link
6249+
}
6250+
}
6251+
6252+
network_interface {
6253+
network = "default"
6254+
}
6255+
6256+
scheduling {
6257+
provisioning_model = "SPOT"
6258+
automatic_restart = false
6259+
preemptible = true
6260+
instance_termination_action = "DELETE"
6261+
}
62346262
}
62356263
`, instance)
62366264
}

website/docs/r/compute_instance.html.markdown

+14-1
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,20 @@ specified, then this instance will have no external IPv6 Internet access. Struct
383383
`false`. For more info about
384384
`SPOT`, read [here](https://cloud.google.com/compute/docs/instances/spot)
385385

386-
* `instance_termination_action` - (Optional) Describe the type of termination action for `SPOT` VM. Can be `STOP` or `DELETE`. Read more on [here](https://cloud.google.com/compute/docs/instances/create-use-spot)
386+
* `instance_termination_action` - (Optional) Describe the type of termination action for VM. Can be `STOP` or `DELETE`. Read more on [here](https://cloud.google.com/compute/docs/instances/create-use-spot)
387+
388+
* `max_run_duration` - (Optional) [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html) The duration of the instance. Instance will run and be terminated after then, the termination action could be defined in `instance_termination_action`. Only support `DELETE` `instance_termination_action` at this point. Structure is [documented below](#nested_max_run_duration).
389+
390+
<a name="nested_max_run_duration"></a>The `max_run_duration` block supports:
391+
392+
* `nanos` - (Optional) Span of time that's a fraction of a second at nanosecond
393+
resolution. Durations less than one second are represented with a 0
394+
`seconds` field and a positive `nanos` field. Must be from 0 to
395+
999,999,999 inclusive.
396+
397+
* `seconds` - (Required) Span of time at a resolution of a second. Must be from 0 to
398+
315,576,000,000 inclusive. Note: these bounds are computed from: 60
399+
sec/min * 60 min/hr * 24 hr/day * 365.25 days/year * 10000 years.
387400

388401
<a name="nested_guest_accelerator"></a>The `guest_accelerator` block supports:
389402

website/docs/r/compute_instance_template.html.markdown

+14
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,20 @@ specified, then this instance will have no external IPv6 Internet access. Struct
562562
`SPOT`, read [here](https://cloud.google.com/compute/docs/instances/spot)
563563

564564
* `instance_termination_action` - (Optional) Describe the type of termination action for `SPOT` VM. Can be `STOP` or `DELETE`. Read more on [here](https://cloud.google.com/compute/docs/instances/create-use-spot)
565+
566+
* `max_run_duration` - (Optional) [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html) The duration of the instance. Instance will run and be terminated after then, the termination action could be defined in `instance_termination_action`. Only support `DELETE` `instance_termination_action` at this point. Structure is [documented below](#nested_max_run_duration).
567+
568+
<a name="nested_max_run_duration"></a>The `max_run_duration` block supports:
569+
570+
* `nanos` - (Optional) Span of time that's a fraction of a second at nanosecond
571+
resolution. Durations less than one second are represented with a 0
572+
`seconds` field and a positive `nanos` field. Must be from 0 to
573+
999,999,999 inclusive.
574+
575+
* `seconds` - (Required) Span of time at a resolution of a second. Must be from 0 to
576+
315,576,000,000 inclusive. Note: these bounds are computed from: 60
577+
sec/min * 60 min/hr * 24 hr/day * 365.25 days/year * 10000 years.
578+
565579

566580
<a name="nested_guest_accelerator"></a>The `guest_accelerator` block supports:
567581

0 commit comments

Comments
 (0)