Skip to content

Commit effdab0

Browse files
committed
fix: container registry images now correct send the query params
1 parent 25a14f4 commit effdab0

File tree

6 files changed

+246
-34
lines changed

6 files changed

+246
-34
lines changed

containerregistry/commons.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package containerregistry
22

3+
import (
4+
"net/url"
5+
"strconv"
6+
"strings"
7+
)
8+
39
type (
410
Meta struct {
511
Page Page `json:"page"`
@@ -12,3 +18,20 @@ type (
1218
Total int `json:"total"`
1319
}
1420
)
21+
22+
func CreatePaginationParams(opts ListOptions) url.Values {
23+
query := make(url.Values)
24+
if opts.Limit != nil {
25+
query.Set("_limit", strconv.Itoa(*opts.Limit))
26+
}
27+
if opts.Offset != nil {
28+
query.Set("_offset", strconv.Itoa(*opts.Offset))
29+
}
30+
if opts.Sort != nil {
31+
query.Set("_sort", *opts.Sort)
32+
}
33+
if len(opts.Expand) > 0 {
34+
query.Set("_expand", strings.Join(opts.Expand, ","))
35+
}
36+
return query
37+
}

containerregistry/commons_test.go

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package containerregistry
2+
3+
import (
4+
"net/url"
5+
"reflect"
6+
"testing"
7+
)
8+
9+
func TestCreatePaginationParams(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
opts ListOptions
13+
want url.Values
14+
}{
15+
{
16+
name: "empty options",
17+
opts: ListOptions{},
18+
want: url.Values{},
19+
},
20+
{
21+
name: "with limit",
22+
opts: ListOptions{
23+
Limit: intPtr(10),
24+
},
25+
want: url.Values{
26+
"_limit": []string{"10"},
27+
},
28+
},
29+
{
30+
name: "with offset",
31+
opts: ListOptions{
32+
Offset: intPtr(5),
33+
},
34+
want: url.Values{
35+
"_offset": []string{"5"},
36+
},
37+
},
38+
{
39+
name: "with sort",
40+
opts: ListOptions{
41+
Sort: strPtr("name:asc"),
42+
},
43+
want: url.Values{
44+
"_sort": []string{"name:asc"},
45+
},
46+
},
47+
{
48+
name: "with single expand",
49+
opts: ListOptions{
50+
Expand: []string{"tags"},
51+
},
52+
want: url.Values{
53+
"_expand": []string{"tags"},
54+
},
55+
},
56+
{
57+
name: "with multiple expand",
58+
opts: ListOptions{
59+
Expand: []string{"tags", "manifest", "layers"},
60+
},
61+
want: url.Values{
62+
"_expand": []string{"tags,manifest,layers"},
63+
},
64+
},
65+
{
66+
name: "with empty expand slice",
67+
opts: ListOptions{
68+
Expand: []string{},
69+
},
70+
want: url.Values{},
71+
},
72+
{
73+
name: "with all options",
74+
opts: ListOptions{
75+
Limit: intPtr(20),
76+
Offset: intPtr(10),
77+
Sort: strPtr("created_at:desc"),
78+
Expand: []string{"tags", "metadata"},
79+
},
80+
want: url.Values{
81+
"_limit": []string{"20"},
82+
"_offset": []string{"10"},
83+
"_sort": []string{"created_at:desc"},
84+
"_expand": []string{"tags,metadata"},
85+
},
86+
},
87+
{
88+
name: "with zero values",
89+
opts: ListOptions{
90+
Limit: intPtr(0),
91+
Offset: intPtr(0),
92+
Sort: strPtr(""),
93+
Expand: []string{""},
94+
},
95+
want: url.Values{
96+
"_limit": []string{"0"},
97+
"_offset": []string{"0"},
98+
"_sort": []string{""},
99+
"_expand": []string{""},
100+
},
101+
},
102+
}
103+
104+
for _, tt := range tests {
105+
t.Run(tt.name, func(t *testing.T) {
106+
got := CreatePaginationParams(tt.opts)
107+
if !reflect.DeepEqual(got, tt.want) {
108+
t.Errorf("CreatePaginationParams() = %v, want %v", got, tt.want)
109+
}
110+
})
111+
}
112+
}

