Skip to content

Commit

Permalink
Feat/filtering server groups (#204)
Browse files Browse the repository at this point in the history
  • Loading branch information
ka-myl authored Jan 23, 2023
1 parent 1c8a32d commit ceab595
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 93 deletions.
15 changes: 9 additions & 6 deletions upcloud/request/server_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,23 +12,26 @@ const (
)

// GetServerGroupsRequest represents a request to list server groups
type GetServerGroupsRequest struct{}
type GetServerGroupsRequest struct {
Filters []QueryFilter
}

func (s GetServerGroupsRequest) RequestURL() string {
return serverGroupBasePath
if len(s.Filters) == 0 {
return serverGroupBasePath
}

return fmt.Sprintf("%s?%s", serverGroupBasePath, encodeQueryFilters(s.Filters))
}

// Deprecated: ServerGroupFilter filter is deprecated. Use QueryFilter instead.
type ServerGroupFilter = QueryFilter

// GetServerGroupsWithFiltersRequest represents a request to get
// all server groups using labels or label keys as filters.
// Using multiple filters returns only groups that match all.
// Deprecated: GetServerGroupsWithFiltersRequest is deprecated. Use GetServerGroupsRequest instead.
type GetServerGroupsWithFiltersRequest struct {
Filters []QueryFilter
}

// RequestURL implements the Request interface.
func (r *GetServerGroupsWithFiltersRequest) RequestURL() string {
if len(r.Filters) == 0 {
return serverGroupBasePath
Expand Down
12 changes: 12 additions & 0 deletions upcloud/request/server_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,18 @@ import (
func TestGetServerGroupsRequest(t *testing.T) {
r := GetServerGroupsRequest{}
assert.Equal(t, "/server-group", r.RequestURL())

rWithFilters := GetServerGroupsRequest{
Filters: []QueryFilter{
FilterLabel{Label: upcloud.Label{
Key: "env",
Value: "test",
}},
FilterLabelKey{Key: "managed"},
},
}

assert.Equal(t, "/server-group?label=env%3Dtest&label=managed", rWithFilters.RequestURL())
}

// TestGetServerGroupsWithFiltersRequest tests that GetServerGroupsWithFiltersRequest objects behave correctly
Expand Down
Loading

0 comments on commit ceab595

Please sign in to comment.