Skip to content

Commit e330540

Browse files
authored
Merge pull request #671 from hashicorp/hs26gill/TF-5351-add-filter-by-project-id-for-listing-workspaces
Add filtering option to list workspaces of a particular project
2 parents 89e7909 + ac30218 commit e330540

4 files changed

+46
-3
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ FEATURES:
55
* Add beta endpoint `ListForProject` to `VariableSets` to list all variable sets applied to a project.
66

77
## Enhancements
8+
* 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)
89
* Adds `Name` filter to allow filtering of projects by @hs26gill [#668](https://github.com/hashicorp/go-tfe/pull/668/files)
910
* Adds `ManageMembership` permission to team `OrganizationAccess` by @JarrettSpiker [#652](https://github.com/hashicorp/go-tfe/pull/652)
1011
* Adds `RotateKey` and `TrimKey` Admin endpoints by @mpminardi [#666](https://github.com/hashicorp/go-tfe/pull/666)

admin_workspace_integration_test.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ import (
1414
"github.com/stretchr/testify/require"
1515
)
1616

17+
// BEWARE: The admin workspaces API can view all of the workspaces created by
18+
// EVERY test organization in EVERY concurrent test run (or other usage) for the
19+
// current TFC instance. It's generally not safe to assume that the workspaces
20+
// you create in a given test will be within the first page of list results, so
21+
// you might have to get creative and/or settle for less when testing the
22+
// behavior of these endpoints.
23+
1724
func TestAdminWorkspaces_ListWithFilter(t *testing.T) {
1825
client := testClient(t)
1926
ctx := context.Background()
@@ -64,7 +71,8 @@ func TestAdminWorkspaces_ListWithSort(t *testing.T) {
6471
})
6572
require.NoError(t, err)
6673
require.NotEmpty(t, wl.Items)
67-
assert.Equal(t, adminWorkspaceItemsContainsID(wl.Items, wTest1.ID), true)
74+
require.GreaterOrEqual(t, len(wl.Items), 2)
75+
assert.Equal(t, wl.Items[0].Name < wl.Items[1].Name, true)
6876
})
6977

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

106-
assert.Equal(t, adminWorkspaceItemsContainsID(wl.Items, wTest1.ID), true)
107-
assert.Equal(t, adminWorkspaceItemsContainsID(wl.Items, wTest2.ID), true)
114+
require.GreaterOrEqual(t, len(wl.Items), 2)
108115
})
109116

110117
t.Run("with list options", func(t *testing.T) {

workspace.go

+3
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,9 @@ type WorkspaceListOptions struct {
263263
// Optional: A search on substring matching to filter the results.
264264
WildcardName string `url:"search[wildcard-name],omitempty"`
265265

266+
// Optional: A filter string to list all the workspaces linked to a given project id in the organization.
267+
ProjectID string `url:"filter[project][id],omitempty"`
268+
266269
// Optional: A list of relations to include. See available resources https://developer.hashicorp.com/terraform/cloud-docs/api-docs/workspaces#available-related-resources
267270
Include []WSIncludeOpt `url:"include,omitempty"`
268271
}

workspace_integration_test.go

+32
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,38 @@ func TestWorkspacesList(t *testing.T) {
217217
assert.NotEmpty(t, wTest.ID)
218218
assert.Equal(t, 0, wl.TotalCount)
219219
})
220+
221+
t.Run("when using project id filter and project contains workspaces", func(t *testing.T) {
222+
// create a project in the orgTest
223+
p, pTestCleanup := createProject(t, client, orgTest)
224+
defer pTestCleanup()
225+
// create a workspace with project
226+
w, wTestCleanup := createWorkspaceWithOptions(t, client, orgTest, WorkspaceCreateOptions{
227+
Name: String(randomString(t)),
228+
Project: p,
229+
})
230+
defer wTestCleanup()
231+
232+
// List all the workspaces under the given ProjectID
233+
wl, err := client.Workspaces.List(ctx, orgTest.Name, &WorkspaceListOptions{
234+
ProjectID: p.ID,
235+
})
236+
require.NoError(t, err)
237+
assert.Contains(t, wl.Items, w)
238+
})
239+
240+
t.Run("when using project id filter but project contains no workspaces", func(t *testing.T) {
241+
// create a project in the orgTest
242+
p, pTestCleanup := createProject(t, client, orgTest)
243+
defer pTestCleanup()
244+
245+
// List all the workspaces under the given ProjectID
246+
wl, err := client.Workspaces.List(ctx, orgTest.Name, &WorkspaceListOptions{
247+
ProjectID: p.ID,
248+
})
249+
require.NoError(t, err)
250+
assert.Empty(t, wl.Items)
251+
})
220252
}
221253

222254
func TestWorkspacesCreateTableDriven(t *testing.T) {

0 commit comments

Comments
 (0)