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 sort field to workspace list options #859

Merged
merged 5 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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 admin_workspace_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ func TestAdminWorkspaces_ListWithSort(t *testing.T) {
require.NoError(t, err)
require.NotEmpty(t, wl.Items)
require.GreaterOrEqual(t, len(wl.Items), 2)
assert.True(t, wl.Items[1].CurrentRun.CreatedAt.After(wl.Items[0].CurrentRun.CreatedAt))
brandonc marked this conversation as resolved.
Show resolved Hide resolved
})
}

Expand Down
4 changes: 4 additions & 0 deletions workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,10 @@ type WorkspaceListOptions struct {

// 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"`

// Optional: May sort on "name" (the default) and "current-run.created-at" (which sorts by the time of the current run)
// Prepending a hyphen to the sort parameter will reverse the order (e.g. "-name" to reverse the default order)
Sort string `url:"sort,omitempty"`
}

// WorkspaceCreateOptions represents the options for creating a new workspace.
Expand Down
35 changes: 32 additions & 3 deletions workspace_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,16 @@ func TestWorkspacesList(t *testing.T) {
t.Cleanup(wTest1Cleanup)
wTest2, wTest2Cleanup := createWorkspace(t, client, orgTest)
t.Cleanup(wTest2Cleanup)
wTest3, wTest3Cleanup := createWorkspace(t, client, orgTest)
t.Cleanup(wTest3Cleanup)

t.Run("without list options", func(t *testing.T) {
wl, err := client.Workspaces.List(ctx, orgTest.Name, nil)
require.NoError(t, err)
assert.Contains(t, wl.Items, wTest1)
assert.Contains(t, wl.Items, wTest2)
assert.Equal(t, 1, wl.CurrentPage)
assert.Equal(t, 2, wl.TotalCount)
assert.Equal(t, 3, wl.TotalCount)
})

t.Run("with list options", func(t *testing.T) {
Expand All @@ -67,7 +69,34 @@ func TestWorkspacesList(t *testing.T) {
require.NoError(t, err)
assert.Empty(t, wl.Items)
assert.Equal(t, 999, wl.CurrentPage)
assert.Equal(t, 2, wl.TotalCount)
assert.Equal(t, 3, wl.TotalCount)
})

t.Run("when sorting by workspace names", func(t *testing.T) {
wl, err := client.Workspaces.List(ctx, orgTest.Name, &WorkspaceListOptions{
Sort: "name",
})
require.NoError(t, err)
require.NotEmpty(t, wl.Items)
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) {
_, unappliedCleanup1 := createRunUnapplied(t, client, wTest2)
t.Cleanup(unappliedCleanup1)

_, unappliedCleanup2 := createRunUnapplied(t, client, wTest3)
t.Cleanup(unappliedCleanup2)

wl, err := client.Workspaces.List(ctx, orgTest.Name, &WorkspaceListOptions{
Sort: "current-run.created-at",
})

require.NoError(t, err)
require.NotEmpty(t, wl.Items)
require.GreaterOrEqual(t, len(wl.Items), 2)
assert.True(t, wl.Items[1].CreatedAt.After(wl.Items[0].CreatedAt))
Maed223 marked this conversation as resolved.
Show resolved Hide resolved
})

t.Run("when searching a known workspace", func(t *testing.T) {
Expand Down Expand Up @@ -108,7 +137,7 @@ func TestWorkspacesList(t *testing.T) {
})

t.Run("when searching using exclude-tags", func(t *testing.T) {
for wsID, tag := range map[string]string{wTest1.ID: "foo", wTest2.ID: "bar"} {
for wsID, tag := range map[string]string{wTest1.ID: "foo", wTest2.ID: "bar", wTest3.ID: "foo"} {
err := client.Workspaces.AddTags(ctx, wsID, WorkspaceAddTagsOptions{
Tags: []*Tag{
{
Expand Down
Loading