diff --git a/plugin/client/plugin_options.go b/plugin/client/plugin_options.go index 7e7789d1..1e22dd8a 100644 --- a/plugin/client/plugin_options.go +++ b/plugin/client/plugin_options.go @@ -19,6 +19,7 @@ package client import ( "encoding/base64" "encoding/json" + "fmt" "strconv" "strings" @@ -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) } diff --git a/plugin/client/plugin_options_test.go b/plugin/client/plugin_options_test.go index a8f8840a..342764af 100644 --- a/plugin/client/plugin_options_test.go +++ b/plugin/client/plugin_options_test.go @@ -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 {