Skip to content

Commit

Permalink
Add request_reason provider support (#5037) (#3513)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <[email protected]>
  • Loading branch information
modular-magician authored Aug 12, 2021
1 parent 4832d04 commit e0898eb
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 5 deletions.
3 changes: 3 additions & 0 deletions .changelog/5037.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:enhancement
provider: Add provider support for `request_reason`
```
4 changes: 4 additions & 0 deletions google-beta/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions google-beta/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 google-beta/provider.go
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
"access_approval_custom_endpoint": {
Type: schema.TypeString,
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion google-beta/resource_gke_hub_feature_membership_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions website/docs/guides/provider_reference.html.markdown
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 e0898eb

Please sign in to comment.