From 641d87f4818e481c4f55b73da57289ff15735d0a Mon Sep 17 00:00:00 2001 From: Lauren Date: Thu, 27 Oct 2022 13:02:08 -0400 Subject: [PATCH] expose search param option to filter using wildcard --- CHANGELOG.md | 2 ++ workspace.go | 3 +++ workspace_integration_test.go | 36 ++++++++++++++++++++++++++++++++++- 3 files changed, 40 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b97861335..9007ad0c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # Unreleased +* Add `search[wildcard-name]` to `WorkspaceListOptions` [#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) diff --git a/workspace.go b/workspace.go index 89554b1b4..b0e6d2730 100644 --- a/workspace.go +++ b/workspace.go @@ -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"` } diff --git a/workspace_integration_test.go b/workspace_integration_test.go index 797db98d5..437445d80 100644 --- a/workspace_integration_test.go +++ b/workspace_integration_test.go @@ -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) { @@ -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"), }