From 747833e39104beec9a2a650a464a2283555b131d Mon Sep 17 00:00:00 2001 From: salrashid123 Date: Wed, 4 Aug 2021 18:01:05 -0400 Subject: [PATCH 1/8] Add request_reason provider support --- mmv1/third_party/terraform/utils/config.go.erb | 3 ++- mmv1/third_party/terraform/utils/header_transport.go | 4 +++- mmv1/third_party/terraform/utils/provider.go.erb | 5 +++++ .../website/docs/guides/provider_reference.html.markdown | 2 ++ 4 files changed, 12 insertions(+), 2 deletions(-) diff --git a/mmv1/third_party/terraform/utils/config.go.erb b/mmv1/third_party/terraform/utils/config.go.erb index df96f8c14c67..3dd0b3837cbe 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 @@ -226,7 +227,7 @@ 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) + headerTransport := newTransportWithHeaders(retryTransport, 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..1e14524ab380 100644 --- a/mmv1/third_party/terraform/utils/header_transport.go +++ b/mmv1/third_party/terraform/utils/header_transport.go @@ -11,7 +11,7 @@ type headerTransportLayer struct { baseTransit http.RoundTripper } -func newTransportWithHeaders(baseTransit http.RoundTripper) headerTransportLayer { +func newTransportWithHeaders(baseTransit http.RoundTripper, configRequestReason string) headerTransportLayer { if baseTransit == nil { baseTransit = http.DefaultTransport } @@ -19,6 +19,8 @@ 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) + } else if configRequestReason != "" { + headers.Set("X-Goog-Request-Reason", configRequestReason) } 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..0da878d2b7cb 100644 --- a/mmv1/third_party/terraform/utils/provider.go.erb +++ b/mmv1/third_party/terraform/utils/provider.go.erb @@ -138,6 +138,11 @@ func Provider() *schema.Provider { Optional: true, }, + "request_reason": { + Type: schema.TypeString, + Optional: true, + }, + // Generated Products <% products.each do |product| -%> "<%= product[:definitions].name.underscore -%>_custom_endpoint": &schema.Schema{ 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..b6b4390d0d3c 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). + The `batching` fields supports: * `send_after` - (Optional) A duration string representing the amount of time From a19b5ec66863485307a5f78a45982be838cfa0b1 Mon Sep 17 00:00:00 2001 From: salrashid123 Date: Wed, 4 Aug 2021 20:47:51 -0400 Subject: [PATCH 2/8] add missing check --- mmv1/third_party/terraform/utils/provider.go.erb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mmv1/third_party/terraform/utils/provider.go.erb b/mmv1/third_party/terraform/utils/provider.go.erb index 0da878d2b7cb..272ad868d425 100644 --- a/mmv1/third_party/terraform/utils/provider.go.erb +++ b/mmv1/third_party/terraform/utils/provider.go.erb @@ -535,6 +535,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", From 225ceee157c0c90774461651c90a5a611fc8c814 Mon Sep 17 00:00:00 2001 From: salrashid123 Date: Thu, 5 Aug 2021 09:29:55 -0400 Subject: [PATCH 3/8] fix imports; docs --- mmv1/third_party/terraform/utils/provider.go.erb | 3 ++- .../website/docs/guides/provider_reference.html.markdown | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/mmv1/third_party/terraform/utils/provider.go.erb b/mmv1/third_party/terraform/utils/provider.go.erb index 272ad868d425..ab86bccddc02 100644 --- a/mmv1/third_party/terraform/utils/provider.go.erb +++ b/mmv1/third_party/terraform/utils/provider.go.erb @@ -9,7 +9,8 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - + "github.com/hashicorp/terraform-provider-google/version" + googleoauth "golang.org/x/oauth2/google" ) 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 b6b4390d0d3c..499b34d13dff 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,7 +148,7 @@ 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). +* `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 value set within the provider is overridden if `CLOUDSDK_CORE_REQUEST_REASON` environment variable is used. The `batching` fields supports: From f7b4587d430cb66ad81360df0893be46bc5e0222 Mon Sep 17 00:00:00 2001 From: salrashid123 Date: Thu, 5 Aug 2021 14:17:04 -0400 Subject: [PATCH 4/8] add default value config; docs --- mmv1/third_party/terraform/utils/header_transport.go | 5 +---- mmv1/third_party/terraform/utils/provider.go.erb | 3 +++ .../website/docs/guides/provider_reference.html.markdown | 2 ++ 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/mmv1/third_party/terraform/utils/header_transport.go b/mmv1/third_party/terraform/utils/header_transport.go index 1e14524ab380..f41704bf2431 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,7 @@ func newTransportWithHeaders(baseTransit http.RoundTripper, configRequestReason } headers := make(http.Header) - if requestReason := os.Getenv("CLOUDSDK_CORE_REQUEST_REASON"); requestReason != "" { - headers.Set("X-Goog-Request-Reason", requestReason) - } else if configRequestReason != "" { + if configRequestReason != "" { headers.Set("X-Goog-Request-Reason", configRequestReason) } diff --git a/mmv1/third_party/terraform/utils/provider.go.erb b/mmv1/third_party/terraform/utils/provider.go.erb index ab86bccddc02..67151c49ea81 100644 --- a/mmv1/third_party/terraform/utils/provider.go.erb +++ b/mmv1/third_party/terraform/utils/provider.go.erb @@ -142,6 +142,9 @@ func Provider() *schema.Provider { "request_reason": { Type: schema.TypeString, Optional: true, + DefaultFunc: schema.MultiEnvDefaultFunc([]string{ + "CLOUDSDK_CORE_REQUEST_REASON", + }, nil), }, // Generated Products 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 499b34d13dff..ec6fe20e227b 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 @@ -268,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, From e2e7cfcee8b28e5dec578d9180ee093f72bff7b8 Mon Sep 17 00:00:00 2001 From: salrashid123 Date: Mon, 9 Aug 2021 18:56:50 -0400 Subject: [PATCH 5/8] reorder addheader --- mmv1/third_party/terraform/utils/config.go.erb | 5 ++++- mmv1/third_party/terraform/utils/header_transport.go | 5 +---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/mmv1/third_party/terraform/utils/config.go.erb b/mmv1/third_party/terraform/utils/config.go.erb index 3dd0b3837cbe..6266978029c9 100644 --- a/mmv1/third_party/terraform/utils/config.go.erb +++ b/mmv1/third_party/terraform/utils/config.go.erb @@ -227,7 +227,10 @@ 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, c.RequestReason) + 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 f41704bf2431..607262cc2fea 100644 --- a/mmv1/third_party/terraform/utils/header_transport.go +++ b/mmv1/third_party/terraform/utils/header_transport.go @@ -10,15 +10,12 @@ type headerTransportLayer struct { baseTransit http.RoundTripper } -func newTransportWithHeaders(baseTransit http.RoundTripper, configRequestReason string) headerTransportLayer { +func newTransportWithHeaders(baseTransit http.RoundTripper) headerTransportLayer { if baseTransit == nil { baseTransit = http.DefaultTransport } headers := make(http.Header) - if configRequestReason != "" { - headers.Set("X-Goog-Request-Reason", configRequestReason) - } return headerTransportLayer{Header: headers, baseTransit: baseTransit} } From f3886f9f500f7e869d7f2ef59be0461a00d564ce Mon Sep 17 00:00:00 2001 From: salrashid123 Date: Tue, 10 Aug 2021 13:36:32 -0400 Subject: [PATCH 6/8] remove import --- mmv1/third_party/terraform/utils/provider.go.erb | 1 - .../website/docs/guides/provider_reference.html.markdown | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/mmv1/third_party/terraform/utils/provider.go.erb b/mmv1/third_party/terraform/utils/provider.go.erb index 67151c49ea81..b4366d14dc11 100644 --- a/mmv1/third_party/terraform/utils/provider.go.erb +++ b/mmv1/third_party/terraform/utils/provider.go.erb @@ -9,7 +9,6 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - "github.com/hashicorp/terraform-provider-google/version" googleoauth "golang.org/x/oauth2/google" ) 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 ec6fe20e227b..c618ac67ca23 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,7 +148,7 @@ 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 value set within the provider is overridden if `CLOUDSDK_CORE_REQUEST_REASON` environment variable is used. +* `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 provider configuration setting overrides any value set using the `CLOUDSDK_CORE_REQUEST_REASON` environment variable. The `batching` fields supports: From 57473655c4f540c470a7932dc297f8f53de8729a Mon Sep 17 00:00:00 2001 From: salrashid123 Date: Tue, 10 Aug 2021 13:38:36 -0400 Subject: [PATCH 7/8] fix tab --- mmv1/third_party/terraform/utils/provider.go.erb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mmv1/third_party/terraform/utils/provider.go.erb b/mmv1/third_party/terraform/utils/provider.go.erb index b4366d14dc11..ba6e464078b0 100644 --- a/mmv1/third_party/terraform/utils/provider.go.erb +++ b/mmv1/third_party/terraform/utils/provider.go.erb @@ -9,7 +9,7 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" - + googleoauth "golang.org/x/oauth2/google" ) From ce83982b0991bf052f33f118cb41a3ec22c55382 Mon Sep 17 00:00:00 2001 From: salrashid123 Date: Wed, 11 Aug 2021 17:14:06 -0400 Subject: [PATCH 8/8] fix env text --- .../website/docs/guides/provider_reference.html.markdown | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 c618ac67ca23..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,7 +148,7 @@ 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 provider configuration setting overrides any value set using the `CLOUDSDK_CORE_REQUEST_REASON` environment variable. +* `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: