Skip to content

Commit

Permalink
fix: plugin sdk passes sorting parameters (#524)
Browse files Browse the repository at this point in the history
  • Loading branch information
nanjingfm committed Apr 24, 2024
1 parent ad0aff2 commit 745e195
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
8 changes: 8 additions & 0 deletions plugin/client/plugin_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package client
import (
"encoding/base64"
"encoding/json"
"fmt"
"strconv"
"strings"

Expand Down Expand Up @@ -58,6 +59,13 @@ func ListOpts(opts metav1alpha1.ListOptions) OptionFunc {
}
request.SetQueryParam("page", strconv.Itoa(opts.Page))
request.SetQueryParam("itemsPerPage", strconv.Itoa(opts.ItemsPerPage))
if len(opts.Sort) > 0 {
var sorts = make([]string, 0, len(opts.Sort))
for _, item := range opts.Sort {
sorts = append(sorts, fmt.Sprintf("%s,%s", item.Order, item.SortBy))
}
request.SetQueryParam("sortBy", strings.Join(sorts, ","))
}

SubResourcesOpts(opts.SubResources)(request)
}
Expand Down
22 changes: 22 additions & 0 deletions plugin/client/plugin_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,28 @@ func secretForTest() v1.Secret {
return secret
}

func TestListOpts(t *testing.T) {
g := NewGomegaWithT(t)
opts := metav1alpha1.ListOptions{
ItemsPerPage: 10,
Page: 1,
Search: map[string][]string{"key": {"value"}},
Sort: []metav1alpha1.SortOptions{{SortBy: "name", Order: "asc"}},
}
opts.SubResources = []string{"subresource1", "subresource2"}

request := resty.New().R()

optionFunc := ListOpts(opts)
optionFunc(request)

g.Expect(request.QueryParam["itemsPerPage"][0]).To(Equal("10"))
g.Expect(request.QueryParam.Get("page")).To(Equal("1"))
g.Expect(request.QueryParam.Get("sortBy")).To(Equal("asc,name"))
g.Expect(request.QueryParam.Get("key")).To(Equal("value"))
g.Expect(request.Header.Get(PluginSubresourcesHeader)).To(Equal("subresource1,subresource2"))
}

func parseSecret(path string) v1.Secret {
filename, err := filepath.Abs(path)
if err != nil {
Expand Down

0 comments on commit 745e195

Please sign in to comment.