diff --git a/mmv1/third_party/cai2hcl/services/compute/compute_instance.go b/mmv1/third_party/cai2hcl/services/compute/compute_instance.go index 0b7b07065e05..69a87445d25c 100644 --- a/mmv1/third_party/cai2hcl/services/compute/compute_instance.go +++ b/mmv1/third_party/cai2hcl/services/compute/compute_instance.go @@ -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 diff --git a/mmv1/third_party/terraform/services/compute/compute_instance_helpers.go.erb b/mmv1/third_party/terraform/services/compute/compute_instance_helpers.go.erb index c4c3f624529a..996dece1ea25 100644 --- a/mmv1/third_party/terraform/services/compute/compute_instance_helpers.go.erb +++ b/mmv1/third_party/terraform/services/compute/compute_instance_helpers.go.erb @@ -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) @@ -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 { @@ -739,6 +743,10 @@ func schedulingHasChangeWithoutReboot(d *schema.ResourceData) bool { return true } + if oScheduling["availability_domain"] != newScheduling["availability_domain"] { + return true + } + return false } diff --git a/mmv1/third_party/terraform/services/compute/resource_compute_instance.go.erb b/mmv1/third_party/terraform/services/compute/resource_compute_instance.go.erb index 7be414a69952..05ad304f60ee 100644 --- a/mmv1/third_party/terraform/services/compute/resource_compute_instance.go.erb +++ b/mmv1/third_party/terraform/services/compute/resource_compute_instance.go.erb @@ -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", @@ -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, diff --git a/mmv1/third_party/terraform/services/compute/resource_compute_instance_template.go.erb b/mmv1/third_party/terraform/services/compute/resource_compute_instance_template.go.erb index 9e652212aead..fc2a180c71f8 100644 --- a/mmv1/third_party/terraform/services/compute/resource_compute_instance_template.go.erb +++ b/mmv1/third_party/terraform/services/compute/resource_compute_instance_template.go.erb @@ -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", @@ -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, diff --git a/mmv1/third_party/terraform/services/compute/resource_compute_instance_template_test.go.erb b/mmv1/third_party/terraform/services/compute/resource_compute_instance_template_test.go.erb index 0e5a6615d473..9e84a872ce9a 100644 --- a/mmv1/third_party/terraform/services/compute/resource_compute_instance_template_test.go.erb +++ b/mmv1/third_party/terraform/services/compute/resource_compute_instance_template_test.go.erb @@ -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() @@ -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(` @@ -4071,4 +4141,4 @@ resource "google_compute_instance_template" "foobar" { } `, context) } -<% end -%> +<% end -%> \ No newline at end of file diff --git a/mmv1/third_party/terraform/services/compute/resource_compute_instance_test.go.erb b/mmv1/third_party/terraform/services/compute/resource_compute_instance_test.go.erb index ef606ea0654c..12ada8161b2b 100644 --- a/mmv1/third_party/terraform/services/compute/resource_compute_instance_test.go.erb +++ b/mmv1/third_party/terraform/services/compute/resource_compute_instance_test.go.erb @@ -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) +} \ No newline at end of file diff --git a/mmv1/third_party/terraform/services/compute/resource_compute_region_instance_template.go.erb b/mmv1/third_party/terraform/services/compute/resource_compute_region_instance_template.go.erb index ca64142f0710..561fb290750b 100644 --- a/mmv1/third_party/terraform/services/compute/resource_compute_region_instance_template.go.erb +++ b/mmv1/third_party/terraform/services/compute/resource_compute_region_instance_template.go.erb @@ -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, diff --git a/mmv1/third_party/terraform/website/docs/r/compute_instance.html.markdown b/mmv1/third_party/terraform/website/docs/r/compute_instance.html.markdown index fc8b157e2899..5198ebbafaf9 100644 --- a/mmv1/third_party/terraform/website/docs/r/compute_instance.html.markdown +++ b/mmv1/third_party/terraform/website/docs/r/compute_instance.html.markdown @@ -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).