Skip to content

Commit

Permalink
Add maintenance_interval field to Instance Template and Instance sche…
Browse files Browse the repository at this point in the history
…duling. (#7365)

* Move testing to handwritten

* Move testing to handwritten

* Fix lint

* Fix typo

* Add remove step and update documentation

* Add remove step and update documentation

* Fix comparing string to nil

* Add field to handwritten defs

* rebase on main & resolve conflicts

* Fix doc formatting

* Add maintenance interval to Instance resource

* Add maintenance interval to Instance resource

* Add maintenance interval to Instance resource

* Change function names to valid

* Fix type mismatch

* Fix type mismatch

* Fix function name
  • Loading branch information
kubalaguna authored Mar 7, 2023
1 parent 47a8723 commit 3e99c66
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ var (
"scheduling.0.instance_termination_action",
<% unless version == 'ga' -%>
"scheduling.0.max_run_duration",
"scheduling.0.maintenance_interval",
<% end -%>

}
Expand Down Expand Up @@ -675,6 +676,12 @@ be from 0 to 999,999,999 inclusive.`,
},
},
},
"maintenance_interval": {
Type: schema.TypeString,
Optional: true,
AtLeastOneOf: schedulingKeys,
Description: `Specifies the frequency of planned maintenance events. The accepted values are: PERIODIC`,
},
<% end -%>

},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ var (
"scheduling.0.instance_termination_action",
<% unless version == 'ga' -%>
"scheduling.0.max_run_duration",
"scheduling.0.maintenance_interval",
<% end -%>
}

Expand Down Expand Up @@ -652,6 +653,11 @@ be from 0 to 999,999,999 inclusive.`,
},
},
},
"maintenance_interval" : {
Type: schema.TypeString,
Optional: true,
Description: `Specifies the frequency of planned maintenance events. The accepted values are: PERIODIC`,
},
<% end -%>
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,43 @@ func TestAccComputeInstanceTemplate_preemptible(t *testing.T) {
})
}

<% unless version == "ga" -%>
func TestAccComputeInstanceTemplate_maintenance_interval(t *testing.T) {
t.Parallel()

var instanceTemplate compute.InstanceTemplate

VcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: TestAccProviders,
CheckDestroy: testAccCheckComputeInstanceTemplateDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeInstanceTemplate_maintenance_interval(RandString(t, 10)),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceTemplateExists(
t, "google_compute_instance_template.foobar", &instanceTemplate),
testAccCheckComputeInstanceTemplateMaintenanceInterval(&instanceTemplate, "PERIODIC"),
),
},
{
ResourceName: "google_compute_instance_template.foobar",
ImportState: true,
ImportStateVerify: true,
},
{
Config: testAccComputeInstanceTemplate_basic(RandString(t, 10)),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceTemplateExists(
t, "google_compute_instance_template.foobar", &instanceTemplate),
testAccCheckComputeInstanceTemplateMaintenanceInterval(&instanceTemplate, ""),
),
},
},
})
}
<% end -%>

func TestAccComputeInstanceTemplate_IP(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -1407,6 +1444,17 @@ func testAccCheckComputeInstanceTemplatePreemptible(instanceTemplate *compute.In
}
}

<% unless version == "ga" -%>
func testAccCheckComputeInstanceTemplateMaintenanceInterval(instanceTemplate *compute.InstanceTemplate, maintenance_interval string) resource.TestCheckFunc {
return func(s *terraform.State) error {
if instanceTemplate.Properties.Scheduling.MaintenanceInterval != maintenance_interval {
return fmt.Errorf("Expected maintenance interval value %v, got %v", maintenance_interval, instanceTemplate.Properties.Scheduling.MaintenanceInterval)
}
return nil
}
}
<% end -%>

