From 3e69e1d582e99fa8dd93b3ef5fbbdb1e2ebcae20 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 --- workspace.go | 3 +++ workspace_integration_test.go | 36 ++++++++++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/workspace.go b/workspace.go index 89554b1b4..8bb7afcf8 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"), }