Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add IsCloud() and IsEnterprise() helper methods to client #675

Merged
merged 2 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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