Skip to content

Commit 4dba306

Browse files
Update provider reference docs on quota usage and always use billing_project as quota project if appropriately specified (#12411) (#9012)
[upstream:4103a8e7d7b0baa7923972348aeb9bfbbd0097a7] Signed-off-by: Modular Magician <[email protected]>
1 parent 4249ec0 commit 4dba306

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

.changelog/12411.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:bug
2+
provider: fixed issue where `GOOGLE_CLOUD_QUOTA_PROJECT` env var would override explicit billing_project
3+
```

google-beta/transport/config.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,11 +1547,20 @@ func (c *Config) LoadAndValidate(ctx context.Context) error {
15471547
}
15481548

15491549
c.TokenSource = tokenSource
1550-
15511550
cleanCtx := context.WithValue(ctx, oauth2.HTTPClient, cleanhttp.DefaultClient())
1551+
clientOptions := []option.ClientOption{option.WithTokenSource(tokenSource)}
1552+
1553+
// The client libraries allow setting the GOOGLE_CLOUD_QUOTA_PROJECT environment variable
1554+
// directly, which unintentionally takes precedence over provider settings. Ensure that
1555+
// provider settings take precedence by applying to the client library's client directly
1556+
// b/360405077#comment8 - go/tpg-issue/17882
1557+
if c.UserProjectOverride && c.BillingProject != "" {
1558+
quotaProject := c.BillingProject
1559+
clientOptions = append(clientOptions, option.WithQuotaProject(quotaProject))
1560+
}
15521561

15531562
// 1. MTLS TRANSPORT/CLIENT - sets up proper auth headers
1554-
client, _, err := transport.NewHTTPClient(cleanCtx, option.WithTokenSource(tokenSource))
1563+
client, _, err := transport.NewHTTPClient(cleanCtx, clientOptions...)
15551564
if err != nil {
15561565
return err
15571566
}
@@ -1580,6 +1589,10 @@ func (c *Config) LoadAndValidate(ctx context.Context) error {
15801589

15811590
// Ensure $userProject is set for all HTTP requests using the client if specified by the provider config
15821591
// See https://cloud.google.com/apis/docs/system-parameters
1592+
// option.WithQuotaProject automatically sets the quota project in the client.
1593+
// However, this setting won't appear in our request logs since our logging
1594+
// transport sits above the Google client's internal transport. To ensure
1595+
// visibility in debug logging, we explicitly set the quota project here as well.
15831596
if c.UserProjectOverride && c.BillingProject != "" {
15841597
headerTransport.Set("X-Goog-User-Project", c.BillingProject)
15851598
}

website/docs/guides/provider_reference.html.markdown

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,15 @@ If you are using Terraform on your workstation we recommend that you install
6767
as a primary authentication method. You can enable ADCs by running the command
6868
`gcloud auth application-default login`.
6969

70+
<!--
71+
TODO: quota project is not currently read from ADC file b/360405077#comment8
72+
7073
Google Cloud reads the quota project for requests will be read automatically
7174
from the `core/project` value. You can override this project by specifying the
7275
`--project` flag when running `gcloud auth application-default login`. `gcloud`
7376
should return this message if you have set the correct billing project:
74-
`Quota project "your-project" was added to ADC which can be used by Google client libraries for billing and quota.`
77+
`Quota project "your-project" was added to ADC which can be used by Google client libraries for billing and quota.`
78+
-->
7579

7680
### Running Terraform on Google Cloud
7781

0 commit comments

Comments
 (0)