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

Remove occurrences of exported Terraform version #24

Merged
merged 1 commit into from
Aug 27, 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
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.