2018-02-28 17:40:43 +01:00
|
|
|
package httpclient
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2018-03-15 14:25:57 +01:00
|
|
|
"log"
|
2018-02-28 17:40:43 +01:00
|
|
|
"net/http"
|
2018-03-15 14:25:57 +01:00
|
|
|
"os"
|
|
|
|
"strings"
|
2018-02-28 17:40:43 +01:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/version"
|
|
|
|
)
|
|
|
|
|
|
|
|
const userAgentFormat = "Terraform/%s"
|
2018-03-15 14:25:57 +01:00
|
|
|
const uaEnvVar = "TF_APPEND_USER_AGENT"
|
2018-02-28 17:40:43 +01:00
|
|
|
|
2019-07-30 20:17:33 +02:00
|
|
|
// Deprecated: Use UserAgent(version) instead
|
2018-02-28 17:40:43 +01:00
|
|
|
func UserAgentString() string {
|
2018-03-15 14:25:57 +01:00
|
|
|
ua := fmt.Sprintf(userAgentFormat, version.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
|
2018-02-28 17:40:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type userAgentRoundTripper struct {
|
|
|
|
inner http.RoundTripper
|
|
|
|
userAgent string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rt *userAgentRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
|
|
if _, ok := req.Header["User-Agent"]; !ok {
|
|
|
|
req.Header.Set("User-Agent", rt.userAgent)
|
|
|
|
}
|
2019-07-13 01:54:31 +02:00
|
|
|
log.Printf("[TRACE] HTTP client %s request to %s", req.Method, req.URL.String())
|
2018-02-28 17:40:43 +01:00
|
|
|
return rt.inner.RoundTrip(req)
|
|
|
|
}
|
2019-07-30 20:17:33 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|