Skip to content

Commit

Permalink
fixed permadiff when subnet is optioanl (#5369)
Browse files Browse the repository at this point in the history
  • Loading branch information
edwardmedia authored Oct 27, 2021
1 parent 8d92838 commit fe708a5
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
2 changes: 2 additions & 0 deletions mmv1/products/compute/terraform.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1440,6 +1440,8 @@ overrides: !ruby/object:Overrides::ResourceOverrides
zone: !ruby/object:Overrides::Terraform::PropertyOverride
required: false
default_from_api: true
subnetwork: !ruby/object:Overrides::Terraform::PropertyOverride
diff_suppress_func: 'compareOptionalSubnet'
GlobalNetworkEndpoint: !ruby/object:Overrides::Terraform::ResourceOverride
id_format: "{{project}}/{{global_network_endpoint_group}}/{{ip_address}}/{{fqdn}}/{{port}}"
mutex: networkEndpoint/{{project}}/{{global_network_endpoint_group}}
Expand Down
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)
}
10 changes: 10 additions & 0 deletions mmv1/third_party/terraform/utils/common_diff_suppress.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,13 @@ func alwaysDiffSuppress(_, _, _ string, _ *schema.ResourceData) bool {
return true
}
<% end -%>

// 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)
}

0 comments on commit fe708a5

Please sign in to comment.