Skip to content

Commit 8551b00

Browse files
Fix updating NIC stack type for google compute instance (#9983) (#17295)
[upstream:a7be02a81382c71ab5647986ef63b01c795da7cb] Signed-off-by: Modular Magician <[email protected]>
1 parent e944daf commit 8551b00

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

.changelog/9983.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
compute: support updating `network_interface.stack_type` field on `google_compute_instance` resource.
3+
```

google/services/compute/resource_compute_instance.go

+17
Original file line numberDiff line numberDiff line change
@@ -1927,6 +1927,23 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err
19271927
}
19281928
}
19291929

1930+
if !updateDuringStop && d.HasChange(prefix+".stack_type") {
1931+
1932+
networkInterfacePatchObj := &compute.NetworkInterface{
1933+
StackType: d.Get(prefix + ".stack_type").(string),
1934+
Fingerprint: instNetworkInterface.Fingerprint,
1935+
}
1936+
updateCall := config.NewComputeClient(userAgent).Instances.UpdateNetworkInterface(project, zone, instance.Name, networkName, networkInterfacePatchObj).Do
1937+
op, err := updateCall()
1938+
if err != nil {
1939+
return errwrap.Wrapf("Error updating network interface: {{err}}", err)
1940+
}
1941+
opErr := ComputeOperationWaitTime(config, op, project, "network interface to update", userAgent, d.Timeout(schema.TimeoutUpdate))
1942+
if opErr != nil {
1943+
return opErr
1944+
}
1945+
}
1946+
19301947
if !updateDuringStop && d.HasChange(prefix+".ipv6_address") {
19311948

19321949
networkInterfacePatchObj := &compute.NetworkInterface{

google/services/compute/resource_compute_instance_test.go

+75
Original file line numberDiff line numberDiff line change
@@ -2789,6 +2789,33 @@ func testAccCheckComputeInstanceUpdateMachineType(t *testing.T, n string) resour
27892789
}
27902790
}
27912791

2792+
func TestAccComputeInstance_NicStackTypeUpdate(t *testing.T) {
2793+
t.Parallel()
2794+
suffix := acctest.RandString(t, 10)
2795+
envRegion := envvar.GetTestRegionFromEnv()
2796+
instanceName := fmt.Sprintf("tf-test-compute-instance-%s", suffix)
2797+
2798+
acctest.VcrTest(t, resource.TestCase{
2799+
PreCheck: func() { acctest.AccTestPreCheck(t) },
2800+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
2801+
CheckDestroy: testAccCheckComputeInstanceDestroyProducer(t),
2802+
Steps: []resource.TestStep{
2803+
{
2804+
Config: testAccComputeInstance_nicStackTypeUpdate(suffix, envRegion, "IPV4_ONLY", instanceName),
2805+
},
2806+
computeInstanceImportStep("us-central1-a", instanceName, []string{"allow_stopping_for_update"}),
2807+
{
2808+
Config: testAccComputeInstance_nicStackTypeUpdate(suffix, envRegion, "IPV4_IPV6", instanceName),
2809+
},
2810+
computeInstanceImportStep("us-central1-a", instanceName, []string{"allow_stopping_for_update"}),
2811+
{
2812+
Config: testAccComputeInstance_nicStackTypeUpdate(suffix, envRegion, "IPV4_ONLY", instanceName),
2813+
},
2814+
computeInstanceImportStep("us-central1-a", instanceName, []string{"allow_stopping_for_update"}),
2815+
},
2816+
})
2817+
}
2818+
27922819
func testAccCheckComputeInstanceDestroyProducer(t *testing.T) func(s *terraform.State) error {
27932820
return func(s *terraform.State) error {
27942821
config := acctest.GoogleProviderConfig(t)
@@ -7355,3 +7382,51 @@ resource "google_compute_disk" "debian" {
73557382
}
73567383
`, instance, diskName, suffix, suffix, suffix)
73577384
}
7385+
7386+
func testAccComputeInstance_nicStackTypeUpdate(suffix, region, stack_type, instance string) string {
7387+
return fmt.Sprintf(`
7388+
data "google_compute_image" "my_image" {
7389+
family = "debian-11"
7390+
project = "debian-cloud"
7391+
}
7392+
7393+
resource "google_compute_network" "net" {
7394+
name = "tf-test-network-%s"
7395+
enable_ula_internal_ipv6 = true
7396+
auto_create_subnetworks = false
7397+
}
7398+
7399+
resource "google_compute_subnetwork" "subnet-ipv6" {
7400+
region = "%s"
7401+
name = "tf-test-subnet-ip6-%s"
7402+
ip_cidr_range = "10.0.0.0/22"
7403+
purpose = "PRIVATE"
7404+
stack_type = "IPV4_IPV6"
7405+
ipv6_access_type = "INTERNAL"
7406+
network = google_compute_network.net.id
7407+
}
7408+
7409+
resource "google_compute_instance" "foobar" {
7410+
name = "%s"
7411+
machine_type = "e2-medium"
7412+
zone = "%s-a"
7413+
tags = ["foo", "bar"]
7414+
7415+
boot_disk {
7416+
initialize_params {
7417+
image = data.google_compute_image.my_image.self_link
7418+
}
7419+
}
7420+
7421+
network_interface {
7422+
network = google_compute_network.net.self_link
7423+
subnetwork = google_compute_subnetwork.subnet-ipv6.self_link
7424+
stack_type = "%s"
7425+
}
7426+
7427+
metadata = {
7428+
foo = "bar"
7429+
}
7430+
}
7431+
`, suffix, region, suffix, instance, region, stack_type)
7432+
}

0 commit comments

Comments
 (0)