Skip to content

Commit 5e0966d

Browse files
Timus1712Timofey Yushchenko
authored andcommitted
Add availability domain field to instance and instance template resource (GoogleCloudPlatform#12566)
Co-authored-by: Timofey Yushchenko <[email protected]>
1 parent 3e4662a commit 5e0966d

11 files changed

+284
-0
lines changed

mmv1/third_party/cai2hcl/services/compute/compute_instance.go

+3
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ func convertScheduling(sched *compute.Scheduling) []map[string]interface{} {
219219
if len(sched.ProvisioningModel) > 0 {
220220
data["provisioning_model"] = sched.ProvisioningModel
221221
}
222+
if sched.AvailabilityDomain > 0 {
223+
data["availability_domain"] = sched.AvailabilityDomain
224+
}
222225
return []map[string]interface{}{data}
223226
}
224227

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

+7
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@ func expandScheduling(v interface{}) (*compute.Scheduling, error) {
146146
scheduling.InstanceTerminationAction = v.(string)
147147
scheduling.ForceSendFields = append(scheduling.ForceSendFields, "InstanceTerminationAction")
148148
}
149+
if v, ok := original["availability_domain"]; ok && v != nil {
150+
scheduling.AvailabilityDomain = int64(v.(int))
151+
}
149152
if v, ok := original["max_run_duration"]; ok {
150153
transformedMaxRunDuration, err := expandComputeMaxRunDuration(v)
151154
if err != nil {
@@ -282,6 +285,7 @@ func flattenScheduling(resp *compute.Scheduling) []map[string]interface{} {
282285
"min_node_cpus": resp.MinNodeCpus,
283286
"provisioning_model": resp.ProvisioningModel,
284287
"instance_termination_action": resp.InstanceTerminationAction,
288+
"availability_domain": resp.AvailabilityDomain,
285289
}
286290

287291
if resp.AutomaticRestart != nil {
@@ -757,6 +761,9 @@ func schedulingHasChangeWithoutReboot(d *schema.ResourceData) bool {
757761
if oScheduling["instance_termination_action"] != newScheduling["instance_termination_action"] {
758762
return true
759763
}
764+
if oScheduling["availability_domain"] != newScheduling["availability_domain"] {
765+
return true
766+
}
760767
{{- if ne $.TargetVersionName "ga" }}
761768
if oScheduling["host_error_timeout_seconds"] != newScheduling["host_error_timeout_seconds"] {
762769
return true

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

+7
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.availability_domain",
9798
"scheduling.0.max_run_duration",
9899
"scheduling.0.on_instance_stop_action",
99100
{{- if ne $.TargetVersionName "ga" }}
@@ -901,6 +902,12 @@ func ResourceComputeInstance() *schema.Resource {
901902
AtLeastOneOf: schedulingKeys,
902903
Description: `Specifies the action GCE should take when SPOT VM is preempted.`,
903904
},
905+
"availability_domain": {
906+
Type: schema.TypeInt,
907+
Optional: true,
908+
AtLeastOneOf: schedulingKeys,
909+
Description: `Specifies the availability domain, which this instance should be scheduled on.`,
910+
},
904911
"max_run_duration" : {
905912
Type: schema.TypeList,
906913
Optional: true,

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

+8
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ var (
3434
"scheduling.0.min_node_cpus",
3535
"scheduling.0.provisioning_model",
3636
"scheduling.0.instance_termination_action",
37+
"scheduling.0.availability_domain",
3738
"scheduling.0.max_run_duration",
3839
"scheduling.0.on_instance_stop_action",
3940
{{- if ne $.TargetVersionName "ga" }}
@@ -728,6 +729,13 @@ Google Cloud KMS.`,
728729
AtLeastOneOf: schedulingInstTemplateKeys,
729730
Description: `Specifies the action GCE should take when SPOT VM is preempted.`,
730731
},
732+
"availability_domain": {
733+
Type: schema.TypeInt,
734+
Optional: true,
735+
ForceNew: true,
736+
AtLeastOneOf: schedulingInstTemplateKeys,
737+
Description: `Specifies the availability domain, which this instance should be scheduled on.`,
738+
},
731739
"max_run_duration" : {
732740
Type: schema.TypeList,
733741
Optional: true,

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

+80
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,34 @@ func TestAccComputeInstanceTemplate_instanceResourcePolicies(t *testing.T) {
738738
})
739739
}
740740

741+
func TestAccComputeInstanceTemplate_instanceResourcePoliciesSpread(t *testing.T) {
742+
t.Parallel()
743+
744+
var template compute.InstanceTemplate
745+
var policyName = "tf-test-policy-" + acctest.RandString(t, 10)
746+
747+
acctest.VcrTest(t, resource.TestCase{
748+
PreCheck: func() { acctest.AccTestPreCheck(t) },
749+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
750+
CheckDestroy: testAccCheckComputeInstanceTemplateDestroyProducer(t),
751+
Steps: []resource.TestStep{
752+
{
753+
Config: testAccComputeInstanceTemplate_instanceResourcePolicySpread(acctest.RandString(t, 10), policyName),
754+
Check: resource.ComposeTestCheckFunc(
755+
testAccCheckComputeInstanceTemplateExists(t, "google_compute_instance_template.foobar", &template),
756+
testAccCheckComputeInstanceTemplateHasInstanceResourcePolicies(&template, policyName),
757+
testAccCheckComputeInstanceTemplateHasAvailabilityDomain(&template, 3),
758+
),
759+
},
760+
{
761+
ResourceName: "google_compute_instance_template.foobar",
762+
ImportState: true,
763+
ImportStateVerify: true,
764+
},
765+
},
766+
})
767+
}
768+
741769
func TestAccComputeInstanceTemplate_reservationAffinities(t *testing.T) {
742770
t.Parallel()
743771

@@ -2235,6 +2263,15 @@ func testAccCheckComputeInstanceTemplateHasInstanceResourcePolicies(instanceTemp
22352263

22362264
}
22372265

2266+
func testAccCheckComputeInstanceTemplateHasAvailabilityDomain(instanceTemplate *compute.InstanceTemplate, availabilityDomain int64) resource.TestCheckFunc {
2267+
return func (s *terraform.State) error {
2268+
if instanceTemplate.Properties.Scheduling.AvailabilityDomain != availabilityDomain {
2269+
return fmt.Errorf("Expected availability_domain %d, got %d", availabilityDomain, instanceTemplate.Properties.Scheduling.AvailabilityDomain)
2270+
}
2271+
return nil
2272+
}
2273+
}
2274+
22382275
func testAccCheckComputeInstanceTemplateHasReservationAffinity(instanceTemplate *compute.InstanceTemplate, consumeReservationType string, specificReservationNames ...string) resource.TestCheckFunc {
22392276
if len(specificReservationNames) > 1 {
22402277
panic("too many specificReservationNames in test")
@@ -3605,6 +3642,49 @@ resource "google_compute_instance_template" "foobar" {
36053642
`, policyName, suffix)
36063643
}
36073644

3645+
func testAccComputeInstanceTemplate_instanceResourcePolicySpread(suffix string, policyName string) string {
3646+
return fmt.Sprintf(`
3647+
resource "google_compute_resource_policy" "foo" {
3648+
name = "%s"
3649+
region = "us-central1"
3650+
group_placement_policy {
3651+
availability_domain_count = 5
3652+
}
3653+
}
3654+
3655+
data "google_compute_image" "my_image" {
3656+
family = "debian-11"
3657+
project = "debian-cloud"
3658+
}
3659+
3660+
resource "google_compute_instance_template" "foobar" {
3661+
name = "tf-test-instance-template-%s"
3662+
machine_type = "e2-standard-4"
3663+
3664+
disk {
3665+
source_image = data.google_compute_image.my_image.self_link
3666+
auto_delete = true
3667+
boot = true
3668+
}
3669+
3670+
network_interface {
3671+
network = "default"
3672+
}
3673+
3674+
scheduling {
3675+
availability_domain = 3
3676+
}
3677+
3678+
resource_policies = [google_compute_resource_policy.foo.self_link]
3679+
3680+
service_account {
3681+
scopes = ["userinfo-email", "compute-ro", "storage-ro"]
3682+
}
3683+
}
3684+
`, policyName, suffix)
3685+
}
3686+
3687+
36083688
func testAccComputeInstanceTemplate_reservationAffinityInstanceTemplate_nonSpecificReservation(templateName, consumeReservationType string) string {
36093689
return fmt.Sprintf(`
36103690
data "google_compute_image" "my_image" {

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

+86
Original file line numberDiff line numberDiff line change
@@ -2999,6 +2999,31 @@ func TestAccComputeInstance_resourcePolicyCollocate(t *testing.T) {
29992999
})
30003000
}
30013001

3002+
func TestAccComputeInstance_resourcePolicySpread(t *testing.T) {
3003+
t.Parallel()
3004+
3005+
instanceName := fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
3006+
var instance compute.Instance
3007+
3008+
acctest.VcrTest(t, resource.TestCase{
3009+
PreCheck: func() { acctest.AccTestPreCheck(t) },
3010+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
3011+
CheckDestroy: testAccCheckComputeInstanceDestroyProducer(t),
3012+
Steps: []resource.TestStep{
3013+
{
3014+
Config: testAccComputeInstance_resourcePolicySpread(instanceName, acctest.RandString(t, 10)),
3015+
Check: resource.ComposeTestCheckFunc(
3016+
testAccCheckComputeInstanceExists(
3017+
t, "google_compute_instance.foobar", &instance),
3018+
testAccCheckComputeInstanceHasStatus(&instance, "RUNNING"),
3019+
testAccCheckComputeInstanceHasAvailabilityDomain(&instance, 3),
3020+
),
3021+
},
3022+
computeInstanceImportStep("us-east4-b", instanceName, []string{"allow_stopping_for_update"}),
3023+
},
3024+
})
3025+
}
3026+
30023027
func TestAccComputeInstance_subnetworkUpdate(t *testing.T) {
30033028
t.Parallel()
30043029
instanceName := fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
@@ -4436,6 +4461,23 @@ func testAccCheckComputeInstanceMaxRunDuration(instance *compute.Instance, insta
44364461
}
44374462
}
44384463

4464+
func testAccCheckComputeInstanceHasAvailabilityDomain(instance *compute.Instance, availabilityDomain int64) resource.TestCheckFunc {
4465+
return func(s *terraform.State) error {
4466+
if instance == nil {
4467+
return fmt.Errorf("instance is nil")
4468+
}
4469+
if instance.Scheduling == nil {
4470+
return fmt.Errorf("no scheduling")
4471+
}
4472+
4473+
if instance.Scheduling.AvailabilityDomain != availabilityDomain {
4474+
return fmt.Errorf("got the wrong availability domain: have %d; want %d", instance.Scheduling.AvailabilityDomain, availabilityDomain)
4475+
}
4476+
4477+
return nil
4478+
}
4479+
}
4480+
44394481
func testAccCheckComputeInstanceLocalSsdRecoveryTimeout(instance *compute.Instance, instanceLocalSsdRecoveryTiemoutWant compute.Duration) resource.TestCheckFunc {
44404482
return func(s *terraform.State) error {
44414483
if instance == nil {
@@ -9349,6 +9391,50 @@ resource "google_compute_resource_policy" "foo" {
93499391
`, instance, instance, suffix)
93509392
}
93519393

9394+
func testAccComputeInstance_resourcePolicySpread(instance, suffix string) string {
9395+
return fmt.Sprintf(`
9396+
data "google_compute_image" "my_image" {
9397+
family = "debian-11"
9398+
project = "debian-cloud"
9399+
}
9400+
9401+
resource "google_compute_instance" "foobar" {
9402+
name = "%s"
9403+
machine_type = "e2-standard-4"
9404+
zone = "us-east4-b"
9405+
can_ip_forward = false
9406+
tags = ["foo", "bar"]
9407+
9408+
//deletion_protection = false is implicit in this config due to default value
9409+
9410+
boot_disk {
9411+
initialize_params {
9412+
image = data.google_compute_image.my_image.self_link
9413+
}
9414+
}
9415+
9416+
network_interface {
9417+
network = "default"
9418+
}
9419+
9420+
scheduling {
9421+
availability_domain = 3
9422+
}
9423+
9424+
resource_policies = [google_compute_resource_policy.foo.self_link]
9425+
}
9426+
9427+
resource "google_compute_resource_policy" "foo" {
9428+
name = "tf-test-policy-%s"
9429+
region = "us-east4"
9430+
group_placement_policy {
9431+
availability_domain_count = 3
9432+
}
9433+
}
9434+
9435+
`, instance, suffix)
9436+
}
9437+
93529438
func testAccComputeInstance_subnetworkUpdate(suffix, instance string) string {
93539439
return fmt.Sprintf(`
93549440
data "google_compute_image" "my_image" {

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

+7
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,13 @@ Google Cloud KMS.`,
687687
AtLeastOneOf: schedulingInstTemplateKeys,
688688
Description: `Specifies the action GCE should take when SPOT VM is preempted.`,
689689
},
690+
"availability_domain": {
691+
Type: schema.TypeInt,
692+
Optional: true,
693+
ForceNew: true,
694+
AtLeastOneOf: schedulingInstTemplateKeys,
695+
Description: `Specifies the availability domain, which this instance should be scheduled on.`,
696+
},
690697
"max_run_duration" : {
691698
Type: schema.TypeList,
692699
Optional: true,

0 commit comments

Comments
 (0)