containerregistry/images.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ type (
5252
// List retrieves a list of images within a repository with optional filtering
5353
func (c *imagesService) List(ctx context.Context, registryID, repositoryName string, opts ListOptions) (*ImagesResponse, error) {
5454
path := fmt.Sprintf("/v0/registries/%s/repositories/%s/images", registryID, repositoryName)
55+
query := CreatePaginationParams(opts)
5556

56-
res, err := mgc_http.ExecuteSimpleRequestWithRespBody[ImagesResponse](ctx, c.client.newRequest, c.client.GetConfig(), http.MethodGet, path, nil, nil)
57+
res, err := mgc_http.ExecuteSimpleRequestWithRespBody[ImagesResponse](ctx, c.client.newRequest, c.client.GetConfig(), http.MethodGet, path, nil, query)
5758
if err != nil {
5859
return nil, err
5960
}

containerregistry/images_test.go

Lines changed: 107 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ func TestImagesService_List(t *testing.T) {
1515
opts ListOptions
1616
response string
1717
statusCode int
18+
expectedQuery map[string]string
1819
want *ImagesResponse
1920
wantErr bool
2021
}{
@@ -43,7 +44,8 @@ func TestImagesService_List(t *testing.T) {
4344
}
4445
]
4546
}`,
46-
statusCode: http.StatusOK,
47+
statusCode: http.StatusOK,
48+
expectedQuery: map[string]string{},
4749
want: &ImagesResponse{
4850
Results: []ImageResponse{
4951
{
@@ -67,12 +69,106 @@ func TestImagesService_List(t *testing.T) {
6769
},
6870
wantErr: false,
6971
},
72+
{
73+
name: "list images with limit",
74+
registryID: "reg-123",
75+
repositoryName: "repo-test",
76+
opts: ListOptions{
77+
Limit: intPtr(10),
78+
},
79+
response: `{
80+
"results": []
81+
}`,
82+
statusCode: http.StatusOK,
83+
expectedQuery: map[string]string{"_limit": "10"},
84+
want: &ImagesResponse{
85+
Results: []ImageResponse{},
86+
},
87+
wantErr: false,
88+
},
89+
{
90+
name: "list images with offset",
91+
registryID: "reg-123",
92+
repositoryName: "repo-test",
93+
opts: ListOptions{
94+
Offset: intPtr(5),
95+
},
96+
response: `{
97+
"results": []
98+
}`,
99+
statusCode: http.StatusOK,
100+
expectedQuery: map[string]string{"_offset": "5"},
101+
want: &ImagesResponse{
102+
Results: []ImageResponse{},
103+
},
104+
wantErr: false,
105+
},
106+
{
107+
name: "list images with sort",
108+
registryID: "reg-123",
109+
repositoryName: "repo-test",
110+
opts: ListOptions{
111+
Sort: strPtr("pushed_at"),
112+
},
113+
response: `{
114+
"results": []
115+
}`,
116+
statusCode: http.StatusOK,
117+
expectedQuery: map[string]string{"_sort": "pushed_at"},
118+
want: &ImagesResponse{
119+
Results: []ImageResponse{},
120+
},
121+
wantErr: false,
122+
},
123+
{
124+
name: "list images with expand",
125+
registryID: "reg-123",
126+
repositoryName: "repo-test",
127+
opts: ListOptions{
128+
Expand: []string{"tags", "manifest"},
129+
},
130+
response: `{
131+
"results": []
132+
}`,
133+
statusCode: http.StatusOK,
134+
expectedQuery: map[string]string{"_expand": "tags,manifest"},
135+
want: &ImagesResponse{
136+
Results: []ImageResponse{},
137+
},
138+
wantErr: false,
139+
},
140+
{
141+
name: "list images with multiple options",
142+
registryID: "reg-123",
143+
repositoryName: "repo-test",
144+
opts: ListOptions{
145+
Limit: intPtr(20),
146+
Offset: intPtr(10),
147+
Sort: strPtr("created_at"),
148+
Expand: []string{"tags"},
149+
},
150+
response: `{
151+
"results": []
152+
}`,
153+
statusCode: http.StatusOK,
154+
expectedQuery: map[string]string{
155+
"_limit": "20",
156+
"_offset": "10",
157+
"_sort": "created_at",
158+
"_expand": "tags",
159+
},
160+
want: &ImagesResponse{
161+
Results: []ImageResponse{},
162+
},
163+
wantErr: false,
164+
},
70165
{
71166
name: "empty response",
72167
registryID: "reg-123",
73168
repositoryName: "repo-test",
74169
response: "",
75170
statusCode: http.StatusOK,
171+
expectedQuery: map[string]string{},
76172
want: nil,
77173
wantErr: true,
78174
},
@@ -82,6 +178,7 @@ func TestImagesService_List(t *testing.T) {
82178
repositoryName: "repo-test",
83179
response: `{"results": [{"digest": "sha256:123"`,
84180
statusCode: http.StatusOK,
181+
expectedQuery: map[string]string{},
85182
want: nil,
86183
wantErr: true,
87184
},
@@ -91,6 +188,7 @@ func TestImagesService_List(t *testing.T) {
91188
repositoryName: "repo-test",
92189
response: `{"error": "internal server error"}`,
93190
statusCode: http.StatusInternalServerError,
191+
expectedQuery: map[string]string{},
94192
want: nil,
95193
wantErr: true,
96194
},
@@ -100,6 +198,7 @@ func TestImagesService_List(t *testing.T) {
100198
repositoryName: "repo-test",
101199
response: `{"error": "unauthorized"}`,
102200
statusCode: http.StatusUnauthorized,
201+
expectedQuery: map[string]string{},
103202
want: nil,
104203
wantErr: true,
105204
},
@@ -109,6 +208,7 @@ func TestImagesService_List(t *testing.T) {
109208
repositoryName: "repo-test",
110209
response: `{"error": "repository not found"}`,
111210
statusCode: http.StatusNotFound,
211+
expectedQuery: map[string]string{},
112212
want: nil,
113213
wantErr: true,
114214
},
@@ -120,6 +220,12 @@ func TestImagesService_List(t *testing.T) {
120220
if r.Method != http.MethodGet {
121221
t.Errorf("expected GET method, got %s", r.Method)
122222
}
223+
query := r.URL.Query()
224+
for key, expectedValue := range tt.expectedQuery {
225+
if actualValue := query.Get(key); actualValue != expectedValue {
226+
t.Errorf("expected query param %s=%s, got %s", key, expectedValue, actualValue)
227+
}
228+
}
123229
w.Header().Set("Content-Type", "application/json")
124230
w.WriteHeader(tt.statusCode)
125231
w.Write([]byte(tt.response))

containerregistry/registries.go

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ import (
44
"context"
55
"fmt"
66
"net/http"
7-
"net/url"
8-
"strconv"
9-
"strings"
107

118
mgc_http "github.com/MagaluCloud/mgc-sdk-go/internal/http"
129
)
@@ -69,21 +66,7 @@ func (c *registriesService) Create(ctx context.Context, request *RegistryRequest
6966
// List retrieves a list of container registries with optional filtering and pagination
7067
func (c *registriesService) List(ctx context.Context, opts ListOptions) (*ListRegistriesResponse, error) {
7168
path := "/v0/registries"
72-
query := make(url.Values)
73-
74-
if opts.Limit != nil {
75-
query.Set("_limit", strconv.Itoa(*opts.Limit))
76-
}
77-
if opts.Offset != nil {
78-
query.Set("_offset", strconv.Itoa(*opts.Offset))
79-
}
80-
if opts.Sort != nil {
81-
query.Set("_sort", *opts.Sort)
82-
}
83-
84-
if len(opts.Expand) > 0 {
85-
query.Set("_expand", strings.Join(opts.Expand, ","))
86-
}
69+
query := CreatePaginationParams(opts)
8770

8871
res, err := mgc_http.ExecuteSimpleRequestWithRespBody[ListRegistriesResponse](ctx, c.client.newRequest, c.client.GetConfig(), http.MethodGet, path, nil, query)
8972
if err != nil {

containerregistry/repositories.go

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ import (
44
"context"
55
"fmt"
66
"net/http"
7-
"net/url"
8-
"strconv"
97

108
mgc_http "github.com/MagaluCloud/mgc-sdk-go/internal/http"
119
)
@@ -47,18 +45,7 @@ type (
4745
// List retrieves a list of repositories within a registry with optional filtering and pagination
4846
func (c *repositoriesService) List(ctx context.Context, registryID string, opts ListOptions) (*RepositoriesResponse, error) {
4947
path := fmt.Sprintf("/v0/registries/%s/repositories", registryID)
50-
51-
query := make(url.Values)
52-
53-
if opts.Limit != nil {
54-
query.Set("_limit", strconv.Itoa(*opts.Limit))
55-
}
56-
if opts.Offset != nil {
57-
query.Set("_offset", strconv.Itoa(*opts.Offset))
58-
}
59-
if opts.Sort != nil {
60-
query.Set("_sort", *opts.Sort)
61-
}
48+
query := CreatePaginationParams(opts)
6249

6350
res, err := mgc_http.ExecuteSimpleRequestWithRespBody[RepositoriesResponse](ctx, c.client.newRequest, c.client.GetConfig(), http.MethodGet, path, nil, query)
6451
if err != nil {

0 commit comments

Comments
 (0)