-
Notifications
You must be signed in to change notification settings - Fork 48
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
tailscale: fix default User-Agent header to include terraform version #361
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,24 +8,10 @@ import ( | |
"github.com/tailscale/terraform-provider-tailscale/tailscale" | ||
) | ||
|
||
// version is filled by goreleaser at build time. | ||
var version = "dev" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved this to |
||
|
||
func main() { | ||
plugin.Serve(&plugin.ServeOpts{ | ||
ProviderFunc: func() *schema.Provider { | ||
return tailscale.Provider(addUserAgent) | ||
return tailscale.Provider() | ||
}, | ||
}) | ||
} | ||
|
||
// 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.", | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,9 @@ import ( | |
"github.com/tailscale/tailscale-client-go/tailscale" | ||
) | ||
|
||
// providerVersion is filled by goreleaser at build time. | ||
var providerVersion = "dev" | ||
|
||
type ProviderOption func(p *schema.Provider) | ||
|
||
// Provider returns the *schema.Provider instance that implements the terraform provider. | ||
|
@@ -24,7 +27,6 @@ func Provider(options ...ProviderOption) *schema.Provider { | |
oauthClientSecretEnvVars := []string{"TAILSCALE_OAUTH_CLIENT_SECRET", "OAUTH_CLIENT_SECRET"} | ||
|
||
provider := &schema.Provider{ | ||
ConfigureContextFunc: providerConfigure, | ||
Schema: map[string]*schema.Schema{ | ||
"api_key": { | ||
Type: schema.TypeString, | ||
|
@@ -64,6 +66,11 @@ func Provider(options ...ProviderOption) *schema.Provider { | |
Optional: true, | ||
Description: "The base URL of the Tailscale API. Defaults to https://api.tailscale.com. Can be set via the TAILSCALE_BASE_URL environment variable.", | ||
}, | ||
"user_agent": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
Description: "User-Agent header for API requests.", | ||
}, | ||
}, | ||
ResourcesMap: map[string]*schema.Resource{ | ||
"tailscale_acl": resourceACL(), | ||
|
@@ -85,14 +92,18 @@ func Provider(options ...ProviderOption) *schema.Provider { | |
}, | ||
} | ||
|
||
provider.ConfigureContextFunc = func(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved setting |
||
return providerConfigure(ctx, provider, d) | ||
} | ||
|
||
for _, option := range options { | ||
option(provider) | ||
} | ||
|
||
return provider | ||
} | ||
|
||
func providerConfigure(_ context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) { | ||
func providerConfigure(_ context.Context, provider *schema.Provider, d *schema.ResourceData) (interface{}, diag.Diagnostics) { | ||
baseURL := d.Get("base_url").(string) | ||
tailnet := d.Get("tailnet").(string) | ||
if tailnet == "" { | ||
|
@@ -114,6 +125,9 @@ func providerConfigure(_ context.Context, d *schema.ResourceData) (interface{}, | |
} | ||
|
||
userAgent := d.Get("user_agent").(string) | ||
if userAgent == "" { | ||
userAgent = provider.UserAgent("terraform-provider-tailscale", providerVersion) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is essentially an identical call to what we were making before, but at this point the underlying |
||
} | ||
|
||
if oauthClientID != "" && oauthClientSecret != "" { | ||
var oauthScopes []string | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be the fully qualified path since it no longer lives in
main.go
. See here in the AzureRM Terraform provider for similar usage.