@@ -251,9 +251,10 @@ func (b *Remote) State(workspace string) (state.State, error) {
251251 }
252252
253253 // Configure the remote workspace name.
254- if workspace == backend .DefaultStateName {
254+ switch {
255+ case workspace == backend .DefaultStateName :
255256 workspace = b .workspace
256- } else if b .prefix != "" && ! strings .HasPrefix (workspace , b .prefix ) {
257+ case b .prefix != "" && ! strings .HasPrefix (workspace , b .prefix ):
257258 workspace = b .prefix + workspace
258259 }
259260
@@ -293,9 +294,10 @@ func (b *Remote) DeleteState(workspace string) error {
293294 }
294295
295296 // Configure the remote workspace name.
296- if workspace == backend .DefaultStateName {
297+ switch {
298+ case workspace == backend .DefaultStateName :
297299 workspace = b .workspace
298- } else if b .prefix != "" && ! strings .HasPrefix (workspace , b .prefix ) {
300+ case b .prefix != "" && ! strings .HasPrefix (workspace , b .prefix ):
299301 workspace = b .prefix + workspace
300302 }
301303
@@ -336,20 +338,39 @@ func (b *Remote) states() ([]string, error) {
336338 }
337339
338340 options := tfe.WorkspaceListOptions {}
339- wl , err := b .client .Workspaces .List (context .Background (), b .organization , options )
340- if err != nil {
341- return nil , err
341+ switch {
342+ case b .workspace != "" :
343+ options .Search = tfe .String (b .workspace )
344+ case b .prefix != "" :
345+ options .Search = tfe .String (b .prefix )
342346 }
343347
348+ // Create a slice to contain all the names.
344349 var names []string
345- for _ , w := range wl .Items {
346- if b .workspace != "" && w .Name == b .workspace {
347- names = append (names , backend .DefaultStateName )
348- continue
350+
351+ for {
352+ wl , err := b .client .Workspaces .List (context .Background (), b .organization , options )
353+ if err != nil {
354+ return nil , err
349355 }
350- if b .prefix != "" && strings .HasPrefix (w .Name , b .prefix ) {
351- names = append (names , strings .TrimPrefix (w .Name , b .prefix ))
356+
357+ for _ , w := range wl .Items {
358+ if b .workspace != "" && w .Name == b .workspace {
359+ names = append (names , backend .DefaultStateName )
360+ continue
361+ }
362+ if b .prefix != "" && strings .HasPrefix (w .Name , b .prefix ) {
363+ names = append (names , strings .TrimPrefix (w .Name , b .prefix ))
364+ }
352365 }
366+
367+ // Exit the loop when we've seen all pages.
368+ if wl .CurrentPage == wl .TotalPages {
369+ break
370+ }
371+
372+ // Update the page number to get the next page.
373+ options .PageNumber = wl .NextPage
353374 }
354375
355376 // Sort the result so we have consistent output.
@@ -361,9 +382,10 @@ func (b *Remote) states() ([]string, error) {
361382// Operation implements backend.Enhanced
362383func (b * Remote ) Operation (ctx context.Context , op * backend.Operation ) (* backend.RunningOperation , error ) {
363384 // Configure the remote workspace name.
364- if op .Workspace == backend .DefaultStateName {
385+ switch {
386+ case op .Workspace == backend .DefaultStateName :
365387 op .Workspace = b .workspace
366- } else if b .prefix != "" && ! strings .HasPrefix (op .Workspace , b .prefix ) {
388+ case b .prefix != "" && ! strings .HasPrefix (op .Workspace , b .prefix ):
367389 op .Workspace = b .prefix + op .Workspace
368390 }
369391
0 commit comments