diff --git a/CHANGELOG.md b/CHANGELOG.md index db2a2088f..c56e07fb1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Enhancements * Add `NotificationTriggerAssessmentCheckFailed` notification trigger type by @rexredinger [#549](https://github.com/hashicorp/go-tfe/pull/549) +* Add `RemoteTFEVersion()` to the `Client` interface, which exposes the `X-TFE-Version` header set by a remote TFE instance by @sebasslash [#563](https://github.com/hashicorp/go-tfe/pull/563) # v1.11.0 diff --git a/tfe.go b/tfe.go index 17e13b0f5..189ba2a20 100644 --- a/tfe.go +++ b/tfe.go @@ -34,6 +34,7 @@ const ( _headerRateLimit = "X-RateLimit-Limit" _headerRateReset = "X-RateLimit-Reset" _headerAPIVersion = "TFP-API-Version" + _headerTFEVersion = "X-TFE-Version" _includeQueryParam = "include" DefaultAddress = "https://app.terraform.io" @@ -115,6 +116,7 @@ type Client struct { retryLogHook RetryLogHook retryServerErrors bool remoteAPIVersion string + remoteTFEVersion string Admin Admin Agents Agents @@ -341,6 +343,9 @@ func NewClient(cfg *Config) (*Client, error) { // method later. client.remoteAPIVersion = meta.APIVersion + // Save the TFE version + client.remoteTFEVersion = meta.TFEVersion + // Create Admin client.Admin = Admin{ Organizations: &adminOrganizations{client: client}, @@ -436,6 +441,17 @@ func (c *Client) SetFakeRemoteAPIVersion(fakeAPIVersion string) { c.remoteAPIVersion = fakeAPIVersion } +// RemoteTFEVersion returns the server's declared TFE version string. +// +// A Terraform Enterprise API server includes its current version in an +// HTTP header field in all responses. This value is saved by the client +// during the initial setup request and RemoteTFEVersion returns that cached +// value. This function returns an empty string for any Terraform Enterprise version +// earlier than v202208-3 and for Terraform Cloud. +func (c *Client) RemoteTFEVersion() string { + return c.remoteTFEVersion +} + // RetryServerErrors configures the retry HTTP check to also retry // unexpected errors or requests that failed with a server error. @@ -515,6 +531,11 @@ type rawAPIMetadata struct { // field was not included in the response. APIVersion string + // TFEVersion is the raw TFE version string reported by the server in the + // X-TFE-Version response header, or an empty string if that header + // field was not included in the response. + TFEVersion string + // RateLimit is the raw API version string reported by the server in the // X-RateLimit-Limit response header, or an empty string if that header // field was not included in the response. @@ -550,6 +571,7 @@ func (c *Client) getRawAPIMetadata() (rawAPIMetadata, error) { meta.APIVersion = resp.Header.Get(_headerAPIVersion) meta.RateLimit = resp.Header.Get(_headerRateLimit) + meta.TFEVersion = resp.Header.Get(_headerTFEVersion) return meta, nil } diff --git a/tfe_integration_test.go b/tfe_integration_test.go index 73faa5b66..b8e67a7fd 100644 --- a/tfe_integration_test.go +++ b/tfe_integration_test.go @@ -27,6 +27,7 @@ func TestClient_newClient(t *testing.T) { w.Header().Set("Content-Type", "application/vnd.api+json") w.Header().Set("X-RateLimit-Limit", "30") w.Header().Set("TFP-API-Version", "34.21.9") + w.Header().Set("X-TFE-Version", "202205-1") w.WriteHeader(204) // We query the configured ping URL which should return a 204. })) defer ts.Close() @@ -83,6 +84,9 @@ func TestClient_newClient(t *testing.T) { if want := "34.21.9"; client.RemoteAPIVersion() != want { t.Errorf("unexpected remote API version %q; want %q", client.RemoteAPIVersion(), want) } + if want := "202205-1"; client.RemoteTFEVersion() != want { + t.Errorf("unexpected remote TFE version %q; want %q", client.RemoteTFEVersion(), want) + } client.SetFakeRemoteAPIVersion("1.0")