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

httpclient: Introduce composable UserAgent() #22272

Merged
merged 1 commit into from
Aug 12, 2019
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
15 changes: 15 additions & 0 deletions httpclient/useragent.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
const userAgentFormat = "Terraform/%s"
const uaEnvVar = "TF_APPEND_USER_AGENT"

// Deprecated: Use UserAgent(version) instead
func UserAgentString() string {
ua := fmt.Sprintf(userAgentFormat, version.Version)

Expand All @@ -38,3 +39,17 @@ func (rt *userAgentRoundTripper) RoundTrip(req *http.Request) (*http.Response, e
}
return rt.inner.RoundTrip(req)
}

func TerraformUserAgent(version string) string {
ua := fmt.Sprintf("HashiCorp Terraform/%s (+https://www.terraform.io)", version)

if add := os.Getenv(uaEnvVar); add != "" {
add = strings.TrimSpace(add)
if len(add) > 0 {
ua += " " + add
log.Printf("[DEBUG] Using modified User-Agent: %s", ua)
}
}

return ua
}
31 changes: 31 additions & 0 deletions httpclient/useragent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,36 @@ func TestUserAgentString_env(t *testing.T) {
}
})
}
}

func TestUserAgentAppendViaEnvVar(t *testing.T) {
if oldenv, isSet := os.LookupEnv(uaEnvVar); isSet {
defer os.Setenv(uaEnvVar, oldenv)
} else {
defer os.Unsetenv(uaEnvVar)
}

expectedBase := "HashiCorp Terraform/0.0.0 (+https://www.terraform.io)"

testCases := []struct {
envVarValue string
expected string
}{
{"", expectedBase},
{" ", expectedBase},
{" \n", expectedBase},
{"test/1", expectedBase + " test/1"},
{"test/1 (comment)", expectedBase + " test/1 (comment)"},
}

for i, tc := range testCases {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
os.Unsetenv(uaEnvVar)
os.Setenv(uaEnvVar, tc.envVarValue)
givenUA := TerraformUserAgent("0.0.0")
if givenUA != tc.expected {
t.Fatalf("Expected User-Agent '%s' does not match '%s'", tc.expected, givenUA)
}
})
}
}
3 changes: 1 addition & 2 deletions terraform/user_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import (

// Generate a UserAgent string
//
// Deprecated: Use httpclient.UserAgentString if you are setting your
// own User-Agent header.
// Deprecated: Use httpclient.UserAgent(version) instead
func UserAgentString() string {
return httpclient.UserAgentString()
}
2 changes: 1 addition & 1 deletion terraform/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/hashicorp/terraform/version"
)

// TODO: update providers to use the version package directly
// Deprecated: Providers should use schema.Provider.TerraformVersion instead
func VersionString() string {
return version.String()
}