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 filtering option to list workspaces of a particular project #671

Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ FEATURES:
* Add beta endpoint `ListForProject` to `VariableSets` to list all variable sets applied to a project.

## Enhancements
* 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)
Expand Down
13 changes: 10 additions & 3 deletions admin_workspace_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ import (
"github.com/stretchr/testify/require"
)

// BEWARE: The admin workspaces API can view all of the workspaces created by
// EVERY test organization in EVERY concurrent test run (or other usage) for the
// current TFC instance. It's generally not safe to assume that the workspaces
// you create in a given test will be within the first page of list results, so
// you might have to get creative and/or settle for less when testing the
// behavior of these endpoints.

func TestAdminWorkspaces_ListWithFilter(t *testing.T) {
client := testClient(t)
ctx := context.Background()
Expand Down Expand Up @@ -64,7 +71,8 @@ func TestAdminWorkspaces_ListWithSort(t *testing.T) {
})
require.NoError(t, err)
require.NotEmpty(t, wl.Items)
assert.Equal(t, adminWorkspaceItemsContainsID(wl.Items, wTest1.ID), true)
require.GreaterOrEqual(t, len(wl.Items), 2)
assert.Equal(t, wl.Items[0].Name < wl.Items[1].Name, true)
})

t.Run("when sorting workspaces on current-run.created-at", func(t *testing.T) {
Expand Down Expand Up @@ -103,8 +111,7 @@ func TestAdminWorkspaces_List(t *testing.T) {
wl, err := client.Admin.Workspaces.List(ctx, nil)
require.NoError(t, err)

assert.Equal(t, adminWorkspaceItemsContainsID(wl.Items, wTest1.ID), true)
assert.Equal(t, adminWorkspaceItemsContainsID(wl.Items, wTest2.ID), true)
require.GreaterOrEqual(t, len(wl.Items), 2)
})

t.Run("with list options", func(t *testing.T) {
Expand Down
3 changes: 3 additions & 0 deletions workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,9 @@ type WorkspaceListOptions struct {
// Optional: A search on substring matching to filter the results.
WildcardName string `url:"search[wildcard-name],omitempty"`

// Optional: A filter string to list all the workspaces linked to a given project id in the organization.
ProjectID string `url:"filter[project][id],omitempty"`

// Optional: A list of relations to include. See available resources https://developer.hashicorp.com/terraform/cloud-docs/api-docs/workspaces#available-related-resources
Include []WSIncludeOpt `url:"include,omitempty"`
}
Expand Down
32 changes: 32 additions & 0 deletions workspace_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,38 @@ func TestWorkspacesList(t *testing.T) {
assert.NotEmpty(t, wTest.ID)
assert.Equal(t, 0, wl.TotalCount)
})

t.Run("when using project id filter and project contains workspaces", func(t *testing.T) {
// create a project in the orgTest
p, pTestCleanup := createProject(t, client, orgTest)
defer pTestCleanup()
// create a workspace with project
w, wTestCleanup := createWorkspaceWithOptions(t, client, orgTest, WorkspaceCreateOptions{
Name: String(randomString(t)),
Project: p,
})
defer wTestCleanup()

// List all the workspaces under the given ProjectID
wl, err := client.Workspaces.List(ctx, orgTest.Name, &WorkspaceListOptions{
ProjectID: p.ID,
})
require.NoError(t, err)
assert.Contains(t, wl.Items, w)
})

t.Run("when using project id filter but project contains no workspaces", func(t *testing.T) {
// create a project in the orgTest
p, pTestCleanup := createProject(t, client, orgTest)
defer pTestCleanup()

// List all the workspaces under the given ProjectID
wl, err := client.Workspaces.List(ctx, orgTest.Name, &WorkspaceListOptions{
ProjectID: p.ID,
})
require.NoError(t, err)
assert.Empty(t, wl.Items)
})
}

func TestWorkspacesCreateTableDriven(t *testing.T) {
Expand Down