Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix condition in forceNewIfNetworkIPNotUpdatableFunc #19135

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/11395.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
compute: fixed force diff replacement logic for `network_ip` on resource `google_compute_instance`
```
4 changes: 3 additions & 1 deletion google/services/compute/resource_compute_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,13 @@ func forceNewIfNetworkIPNotUpdatableFunc(d tpgresource.TerraformResourceDiff) er
for i := 0; i < newCount.(int); i++ {
prefix := fmt.Sprintf("network_interface.%d", i)
networkKey := prefix + ".network"
oldN, newN := d.GetChange(networkKey)
subnetworkKey := prefix + ".subnetwork"
oldS, newS := d.GetChange(subnetworkKey)
subnetworkProjectKey := prefix + ".subnetwork_project"
networkIPKey := prefix + ".network_ip"
if d.HasChange(networkIPKey) {
if !d.HasChange(networkKey) && !d.HasChange(subnetworkKey) && !d.HasChange(subnetworkProjectKey) {
if tpgresource.CompareSelfLinkOrResourceName("", oldS.(string), newS.(string), nil) && !d.HasChange(subnetworkProjectKey) && tpgresource.CompareSelfLinkOrResourceName("", oldN.(string), newN.(string), nil) {
if err := d.ForceNew(networkIPKey); err != nil {
return err
}
Expand Down
178 changes: 178 additions & 0 deletions google/services/compute/resource_compute_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2668,6 +2668,46 @@ func TestAccComputeInstance_subnetworkUpdate(t *testing.T) {
})
}

func TestAccComputeInstance_networkIpUpdate(t *testing.T) {
t.Parallel()

var instance compute.Instance
instanceName := fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
suffix := fmt.Sprintf("%s", acctest.RandString(t, 10))

acctest.VcrTest(t, resource.TestCase{
PreCheck: func() { acctest.AccTestPreCheck(t) },
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
CheckDestroy: testAccCheckComputeInstanceDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeInstance_networkIpUpdate(suffix, instanceName),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceExists(t, "google_compute_instance.foobar", &instance),
testAccCheckComputeInstanceHasNetworkIP(&instance, "10.3.0.3"),
),
},
computeInstanceImportStep("us-east1-d", instanceName, []string{}),
{
Config: testAccComputeInstance_networkIpUpdateByHand(suffix, instanceName),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceExists(t, "google_compute_instance.foobar", &instance),
testAccCheckComputeInstanceHasNetworkIP(&instance, "10.3.0.4"),
),
},
computeInstanceImportStep("us-east1-d", instanceName, []string{}),
{
Config: testAccComputeInstance_networkIpUpdateWithComputeAddress(suffix, instanceName),
Check: resource.ComposeTestCheckFunc(
testAccCheckComputeInstanceExists(t, "google_compute_instance.foobar", &instance),
testAccCheckComputeInstanceHasNetworkIP(&instance, "10.3.0.5"),
),
},
computeInstanceImportStep("us-east1-d", instanceName, []string{}),
},
})
}

func TestAccComputeInstance_queueCount(t *testing.T) {
t.Parallel()
instanceName := fmt.Sprintf("tf-test-%s", acctest.RandString(t, 10))
Expand Down Expand Up @@ -7979,6 +8019,144 @@ func testAccComputeInstance_subnetworkUpdateTwo(suffix, instance string) string
`, suffix, suffix, suffix, suffix, instance)
}

func testAccComputeInstance_networkIpUpdate(suffix, instance string) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
family = "debian-11"
project = "debian-cloud"
}

resource "google_compute_network" "inst-test-network" {
name = "tf-test-network-%s"
auto_create_subnetworks = false
}

resource "google_compute_subnetwork" "inst-test-subnetwork" {
name = "tf-test-compute-subnet-%s"
ip_cidr_range = "10.3.0.0/16"
region = "us-east1"
network = google_compute_network.inst-test-network.id
}

resource "google_compute_address" "inst-test-address" {
name = "tf-test-compute-address-%s"
region = "us-east1"
subnetwork = google_compute_subnetwork.inst-test-subnetwork.id
address_type = "INTERNAL"
address = "10.3.0.5"
}

resource "google_compute_instance" "foobar" {
name = "%s"
machine_type = "e2-medium"
zone = "us-east1-d"

boot_disk {
initialize_params {
image = data.google_compute_image.my_image.id
}
}

network_interface {
subnetwork = google_compute_subnetwork.inst-test-subnetwork.id
network_ip = "10.3.0.3"
}
}
`, suffix, suffix, suffix, instance)
}

func testAccComputeInstance_networkIpUpdateByHand(suffix, instance string) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
family = "debian-11"
project = "debian-cloud"
}

resource "google_compute_network" "inst-test-network" {
name = "tf-test-network-%s"
auto_create_subnetworks = false
}

resource "google_compute_subnetwork" "inst-test-subnetwork" {
name = "tf-test-compute-subnet-%s"
ip_cidr_range = "10.3.0.0/16"
region = "us-east1"
network = google_compute_network.inst-test-network.id
}

resource "google_compute_address" "inst-test-address" {
name = "tf-test-compute-address-%s"
region = "us-east1"
subnetwork = google_compute_subnetwork.inst-test-subnetwork.id
address_type = "INTERNAL"
address = "10.3.0.5"
}

resource "google_compute_instance" "foobar" {
name = "%s"
machine_type = "e2-medium"
zone = "us-east1-d"

boot_disk {
initialize_params {
image = data.google_compute_image.my_image.id
}
}

network_interface {
subnetwork = google_compute_subnetwork.inst-test-subnetwork.id
network_ip = "10.3.0.4"
}
}
`, suffix, suffix, suffix, instance)
}

func testAccComputeInstance_networkIpUpdateWithComputeAddress(suffix, instance string) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
family = "debian-11"
project = "debian-cloud"
}

resource "google_compute_network" "inst-test-network" {
name = "tf-test-network-%s"
auto_create_subnetworks = false
}

resource "google_compute_subnetwork" "inst-test-subnetwork" {
name = "tf-test-compute-subnet-%s"
ip_cidr_range = "10.3.0.0/16"
region = "us-east1"
network = google_compute_network.inst-test-network.id
}

resource "google_compute_address" "inst-test-address" {
name = "tf-test-compute-address-%s"
region = "us-east1"
subnetwork = google_compute_subnetwork.inst-test-subnetwork.id
address_type = "INTERNAL"
address = "10.3.0.5"
}

resource "google_compute_instance" "foobar" {
name = "%s"
machine_type = "e2-medium"
zone = "us-east1-d"

boot_disk {
initialize_params {
image = data.google_compute_image.my_image.id
}
}

network_interface {
subnetwork = google_compute_subnetwork.inst-test-subnetwork.id
network_ip = google_compute_address.inst-test-address.address
}
}
`, suffix, suffix, suffix, instance)
}

func testAccComputeInstance_queueCountSet(instance string) string {
return fmt.Sprintf(`
data "google_compute_image" "my_image" {
Expand Down