Skip to content

Commit

Permalink
Merge pull request #859 from hashicorp/add-sort-field-workspace-list-…
Browse files Browse the repository at this point in the history
…options

Add sort field to workspace list options
  • Loading branch information
Maed223 authored Feb 26, 2024
2 parents 431c527 + 2cf5722 commit 2d5c976
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
4 changes: 3 additions & 1 deletion admin_workspace_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,14 @@ func TestAdminWorkspaces_ListWithSort(t *testing.T) {
t.Cleanup(unappliedCleanup2)

wl, err := client.Admin.Workspaces.List(ctx, &AdminWorkspaceListOptions{
Sort: "current-run.created-at",
Include: []AdminWorkspaceIncludeOpt{AdminWorkspaceCurrentRun},
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].CurrentRun.CreatedAt.After(wl.Items[0].CurrentRun.CreatedAt))
})
}

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
36 changes: 33 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,35 @@ 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{
Include: []WSIncludeOpt{WSCurrentRun},
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].CurrentRun.CreatedAt.After(wl.Items[0].CurrentRun.CreatedAt))
})

t.Run("when searching a known workspace", func(t *testing.T) {
Expand Down Expand Up @@ -108,7 +138,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

0 comments on commit 2d5c976

Please sign in to comment.