From 885935962c52933ce49573bb5181b2a52e57c009 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Thu, 21 Jul 2016 10:56:32 -0400 Subject: [PATCH 1/4] Add a terraform version header to all atlas calls Using the DefaultHeader added to the atlas.Client --- command/push.go | 3 +++ terraform/version.go | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/command/push.go b/command/push.go index 67cac67403..1abe13e0b5 100644 --- a/command/push.go +++ b/command/push.go @@ -10,6 +10,7 @@ import ( "github.com/hashicorp/atlas-go/archive" "github.com/hashicorp/atlas-go/v1" + "github.com/hashicorp/terraform/terraform" ) type PushCommand struct { @@ -126,6 +127,8 @@ func (c *PushCommand) Run(args []string) int { } } + client.DefaultHeader.Set(terraform.VersionHeader, terraform.Version) + if atlasToken != "" { client.Token = atlasToken } diff --git a/terraform/version.go b/terraform/version.go index e781d9c259..7462a3c67c 100644 --- a/terraform/version.go +++ b/terraform/version.go @@ -16,3 +16,7 @@ const VersionPrerelease = "dev" // benefit of verifying during tests and init time that our version is a // proper semantic version, which should always be the case. var SemVersion = version.Must(version.NewVersion(Version)) + +// VersionHeader is the header name used to send the current terraform version +// in http requests. +const VersionHeader = "Terraform-Version" From bd99c7a9040d27534de61db91f12615382a85b6b Mon Sep 17 00:00:00 2001 From: James Bardin Date: Thu, 21 Jul 2016 11:04:40 -0400 Subject: [PATCH 2/4] Add the VersionHeader to the atlas provider too --- builtin/providers/atlas/provider.go | 1 + 1 file changed, 1 insertion(+) diff --git a/builtin/providers/atlas/provider.go b/builtin/providers/atlas/provider.go index 3d14edca8b..e7034a7cd7 100644 --- a/builtin/providers/atlas/provider.go +++ b/builtin/providers/atlas/provider.go @@ -52,6 +52,7 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) { return nil, err } } + client.DefaultHeader.Set(terraform.VersionHeader, terraform.Version) client.Token = d.Get("token").(string) return client, nil From f66d1a10a4a6ce7dafa8196c82fb236540f2a6b5 Mon Sep 17 00:00:00 2001 From: James Bardin Date: Thu, 21 Jul 2016 14:07:16 -0400 Subject: [PATCH 3/4] Add VersionString We conditionally format version with VersionPrerelease in a number of places. Add a package-level function where we can unify the version format. Replace most of version formatting in terraform, but leave th few instances set from the top-level package to make sure we don't break anything before release. --- builtin/providers/atlas/provider.go | 2 +- builtin/providers/aws/config.go | 2 +- builtin/providers/azurerm/config.go | 8 +------- builtin/providers/google/config.go | 6 +----- terraform/version.go | 9 +++++++++ 5 files changed, 13 insertions(+), 14 deletions(-) diff --git a/builtin/providers/atlas/provider.go b/builtin/providers/atlas/provider.go index e7034a7cd7..14928de635 100644 --- a/builtin/providers/atlas/provider.go +++ b/builtin/providers/atlas/provider.go @@ -52,7 +52,7 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) { return nil, err } } - client.DefaultHeader.Set(terraform.VersionHeader, terraform.Version) + client.DefaultHeader.Set(terraform.VersionHeader, terraform.VersionString()) client.Token = d.Get("token").(string) return client, nil diff --git a/builtin/providers/aws/config.go b/builtin/providers/aws/config.go index ab50b8ae2a..85f1f30ea0 100644 --- a/builtin/providers/aws/config.go +++ b/builtin/providers/aws/config.go @@ -397,7 +397,7 @@ func (c *Config) ValidateAccountId(accountId string) error { var addTerraformVersionToUserAgent = request.NamedHandler{ Name: "terraform.TerraformVersionUserAgentHandler", Fn: request.MakeAddToUserAgentHandler( - "terraform", terraform.Version, terraform.VersionPrerelease), + "terraform", terraform.VersionString()), } type awsLogger struct{} diff --git a/builtin/providers/azurerm/config.go b/builtin/providers/azurerm/config.go index 50e9c89f40..e44a9222ab 100644 --- a/builtin/providers/azurerm/config.go +++ b/builtin/providers/azurerm/config.go @@ -92,13 +92,7 @@ func withRequestLogging() autorest.SendDecorator { } func setUserAgent(client *autorest.Client) { - var version string - if terraform.VersionPrerelease != "" { - version = fmt.Sprintf("%s-%s", terraform.Version, terraform.VersionPrerelease) - } else { - version = terraform.Version - } - + version := terraform.VersionString() client.UserAgent = fmt.Sprintf("HashiCorp-Terraform-v%s", version) } diff --git a/builtin/providers/google/config.go b/builtin/providers/google/config.go index 159a57e093..c824c9ee6b 100644 --- a/builtin/providers/google/config.go +++ b/builtin/providers/google/config.go @@ -85,11 +85,7 @@ func (c *Config) loadAndValidate() error { } } - versionString := terraform.Version - prerelease := terraform.VersionPrerelease - if len(prerelease) > 0 { - versionString = fmt.Sprintf("%s-%s", versionString, prerelease) - } + versionString := terraform.VersionString() userAgent := fmt.Sprintf( "(%s %s) Terraform/%s", runtime.GOOS, runtime.GOARCH, versionString) diff --git a/terraform/version.go b/terraform/version.go index 7462a3c67c..d753ff8e78 100644 --- a/terraform/version.go +++ b/terraform/version.go @@ -1,6 +1,8 @@ package terraform import ( + "fmt" + "github.com/hashicorp/go-version" ) @@ -20,3 +22,10 @@ var SemVersion = version.Must(version.NewVersion(Version)) // VersionHeader is the header name used to send the current terraform version // in http requests. const VersionHeader = "Terraform-Version" + +func VersionString() string { + if VersionPrerelease != "" { + return fmt.Sprintf("%s-%s", Version, VersionPrerelease) + } + return Version +} From 640c3a891fbead844cd1714ade1865294251feea Mon Sep 17 00:00:00 2001 From: James Bardin Date: Fri, 22 Jul 2016 10:22:46 -0400 Subject: [PATCH 4/4] Update vendored atlas client --- .../hashicorp/atlas-go/v1/client.go | 19 ++++++++++++++----- vendor/vendor.json | 4 +++- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/vendor/github.com/hashicorp/atlas-go/v1/client.go b/vendor/github.com/hashicorp/atlas-go/v1/client.go index 2e61e064b2..b5ee211a37 100644 --- a/vendor/github.com/hashicorp/atlas-go/v1/client.go +++ b/vendor/github.com/hashicorp/atlas-go/v1/client.go @@ -70,6 +70,10 @@ type Client struct { // HTTPClient is the underlying http client with which to make requests. HTTPClient *http.Client + + // DefaultHeaders is a set of headers that will be added to every request. + // This minimally includes the atlas user-agent string. + DefaultHeader http.Header } // DefaultClient returns a client that connects to the Atlas API. @@ -108,10 +112,13 @@ func NewClient(urlString string) (*Client, error) { } client := &Client{ - URL: parsedURL, - Token: token, + URL: parsedURL, + Token: token, + DefaultHeader: make(http.Header), } + client.DefaultHeader.Set("User-Agent", userAgent) + if err := client.init(); err != nil { return nil, err } @@ -227,10 +234,12 @@ func (c *Client) rawRequest(verb string, u *url.URL, ro *RequestOptions) (*http. return nil, err } - // Set the User-Agent - request.Header.Set("User-Agent", userAgent) + // set our default headers first + for k, v := range c.DefaultHeader { + request.Header[k] = v + } - // Add any headers (auth will be here if set) + // Add any request headers (auth will be here if set) for k, v := range ro.Headers { request.Header.Add(k, v) } diff --git a/vendor/vendor.json b/vendor/vendor.json index 931fedb092..0d225698a4 100644 --- a/vendor/vendor.json +++ b/vendor/vendor.json @@ -920,9 +920,11 @@ "revision": "95fa852edca41c06c4ce526af4bb7dec4eaad434" }, { + "checksumSHA1": "EWGfo74RcoKaYFZNSkvzYRJMgrY=", "comment": "20141209094003-92-g95fa852", "path": "github.com/hashicorp/atlas-go/v1", - "revision": "95fa852edca41c06c4ce526af4bb7dec4eaad434" + "revision": "c8b26aa95f096efc0f378b2d2830ca909631d584", + "revisionTime": "2016-07-22T13:58:36Z" }, { "comment": "v0.6.3-28-g3215b87",