diff --git a/plugin/client/base/options.go b/plugin/client/base/options.go index 8680e717..ef7cd2cb 100644 --- a/plugin/client/base/options.go +++ b/plugin/client/base/options.go @@ -19,6 +19,7 @@ package base 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/base/options_test.go b/plugin/client/base/options_test.go index efe379b7..33dbb04a 100644 --- a/plugin/client/base/options_test.go +++ b/plugin/client/base/options_test.go @@ -98,3 +98,25 @@ 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")) +} diff --git a/plugin/client/v2/repository.go b/plugin/client/v2/repository.go index 52838279..c0a30f90 100644 --- a/plugin/client/v2/repository.go +++ b/plugin/client/v2/repository.go @@ -28,8 +28,15 @@ import ( func (p *PluginClient) ListRepositories(ctx context.Context, params metav1alpha1.RepositoryOptions, option metav1alpha1.ListOptions) (*metav1alpha1.RepositoryList, error) { list := &metav1alpha1.RepositoryList{} + options := []base.OptionFunc{ + base.QueryOpts(map[string]string{ + "subType": params.SubType.String(), + }), + base.ResultOpts(list), + base.ListOpts(option), + } + uri := fmt.Sprintf("projects/%s/repositories", params.Project) - options := []base.OptionFunc{base.ResultOpts(list), base.ListOpts(option)} if err := p.Get(ctx, p.ClassAddress, uri, options...); err != nil { return nil, err }