Skip to content

Commit

Permalink
Merge pull request #675 from hashicorp/add-app-name-header
Browse files Browse the repository at this point in the history
Add `IsCloud()` and `IsEnterprise()` helper methods to client
  • Loading branch information
brandonc authored Apr 11, 2023
2 parents be24bd1 + 98ac7fb commit 2a1107b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
7 changes: 3 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
# Unreleased

FEATURES:
* Add beta endpoints `ApplyToProjects` and `RemoveFromProjects` to `VariableSets`. Applying a variable set to a project will apply that variable set to all current and future workspaces in that project.
* Add beta endpoint `ListForProject` to `VariableSets` to list all variable sets applied to a project.

## Enhancements
* Add beta endpoints `ApplyToProjects` and `RemoveFromProjects` to `VariableSets`. Applying a variable set to a project will apply that variable set to all current and future workspaces in that project.
* Add beta endpoint `ListForProject` to `VariableSets` to list all variable sets applied to a project.
* Adds `ProjectID` filter to allow filtering of workspaces of a given project in an organization by @hs26gill [#671](https://github.com/hashicorp/go-tfe/pull/671)
* Adds `Name` filter to allow filtering of projects by @hs26gill [#668](https://github.com/hashicorp/go-tfe/pull/668/files)
* Adds `ManageMembership` permission to team `OrganizationAccess` by @JarrettSpiker [#652](https://github.com/hashicorp/go-tfe/pull/652)
* Adds `RotateKey` and `TrimKey` Admin endpoints by @mpminardi [#666](https://github.com/hashicorp/go-tfe/pull/666)
* Adds `Permissions` to `User` by @jeevanragula [#674](https://github.com/hashicorp/go-tfe/pull/674)
* Adds `IsEnterprise` and `IsCloud` boolean methods to the client by @sebasslash [#675](https://github.com/hashicorp/go-tfe/pull/675)

# v1.20.0

Expand Down
26 changes: 26 additions & 0 deletions tfe.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (
_userAgent = "go-tfe"
_headerRateLimit = "X-RateLimit-Limit"
_headerRateReset = "X-RateLimit-Reset"
_headerAppName = "TFP-AppName"
_headerAPIVersion = "TFP-API-Version"
_headerTFEVersion = "X-TFE-Version"
_includeQueryParam = "include"
Expand Down Expand Up @@ -120,6 +121,7 @@ type Client struct {
retryServerErrors bool
remoteAPIVersion string
remoteTFEVersion string
appName string

Admin Admin
Agents Agents
Expand Down Expand Up @@ -361,6 +363,9 @@ func NewClient(cfg *Config) (*Client, error) {
// Save the TFE version
client.remoteTFEVersion = meta.TFEVersion

// Save the app name
client.appName = meta.AppName

// Create Admin
client.Admin = Admin{
Organizations: &adminOrganizations{client: client},
Expand Down Expand Up @@ -431,6 +436,23 @@ func NewClient(cfg *Config) (*Client, error) {
return client, nil
}

// IsCloud returns true if the client is configured against a Terraform Cloud
// instance.
//
// Whether an instance is TFC or TFE is derived from the TFP-AppName header.
func (c Client) IsCloud() bool {
return c.appName == "Terraform Cloud"
}

// IsEnterprise returns true if the client is configured against a Terraform
// Enterprise instance.
//
// Whether an instance is TFC or TFE is derived from the TFP-AppName header. Note:
// not all TFE releases include this header in API responses.
func (c Client) IsEnterprise() bool {
return !c.IsCloud()
}

// RemoteAPIVersion returns the server's declared API version string.
//
// A Terraform Cloud or Enterprise API server returns its API version in an
Expand Down Expand Up @@ -565,6 +587,9 @@ type rawAPIMetadata struct {
// X-RateLimit-Limit response header, or an empty string if that header
// field was not included in the response.
RateLimit string

// AppName is either 'Terraform Cloud' or 'Terraform Enterprise'
AppName string
}

func (c *Client) getRawAPIMetadata() (rawAPIMetadata, error) {
Expand Down Expand Up @@ -597,6 +622,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)
meta.AppName = resp.Header.Get(_headerAppName)

return meta, nil
}
Expand Down
11 changes: 11 additions & 0 deletions tfe_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ func TestClient_newClient(t *testing.T) {
w.Header().Set("X-RateLimit-Limit", "30")
w.Header().Set("TFP-API-Version", "34.21.9")
w.Header().Set("X-TFE-Version", "202205-1")
if enterpriseEnabled() {
w.Header().Set("TFP-AppName", "Terraform Enterprise")
} else {
w.Header().Set("TFP-AppName", "Terraform Cloud")
}
w.WriteHeader(204) // We query the configured ping URL which should return a 204.
}))
defer ts.Close()
Expand Down Expand Up @@ -85,6 +90,12 @@ func TestClient_newClient(t *testing.T) {
t.Errorf("unexpected remote TFE version %q; want %q", client.RemoteTFEVersion(), want)
}

if enterpriseEnabled() {
assert.True(t, client.IsEnterprise())
} else {
assert.True(t, client.IsCloud())
}

client.SetFakeRemoteAPIVersion("1.0")

if want := "1.0"; client.RemoteAPIVersion() != want {
Expand Down

0 comments on commit 2a1107b

Please sign in to comment.