Skip to content

Commit 93486b0

Browse files
committed
compute: Add scheduling termination time
Signed-off-by: Norbert Kamiński <[email protected]>
1 parent 0196090 commit 93486b0

File tree

4 files changed

+102
-1
lines changed

4 files changed

+102
-1
lines changed

mmv1/third_party/terraform/services/compute/compute_instance_helpers.go.tmpl

+26-1
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,10 @@ func expandScheduling(v interface{}) (*compute.Scheduling, error) {
190190
scheduling.LocalSsdRecoveryTimeout = transformedLocalSsdRecoveryTimeout
191191
scheduling.ForceSendFields = append(scheduling.ForceSendFields, "LocalSsdRecoveryTimeout")
192192
}
193+
if v, ok := original["termination_time"]; ok {
194+
scheduling.TerminationTime = v.(string)
195+
scheduling.ForceSendFields = append(scheduling.ForceSendFields, "TerminationTime")
196+
}
193197
return scheduling, nil
194198
}
195199

@@ -286,6 +290,7 @@ func flattenScheduling(resp *compute.Scheduling) []map[string]interface{} {
286290
"provisioning_model": resp.ProvisioningModel,
287291
"instance_termination_action": resp.InstanceTerminationAction,
288292
"availability_domain": resp.AvailabilityDomain,
293+
"termination_time": resp.TerminationTime,
289294
}
290295

