Skip to content

Commit

Permalink
Add request_reason provider support (#5037)
Browse files Browse the repository at this point in the history
  • Loading branch information
salrashid123 authored Aug 12, 2021
1 parent 88efaf2 commit f4804a7
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
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
12 changes: 12 additions & 0 deletions mmv1/third_party/terraform/utils/provider.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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",
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 `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

0 comments on commit f4804a7

Please sign in to comment.