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

tailscale: fix default User-Agent header to include terraform version #361

Merged
merged 1 commit into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ builds:
flags:
- -trimpath
ldflags:
- "-s -w -X main.version={{.Version}} -X main.commit={{.Commit}}"
- "-s -w -X github.com/tailscale/terraform-provider-tailscale/tailscale.providerVersion={{.Version}} -X main.commit={{.Commit}}"
Copy link
Member Author

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.

goos:
- freebsd
- windows
Expand Down
16 changes: 1 addition & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,10 @@ import (
"github.com/tailscale/terraform-provider-tailscale/tailscale"
)

// version is filled by goreleaser at build time.
var version = "dev"
Copy link
Member Author

Choose a reason for hiding this comment

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

Moved this to provider.go as that is the one place it is used currently and to avoid messiness with having to pass the version into the Provider method.


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.",
}
}
18 changes: 16 additions & 2 deletions tailscale/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand Down Expand Up @@ -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(),
Expand All @@ -85,14 +92,18 @@ func Provider(options ...ProviderOption) *schema.Provider {
},
}

provider.ConfigureContextFunc = func(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Moved setting ConfigureContextFunc here so we can pass the provider into providerConfigure.

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 == "" {
Expand All @@ -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)
Copy link
Member Author

Choose a reason for hiding this comment

The 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 provider.TerraformVersion value used in the UserAgent call is populated properly.

}

if oauthClientID != "" && oauthClientSecret != "" {
var oauthScopes []string
Expand Down
Loading