Skip to content

Commit 90df703

Browse files
Fix address group ordering for network firewall policy rule (#12182) (#8592)
[upstream:b41c48e34de620d6042a2af0a1e905de9d66c211] Signed-off-by: Modular Magician <[email protected]>
1 parent 2dd0fd6 commit 90df703

File tree

3 files changed

+153
-2
lines changed

3 files changed

+153
-2
lines changed

.changelog/12182.txt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
compute: fixed a diff based on server-side reordering of `match.src_address_groups` and `match.dest_address_groups` in `google_compute_network_firewall_policy_rule`
3+
```

google-beta/services/compute/resource_compute_network_firewall_policy_rule.go

+46-2
Original file line numberDiff line numberDiff line change
@@ -874,11 +874,55 @@ func flattenComputeNetworkFirewallPolicyRuleMatchSrcSecureTagsState(v interface{
874874
}
875875

876876
func flattenComputeNetworkFirewallPolicyRuleMatchDestAddressGroups(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
877-
return v
877+
rawConfigValue := d.Get("match.0.dest_address_groups")
878+
879+
// Convert config value to []string
880+
configValue, err := tpgresource.InterfaceSliceToStringSlice(rawConfigValue)
881+
if err != nil {
882+
log.Printf("[ERROR] Failed to convert config value: %s", err)
883+
return v
884+
}
885+
886+
// Convert v to []string
887+
apiStringValue, err := tpgresource.InterfaceSliceToStringSlice(v)
888+
if err != nil {
889+
log.Printf("[ERROR] Failed to convert API value: %s", err)
890+
return v
891+
}
892+
893+
sortedStrings, err := tpgresource.SortStringsByConfigOrder(configValue, apiStringValue)
894+
if err != nil {
895+
log.Printf("[ERROR] Could not sort API response value: %s", err)
896+
return v
897+
}
898+
899+
return sortedStrings
878900
}
879901

880902
func flattenComputeNetworkFirewallPolicyRuleMatchSrcAddressGroups(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {
881-
return v
903+
rawConfigValue := d.Get("match.0.src_address_groups")
904+
905+
// Convert config value to []string
906+
configValue, err := tpgresource.InterfaceSliceToStringSlice(rawConfigValue)
907+
if err != nil {
908+
log.Printf("[ERROR] Failed to convert config value: %s", err)
909+
return v
910+
}
911+
912+
// Convert v to []string
913+
apiStringValue, err := tpgresource.InterfaceSliceToStringSlice(v)
914+
if err != nil {
915+
log.Printf("[ERROR] Failed to convert API value: %s", err)
916+
return v
917+
}
918+
919+
sortedStrings, err := tpgresource.SortStringsByConfigOrder(configValue, apiStringValue)
920+
if err != nil {
921+
log.Printf("[ERROR] Could not sort API response value: %s", err)
922+
return v
923+
}
924+
925+
return sortedStrings
882926
}
883927

884928
func flattenComputeNetworkFirewallPolicyRuleMatchSrcFqdns(v interface{}, d *schema.ResourceData, config *transport_tpg.Config) interface{} {

google-beta/services/compute/resource_compute_network_firewall_policy_rule_test.go

+104
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,41 @@ func TestAccComputeNetworkFirewallPolicyRule_multipleRules(t *testing.T) {
142142
})
143143
}
144144

145+
func TestAccComputeNetworkFirewallPolicyRule_addressGroupOrder(t *testing.T) {
146+
t.Parallel()
147+
148+
context := map[string]interface{}{
149+
"random_suffix": acctest.RandString(t, 10),
150+
"project": envvar.GetTestProjectFromEnv(),
151+
}
152+
153+
acctest.VcrTest(t, resource.TestCase{
154+
PreCheck: func() { acctest.AccTestPreCheck(t) },
155+
ProtoV5ProviderFactories: acctest.ProtoV5ProviderFactories(t),
156+
Steps: []resource.TestStep{
157+
{
158+
Config: testAccComputeNetworkFirewallPolicyRule_addressGroupOrder(context),
159+
},
160+
{
161+
ResourceName: "google_compute_network_firewall_policy_rule.src_test",
162+
ImportState: true,
163+
ImportStateVerify: true,
164+
// Referencing using ID causes import to fail
165+
// Client-side reordering doesn't work with no state, so ignore on import
166+
ImportStateVerifyIgnore: []string{"firewall_policy", "match.0.src_address_groups"},
167+
},
168+
{
169+
ResourceName: "google_compute_network_firewall_policy_rule.dest_test",
170+
ImportState: true,
171+
ImportStateVerify: true,
172+
// Referencing using ID causes import to fail
173+
// Client-side reordering doesn't work with no state, so ignore on import
174+
ImportStateVerifyIgnore: []string{"firewall_policy", "match.0.dest_address_groups"},
175+
},
176+
},
177+
})
178+
}
179+
145180
func TestAccComputeNetworkFirewallPolicyRule_securityProfileGroup_update(t *testing.T) {
146181
t.Parallel()
147182

@@ -898,3 +933,72 @@ resource "google_compute_network_firewall_policy_rule" "fw_policy_rule3" {
898933
}
899934
`, context)
900935
}
936+
937+
func testAccComputeNetworkFirewallPolicyRule_addressGroupOrder(context map[string]interface{}) string {
938+
return acctest.Nprintf(`
939+
resource "google_compute_network_firewall_policy" "policy" {
940+
name = "tf-test-policy-%{random_suffix}"
941+
description = "Resource created for Terraform acceptance testing"
942+
}
943+
944+
resource "google_network_security_address_group" "add-group1" {
945+
name = "tf-test-group-1-%{random_suffix}"
946+
parent = "projects/%{project}"
947+
location = "global"
948+
type = "IPV4"
949+
capacity = "10"
950+
items = ["10.0.1.1/32"]
951+
}
952+
resource "google_network_security_address_group" "add-group2" {
953+
name = "tf-test-group-2-%{random_suffix}"
954+
parent = "projects/%{project}"
955+
location = "global"
956+
type = "IPV4"
957+
capacity = "10"
958+
items = ["10.0.2.2/32"]
959+
}
960+
resource "google_network_security_address_group" "add-group3" {
961+
name = "tf-test-group-3-%{random_suffix}"
962+
parent = "projects/%{project}"
963+
location = "global"
964+
type = "IPV4"
965+
capacity = "10"
966+
items = ["10.0.3.3/32"]
967+
}
968+
969+
resource "google_compute_network_firewall_policy_rule" "src_test" {
970+
firewall_policy = google_compute_network_firewall_policy.policy.id
971+
action = "allow"
972+
priority = 1000
973+
description = "Testing address group order issue"
974+
direction = "INGRESS"
975+
enable_logging = true
976+
match {
977+
src_address_groups = [google_network_security_address_group.add-group2.id,
978+
google_network_security_address_group.add-group1.id]
979+
dest_ip_ranges = ["192.168.2.0/24", "10.0.3.4/32"]
980+
layer4_configs {
981+
ip_protocol = "all"
982+
}
983+
}
984+
}
985+
986+
resource "google_compute_network_firewall_policy_rule" "dest_test" {
987+
firewall_policy = google_compute_network_firewall_policy.policy.id
988+
action = "allow"
989+
priority = 1100
990+
description = "Testing address group order issue"
991+
direction = "EGRESS"
992+
enable_logging = true
993+
match {
994+
dest_address_groups = [google_network_security_address_group.add-group3.id,
995+
google_network_security_address_group.add-group2.id]
996+
src_ip_ranges = ["192.168.2.0/24", "10.0.3.4/32"]
997+
layer4_configs {
998+
ip_protocol = "all"
999+
}
1000+
}
1001+
}
1002+
1003+
`, context)
1004+
}

0 commit comments

Comments
 (0)