Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add All and AllWithOpts methods to DedicatedServerApi type #90

Merged
merged 1 commit into from
Sep 11, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions dedicated_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,41 @@ func (dsa DedicatedServerApi) List(ctx context.Context, opts DedicatedServerList
return result, nil
}

// AllWithOpts will return all dedicated servers and allows filtering options be specified as an argument
func (dsa DedicatedServerApi) AllWithOpts(ctx context.Context, opts DedicatedServerListOptions) ([]DedicatedServer, error) {
var allServers []DedicatedServer

// handle default pagination if Offset or Limit are undefined
if opts.PaginationOptions.Offset == nil || opts.PaginationOptions.Limit == nil {
opts.PaginationOptions = PaginationOptions{Offset: Int(0), Limit: Int(20)}
}

for {
// List all dedicated servers with defined opts
result, err := DedicatedServerApi{}.List(ctx, opts)
if err != nil {
return nil, err
}

// Break if no more servers are found
if len(result.Servers) == 0 {
break
}

// Append servers to list
allServers = append(allServers, result.Servers...)
// Increment Offset with the value of Limit
*opts.PaginationOptions.Offset += *opts.PaginationOptions.Limit
}

return allServers, nil
}

// All will return all dedicated servers without optional filters specified
func (dsa DedicatedServerApi) All(ctx context.Context) ([]DedicatedServer, error) {
return dsa.AllWithOpts(ctx, DedicatedServerListOptions{})
}

func (dsa DedicatedServerApi) Get(ctx context.Context, serverId string) (*DedicatedServer, error) {
path := dsa.getPath("/servers/" + serverId)
result := &DedicatedServer{}
Expand Down