Skip to content

Commit

Permalink
Merge pull request #24 from hashicorp/remove-version
Browse files Browse the repository at this point in the history
Remove occurrences of exported Terraform version
  • Loading branch information
radeksimko authored Aug 27, 2019
2 parents f96cacb + b4771cb commit ea2020c
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 116 deletions.
18 changes: 0 additions & 18 deletions httpclient/client.go

This file was deleted.

32 changes: 0 additions & 32 deletions httpclient/useragent.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,44 +3,12 @@ package httpclient
import (
"fmt"
"log"
"net/http"
"os"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/internal/version"
)

const userAgentFormat = "Terraform/%s"
const uaEnvVar = "TF_APPEND_USER_AGENT"

// Deprecated: Use UserAgent(version) instead
func UserAgentString() string {
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
}

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)
}
log.Printf("[TRACE] HTTP client %s request to %s", req.Method, req.URL.String())
return rt.inner.RoundTrip(req)
}

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

Expand Down
39 changes: 0 additions & 39 deletions httpclient/useragent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,8 @@ import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/internal/version"
)

func TestUserAgentString_env(t *testing.T) {
expectedBase := fmt.Sprintf(userAgentFormat, version.Version)
if oldenv, isSet := os.LookupEnv(uaEnvVar); isSet {
defer os.Setenv(uaEnvVar, oldenv)
} else {
defer os.Unsetenv(uaEnvVar)
}

for i, c := range []struct {
expected string
additional string
}{
{expectedBase, ""},
{expectedBase, " "},
{expectedBase, " \n"},

{fmt.Sprintf("%s test/1", expectedBase), "test/1"},
{fmt.Sprintf("%s test/2", expectedBase), "test/2 "},
{fmt.Sprintf("%s test/3", expectedBase), " test/3 "},
{fmt.Sprintf("%s test/4", expectedBase), "test/4 \n"},
} {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
if c.additional == "" {
os.Unsetenv(uaEnvVar)
} else {
os.Setenv(uaEnvVar, c.additional)
}

actual := UserAgentString()

if c.expected != actual {
t.Fatalf("Expected User-Agent '%s' does not match '%s'", c.expected, actual)
}
})
}
}

func TestUserAgentAppendViaEnvVar(t *testing.T) {
if oldenv, isSet := os.LookupEnv(uaEnvVar); isSet {
defer os.Setenv(uaEnvVar, oldenv)
Expand Down
53 changes: 53 additions & 0 deletions internal/httpclient/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package httpclient

import (
"fmt"
"log"
"net/http"
"os"
"strings"

cleanhttp "github.com/hashicorp/go-cleanhttp"
"github.com/hashicorp/terraform-plugin-sdk/internal/version"
)

const uaEnvVar = "TF_APPEND_USER_AGENT"
const userAgentFormat = "Terraform/%s"

// New returns the DefaultPooledClient from the cleanhttp
// package that will also send a Terraform User-Agent string.
func New() *http.Client {
cli := cleanhttp.DefaultPooledClient()
cli.Transport = &userAgentRoundTripper{
userAgent: UserAgentString(),
inner: cli.Transport,
}
return cli
}

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)
}
log.Printf("[TRACE] HTTP client %s request to %s", req.Method, req.URL.String())
return rt.inner.RoundTrip(req)
}

func UserAgentString() string {
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
}
38 changes: 38 additions & 0 deletions httpclient/client_test.go → internal/httpclient/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,49 @@ import (
"net/http"
"net/http/httptest"
"net/url"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/internal/version"
)

func TestUserAgentString_env(t *testing.T) {
expectedBase := fmt.Sprintf(userAgentFormat, version.Version)
if oldenv, isSet := os.LookupEnv(uaEnvVar); isSet {
defer os.Setenv(uaEnvVar, oldenv)
} else {
defer os.Unsetenv(uaEnvVar)
}

for i, c := range []struct {
expected string
additional string
}{
{expectedBase, ""},
{expectedBase, " "},
{expectedBase, " \n"},

{fmt.Sprintf("%s test/1", expectedBase), "test/1"},
{fmt.Sprintf("%s test/2", expectedBase), "test/2 "},
{fmt.Sprintf("%s test/3", expectedBase), " test/3 "},
{fmt.Sprintf("%s test/4", expectedBase), "test/4 \n"},
} {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
if c.additional == "" {
os.Unsetenv(uaEnvVar)
} else {
os.Setenv(uaEnvVar, c.additional)
}

actual := UserAgentString()

if c.expected != actual {
t.Fatalf("Expected User-Agent '%s' does not match '%s'", c.expected, actual)
}
})
}
}

func TestNew_userAgent(t *testing.T) {
var actualUserAgent string
ts := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
Expand Down
2 changes: 1 addition & 1 deletion internal/plugin/discovery/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/hashicorp/errwrap"
getter "github.com/hashicorp/go-getter"
multierror "github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/httpclient"
"github.com/hashicorp/terraform-plugin-sdk/internal/httpclient"
"github.com/hashicorp/terraform-plugin-sdk/internal/registry"
"github.com/hashicorp/terraform-plugin-sdk/internal/registry/regsrc"
"github.com/hashicorp/terraform-plugin-sdk/internal/registry/response"
Expand Down
2 changes: 1 addition & 1 deletion internal/registry/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/httpclient"
"github.com/hashicorp/terraform-plugin-sdk/internal/httpclient"
"github.com/hashicorp/terraform-plugin-sdk/internal/registry/regsrc"
"github.com/hashicorp/terraform-plugin-sdk/internal/registry/response"
"github.com/hashicorp/terraform-plugin-sdk/internal/svchost"
Expand Down
2 changes: 1 addition & 1 deletion internal/svchost/disco/disco.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"time"

cleanhttp "github.com/hashicorp/go-cleanhttp"
"github.com/hashicorp/terraform-plugin-sdk/httpclient"
"github.com/hashicorp/terraform-plugin-sdk/internal/httpclient"
"github.com/hashicorp/terraform-plugin-sdk/internal/svchost"
"github.com/hashicorp/terraform-plugin-sdk/internal/svchost/auth"
)
Expand Down
2 changes: 1 addition & 1 deletion internal/svchost/disco/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"time"

"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform-plugin-sdk/httpclient"
"github.com/hashicorp/terraform-plugin-sdk/internal/httpclient"
)

const versionServiceID = "versions.v1"
Expand Down
2 changes: 1 addition & 1 deletion internal/version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ var Version = "0.12.7"
// 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 Prerelease = "dev"
var Prerelease = "sdk"

// SemVer is an instance of version.Version. This has the secondary
// benefit of verifying during tests and init time that our version is a
Expand Down
12 changes: 0 additions & 12 deletions terraform/user_agent.go

This file was deleted.

10 changes: 0 additions & 10 deletions terraform/version.go

This file was deleted.

0 comments on commit ea2020c

Please sign in to comment.