Skip to content

Commit bdbbf91

Browse files
fixed permadiff when subnet is optioanl (#5369) (#10420)
Signed-off-by: Modular Magician <[email protected]>
1 parent e427812 commit bdbbf91

5 files changed

+63
-2
lines changed

.changelog/5369.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
compute: fixed a permadiff on `subnetwork` when it is optional on `google_compute_network_endpoint_group`
3+
```

google/common_diff_suppress.go

+10
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,13 @@ func compareIpAddressOrSelfLinkOrResourceName(_, old, new string, _ *schema.Reso
195195
// otherwise compare as self links
196196
return compareSelfLinkOrResourceName("", old, new, nil)
197197
}
198+
199+
// Use this method when subnet is optioanl and auto_create_subnetworks = true
200+
// API sometimes choose a subnet so the diff needs to be ignored
201+
func compareOptionalSubnet(_, old, new string, _ *schema.ResourceData) bool {
202+
if isEmptyValue(reflect.ValueOf(new)) {
203+
return true
204+
}
205+
// otherwise compare as self links
206+
return compareSelfLinkOrResourceName("", old, new, nil)
207+
}

google/resource_compute_network_endpoint_group.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ you create the resource.`,
8888
Type: schema.TypeString,
8989
Optional: true,
9090
ForceNew: true,
91-
DiffSuppressFunc: compareSelfLinkOrResourceName,
91+
DiffSuppressFunc: compareOptionalSubnet,
9292
Description: `Optional subnetwork to which all network endpoints in the NEG belong.`,
9393
},
9494
"zone": {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package google
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccComputeNetworkEndpointGroup_networkEndpointGroup(t *testing.T) {
10+
t.Parallel()
11+
12+
context := map[string]interface{}{
13+
"random_suffix": randString(t, 10),
14+
}
15+
16+
vcrTest(t, resource.TestCase{
17+
PreCheck: func() { testAccPreCheck(t) },
18+
Providers: testAccProviders,
19+
CheckDestroy: testAccCheckComputeNetworkEndpointGroupDestroyProducer(t),
20+
Steps: []resource.TestStep{
21+
{
22+
Config: testAccComputeNetworkEndpointGroup_networkEndpointGroup(context),
23+
},
24+
{
25+
ResourceName: "google_compute_network_endpoint_group.neg",
26+
ImportState: true,
27+
ImportStateVerify: true,
28+
ImportStateVerifyIgnore: []string{"network", "subnetwork", "zone"},
29+
},
30+
},
31+
})
32+
}
33+
34+
func testAccComputeNetworkEndpointGroup_networkEndpointGroup(context map[string]interface{}) string {
35+
return Nprintf(`
36+
resource "google_compute_network_endpoint_group" "neg" {
37+
name = "tf-test-my-lb-neg%{random_suffix}"
38+
network = google_compute_network.default.id
39+
default_port = "90"
40+
zone = "us-central1-a"
41+
}
42+
43+
resource "google_compute_network" "default" {
44+
name = "tf-test-neg-network%{random_suffix}"
45+
auto_create_subnetworks = true
46+
}
47+
`, context)
48+
}

google/resource_container_node_pool.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package google
22

33
import (
4+
"context"
45
"fmt"
56
"log"
67
"regexp"
78
"strings"
89
"time"
910

10-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
1111
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1212
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1313
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"

0 commit comments

Comments
 (0)