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

Add firewall source range DSF #10439

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/5309.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:breaking-change
compute: changed `source_ranges` in `google_compute_firewall_rule` to track changes when it is not set in a config file
```
40 changes: 37 additions & 3 deletions google/resource_compute_firewall.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,40 @@ func resourceComputeFirewallSourceFieldsCustomizeDiff(_ context.Context, diff *s
return nil
}

func diffSuppressSourceRanges(k, old, new string, d *schema.ResourceData) bool {
if k == "source_ranges.#" {
if old == "1" && new == "0" {
// Allow diffing on the individual element if we are going from 1 -> 0
// this allows for diff suppress on ["0.0.0.0/0"] -> []
return true
}
return old == new
}
kLength := "source_ranges.#"
oldLength, newLength := d.GetChange(kLength)
oldInt, ok := oldLength.(int)

if !ok {
return false
}

newInt, ok := newLength.(int)
if !ok {
return false
}

// Diff suppress only should suppress removing the default range
// This should probably be newInt == 0, but due to Terraform core internals
// (bug?) values found via GetChange may not have the correct new value
// in some circumstances
if oldInt == 1 && newInt == 1 {
if old == "0.0.0.0/0" && new == "" {
return true
}
}
return old == new
}

func resourceComputeFirewall() *schema.Resource {
return &schema.Resource{
Create: resourceComputeFirewallCreate,
Expand Down Expand Up @@ -232,9 +266,9 @@ precedence over ALLOW rules having equal priority.`,
Default: 1000,
},
"source_ranges": {
Type: schema.TypeSet,
Computed: true,
Optional: true,
Type: schema.TypeSet,
Optional: true,
DiffSuppressFunc: diffSuppressSourceRanges,
Description: `If source ranges are specified, the firewall will apply only to
traffic that has source IP address in these ranges. These ranges must
be expressed in CIDR format. One or both of sourceRanges and
Expand Down