From f4804a7d54696b265bb64686815770d10a1636ba Mon Sep 17 00:00:00 2001 From: salrashid123 Date: Thu, 12 Aug 2021 13:08:35 -0400 Subject: [PATCH] Add request_reason provider support (#5037) --- mmv1/third_party/terraform/utils/config.go.erb | 4 ++++ mmv1/third_party/terraform/utils/header_transport.go | 4 ---- mmv1/third_party/terraform/utils/provider.go.erb | 12 ++++++++++++ .../docs/guides/provider_reference.html.markdown | 4 ++++ 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/mmv1/third_party/terraform/utils/config.go.erb b/mmv1/third_party/terraform/utils/config.go.erb index df96f8c14c67..6266978029c9 100644 --- a/mmv1/third_party/terraform/utils/config.go.erb +++ b/mmv1/third_party/terraform/utils/config.go.erb @@ -92,6 +92,7 @@ type Config struct { Scopes []string BatchingConfig *batchingConfig UserProjectOverride bool + RequestReason string RequestTimeout time.Duration // PollInterval is passed to resource.StateChangeConf in common_operation.go // It controls the interval at which we poll for successful operations @@ -227,6 +228,9 @@ func (c *Config) LoadAndValidate(ctx context.Context) error { // 4. Header Transport - outer wrapper to inject additional headers we want to apply // before making requests headerTransport := newTransportWithHeaders(retryTransport) + if c.RequestReason != "" { + headerTransport.Set("X-Goog-Request-Reason", c.RequestReason) + } // Set final transport value. client.Transport = headerTransport diff --git a/mmv1/third_party/terraform/utils/header_transport.go b/mmv1/third_party/terraform/utils/header_transport.go index c372bedaa4ef..607262cc2fea 100644 --- a/mmv1/third_party/terraform/utils/header_transport.go +++ b/mmv1/third_party/terraform/utils/header_transport.go @@ -2,7 +2,6 @@ package google import ( "net/http" - "os" ) // adapted from https://stackoverflow.com/questions/51325704/adding-a-default-http-header-in-go @@ -17,9 +16,6 @@ func newTransportWithHeaders(baseTransit http.RoundTripper) headerTransportLayer } headers := make(http.Header) - if requestReason := os.Getenv("CLOUDSDK_CORE_REQUEST_REASON"); requestReason != "" { - headers.Set("X-Goog-Request-Reason", requestReason) - } return headerTransportLayer{Header: headers, baseTransit: baseTransit} } diff --git a/mmv1/third_party/terraform/utils/provider.go.erb b/mmv1/third_party/terraform/utils/provider.go.erb index 65cf2b66983f..ba6e464078b0 100644 --- a/mmv1/third_party/terraform/utils/provider.go.erb +++ b/mmv1/third_party/terraform/utils/provider.go.erb @@ -138,6 +138,14 @@ func Provider() *schema.Provider { Optional: true, }, + "request_reason": { + Type: schema.TypeString, + Optional: true, + DefaultFunc: schema.MultiEnvDefaultFunc([]string{ + "CLOUDSDK_CORE_REQUEST_REASON", + }, nil), + }, + // Generated Products <% products.each do |product| -%> "<%= product[:definitions].name.underscore -%>_custom_endpoint": &schema.Schema{ @@ -530,6 +538,10 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData, p *schema.Pr } } + if v, ok := d.GetOk("request_reason"); ok { + config.RequestReason = v.(string) + } + // Search for default credentials config.Credentials = multiEnvSearch([]string{ "GOOGLE_CREDENTIALS", diff --git a/mmv1/third_party/terraform/website/docs/guides/provider_reference.html.markdown b/mmv1/third_party/terraform/website/docs/guides/provider_reference.html.markdown index 42d127facbfa..28be10d61423 100644 --- a/mmv1/third_party/terraform/website/docs/guides/provider_reference.html.markdown +++ b/mmv1/third_party/terraform/website/docs/guides/provider_reference.html.markdown @@ -148,6 +148,8 @@ the provider should wait for a single HTTP request. This will not adjust the amount of time the provider will wait for a logical operation - use the resource timeout blocks for that. +* `request_reason` - (Optional) Send a Request Reason [System Parameter](https://cloud.google.com/apis/docs/system-parameters) for each API call made by the provider. The `X-Goog-Request-Reason` header value is used to provide a user-supplied justification into GCP AuditLogs. + The `batching` fields supports: * `send_after` - (Optional) A duration string representing the amount of time @@ -266,6 +268,8 @@ an access token using the service account key specified in `credentials`. * https://www.googleapis.com/auth/devstorage.full_control * https://www.googleapis.com/auth/userinfo.email +* `request_reason` - (Optional) Send a Request Reason [System Parameter](https://cloud.google.com/apis/docs/system-parameters) for each API call made by the provider. The `X-Goog-Request-Reason` header value is used to provide a user-supplied justification into GCP AuditLogs. Alternatively, this can be specified using the `CLOUDSDK_CORE_REQUEST_REASON` environment variable. + --- * `{{service}}_custom_endpoint` - (Optional) The endpoint for a service's APIs,