func testAccCheckComputeInstanceTemplateProvisioningModel(instanceTemplate *compute.InstanceTemplate, provisioning_model string) resource.TestCheckFunc {
return func(s *terraform.State) error {
if instanceTemplate.Properties.Scheduling.ProvisioningModel != provisioning_model {
Expand Down Expand Up @@ -1793,6 +1841,52 @@ resource "google_compute_instance_template" "foobar" {
`, suffix)
}

<% unless version == 'ga' -%>
func testAccComputeInstanceTemplate_maintenance_interval(suffix string) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
family = "debian-11"
project = "debian-cloud"
}

resource "google_compute_instance_template" "foobar" {
name = "tf-test-instance-template-%s"
machine_type = "e2-medium"
can_ip_forward = false
tags = ["foo", "bar"]

disk {
source_image = data.google_compute_image.my_image.self_link
auto_delete = true
boot = true
}

network_interface {
network = "default"
}

scheduling {
preemptible = false
automatic_restart = true
maintenance_interval = "PERIODIC"
}

metadata = {
foo = "bar"
}

service_account {
scopes = ["userinfo-email", "compute-ro", "storage-ro"]
}

labels = {
my_label = "foobar"
}
}
`, suffix)
}
<% end -%>

func testAccComputeInstanceTemplate_ip(suffix string) string {
return fmt.Sprintf(`
resource "google_compute_address" "foo" {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,9 @@ func expandScheduling(v interface{}) (*compute.Scheduling, error) {
scheduling.MaxRunDuration = transformedMaxRunDuration
scheduling.ForceSendFields = append(scheduling.ForceSendFields, "MaxRunDuration")
}
if v, ok := original["maintenance_interval"]; ok {
scheduling.MaintenanceInterval = v.(string)
}
<% end -%>
return scheduling, nil
}
Expand Down Expand Up @@ -200,6 +203,9 @@ func flattenScheduling(resp *compute.Scheduling) []map[string]interface{} {
if resp.MaxRunDuration != nil {
schedulingMap["max_run_duration"] = flattenComputeMaxRunDuration(resp.MaxRunDuration)
}
if resp.MaintenanceInterval != "" {
schedulingMap["maintenance_interval"] = resp.MaintenanceInterval
}
<% end -%>

nodeAffinities := schema.NewSet(schema.HashResource(instanceSchedulingNodeAffinitiesElemSchema()), nil)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,6 @@ specified, then this instance will have no external IPv6 Internet access. Struct
* `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)

* `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).

<a name="nested_max_run_duration"></a>The `max_run_duration` block supports:

* `nanos` - (Optional) Span of time that's a fraction of a second at nanosecond
Expand All @@ -397,6 +396,7 @@ specified, then this instance will have no external IPv6 Internet access. Struct
315,576,000,000 inclusive. Note: these bounds are computed from: 60
sec/min * 60 min/hr * 24 hr/day * 365.25 days/year * 10000 years.

* `maintenance_interval` - (Optional) [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html) Specifies the frequency of planned maintenance events. The accepted values are: `PERIODIC`.
<a name="nested_guest_accelerator"></a>The `guest_accelerator` block supports:

* `type` (Required) - The accelerator type resource to expose to this instance. E.g. `nvidia-tesla-k80`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -565,19 +565,18 @@ specified, then this instance will have no external IPv6 Internet access. Struct
* `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)

* `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).

<a name="nested_max_run_duration"></a>The `max_run_duration` block supports:

* `nanos` - (Optional) Span of time that's a fraction of a second at nanosecond
resolution. Durations less than one second are represented with a 0
`seconds` field and a positive `nanos` field. Must be from 0 to
999,999,999 inclusive.
resolution. Durations less than one second are represented with a 0
`seconds` field and a positive `nanos` field. Must be from 0 to
999,999,999 inclusive.

* `seconds` - (Required) Span of time at a resolution of a second. Must be from 0 to
315,576,000,000 inclusive. Note: these bounds are computed from: 60
sec/min * 60 min/hr * 24 hr/day * 365.25 days/year * 10000 years.

* `maintenance_interval` - (Optional) [Beta](https://terraform.io/docs/providers/google/guides/provider_versions.html) Specifies the frequency of planned maintenance events. The accepted values are: `PERIODIC`.
<a name="nested_guest_accelerator"></a>The `guest_accelerator` block supports:

* `type` (Required) - The accelerator type resource to expose to this instance. E.g. `nvidia-tesla-k80`.
Expand Down

0 comments on commit 3e99c66

Please sign in to comment.