Skip to content

Commit

Permalink
Fix updating NIC stack type for google compute instance (#9983) (#17295)
Browse files Browse the repository at this point in the history
[upstream:a7be02a81382c71ab5647986ef63b01c795da7cb]

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician committed Feb 15, 2024
1 parent e944daf commit 8551b00
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/9983.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
compute: support updating `network_interface.stack_type` field on `google_compute_instance` resource.
```
17 changes: 17 additions & 0 deletions google/services/compute/resource_compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -1927,6 +1927,23 @@ func resourceComputeInstanceUpdate(d *schema.ResourceData, meta interface{}) err
}
}

if !updateDuringStop && d.HasChange(prefix+".stack_type") {

networkInterfacePatchObj := &compute.NetworkInterface{
StackType: d.Get(prefix + ".stack_type").(string),
Fingerprint: instNetworkInterface.Fingerprint,
}
updateCall := config.NewComputeClient(userAgent).Instances.UpdateNetworkInterface(project, zone, instance.Name, networkName, networkInterfacePatchObj).Do
op, err := updateCall()
if err != nil {
return errwrap.Wrapf("Error updating network interface: {{err}}", err)
}
opErr := ComputeOperationWaitTime(config, op, project, "network interface to update", userAgent, d.Timeout(schema.TimeoutUpdate))
if opErr != nil {
return opErr
}
}

if !updateDuringStop && d.HasChange(prefix+".ipv6_address") {

networkInterfacePatchObj := &compute.NetworkInterface{
Expand Down
75 changes: 75 additions & 0 deletions google/services/compute/resource_compute_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2789,6 +2789,33 @@ func testAccCheckComputeInstanceUpdateMachineType(t *testing.T, n string) resour
}
}

func TestAccComputeInstance_NicStackTypeUpdate(t *testing.T) {
t.Parallel()
suffix := acctest.RandString(t, 10)
envRegion := envvar.GetTestRegionFromEnv()
instanceName := fmt.Sprintf("tf-test-compute-instance-%s", suffix)

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckComputeInstanceDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeInstance_nicStackTypeUpdate(suffix, envRegion, "IPV4_ONLY", instanceName),
},
computeInstanceImportStep("us-central1-a", instanceName, []string{"allow_stopping_for_update"}),
{
Config: testAccComputeInstance_nicStackTypeUpdate(suffix, envRegion, "IPV4_IPV6", instanceName),
},
computeInstanceImportStep("us-central1-a", instanceName, []string{"allow_stopping_for_update"}),
{
Config: testAccComputeInstance_nicStackTypeUpdate(suffix, envRegion, "IPV4_ONLY", instanceName),
},
computeInstanceImportStep("us-central1-a", instanceName, []string{"allow_stopping_for_update"}),
},
})
}

func testAccCheckComputeInstanceDestroyProducer(t *testing.T) func(s *terraform.State) error {
return func(s *terraform.State) error {
config := acctest.GoogleProviderConfig(t)
Expand Down Expand Up @@ -7355,3 +7382,51 @@ resource "google_compute_disk" "debian" {
}
`, instance, diskName, suffix, suffix, suffix)
}

func testAccComputeInstance_nicStackTypeUpdate(suffix, region, stack_type, instance string) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
family = "debian-11"
project = "debian-cloud"
}
resource "google_compute_network" "net" {
name = "tf-test-network-%s"
enable_ula_internal_ipv6 = true
auto_create_subnetworks = false
}
resource "google_compute_subnetwork" "subnet-ipv6" {
region = "%s"
name = "tf-test-subnet-ip6-%s"
ip_cidr_range = "10.0.0.0/22"
purpose = "PRIVATE"
stack_type = "IPV4_IPV6"
ipv6_access_type = "INTERNAL"
network = google_compute_network.net.id
}
resource "google_compute_instance" "foobar" {
name = "%s"
machine_type = "e2-medium"
zone = "%s-a"
tags = ["foo", "bar"]
boot_disk {
initialize_params {
image = data.google_compute_image.my_image.self_link
}
}
network_interface {
network = google_compute_network.net.self_link
subnetwork = google_compute_subnetwork.subnet-ipv6.self_link
stack_type = "%s"
}
metadata = {
foo = "bar"
}
}
`, suffix, region, suffix, instance, region, stack_type)
}

0 comments on commit 8551b00

Please sign in to comment.