Skip to content

compute: add filter argument to google_compute_forwarding_rules data source (#20052)#17493

Open
jbbqqf wants to merge 2 commits intoGoogleCloudPlatform:mainfrom
jbbqqf:feat/20052-forwarding-rules-filter
Open

compute: add filter argument to google_compute_forwarding_rules data source (#20052)#17493
jbbqqf wants to merge 2 commits intoGoogleCloudPlatform:mainfrom
jbbqqf:feat/20052-forwarding-rules-filter

Conversation

@jbbqqf
Copy link
Copy Markdown

@jbbqqf jbbqqf commented May 9, 2026

Summary

Adds a filter argument to the google_compute_forwarding_rules data
source, mirroring the filter query parameter supported by the
underlying compute.forwardingRules.list REST API. Without it, users
who want to enumerate forwarding rules for a specific label, network,
target type, or load-balancing scheme have to fetch every rule in the
region and filter in HCL with for_each + tomap workarounds.

Fixes hashicorp/terraform-provider-google#20052 — see hashicorp/terraform-provider-google#20052

Why

The Compute Engine ForwardingRules.list API exposes a filter query
parameter that uses the documented Google filter syntax
(labels.env:prod, network eq ".*/my-vpc", etc.). The vendored Go
client has had (*ForwardingRulesListCall).Filter(string) for years
(see google.golang.org/api/compute/v1/compute2-gen.go), but the
Terraform data source schema only exposed project / region / rules
— so users couldn't take advantage of server-side filtering, and large
GCP projects (hundreds of forwarding rules) saw both unnecessary API
load and inefficient state plans.

GCP API reference:

What changed

This change is to a hand-written Terraform data source (whose source
of truth lives in magic-modules mmv1/third_party/...). Files touched:

 .../data_source_google_compute_forwarding_rules.go | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)

Three small edits in
mmv1/third_party/terraform/services/compute/data_source_google_compute_forwarding_rules.go:

  1. Added a filter Optional TypeString schema entry with a
    description pointing to the API docs.
  2. In the Read function, retrieved the user-supplied filter, included
    it in the data source ID (so distinct filter expressions produce
    distinct IDs), and applied it via listCall.Filter(filter) only
    when non-empty.
  3. Set the resolved filter back into state at the end of Read (mirrors
    the existing pattern for project / region).

Edge cases tested

# Scenario HCL excerpt Expected Verified by
1 filter unset (default) # filter omitted Unchanged behavior — ID is projects/.../regions/.../forwardingRules, every rule in the region returned Static: the if filter != "" guard skips both the ID suffix and the Filter() call
2 filter = "labels.env:prod" filter = "labels.env:prod" Server-side filtered list Vendored client has (*ForwardingRulesListCall).Filter(string); the data source now wires it
3 filter with regex syntax filter = "name eq \".*-prod-.*\"" Server-side regex filter Same path as scenario 2; the API accepts the documented filter syntax verbatim
4 Two data-source instances with different filters one with filter=...env:prod, one with filter=...env:dev Distinct IDs, no state collision The ID now includes /filter=<expr> when set

Test protocol

Test Result Notes
cd mmv1 && go run . --output ... --version ga --no-docs OK Regeneration completed cleanly
go build ./google/services/compute/... (TPG) OK full compute service compiles
go vet ./google/services/compute/... (TPG) OK only pre-existing unreachable-code warnings in *_firewall_policy_with_rules.go (unchanged by this PR)
Generated source contains filter schema entry and Filter(filter) call OK grep'd data_source_google_compute_forwarding_rules.go after regen — confirmed

This is purely additive (data source only — no resource lifecycle
implications): omitting filter reproduces the exact previous
behavior. The only risk surface is the server's reaction to an invalid
filter expression, and that surface is identical to what
(*ForwardingRulesListCall).Filter(...) already provides — i.e. the
caller gets the same 400 Bad Request they'd get from gcloud compute forwarding-rules list --filter=....

Resources

Release notes

compute: added `filter` argument to the `google_compute_forwarding_rules` data source.

Disclosure

This PR was implemented with assistance from Claude Code. The diff was
modeled on the existing filter patterns used by other data sources
(e.g. google_compute_instances) and the vendored Go client's
Filter() builder. The regenerated provider compiled and vetted
cleanly. The author (a human) reviewed the diff and the regenerated
data source before opening this PR.

…source (#20052)

Adds a filter argument to the data source so users can scope the result
set the same way the underlying compute.forwardingRules.list API allows
(e.g. "labels.env:prod"). The Filter() builder is already exposed by the
Go client; without a Terraform-side argument, the data source returns
the entire forwarding-rule list of the region, forcing users to filter
in HCL with for_each + tomap workarounds.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@modular-magician modular-magician added the awaiting-approval Pull requests that need reviewer's approval to run presubmit tests label May 9, 2026
@google-cla
Copy link
Copy Markdown

google-cla Bot commented May 9, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@github-actions github-actions Bot requested a review from NickElliot May 9, 2026 09:16
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 9, 2026

Googlers: For automatic test runs see go/terraform-auto-test-runs.

@NickElliot, a repository maintainer, has been assigned to review your changes. If you have not received review feedback within 2 business days, please leave a comment on this PR asking them to take a look.

You can help make sure that review is quick by doing a self-review and by running impacted tests locally.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting-approval Pull requests that need reviewer's approval to run presubmit tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add filter argument to forwarding_rules data source

2 participants