Update internal/httpclient folder to OpenTofu (#467)

This commit is contained in:
Elbaz 2023-09-20 14:59:20 +03:00 committed by GitHub
parent 8aad786548
commit b524b2bdbe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
16 changed files with 27 additions and 27 deletions

View File

@ -238,7 +238,7 @@ func (c *ArmClient) configureClient(client *autorest.Client, auth autorest.Autho
}
func buildUserAgent() string {
userAgent := httpclient.OpenTfUserAgent(version.Version)
userAgent := httpclient.OpenTofuUserAgent(version.Version)
// append the CloudShell version to the user agent if it exists
if azureAgent := os.Getenv("AZURE_HTTP_USER_AGENT"); azureAgent != "" {

View File

@ -208,7 +208,7 @@ func (b *Backend) configure(ctx context.Context) error {
opts = append(opts, credOptions...)
}
opts = append(opts, option.WithUserAgent(httpclient.OpenTfUserAgent(version.Version)))
opts = append(opts, option.WithUserAgent(httpclient.OpenTofuUserAgent(version.Version)))
// Custom endpoint for storage API
if storageEndpoint, ok := data.GetOk("storage_custom_endpoint"); ok {

View File

@ -439,7 +439,7 @@ func testGetClientOptions(t *testing.T) ([]option.ClientOption, error) {
}
credOptions = append(credOptions, option.WithCredentialsJSON([]byte(contents)))
opts = append(opts, credOptions...)
opts = append(opts, option.WithUserAgent(httpclient.OpenTfUserAgent(version.Version)))
opts = append(opts, option.WithUserAgent(httpclient.OpenTofuUserAgent(version.Version)))
return opts, nil
}

View File

@ -243,7 +243,7 @@ func (b *Backend) configure(ctx context.Context) error {
}
// Overriding with static configuration
cfg.UserAgent = httpclient.OpenTfUserAgent(version.Version)
cfg.UserAgent = httpclient.OpenTofuUserAgent(version.Version)
if v, ok := data.GetOk("host"); ok {
cfg.Host = v.(string)

View File

@ -404,7 +404,7 @@ func (b *Backend) configure(ctx context.Context) error {
if securityToken != "" {
options = append(options, oss.SecurityToken(securityToken))
}
options = append(options, oss.UserAgent(httpclient.OpenTfUserAgent(TerraformVersion)))
options = append(options, oss.UserAgent(httpclient.OpenTofuUserAgent(TerraformVersion)))
proxyUrl := getHttpProxyUrl()
if proxyUrl != nil {

View File

@ -295,7 +295,7 @@ func testDisco(s *httptest.Server) *disco.Disco {
"versions.v1": fmt.Sprintf("%s/v1/versions/", s.URL),
}
d := disco.NewWithCredentialsSource(credsSrc)
d.SetUserAgent(httpclient.OpenTfUserAgent(version.String()))
d.SetUserAgent(httpclient.OpenTofuUserAgent(version.String()))
d.ForceHostServices(svchost.Hostname("app.terraform.io"), services)
d.ForceHostServices(svchost.Hostname("localhost"), services)

View File

@ -579,7 +579,7 @@ func testDisco(s *httptest.Server) *disco.Disco {
"tfe.v2": fmt.Sprintf("%s/api/v2/", s.URL),
}
d := disco.NewWithCredentialsSource(credsSrc)
d.SetUserAgent(httpclient.OpenTfUserAgent(version.String()))
d.SetUserAgent(httpclient.OpenTofuUserAgent(version.String()))
d.ForceHostServices(svchost.Hostname("app.terraform.io"), services)
d.ForceHostServices(svchost.Hostname("localhost"), services)

View File

@ -52,7 +52,7 @@ func TestLogin(t *testing.T) {
browserLauncher := webbrowser.NewMockLauncher(ctx)
creds := cliconfig.EmptyCredentialsSourceForTests(filepath.Join(workDir, "credentials.tfrc.json"))
svcs := disco.NewWithCredentialsSource(creds)
svcs.SetUserAgent(httpclient.OpenTfUserAgent(version.String()))
svcs.SetUserAgent(httpclient.OpenTofuUserAgent(version.String()))
svcs.ForceHostServices(svchost.Hostname("example.com"), map[string]interface{}{
"login.v1": map[string]interface{}{

View File

@ -15,7 +15,7 @@ import (
func New() *http.Client {
cli := cleanhttp.DefaultPooledClient()
cli.Transport = &userAgentRoundTripper{
userAgent: OpenTfUserAgent(version.Version),
userAgent: OpenTofuUserAgent(version.Version),
inner: cli.Transport,
}
return cli

View File

@ -30,7 +30,7 @@ func TestNew_userAgent(t *testing.T) {
request func(c *http.Client) error
}{
{
fmt.Sprintf("OpenTF/%s", version.Version),
fmt.Sprintf("OpenTofu/%s", version.Version),
func(c *http.Client) error {
_, err := c.Get(ts.URL)
return err

View File

@ -13,8 +13,8 @@ import (
const (
appendUaEnvVar = "TF_APPEND_USER_AGENT"
customUaEnvVar = "OPENTF_USER_AGENT"
DefaultApplicationName = "OpenTF"
customUaEnvVar = "OPENTOFU_USER_AGENT"
DefaultApplicationName = "OpenTofu"
)
type userAgentRoundTripper struct {
@ -30,7 +30,7 @@ func (rt *userAgentRoundTripper) RoundTrip(req *http.Request) (*http.Response, e
return rt.inner.RoundTrip(req)
}
func OpenTfUserAgent(version string) string {
func OpenTofuUserAgent(version string) string {
ua := fmt.Sprintf("%s/%s", DefaultApplicationName, version)
if customUa := os.Getenv(customUaEnvVar); customUa != "" {
ua = customUa

View File

@ -39,7 +39,7 @@ func TestUserAgentString_env(t *testing.T) {
os.Setenv(appendUaEnvVar, c.additional)
}
actual := OpenTfUserAgent(version.Version)
actual := OpenTofuUserAgent(version.Version)
if c.expected != actual {
t.Fatalf("Expected User-Agent '%s' does not match '%s'", c.expected, actual)
@ -55,7 +55,7 @@ func TestUserAgentAppendViaEnvVar(t *testing.T) {
defer os.Unsetenv(appendUaEnvVar)
}
expectedBase := "OpenTF/0.0.0"
expectedBase := "OpenTofu/0.0.0"
testCases := []struct {
envVarValue string
@ -72,7 +72,7 @@ func TestUserAgentAppendViaEnvVar(t *testing.T) {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
os.Unsetenv(appendUaEnvVar)
os.Setenv(appendUaEnvVar, tc.envVarValue)
givenUA := OpenTfUserAgent("0.0.0")
givenUA := OpenTofuUserAgent("0.0.0")
if givenUA != tc.expected {
t.Fatalf("Expected User-Agent '%s' does not match '%s'", tc.expected, givenUA)
}
@ -99,7 +99,7 @@ func TestCustomUserAgentViaEnvVar(t *testing.T) {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
os.Unsetenv(customUaEnvVar)
os.Setenv(customUaEnvVar, tc.envVarValue)
givenUA := OpenTfUserAgent("0.0.0")
givenUA := OpenTofuUserAgent("0.0.0")
if givenUA != tc.envVarValue {
t.Fatalf("Expected User-Agent '%s' does not match '%s'", tc.envVarValue, givenUA)
}
@ -123,11 +123,11 @@ func TestCustomUserAgentAndAppendViaEnvVar(t *testing.T) {
appendUaValue string
expected string
}{
{"", "", "OpenTF/0.0.0"},
{"", " ", "OpenTF/0.0.0"},
{"", " \n", "OpenTF/0.0.0"},
{"", "testy test", "OpenTF/0.0.0 testy test"},
{"opensource", "opentf", "opensource opentf"},
{"", "", "OpenTofu/0.0.0"},
{"", " ", "OpenTofu/0.0.0"},
{"", " \n", "OpenTofu/0.0.0"},
{"", "testy test", "OpenTofu/0.0.0 testy test"},
{"opensource", "opentofu", "opensource opentofu"},
}
for i, tc := range testCases {
@ -136,7 +136,7 @@ func TestCustomUserAgentAndAppendViaEnvVar(t *testing.T) {
os.Unsetenv(appendUaEnvVar)
os.Setenv(customUaEnvVar, tc.customUaValue)
os.Setenv(appendUaEnvVar, tc.appendUaValue)
givenUA := OpenTfUserAgent("0.0.0")
givenUA := OpenTofuUserAgent("0.0.0")
if givenUA != tc.expected {
t.Fatalf("Expected User-Agent '%s' does not match '%s'", tc.expected, givenUA)
}

View File

@ -92,7 +92,7 @@ func NewClient(services *disco.Disco, client *http.Client) *Client {
services.Transport = retryableClient.HTTPClient.Transport
services.SetUserAgent(httpclient.OpenTfUserAgent(version.String()))
services.SetUserAgent(httpclient.OpenTofuUserAgent(version.String()))
return &Client{
client: retryableClient,

View File

@ -214,7 +214,7 @@ func TestAccLookupModuleVersions(t *testing.T) {
t.Skip()
}
regDisco := disco.New()
regDisco.SetUserAgent(httpclient.OpenTfUserAgent(tfversion.String()))
regDisco.SetUserAgent(httpclient.OpenTofuUserAgent(tfversion.String()))
// test with and without a hostname
for _, src := range []string{

View File

@ -29,7 +29,7 @@ func Disco(s *httptest.Server) *disco.Disco {
"providers.v1": fmt.Sprintf("%s/v1/providers", s.URL),
}
d := disco.NewWithCredentialsSource(credsSrc)
d.SetUserAgent(httpclient.OpenTfUserAgent(tfversion.String()))
d.SetUserAgent(httpclient.OpenTofuUserAgent(tfversion.String()))
d.ForceHostServices(svchost.Hostname("registry.terraform.io"), services)
d.ForceHostServices(svchost.Hostname("localhost"), services)

View File

@ -181,7 +181,7 @@ func realMain() int {
// object checks that and just acts as though no credentials are present.
services = disco.NewWithCredentialsSource(nil)
}
services.SetUserAgent(httpclient.OpenTfUserAgent(version.String()))
services.SetUserAgent(httpclient.OpenTofuUserAgent(version.String()))
providerSrc, diags := providerSource(config.ProviderInstallation, services)
if len(diags) > 0 {