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

Conversation

mpminardi
Copy link
Contributor

@mpminardi mpminardi commented Apr 30, 2024

The schema.Provider.UserAgent helper function was previously being called at a point in the plugin lifecycle before the TerraformVersion for the provider was set by Terraform Core. This was resulting in the Terraform version information being missing from the User-Agent header. The call to schema.Provider.UserAgent has now been moved into the providerConfigure func as this is called later in the configure lifecycle where the Terraform version is properly set.

Header Before

Terraform/ (+https://www.terraform.io) Terraform-Plugin-SDK/2.33.0 terraform-provider-tailscale/dev

Header After

Terraform/1.8.2 (+https://www.terraform.io) Terraform-Plugin-SDK/2.33.0 terraform-provider-tailscale/dev

Fixes https://github.com/tailscale/corp/issues/19630

@mpminardi mpminardi self-assigned this Apr 30, 2024
@mpminardi mpminardi force-pushed the mpminardi/user-agent-changes branch 2 times, most recently from a418631 to 0af5102 Compare April 30, 2024 14:56
@@ -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
Contributor 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.

@@ -84,14 +91,18 @@ func Provider(options ...ProviderOption) *schema.Provider {
},
}

provider.ConfigureContextFunc = func(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
Copy link
Contributor 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.

@@ -113,6 +124,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
Contributor 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.

@@ -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
Contributor 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.

@mpminardi mpminardi marked this pull request as ready for review April 30, 2024 15:08
Copy link
Member

@soniaappasamy soniaappasamy left a comment

Choose a reason for hiding this comment

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

Nice! Ty for digging into this

The `schema.Provider.UserAgent` helper function was previously being
called at a point in the plugin lifecycle before the `TerraformVersion`
for the provider was set by Terraform Core. This was resulting in the
Terraform version information being missing from the User-Agent header.
The call to `schema.Provider.UserAgent` has now been moved into the
`providerConfigure` func as this is called later in the configure
lifecycle where the Terraform version is properly set.

Fixes tailscale/corp#19630

Signed-off-by: Mario Minardi <mario@tailscale.com>
@mpminardi mpminardi force-pushed the mpminardi/user-agent-changes branch from 0af5102 to 5aaffc9 Compare May 1, 2024 19:16
@mpminardi mpminardi merged commit 380cf9a into main May 1, 2024
3 checks passed
@mpminardi mpminardi deleted the mpminardi/user-agent-changes branch May 1, 2024 19:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants