Skip to content

Commit 0beb480

Browse files
authored
Merge pull request #1246 from hashicorp/skj/new-tfe-numeric-version
added RemoteTFENumericVersion() for TFE numeric version
2 parents ab06fea + b682c9a commit 0beb480

File tree

3 files changed

+48
-21
lines changed

3 files changed

+48
-21
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Unreleased
22

33
* Remove org settings validation in RegistryModulesCreateMonorepo tests, by @jillirami ([#1236](https://github.com/hashicorp/go-tfe/pull/1236))
4+
* Add `RemoteTFENumericVersion()` to the `Client` interface, which exposes the `X-TFE-Current-Version` header set by a remote TFE instance by @skj-skj [#1246](https://github.com/hashicorp/go-tfe/pull/1246)
45

56
# v1.95.0
67

tfe.go

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,14 @@ import (
3232
)
3333

3434
const (
35-
_userAgent = "go-tfe"
36-
_headerRateLimit = "X-RateLimit-Limit"
37-
_headerRateReset = "X-RateLimit-Reset"
38-
_headerAppName = "TFP-AppName"
39-
_headerAPIVersion = "TFP-API-Version"
40-
_headerTFEVersion = "X-TFE-Version"
41-
_includeQueryParam = "include"
35+
_userAgent = "go-tfe"
36+
_headerRateLimit = "X-RateLimit-Limit"
37+
_headerRateReset = "X-RateLimit-Reset"
38+
_headerAppName = "TFP-AppName"
39+
_headerAPIVersion = "TFP-API-Version"
40+
_headerTFEVersion = "X-TFE-Version"
41+
_headerTFENumericVersion = "X-TFE-Current-Version"
42+
_includeQueryParam = "include"
4243

4344
DefaultAddress = "https://app.terraform.io"
4445
DefaultBasePath = "/api/v2/"
@@ -111,17 +112,18 @@ func DefaultConfig() *Config {
111112
// Client is the Terraform Enterprise API client. It provides the basic
112113
// connectivity and configuration for accessing the TFE API
113114
type Client struct {
114-
baseURL *url.URL
115-
registryBaseURL *url.URL
116-
token string
117-
headers http.Header
118-
http *retryablehttp.Client
119-
limiter *rate.Limiter
120-
retryLogHook RetryLogHook
121-
retryServerErrors bool
122-
remoteAPIVersion string
123-
remoteTFEVersion string
124-
appName string
115+
baseURL *url.URL
116+
registryBaseURL *url.URL
117+
token string
118+
headers http.Header
119+
http *retryablehttp.Client
120+
limiter *rate.Limiter
121+
retryLogHook RetryLogHook
122+
retryServerErrors bool
123+
remoteAPIVersion string
124+
remoteTFEVersion string
125+
remoteTFENumericVersion string
126+
appName string
125127

126128
Admin Admin
127129
Agents Agents
@@ -449,6 +451,9 @@ func NewClient(cfg *Config) (*Client, error) {
449451
// Save the TFE version
450452
client.remoteTFEVersion = meta.TFEVersion
451453

454+
// Save the TFE Numeric version
455+
client.remoteTFENumericVersion = meta.TFENumericVersion
456+
452457
// Save the app name
453458
client.appName = meta.AppName
454459

@@ -613,7 +618,7 @@ func (c *Client) SetFakeRemoteAPIVersion(fakeAPIVersion string) {
613618
c.remoteAPIVersion = fakeAPIVersion
614619
}
615620

616-
// RemoteTFEVersion returns the server's declared TFE version string.
621+
// RemoteTFEVersion returns the server's declared TFE monthly version string.
617622
//
618623
// A Terraform Enterprise API server includes its current version in an
619624
// HTTP header field in all responses. This value is saved by the client
@@ -624,6 +629,17 @@ func (c Client) RemoteTFEVersion() string {
624629
return c.remoteTFEVersion
625630
}
626631

632+
// RemoteTFENumericVersion returns the server's declared TFE version string.
633+
//
634+
// A Terraform Enterprise API server includes its current numeric version in an
635+
// HTTP header field in all responses. This value is saved by the client
636+
// during the initial setup request and RemoteTFENumericVersion returns that cached
637+
// value. This function returns an empty string for any Terraform Enterprise version
638+
// earlier than 1.0.3 and for HCP Terraform.
639+
func (c Client) RemoteTFENumericVersion() string {
640+
return c.remoteTFENumericVersion
641+
}
642+
627643
// RetryServerErrors configures the retry HTTP check to also retry
628644
// unexpected errors or requests that failed with a server error.
629645
func (c *Client) RetryServerErrors(retry bool) {
@@ -699,11 +715,16 @@ type rawAPIMetadata struct {
699715
// field was not included in the response.
700716
APIVersion string
701717

702-
// TFEVersion is the raw TFE version string reported by the server in the
718+
// TFEVersion is the raw TFE monthly version string reported by the server in the
703719
// X-TFE-Version response header, or an empty string if that header
704720
// field was not included in the response.
705721
TFEVersion string
706722

723+
// TFENumericVersion is the raw TFE Numeric version string reported by the server in the
724+
// X-TFE-Current-Version response header, or an empty string if that header
725+
// field was not included in the response.
726+
TFENumericVersion string
727+
707728
// RateLimit is the raw API version string reported by the server in the
708729
// X-RateLimit-Limit response header, or an empty string if that header
709730
// field was not included in the response.
@@ -743,6 +764,7 @@ func (c *Client) getRawAPIMetadata() (rawAPIMetadata, error) {
743764
meta.APIVersion = resp.Header.Get(_headerAPIVersion)
744765
meta.RateLimit = resp.Header.Get(_headerRateLimit)
745766
meta.TFEVersion = resp.Header.Get(_headerTFEVersion)
767+
meta.TFENumericVersion = resp.Header.Get(_headerTFENumericVersion)
746768
meta.AppName = resp.Header.Get(_headerAppName)
747769

748770
return meta, nil

tfe_integration_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func TestClient_newClient(t *testing.T) {
2525
w.Header().Set("X-RateLimit-Limit", "30")
2626
w.Header().Set("TFP-API-Version", "34.21.9")
2727
w.Header().Set("X-TFE-Version", "202205-1")
28+
w.Header().Set("X-TFE-Current-Version", "1.1.0")
2829
if enterpriseEnabled() {
2930
w.Header().Set("TFP-AppName", "Terraform Enterprise")
3031
} else {
@@ -87,7 +88,10 @@ func TestClient_newClient(t *testing.T) {
8788
t.Errorf("unexpected remote API version %q; want %q", client.RemoteAPIVersion(), want)
8889
}
8990
if want := "202205-1"; client.RemoteTFEVersion() != want {
90-
t.Errorf("unexpected remote TFE version %q; want %q", client.RemoteTFEVersion(), want)
91+
t.Errorf("unexpected remote TFE monthly version %q; want %q", client.RemoteTFEVersion(), want)
92+
}
93+
if want := "1.1.0"; client.RemoteTFENumericVersion() != want {
94+
t.Errorf("unexpected remote TFE numeric version %q; want %q", client.RemoteTFENumericVersion(), want)
9195
}
9296

9397
if enterpriseEnabled() {

0 commit comments

Comments
 (0)