Skip to content

Commit aa61d1b

Browse files
authored
add sort and filter to admin/workspaces#list-all-workspaces (#641)
add list options sort and filter
1 parent 7652809 commit aa61d1b

File tree

2 files changed

+81
-0
lines changed

2 files changed

+81
-0
lines changed

admin_workspace.go

+9
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,18 @@ type AdminWorkspaceListOptions struct {
6666
// A query string (partial workspace name) used to filter the results.
6767
// https://developer.hashicorp.com/terraform/enterprise/api-docs/admin/workspaces#query-parameters
6868
Query string `url:"q,omitempty"`
69+
6970
// Optional: A list of relations to include. See available resources
7071
// https://developer.hashicorp.com/terraform/enterprise/api-docs/admin/workspaces#available-related-resources
7172
Include []AdminWorkspaceIncludeOpt `url:"include,omitempty"`
73+
74+
// Optional: A comma-separated list of Run statuses to restrict results. See available resources
75+
// https://developer.hashicorp.com/terraform/enterprise/api-docs/admin/workspaces#query-parameters
76+
Filter string `url:"filter[current_run][status],omitempty"`
77+
78+
// Optional: May sort on "name" (the default) and "current-run.created-at" (which sorts by the time of the current run)
79+
// Prepending a hyphen to the sort parameter will reverse the order (e.g. "-name" to reverse the default order)
80+
Sort string `url:"sort,omitempty"`
7281
}
7382

7483
// AdminWorkspaceList represents a list of workspaces.

admin_workspace_integration_test.go

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

17+
func TestAdminWorkspaces_ListWithFilter(t *testing.T) {
18+
client := testClient(t)
19+
ctx := context.Background()
20+
21+
org, orgCleanup := createOrganization(t, client)
22+
defer orgCleanup()
23+
24+
wTest1, wTest1Cleanup := createWorkspace(t, client, org)
25+
defer wTest1Cleanup()
26+
27+
wTest2, wTest2Cleanup := createWorkspace(t, client, org)
28+
defer wTest2Cleanup()
29+
30+
t.Run("when filtering workspaces on a current run status", func(t *testing.T) {
31+
_, appliedCleanup := createRunApply(t, client, wTest1)
32+
t.Cleanup(appliedCleanup)
33+
34+
_, unAppliedCleanup := createRunUnapplied(t, client, wTest2)
35+
t.Cleanup(unAppliedCleanup)
36+
37+
wl, err := client.Admin.Workspaces.List(ctx, &AdminWorkspaceListOptions{
38+
Filter: string(RunApplied), Include: []AdminWorkspaceIncludeOpt{AdminWorkspaceCurrentRun},
39+
})
40+
41+
require.NoError(t, err)
42+
require.NotEmpty(t, wl.Items)
43+
assert.Equal(t, wl.Items[0].CurrentRun.Status, RunApplied)
44+
assert.NotContains(t, wl.Items, wTest2)
45+
})
46+
}
47+
48+
func TestAdminWorkspaces_ListWithSort(t *testing.T) {
49+
client := testClient(t)
50+
ctx := context.Background()
51+
52+
org, orgCleanup := createOrganization(t, client)
53+
defer orgCleanup()
54+
55+
wTest1, wTest1Cleanup := createWorkspace(t, client, org)
56+
defer wTest1Cleanup()
57+
58+
wTest2, wTest2Cleanup := createWorkspace(t, client, org)
59+
defer wTest2Cleanup()
60+
61+
t.Run("when sorting by workspace names", func(t *testing.T) {
62+
wl, err := client.Admin.Workspaces.List(ctx, &AdminWorkspaceListOptions{
63+
Sort: "name",
64+
})
65+
require.NoError(t, err)
66+
require.NotEmpty(t, wl.Items)
67+
assert.Equal(t, adminWorkspaceItemsContainsID(wl.Items, wTest1.ID), true)
68+
})
69+
70+
t.Run("when sorting workspaces on current-run.created-at", func(t *testing.T) {
71+
_, unappliedCleanup1 := createRunUnapplied(t, client, wTest1)
72+
t.Cleanup(unappliedCleanup1)
73+
74+
_, unappliedCleanup2 := createRunUnapplied(t, client, wTest2)
75+
t.Cleanup(unappliedCleanup2)
76+
77+
wl, err := client.Admin.Workspaces.List(ctx, &AdminWorkspaceListOptions{
78+
Sort: "current-run.created-at",
79+
})
80+
81+
require.NoError(t, err)
82+
require.NotEmpty(t, wl.Items)
83+
require.GreaterOrEqual(t, len(wl.Items), 2)
84+
})
85+
}
86+
1787
func TestAdminWorkspaces_List(t *testing.T) {
1888
skipUnlessEnterprise(t)
1989

@@ -97,6 +167,8 @@ func TestAdminWorkspaces_List(t *testing.T) {
97167
assert.NotEmpty(t, wl.Items[0].Organization.Name)
98168
})
99169

170+
// This sub-test should remain last because it creates a run that does not apply
171+
// Any subsequent runs will be queued until a timeout is triggered
100172
t.Run("with current_run included", func(t *testing.T) {
101173
cvTest, cvCleanup := createUploadedConfigurationVersion(t, client, wTest1)
102174
defer cvCleanup()

0 commit comments

Comments
 (0)