Skip to content

Commit 710609b

Browse files
Send billing_project for every request when set (#5086) (#3539)
Signed-off-by: Modular Magician <[email protected]>
1 parent f852342 commit 710609b

File tree

4 files changed

+50
-32
lines changed

4 files changed

+50
-32
lines changed

.changelog/5086.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:enhancement
2+
provider: added support for `billing_project` across all resources. If `user_project_override` is set to `true` and a `billing_project` is set, the `X-Goog-User-Project` header will be sent for all resources.
3+
```

google-beta/config.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,7 @@ func (c *Config) LoadAndValidate(ctx context.Context) error {
397397
if err != nil {
398398
return err
399399
}
400+
400401
// Userinfo is fetched before request logging is enabled to reduce additional noise.
401402
err = c.logGoogleIdentities()
402403
if err != nil {
@@ -419,6 +420,12 @@ func (c *Config) LoadAndValidate(ctx context.Context) error {
419420
headerTransport.Set("X-Goog-Request-Reason", c.RequestReason)
420421
}
421422

423+
// Ensure $userProject is set for all HTTP requests using the client if specified by the provider config
424+
// See https://cloud.google.com/apis/docs/system-parameters
425+
if c.UserProjectOverride && c.BillingProject != "" {
426+
headerTransport.Set("X-Goog-User-Project", c.BillingProject)
427+
}
428+
422429
// Set final transport value.
423430
client.Transport = headerTransport
424431

google-beta/resource_gke_hub_feature_membership_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"fmt"
66
"testing"
77

8-
dcl "github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl"
8+
"github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl"
99
gkehub "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/gkehub/beta"
1010
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1111
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"

website/docs/guides/provider_reference.html.markdown

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,18 @@ authenticate HTTP requests to GCP APIs. This is an alternative to `credentials`,
121121
and ignores the `scopes` field. If both are specified, `access_token` will be
122122
used over the `credentials` field.
123123

124-
* `user_project_override` - (Optional) Defaults to false. If true, uses the
125-
resource project for preconditions, quota, and billing, instead of the project
126-
the credentials belong to. Not all resources support this- see the
127-
documentation for each resource to learn whether it does.
128-
129-
* `billing_project` - (Optional) This fields specifies a project that's used for
130-
preconditions, quota, and billing for requests. All resources that support user project
131-
overrides will use this project instead of the resource's project (if available). This
132-
field is ignored if `user_project_override` is set to false or unset.
124+
* `user_project_override` - (Optional) Defaults to `false`. Controls the quota
125+
project used in requests to GCP APIs for the purpose of preconditions, quota,
126+
and billing. If `false`, the quota project is determined by the API and may be
127+
the project associated with your credentials, or the resource project. If `true`,
128+
most resources in the provider will explicitly supply their resource project, as
129+
described in their documentation. Otherwise, a `billing_project` value must be
130+
supplied.
131+
132+
* `billing_project` - (Optional) A quota project to send in `user_project_override`,
133+
used for all requests sent from the provider. If set on a resource that supports
134+
sending the resource project, this value will supersede the resource project.
135+
This field is ignored if `user_project_override` is set to false or unset.
133136

134137
* `{{service}}_custom_endpoint` - (Optional) The endpoint for a service's APIs,
135138
such as `compute_custom_endpoint`. Defaults to the production GCP endpoint for
@@ -212,13 +215,6 @@ following ordered by precedence.
212215

213216
---
214217

215-
* `billing_project` - (Optional) This fields allows Terraform to set X-Goog-User-Project
216-
for APIs that require a billing project to be specified like Access Context Manager APIs if
217-
User ADCs are being used. This can also be
218-
specified using the `GOOGLE_BILLING_PROJECT` environment variable.
219-
220-
---
221-
222218
* `region` - (Optional) The default region to manage resources in. If another
223219
region is specified on a regional resource, it will take precedence.
224220
Alternatively, this can be specified using the `GOOGLE_REGION` environment
@@ -450,18 +446,30 @@ to create the resource. This may help in those cases.
450446

451447
---
452448

453-
* `user_project_override` - (Optional) Defaults to false. If true, uses the
454-
resource project for preconditions, quota, and billing, instead of the project
455-
the credentials belong to. Not all resources support this- see the
456-
documentation for each resource to learn whether it does. Alternatively, this can
457-
be specified using the `USER_PROJECT_OVERRIDE` environment variable.
458-
459-
When set to false, the project the credentials belong to will be billed for the
460-
request, and quota / API enablement checks will be done against that project.
461-
For service account credentials, this is the project the service account was
462-
created in. For credentials that come from the gcloud tool, this is a project
463-
owned by Google. In order to properly use credentials that come from gcloud
464-
with Terraform, it is recommended to set this property to true.
465-
466-
When set to true, the caller must have `serviceusage.services.use` permission
467-
on the resource project.
449+
* `user_project_override` - (Optional) Defaults to `false`. Controls the quota
450+
project used in requests to GCP APIs for the purpose of preconditions, quota,
451+
and billing. If `false`, the quota project is determined by the API and may be
452+
the project associated with your credentials, or the resource project. If `true`,
453+
most resources in the provider will explicitly supply their resource project, as
454+
described in their documentation. Otherwise, a `billing_project` value must be
455+
supplied. Alternatively, this can be specified using the `USER_PROJECT_OVERRIDE`
456+
environment variable.
457+
458+
Service account credentials are associated with the project the service account
459+
was created in. Credentials that come from the gcloud tool are associated with a
460+
project owned by Google. In order to properly use credentials that come from
461+
gcloud with Terraform, it is recommended to set this property to true.
462+
463+
`user_project_override` uses the `X-Goog-User-Project`
464+
[system parameter](https://cloud.google.com/apis/docs/system-parameters). When
465+
set to true, the caller must have `serviceusage.services.use` permission on the
466+
quota project.
467+
468+
---
469+
470+
* `billing_project` - (Optional) A quota project to send in `user_project_override`,
471+
used for all requests sent from the provider. If set on a resource that supports
472+
sending the resource project, this value will supersede the resource project.
473+
This field is ignored if `user_project_override` is set to false or unset.
474+
Alternatively, this can be specified using the `GOOGLE_BILLING_PROJECT`
475+
environment variable.

0 commit comments

Comments
 (0)