Skip to content

Commit

Permalink
Add availability_domain field to GCE instance and instance template
Browse files Browse the repository at this point in the history
  • Loading branch information
jlagun committed May 28, 2024
1 parent bf9c43f commit 09d1f56
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,8 @@ func convertScheduling(sched *compute.Scheduling) []map[string]interface{} {
"preemptible": sched.Preemptible,
"on_host_maintenance": sched.OnHostMaintenance,
// node_affinities are not converted into cai
"node_affinities": convertSchedulingNodeAffinity(sched.NodeAffinities),
"node_affinities": convertSchedulingNodeAffinity(sched.NodeAffinities),
"availability_domain": sched.AvailabilityDomain,
}
if sched.MinNodeCpus > 0 {
data["min_node_cpus"] = sched.MinNodeCpus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ func expandScheduling(v interface{}) (*compute.Scheduling, error) {
scheduling.InstanceTerminationAction = v.(string)
scheduling.ForceSendFields = append(scheduling.ForceSendFields, "InstanceTerminationAction")
}
if v, ok := original["availability_domain"]; ok {
scheduling.AvailabilityDomain = int64(v.(int))
}
<% unless version == 'ga' -%>
if v, ok := original["max_run_duration"]; ok {
transformedMaxRunDuration, err := expandComputeMaxRunDuration(v)
Expand Down Expand Up @@ -261,6 +264,7 @@ func flattenScheduling(resp *compute.Scheduling) []map[string]interface{} {
"min_node_cpus": resp.MinNodeCpus,
"provisioning_model": resp.ProvisioningModel,
"instance_termination_action": resp.InstanceTerminationAction,
"availability_domain": resp.AvailabilityDomain,
}

if resp.AutomaticRestart != nil {
Expand Down Expand Up @@ -739,6 +743,10 @@ func schedulingHasChangeWithoutReboot(d *schema.ResourceData) bool {
return true
}

if oScheduling["availability_domain"] != newScheduling["availability_domain"] {
return true
}

return false
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ var (
"scheduling.0.min_node_cpus",
"scheduling.0.provisioning_model",
"scheduling.0.instance_termination_action",
"scheduling.0.availability_domain",
<% unless version == 'ga' -%>
"scheduling.0.max_run_duration",
"scheduling.0.maintenance_interval",
Expand Down Expand Up @@ -791,6 +792,14 @@ func ResourceComputeInstance() *schema.Resource {
AtLeastOneOf: schedulingKeys,
Description: `Specifies the action GCE should take when SPOT VM is preempted.`,
},

"availability_domain": {
Type: schema.TypeInt,
Optional: true,
AtLeastOneOf: schedulingKeys,
Description: `Specifies the availability domain (AD), which this instance should be scheduled on.`,

},
<% unless version == 'ga' -%>
"max_run_duration" : {
Type: schema.TypeList,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ var (
"scheduling.0.min_node_cpus",
"scheduling.0.provisioning_model",
"scheduling.0.instance_termination_action",
"scheduling.0.availability_domain",
<% unless version == 'ga' -%>
"scheduling.0.max_run_duration",
"scheduling.0.maintenance_interval",
Expand Down Expand Up @@ -705,6 +706,13 @@ Google Cloud KMS.`,
AtLeastOneOf: schedulingInstTemplateKeys,
Description: `Specifies the action GCE should take when SPOT VM is preempted.`,
},
"availability_domain": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
AtLeastOneOf: schedulingInstTemplateKeys,
Description: `Specifies the availability domain (AD), which this instance should be scheduled on.`,
},
<% unless version == 'ga' -%>
"max_run_duration" : {
Type: schema.TypeList,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,33 @@ func TestAccComputeInstanceTemplate_instanceResourcePolicies(t *testing.T) {
})
}

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

var template compute.InstanceTemplate
var policyName = "tf-test-policy-" + acctest.RandString(t, 10)

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckComputeInstanceTemplateDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeInstanceTemplate_instanceWithAvailabilityDomain(acctest.RandString(t, 10), policyName),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceTemplateExists(t, "google_compute_instance_template.foobar", &template),
testAccCheckComputeInstanceTemplateHasInstanceResourcePolicies(&template, policyName),
),
},
{
ResourceName: "google_compute_instance_template.foobar",
ImportState: true,
ImportStateVerify: true,
},
},
})
}

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

Expand Down Expand Up @@ -4037,6 +4064,49 @@ resource "google_compute_instance_template" "foobar" {
`, context)
}

func testAccComputeInstanceTemplate_instanceWithAvailabilityDomain(suffix string, policyName string) string {
return fmt.Sprintf(`
resource "google_compute_resource_policy" "foo" {
name = "%s"
region = "us-central1"
group_placement_policy {
availability_domain_count = 8
}
}

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-standard-4"

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

network_interface {
network = "default"
}

scheduling {
availability_domain = 5
}

resource_policies = [google_compute_resource_policy.foo.self_link]

service_account {
scopes = ["userinfo-email", "compute-ro", "storage-ro"]
}
}
`, policyName, suffix)
}


<% unless version == 'ga' -%>
func testAccComputeInstanceTemplate_network_attachment(context map[string]interface{}) string {
return acctest.Nprintf(`
Expand Down Expand Up @@ -4071,4 +4141,4 @@ resource "google_compute_instance_template" "foobar" {
}
`, context)
}
<% end -%>
<% end -%>
Original file line number Diff line number Diff line change
Expand Up @@ -9329,3 +9329,45 @@ resource "google_compute_instance" "foobar" {
}
`, suffix, region, suffix, instance, region, stack_type)
}

func testAccComputeInstance_setAvailabilityDomain(instance, suffix string) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
family = "debian-11"
project = "debian-cloud"
}

resource "google_compute_instance" "foobar" {
name = "%s"
machine_type = "c2-standard-4"
zone = "us-east4-b"
can_ip_forward = false
tags = ["foo", "bar"]

boot_disk {
initialize_params {
image = data.google_compute_image.my_image.self_link
}
}

network_interface {
network = "default"
}

scheduling {
availability_domain = 5
}

resource_policies = [google_compute_resource_policy.foo.self_link]
}


resource "google_compute_resource_policy" "foo" {
name = "tf-test-policy-%s"
region = "us-east4"
group_placement_policy {
availability_domain_count = 8
}
}
`, instance, instance, suffix)
}
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,13 @@ Google Cloud KMS.`,
AtLeastOneOf: schedulingInstTemplateKeys,
Description: `Specifies the action GCE should take when SPOT VM is preempted.`,
},
"availability_domain": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
AtLeastOneOf: schedulingInstTemplateKeys,
Description: `Specifies the availability domain, which this instance should be scheduled on.`,
},
<% unless version == 'ga' -%>
"max_run_duration" : {
Type: schema.TypeList,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,8 @@ 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)

* `availability_domain` - (Optional) Specifies the availability domain (AD), which this instance should be scheduled on. The AD belongs to the spread placement policy that has been assigned to the instance. Specify a value from 1 to max count of availability domains in your spread placement policy.

* `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`. Structure is [documented below](#nested_max_run_duration).


Expand Down

0 comments on commit 09d1f56

Please sign in to comment.