Skip to content

Commit

Permalink
fixed permadiff when subnet is optioanl (#5369) (#10420)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored Oct 27, 2021
1 parent e427812 commit bdbbf91
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
3 changes: 3 additions & 0 deletions .changelog/5369.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
compute: fixed a permadiff on `subnetwork` when it is optional on `google_compute_network_endpoint_group`
```
10 changes: 10 additions & 0 deletions google/common_diff_suppress.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,13 @@ func compareIpAddressOrSelfLinkOrResourceName(_, old, new string, _ *schema.Reso
// otherwise compare as self links
return compareSelfLinkOrResourceName("", old, new, nil)
}

// Use this method when subnet is optioanl and auto_create_subnetworks = true
// API sometimes choose a subnet so the diff needs to be ignored
func compareOptionalSubnet(_, old, new string, _ *schema.ResourceData) bool {
if isEmptyValue(reflect.ValueOf(new)) {
return true
}
// otherwise compare as self links
return compareSelfLinkOrResourceName("", old, new, nil)
}
2 changes: 1 addition & 1 deletion google/resource_compute_network_endpoint_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ you create the resource.`,
Type: schema.TypeString,
Optional: true,
ForceNew: true,
DiffSuppressFunc: compareSelfLinkOrResourceName,
DiffSuppressFunc: compareOptionalSubnet,
Description: `Optional subnetwork to which all network endpoints in the NEG belong.`,
},
"zone": {
Expand Down
48 changes: 48 additions & 0 deletions google/resource_compute_network_endpoint_group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package google

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

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

context := map[string]interface{}{
"random_suffix": randString(t, 10),
}

vcrTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckComputeNetworkEndpointGroupDestroyProducer(t),
Steps: []resource.TestStep{
{
Config: testAccComputeNetworkEndpointGroup_networkEndpointGroup(context),
},
{
ResourceName: "google_compute_network_endpoint_group.neg",
ImportState: true,
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"network", "subnetwork", "zone"},
},
},
})
}

func testAccComputeNetworkEndpointGroup_networkEndpointGroup(context map[string]interface{}) string {
return Nprintf(`
resource "google_compute_network_endpoint_group" "neg" {
name = "tf-test-my-lb-neg%{random_suffix}"
network = google_compute_network.default.id
default_port = "90"
zone = "us-central1-a"
}
resource "google_compute_network" "default" {
name = "tf-test-neg-network%{random_suffix}"
auto_create_subnetworks = true
}
`, context)
}
2 changes: 1 addition & 1 deletion google/resource_container_node_pool.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package google

import (
"context"
"fmt"
"log"
"regexp"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
Expand Down

0 comments on commit bdbbf91

Please sign in to comment.