Skip to content

Commit

Permalink
fix: plugin sdk passes sorting parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
nanjingfm committed Jan 17, 2024
1 parent fabd9c8 commit 28d88ff
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
8 changes: 8 additions & 0 deletions plugin/client/base/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package base
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/base/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
}
9 changes: 8 additions & 1 deletion plugin/client/v2/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 28d88ff

Please sign in to comment.