Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add request_reason provider support #5037

Merged
merged 8 commits into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions mmv1/third_party/terraform/utils/config.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions mmv1/third_party/terraform/utils/header_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package google

import (
"net/http"
"os"
)

// adapted from https://stackoverflow.com/questions/51325704/adding-a-default-http-header-in-go
Expand All @@ -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}
}
Expand Down
15 changes: 14 additions & 1 deletion mmv1/third_party/terraform/utils/provider.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
salrashid123 marked this conversation as resolved.
Show resolved Hide resolved

googleoauth "golang.org/x/oauth2/google"
)

Expand Down Expand Up @@ -138,6 +139,14 @@ func Provider() *schema.Provider {
Optional: true,
},

"request_reason": {
Type: schema.TypeString,
Optional: true,
salrashid123 marked this conversation as resolved.
Show resolved Hide resolved
DefaultFunc: schema.MultiEnvDefaultFunc([]string{
"CLOUDSDK_CORE_REQUEST_REASON",
}, nil),
},

// Generated Products
<% products.each do |product| -%>
"<%= product[:definitions].name.underscore -%>_custom_endpoint": &schema.Schema{
Expand Down Expand Up @@ -530,6 +539,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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 value set within the provider is overridden if `CLOUDSDK_CORE_REQUEST_REASON` environment variable is used.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be added under "Full Reference" as well. For consistency with the rest of the file, we should only include the env var there and use text like Alternatively, this can be specified using the 'CLOUDSDK_CORE_REQUEST_REASON' environment variable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Provider config will override the environment variable w/ this implementation (which is the correct behaviour imo) so the docs should reflect as much!


The `batching` fields supports:

* `send_after` - (Optional) A duration string representing the amount of time
Expand Down Expand Up @@ -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,
Expand Down