Skip to content

Commit

Permalink
Fix updating NIC stack type for google compute instance (GoogleCloudP…
Browse files Browse the repository at this point in the history
…latform#9983)

Co-authored-by: Timofey Yushchenko <[email protected]>
  • Loading branch information
2 people authored and balanaguharsha committed Apr 19, 2024
1 parent faeda5c commit 60d725c
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ func ResourceComputeInstance() *schema.Resource {

"ipv6_access_config": {
Type: schema.TypeList,
Optional: true,
Optional: true,
Description: `An array of IPv6 access configurations for this interface. Currently, only one IPv6 access config, DIRECT_IPV6, is supported. If there is no ipv6AccessConfig specified, then this instance will have no external IPv6 Internet access.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -2067,6 +2067,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
Original file line number Diff line number Diff line change
Expand Up @@ -3279,6 +3279,33 @@ func TestAccComputeInstance_NetworkAttachmentUpdate(t *testing.T) {
}
<% end %>

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 @@ -8964,3 +8991,50 @@ resource "google_compute_instance" "foobar" {
}
<% end %>

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 60d725c

Please sign in to comment.