291296
if resp.AutomaticRestart != nil {
@@ -723,7 +728,9 @@ func schedulingHasChangeRequiringReboot(d *schema.ResourceData) bool {
723728
oScheduling := o.([]interface{})[0].(map[string]interface{})
724729
newScheduling := n.([]interface{})[0].(map[string]interface{})
725730

726-
return hasNodeAffinitiesChanged(oScheduling, newScheduling) || hasMaxRunDurationChanged(oScheduling, newScheduling)
731+
return (hasNodeAffinitiesChanged(oScheduling, newScheduling) ||
732+
hasMaxRunDurationChanged(oScheduling, newScheduling) ||
733+
hasTerminationTimeChanged(oScheduling, newScheduling))
727734
}
728735

729736
// Terraform doesn't correctly calculate changes on schema.Set, so we do it manually
@@ -773,6 +780,24 @@ func schedulingHasChangeWithoutReboot(d *schema.ResourceData) bool {
773780
return false
774781
}
775782

783+
func hasTerminationTimeChanged(oScheduling, nScheduling map[string]interface{}) bool {
784+
oTerminationTime := oScheduling["termination_time"].(string)
785+
nTerminationTime := nScheduling["termination_time"].(string)
786+
787+
if len(oTerminationTime) == 0 && len(nTerminationTime) == 0 {
788+
return false
789+
}
790+
if len(oTerminationTime) == 0 || len(nTerminationTime) == 0 {
791+
return true
792+
}
793+
794+
if oTerminationTime != nTerminationTime {
795+
return true
796+
}
797+
798+
return false
799+
}
800+
776801
func hasMaxRunDurationChanged(oScheduling, nScheduling map[string]interface{}) bool {
777802
oMrd := oScheduling["max_run_duration"].([]interface{})
778803
nMrd := nScheduling["max_run_duration"].([]interface{})

mmv1/third_party/terraform/services/compute/resource_compute_instance.go.tmpl

+9
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ var (
9494
"scheduling.0.min_node_cpus",
9595
"scheduling.0.provisioning_model",
9696
"scheduling.0.instance_termination_action",
97+
"scheduling.0.termination_time",
9798
"scheduling.0.availability_domain",
9899
"scheduling.0.max_run_duration",
99100
"scheduling.0.on_instance_stop_action",
@@ -902,6 +903,14 @@ func ResourceComputeInstance() *schema.Resource {
902903
AtLeastOneOf: schedulingKeys,
903904
Description: `Specifies the action GCE should take when SPOT VM is preempted.`,
904905
},
906+
"termination_time": {
907+
Type: schema.TypeString,
908+
Optional: true,
909+
AtLeastOneOf: schedulingKeys,
910+
Description: `Specifies the timestamp, when the instance will be terminated,
911+
in RFC3339 text format. If specified, the instance termination action
912+
will be performed at the termination time.`,
913+
},
905914
"availability_domain": {
906915
Type: schema.TypeInt,
907916
Optional: true,

mmv1/third_party/terraform/services/compute/resource_compute_instance_test.go.tmpl

+65
Original file line numberDiff line numberDiff line change
@@ -1437,6 +1437,40 @@ func TestAccComputeInstance_scheduling(t *testing.T) {
14371437
})
14381438
}
14391439

1440+
func TestAccComputeInstance_schedulingTerminationTime(t *testing.T) {
1441+
t.Parallel()
1442+
1443+
var instance compute.Instance
1444+
var instanceName = fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
1445+
now := time.Now().UTC()
1446+
terminationTime := now.Add(24 * time.Hour).Format(time.RFC3339)
1447+
terminationTimeUpdated := now.Add(25 * time.Hour).Format(time.RFC3339)
1448+
1449+
acctest.VcrTest(t, resource.TestCase{
1450+
PreCheck: func() { acctest.AccTestPreCheck(t) },
1451+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
1452+
CheckDestroy: testAccCheckComputeInstanceDestroyProducer(t),
1453+
Steps: []resource.TestStep{
1454+
{
1455+
Config: testAccComputeInstance_TerminationTime(instanceName, terminationTime),
1456+
Check: resource.ComposeTestCheckFunc(
1457+
testAccCheckComputeInstanceExists(
1458+
t, "google_compute_instance.foobar", &instance),
1459+
),
1460+
},
1461+
computeInstanceImportStep("us-central1-a", instanceName, []string{"allow_stopping_for_update"}),
1462+
{
1463+
Config: testAccComputeInstance_TerminationTime(instanceName, terminationTimeUpdated),
1464+
Check: resource.ComposeTestCheckFunc(
1465+
testAccCheckComputeInstanceExists(
1466+
t, "google_compute_instance.foobar", &instance),
1467+
),
1468+
},
1469+
computeInstanceImportStep("us-central1-a", instanceName, []string{"allow_stopping_for_update"}),
1470+
},
1471+
})
1472+
}
1473+
14401474
func TestAccComputeInstance_advancedMachineFeatures(t *testing.T) {
14411475
t.Parallel()
14421476

@@ -7371,6 +7405,37 @@ resource "google_compute_instance" "foobar" {
73717405
`, instance)
73727406
}
73737407

7408+
func testAccComputeInstance_TerminationTime(instance string, terminationTime string) string {
7409+
return fmt.Sprintf(`
7410+
data "google_compute_image" "my_image" {
7411+
family = "debian-11"
7412+
project = "debian-cloud"
7413+
}
7414+
7415+
resource "google_compute_instance" "foobar" {
7416+
name = "%s"
7417+
machine_type = "e2-medium"
7418+
zone = "us-central1-a"
7419+
7420+
boot_disk {
7421+
initialize_params {
7422+
image = data.google_compute_image.my_image.self_link
7423+
}
7424+
}
7425+
7426+
network_interface {
7427+
network = "default"
7428+
}
7429+
7430+
scheduling {
7431+
instance_termination_action = "STOP"
7432+
termination_time = "%s"
7433+
}
7434+
allow_stopping_for_update = true
7435+
}
7436+
`, instance, terminationTime)
7437+
}
7438+
73747439
func testAccComputeInstance_advancedMachineFeatures(instance string) string {
73757440
return fmt.Sprintf(`
73767441
data "google_compute_image" "my_image" {

mmv1/third_party/terraform/website/docs/r/compute_instance.html.markdown

+2
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,8 @@ specified, then this instance will have no external IPv6 Internet access. Struct
493493

494494
* `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)
495495

496+
* `termination_time` - (Optional) Specifies the timestamp, when the instance will be terminated, in RFC3339 text format. If specified, the instance termination action will be performed at the termination time.
497+
496498
* `availability_domain` - (Optional) Specifies the availability domain to place the instance in. The value must be a number between 1 and the number of availability domains specified in the spread placement policy attached to the instance.
497499

498500
* `max_run_duration` - (Optional) The duration of the instance. Instance will run and be terminated after then, the termination action could be defined in `instance_termination_action`. Structure is [documented below](#nested_max_run_duration).

0 commit comments

Comments
 (0)