compute: add filter argument to google_compute_forwarding_rules data source (#20052)#17493
Open
jbbqqf wants to merge 2 commits intoGoogleCloudPlatform:mainfrom
Open
compute: add filter argument to google_compute_forwarding_rules data source (#20052)#17493jbbqqf wants to merge 2 commits intoGoogleCloudPlatform:mainfrom
jbbqqf wants to merge 2 commits intoGoogleCloudPlatform:mainfrom
Conversation
…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>
|
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. |
|
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a
filterargument to thegoogle_compute_forwarding_rulesdatasource, mirroring the
filterquery parameter supported by theunderlying
compute.forwardingRules.listREST API. Without it, userswho 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 + tomapworkarounds.Fixes hashicorp/terraform-provider-google#20052 — see hashicorp/terraform-provider-google#20052
Why
The Compute Engine
ForwardingRules.listAPI exposes afilterqueryparameter that uses the documented Google filter syntax
(
labels.env:prod,network eq ".*/my-vpc", etc.). The vendored Goclient has had
(*ForwardingRulesListCall).Filter(string)for years(see
google.golang.org/api/compute/v1/compute2-gen.go), but theTerraform 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:Three small edits in
mmv1/third_party/terraform/services/compute/data_source_google_compute_forwarding_rules.go:filterOptionalTypeStringschema entry with adescription pointing to the API docs.
Readfunction, retrieved the user-supplied filter, includedit in the data source ID (so distinct filter expressions produce
distinct IDs), and applied it via
listCall.Filter(filter)onlywhen non-empty.
Read(mirrorsthe existing pattern for
project/region).Edge cases tested
filterunset (default)# filter omittedprojects/.../regions/.../forwardingRules, every rule in the region returnedif filter != ""guard skips both the ID suffix and theFilter()callfilter = "labels.env:prod"filter = "labels.env:prod"(*ForwardingRulesListCall).Filter(string); the data source now wires itfilterwith regex syntaxfilter = "name eq \".*-prod-.*\""filter=...env:prod, one withfilter=...env:dev/filter=<expr>when setTest protocol
cd mmv1 && go run . --output ... --version ga --no-docsgo build ./google/services/compute/...(TPG)go vet ./google/services/compute/...(TPG)*_firewall_policy_with_rules.go(unchanged by this PR)filterschema entry andFilter(filter)calldata_source_google_compute_forwarding_rules.goafter regen — confirmedThis is purely additive (data source only — no resource lifecycle
implications): omitting
filterreproduces the exact previousbehavior. 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. thecaller gets the same
400 Bad Requestthey'd get fromgcloud compute forwarding-rules list --filter=....Resources
filterparam):https://cloud.google.com/compute/docs/reference/rest/v1/forwardingRules/list
https://github.com/jbbqqf/magic-modules/blob/feat/20052-forwarding-rules-filter/mmv1/third_party/terraform/services/compute/data_source_google_compute_forwarding_rules.go
Release notes
Disclosure
This PR was implemented with assistance from Claude Code. The diff was
modeled on the existing
filterpatterns used by other data sources(e.g.
google_compute_instances) and the vendored Go client'sFilter()builder. The regenerated provider compiled and vettedcleanly. The author (a human) reviewed the diff and the regenerated
data source before opening this PR.