From e0898eb58e19ed68aa1b81b1ce6c24732a2abd43 Mon Sep 17 00:00:00 2001 From: The Magician Date: Thu, 12 Aug 2021 12:19:14 -0500 Subject: [PATCH] Add request_reason provider support (#5037) (#3513) Signed-off-by: Modular Magician --- .changelog/5037.txt | 3 +++ google-beta/config.go | 4 ++++ google-beta/header_transport.go | 4 ---- google-beta/provider.go | 12 ++++++++++++ .../resource_gke_hub_feature_membership_test.go | 2 +- website/docs/guides/provider_reference.html.markdown | 4 ++++ 6 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 .changelog/5037.txt diff --git a/.changelog/5037.txt b/.changelog/5037.txt new file mode 100644 index 0000000000..1788af37d2 --- /dev/null +++ b/.changelog/5037.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +provider: Add provider support for `request_reason` +``` diff --git a/google-beta/config.go b/google-beta/config.go index affe24eeba..6143b287c0 100644 --- a/google-beta/config.go +++ b/google-beta/config.go @@ -69,6 +69,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 @@ -414,6 +415,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/google-beta/header_transport.go b/google-beta/header_transport.go index c372bedaa4..607262cc2f 100644 --- a/google-beta/header_transport.go +++ b/google-beta/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/google-beta/provider.go b/google-beta/provider.go index af26ab630d..6f1374f3a8 100644 --- a/google-beta/provider.go +++ b/google-beta/provider.go @@ -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 "access_approval_custom_endpoint": { Type: schema.TypeString, @@ -1416,6 +1424,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/google-beta/resource_gke_hub_feature_membership_test.go b/google-beta/resource_gke_hub_feature_membership_test.go index 9e1f2ea64d..32fa84c196 100644 --- a/google-beta/resource_gke_hub_feature_membership_test.go +++ b/google-beta/resource_gke_hub_feature_membership_test.go @@ -5,7 +5,7 @@ import ( "fmt" "testing" - "github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl" + dcl "github.com/GoogleCloudPlatform/declarative-resource-client-library/dcl" gkehub "github.com/GoogleCloudPlatform/declarative-resource-client-library/services/google/gkehub/beta" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" diff --git a/website/docs/guides/provider_reference.html.markdown b/website/docs/guides/provider_reference.html.markdown index 42d127facb..28be10d614 100644 --- a/website/docs/guides/provider_reference.html.markdown +++ b/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,