Skip to content

Commit

Permalink
expose search param option to filter using wildcard
Browse files Browse the repository at this point in the history
  • Loading branch information
laurenolivia committed Nov 2, 2022
1 parent 6777fea commit 4c23de2
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,7 @@
# Unreleased

* Add `search[wildcard-name]` to `WorkspaceListOptions` by @laurenolivia [#569](https://github.com/hashicorp/go-tfe/pull/569)

## Enhancements

* Add `NotificationTriggerAssessmentCheckFailed` notification trigger type by @rexredinger [#549](https://github.com/hashicorp/go-tfe/pull/549)
Expand Down
3 changes: 3 additions & 0 deletions workspace.go
Expand Up @@ -252,6 +252,9 @@ type WorkspaceListOptions struct {
// Optional: A search string (comma-separated tag names to exclude) used to filter the results.
ExcludeTags string `url:"search[exclude-tags],omitempty"`

// Optional: A search on substring matching to filter the results.
WildcardName string `url:"search[wildcard-name],omitempty"`

// Optional: A list of relations to include. See available resources https://www.terraform.io/docs/cloud/api/workspaces.html#available-related-resources
Include []WSIncludeOpt `url:"include,omitempty"`
}
Expand Down
36 changes: 35 additions & 1 deletion workspace_integration_test.go
Expand Up @@ -184,6 +184,40 @@ func TestWorkspacesList(t *testing.T) {

assert.True(t, foundWTest1)
})

t.Run("when searching a known substring", func(t *testing.T) {
wildcardSearch := "*-prod"
// should be successful, and return 1 result
wTest, wTestCleanup := createWorkspaceWithOptions(t, client, orgTest, WorkspaceCreateOptions{
Name: String("hashicorp-prod"),
})
t.Cleanup(wTestCleanup)

wl, err := client.Workspaces.List(ctx, orgTest.Name, &WorkspaceListOptions{
WildcardName: wildcardSearch,
})

require.NoError(t, err)
assert.NotEmpty(t, wTest.ID)
assert.Equal(t, 1, wl.TotalCount)
})

t.Run("when wildcard match does not exist", func(t *testing.T) {
wildcardSearch := "*-dev"
// should be successful, but return no results
wTest, wTestCleanup := createWorkspaceWithOptions(t, client, orgTest, WorkspaceCreateOptions{
Name: String("hashicorp-staging"),
})
t.Cleanup(wTestCleanup)

wl, err := client.Workspaces.List(ctx, orgTest.Name, &WorkspaceListOptions{
WildcardName: wildcardSearch,
})

require.NoError(t, err)
assert.NotEmpty(t, wTest.ID)
assert.Equal(t, 0, wl.TotalCount)
})
}

func TestWorkspacesCreateTableDriven(t *testing.T) {
Expand Down Expand Up @@ -816,7 +850,7 @@ func TestWorkspacesUpdate(t *testing.T) {
assert.Equal(t, err, ErrUnsupportedOperations)
})

t.Run("when 'agent' execution mode is specified without an an agent pool ID", func(t *testing.T) {
t.Run("when 'agent' execution mode is specified without an agent pool ID", func(t *testing.T) {
options := WorkspaceUpdateOptions{
ExecutionMode: String("agent"),
}
Expand Down

0 comments on commit 4c23de2

Please sign in to comment.