Skip to content

Commit

Permalink
tailscale: use a custom User-Agent header while making API requests
Browse files Browse the repository at this point in the history
This passes a custom user-agent header value to the API client for
making HTTP requests.

It uses an existing UserAgent helper provided by the plugin SDK:
https://github.com/hashicorp/terraform-plugin-sdk/blob/8bde488a9529d281bc6858ca2b0a3708c2f93658/helper/schema/provider.go#L489

Signed-off-by: Anton Tolchanov <[email protected]>
  • Loading branch information
knyar committed Aug 17, 2023
1 parent da3ced6 commit 6c667f6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,4 @@ provider "tailscale" {
- `oauth_client_secret` (String, Sensitive) The OAuth application's secret when using OAuth client credentials. Can be set via the OAUTH_CLIENT_SECRET environment variable. Both 'oauth_client_id' and 'oauth_client_secret' must be set. Conflicts with 'api_key'.
- `scopes` (List of String) The OAuth 2.0 scopes to request when for the access token generated using the supplied OAuth client credentials. See https://tailscale.com/kb/1215/oauth-clients/#scopes for available scopes. Only valid when both 'oauth_client_id' and 'oauth_client_secret' are set.
- `tailnet` (String) The Tailnet to perform actions in. Can be set via the TAILSCALE_TAILNET environment variable.
- `user_agent` (String) User-Agent header for API requests.
16 changes: 15 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,24 @@ import (
"github.com/tailscale/terraform-provider-tailscale/tailscale"
)

// version is filled by goreleaser at build time.
var version = "dev"

func main() {
plugin.Serve(&plugin.ServeOpts{
ProviderFunc: func() *schema.Provider {
return tailscale.Provider()
return tailscale.Provider(addUserAgent)
},
})
}

// addUserAgent adds a `user_agent` configuration key to the provider with a
// default value based on provider version.
func addUserAgent(p *schema.Provider) {
p.Schema["user_agent"] = &schema.Schema{
Type: schema.TypeString,
Default: p.UserAgent("terraform-provider-tailscale", version),
Optional: true,
Description: "User-Agent header for API requests.",
}
}
4 changes: 4 additions & 0 deletions tailscale/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ func providerConfigure(_ context.Context, d *schema.ResourceData) (interface{},
return nil, diag.Errorf("tailscale provider argument 'oauth_client_secret' is empty")
}

userAgent := d.Get("user_agent").(string)

if oauthClientID != "" && oauthClientSecret != "" {
var oauthScopes []string
oauthScopesFromConfig := d.Get("scopes").([]interface{})
Expand All @@ -121,6 +123,7 @@ func providerConfigure(_ context.Context, d *schema.ResourceData) (interface{},
"",
tailnet,
tailscale.WithBaseURL(baseURL),
tailscale.WithUserAgent(userAgent),
tailscale.WithOAuthClientCredentials(oauthClientID, oauthClientSecret, oauthScopes),
)
if err != nil {
Expand All @@ -134,6 +137,7 @@ func providerConfigure(_ context.Context, d *schema.ResourceData) (interface{},
apiKey,
tailnet,
tailscale.WithBaseURL(baseURL),
tailscale.WithUserAgent(userAgent),
)
if err != nil {
return nil, diagnosticsError(err, "failed to initialise client")
Expand Down

0 comments on commit 6c667f6

Please sign in to comment.