diff --git a/httpclient/useragent.go b/httpclient/useragent.go index 5d444ae511..36b494c014 100644 --- a/httpclient/useragent.go +++ b/httpclient/useragent.go @@ -5,12 +5,14 @@ import ( "log" "os" "strings" + + "github.com/hashicorp/terraform-plugin-sdk/meta" ) const uaEnvVar = "TF_APPEND_USER_AGENT" func TerraformUserAgent(version string) string { - ua := fmt.Sprintf("HashiCorp Terraform/%s (+https://www.terraform.io)", version) + ua := fmt.Sprintf("HashiCorp Terraform/%s (+https://www.terraform.io) Terraform Plugin SDK/%s", version, meta.SDKVersionString()) if add := os.Getenv(uaEnvVar); add != "" { add = strings.TrimSpace(add) diff --git a/httpclient/useragent_test.go b/httpclient/useragent_test.go index afa9a545b8..e5d88ed9a2 100644 --- a/httpclient/useragent_test.go +++ b/httpclient/useragent_test.go @@ -4,6 +4,8 @@ import ( "fmt" "os" "testing" + + "github.com/hashicorp/terraform-plugin-sdk/meta" ) func TestUserAgentAppendViaEnvVar(t *testing.T) { @@ -13,7 +15,7 @@ func TestUserAgentAppendViaEnvVar(t *testing.T) { defer os.Unsetenv(uaEnvVar) } - expectedBase := "HashiCorp Terraform/0.0.0 (+https://www.terraform.io)" + expectedBase := "HashiCorp Terraform/0.0.0 (+https://www.terraform.io) Terraform Plugin SDK/" + meta.SDKVersionString() testCases := []struct { envVarValue string diff --git a/meta/meta.go b/meta/meta.go new file mode 100644 index 0000000000..d102894d2a --- /dev/null +++ b/meta/meta.go @@ -0,0 +1,36 @@ +// The meta package provides a location to set the release version +// and any other relevant metadata for the SDK. +// +// This package should not import any other SDK packages. +package meta + +import ( + "fmt" + + version "github.com/hashicorp/go-version" +) + +// The main version number that is being run at the moment. +var SDKVersion = "1.0.0" + +// A pre-release marker for the version. If this is "" (empty string) +// then it means that it is a final release. Otherwise, this is a pre-release +// such as "dev" (in development), "beta", "rc1", etc. +var SDKPrerelease = "dev" + +// SemVer is an instance of version.Version. This has the secondary +// benefit of verifying during tests and init time that our version is a +// proper semantic version, which should always be the case. +var SemVer *version.Version + +func init() { + SemVer = version.Must(version.NewVersion(SDKVersion)) +} + +// VersionString returns the complete version string, including prerelease +func SDKVersionString() string { + if SDKPrerelease != "" { + return fmt.Sprintf("%s-%s", SDKVersion, SDKPrerelease) + } + return SDKVersion +}