Skip to content

Commit 40accec

Browse files
Alexandre CatalanAlexandre Catalan
authored andcommitted
fix(container-registry): address PR review comments for pagination, validation, and naming
Addresses feedback from PR #1693 reviews: - Refactor artifactory list command to use SDK paginate.All eager pagination with request/response adapters matching terraform-provider-stackit PR #1870. - Keep a single request in replication-adapter list: the endpoint does not accept page_token/page_size query parameters, so the SDK request only exposes Execute(). Document the limitation and add fetch tests. - Switch replication-adapter table rendering to table.Display(p) for consistency. - Add nil check for response in artifactory create command before accessing Id. - Enforce proxy credentials when configuring a proxy via MarkFlagsRequiredTogether and validate proxy-description usage to prevent HTTP 400 Bad Request responses. - Update artifactory delete warning message to reference "artifactories" rather than "projects" to avoid confusion with STACKIT projects. Relates to STACKITCRE-382
1 parent 05b971b commit 40accec

8 files changed

Lines changed: 261 additions & 71 deletions

File tree

‎internal/cmd/container-registry/artifactory/create/create.go‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
9595
if err != nil {
9696
return fmt.Errorf("create artifactory: %w", err)
9797
}
98+
if resp == nil {
99+
return fmt.Errorf("create artifactory: nil response")
100+
}
98101
artifactoryId := resp.Id
99102

100103
// Wait for async operation, if async mode not enabled
@@ -132,8 +135,7 @@ func configureFlags(cmd *cobra.Command, params *types.CmdParams) {
132135
err := flags.MarkFlagsRequired(cmd, nameFlag)
133136
cobra.CheckErr(err)
134137

135-
cmd.MarkFlagsRequiredTogether(proxyUrlFlag, proxyRegistryTypeFlag)
136-
cmd.MarkFlagsRequiredTogether(proxyAccessKeyFlag, proxyAccessSecretFlag)
138+
cmd.MarkFlagsRequiredTogether(proxyUrlFlag, proxyRegistryTypeFlag, proxyAccessKeyFlag, proxyAccessSecretFlag)
137139
}
138140

139141
func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel, error) {
@@ -151,9 +153,9 @@ func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel,
151153
return nil, err
152154
}
153155

154-
hasProxyDetails := proxyDesc != nil || proxyKey != nil || proxySecret != nil
155-
if hasProxyDetails && (proxyUrl == nil || proxyRegistryType == nil) {
156-
return nil, fmt.Errorf("both --%s and --%s are required when configuring a proxy", proxyUrlFlag, proxyRegistryTypeFlag)
156+
hasProxy := proxyUrl != nil || proxyRegistryType != nil || proxyKey != nil || proxySecret != nil
157+
if proxyDesc != nil && !hasProxy {
158+
return nil, fmt.Errorf("--%s can only be set when configuring a proxy", proxyDescriptionFlag)
157159
}
158160

159161
model := inputModel{

‎internal/cmd/container-registry/artifactory/create/create_test.go‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,25 @@ func TestParseInput(t *testing.T) {
9696
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
9797
flagValues[proxyUrlFlag] = testProxyUrl
9898
flagValues[proxyRegistryTypeFlag] = testProxyRegistryType
99+
flagValues[proxyAccessKeyFlag] = testProxyAccessKey
100+
flagValues[proxyAccessSecretFlag] = "@" + testSecretFile
99101
}),
100102
isValid: true,
101103
expectedModel: fixtureInputModel(func(model *inputModel) {
102104
model.ProxyUrl = utils.Ptr(testProxyUrl)
103105
model.ProxyRegistryType = utils.Ptr(testProxyRegistryType)
106+
model.ProxyAccessKey = utils.Ptr(testProxyAccessKey)
107+
model.ProxyAccessSecret = utils.Ptr(testProxyAccessSecret)
104108
}),
105109
},
110+
{
111+
description: "proxy url without credentials",
112+
flagValues: fixtureFlagValues(func(flagValues map[string]string) {
113+
flagValues[proxyUrlFlag] = testProxyUrl
114+
flagValues[proxyRegistryTypeFlag] = testProxyRegistryType
115+
}),
116+
isValid: false,
117+
},
106118
{
107119
description: "with proxy full",
108120
flagValues: fixtureFlagValues(func(flagValues map[string]string) {

‎internal/cmd/container-registry/artifactory/delete/delete.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222

2323
const (
2424
artifactoryIdArg = "ARTIFACTORY_ID"
25-
warningMessage = "Your STACKIT Container Registry projects are recoverable for up to 30 days after deletion via a support request. After this period, projects and their data are permanently unrecoverable."
25+
warningMessage = "Your STACKIT Container Registry artifactories are recoverable for up to 30 days after deletion via a support request. After this period, artifactories and their data are permanently unrecoverable."
2626
)
2727

2828
type inputModel struct {

‎internal/cmd/container-registry/artifactory/delete/delete_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ func TestPrintWarning(t *testing.T) {
214214
}
215215

216216
func TestWarningMessageContent(t *testing.T) {
217-
expected := "Your STACKIT Container Registry projects are recoverable for up to 30 days after deletion via a support request. After this period, projects and their data are permanently unrecoverable."
217+
expected := "Your STACKIT Container Registry artifactories are recoverable for up to 30 days after deletion via a support request. After this period, artifactories and their data are permanently unrecoverable."
218218
if warningMessage != expected {
219219
t.Errorf("warning message mismatch:\ngot: %q\nwant: %q", warningMessage, expected)
220220
}

‎internal/cmd/container-registry/artifactory/list/list.go‎

Lines changed: 62 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ package list
33
import (
44
"context"
55
"fmt"
6-
"math"
76

87
"github.com/spf13/cobra"
8+
"github.com/stackitcloud/stackit-sdk-go/core/experimental/paginate"
99
registry "github.com/stackitcloud/stackit-sdk-go/services/registry/v1api"
1010

1111
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
@@ -107,51 +107,76 @@ func parseInput(p *print.Printer, cmd *cobra.Command, _ []string) (*inputModel,
107107
return &model, nil
108108
}
109109

110-
func buildRequest(ctx context.Context, model *inputModel, apiClient *registry.APIClient, pageToken string, pageSize int32) registry.ApiListArtifactoriesRequest {
111-
req := apiClient.DefaultAPI.ListArtifactories(ctx, model.ProjectId, model.Region)
112-
if pageToken != "" {
113-
req = req.NextPageToken(pageToken)
110+
func buildRequest(ctx context.Context, model *inputModel, apiClient *registry.APIClient) registry.ApiListArtifactoriesRequest {
111+
return apiClient.DefaultAPI.ListArtifactories(ctx, model.ProjectId, model.Region)
112+
}
113+
114+
type artifactoryPageRequest struct {
115+
req registry.ApiListArtifactoriesRequest
116+
}
117+
118+
type artifactoryPageResponse struct {
119+
resp *registry.ArtifactoryList
120+
}
121+
122+
func (r *artifactoryPageRequest) PageSize(pageSize int32) *artifactoryPageRequest {
123+
r.req = r.req.PageSize(pageSize)
124+
return r
125+
}
126+
127+
func (r *artifactoryPageRequest) PageToken(pageToken string) *artifactoryPageRequest {
128+
r.req = r.req.NextPageToken(pageToken)
129+
return r
130+
}
131+
132+
func (r *artifactoryPageRequest) Execute() (*artifactoryPageResponse, error) {
133+
resp, err := r.req.Execute()
134+
if err != nil {
135+
return nil, err
114136
}
115-
if pageSize > 0 {
116-
req = req.PageSize(pageSize)
137+
if resp == nil {
138+
return nil, fmt.Errorf("empty response")
139+
}
140+
141+
return &artifactoryPageResponse{resp: resp}, nil
142+
}
143+
144+
func (r *artifactoryPageResponse) GetItems() []registry.Artifactory {
145+
if r.resp == nil {
146+
return nil
117147
}
118-
return req
148+
149+
return r.resp.GetArtifactories()
150+
}
151+
152+
func (r *artifactoryPageResponse) GetNextPageToken() string {
153+
if r.resp == nil {
154+
return ""
155+
}
156+
157+
return r.resp.GetNextPageToken()
119158
}
120159

121160
func fetchArtifactories(ctx context.Context, model *inputModel, apiClient *registry.APIClient) ([]registry.Artifactory, error) {
122-
var pageToken string
123-
artifactories := make([]registry.Artifactory, 0)
124-
received := int64(0)
125-
limit := int64(math.MaxInt64)
161+
req := buildRequest(ctx, model, apiClient)
162+
163+
opts := []paginate.Option{
164+
paginate.WithPageSize(maxPageSize),
165+
}
126166
if model.Limit != nil {
127-
limit = *model.Limit
167+
opts = append(opts, paginate.WithLimit(int(*model.Limit)))
128168
}
129-
for {
130-
want := min(int64(maxPageSize), limit-received)
131-
request := buildRequest(ctx, model, apiClient, pageToken, int32(want))
132-
response, err := request.Execute()
133-
if err != nil {
134-
return nil, fmt.Errorf("get artifactories: %w", err)
135-
}
136-
if response == nil {
137-
return nil, fmt.Errorf("get artifactories: empty response")
138-
}
139-
if response.Artifactories != nil {
140-
artifactories = append(artifactories, response.Artifactories...)
141-
}
142-
pageToken = ""
143-
if response.NextPageToken != nil {
144-
pageToken = *response.NextPageToken
145-
}
146-
received += want
147-
if pageToken == "" || received >= limit {
148-
break
149-
}
169+
170+
items, err := paginate.All(&artifactoryPageRequest{req: req}, opts...)
171+
if err != nil {
172+
return nil, fmt.Errorf("get artifactories: %w", err)
150173
}
151-
if model.Limit != nil && len(artifactories) > int(*model.Limit) {
152-
artifactories = artifactories[:*model.Limit]
174+
175+
if items == nil {
176+
items = []registry.Artifactory{}
153177
}
154-
return artifactories, nil
178+
179+
return items, nil
155180
}
156181

157182
func outputResult(p *print.Printer, outputFormat, projectLabel string, artifactories []registry.Artifactory) error {

‎internal/cmd/container-registry/artifactory/list/list_test.go‎

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func fixtureInputModel(mods ...func(model *inputModel)) *inputModel {
5555
}
5656

5757
func fixtureRequest(mods ...func(request *registry.ApiListArtifactoriesRequest)) registry.ApiListArtifactoriesRequest {
58-
request := testClient.DefaultAPI.ListArtifactories(testCtx, testProjectId, testRegion).PageSize(maxPageSize)
58+
request := testClient.DefaultAPI.ListArtifactories(testCtx, testProjectId, testRegion)
5959
for _, mod := range mods {
6060
mod(&request)
6161
}
@@ -138,30 +138,18 @@ func TestBuildRequest(t *testing.T) {
138138
tests := []struct {
139139
description string
140140
model *inputModel
141-
pageToken string
142-
pageSize int32
143141
expectedRequest registry.ApiListArtifactoriesRequest
144142
}{
145143
{
146144
description: "base",
147145
model: fixtureInputModel(),
148-
pageSize: maxPageSize,
149146
expectedRequest: fixtureRequest(),
150147
},
151-
{
152-
description: "with page token",
153-
model: fixtureInputModel(),
154-
pageToken: "next-token",
155-
pageSize: maxPageSize,
156-
expectedRequest: fixtureRequest(func(request *registry.ApiListArtifactoriesRequest) {
157-
*request = request.NextPageToken("next-token")
158-
}),
159-
},
160148
}
161149

162150
for _, tt := range tests {
163151
t.Run(tt.description, func(t *testing.T) {
164-
request := buildRequest(testCtx, tt.model, testClient, tt.pageToken, tt.pageSize)
152+
request := buildRequest(testCtx, tt.model, testClient)
165153

166154
diff := cmp.Diff(request, tt.expectedRequest,
167155
cmp.AllowUnexported(tt.expectedRequest, registry.DefaultAPIService{}),
@@ -174,6 +162,50 @@ func TestBuildRequest(t *testing.T) {
174162
}
175163
}
176164

165+
func TestArtifactoryPageRequest(t *testing.T) {
166+
req := buildRequest(testCtx, fixtureInputModel(), testClient)
167+
pageReq := &artifactoryPageRequest{req: req}
168+
169+
pageReq = pageReq.PageSize(10)
170+
pageReq = pageReq.PageToken("test-token")
171+
172+
if pageReq == nil {
173+
t.Fatal("expected non-nil pageReq")
174+
}
175+
}
176+
177+
func TestArtifactoryPageResponse(t *testing.T) {
178+
t.Run("nil response", func(t *testing.T) {
179+
resp := &artifactoryPageResponse{resp: nil}
180+
if items := resp.GetItems(); items != nil {
181+
t.Errorf("expected nil items, got %v", items)
182+
}
183+
if token := resp.GetNextPageToken(); token != "" {
184+
t.Errorf("expected empty token, got %q", token)
185+
}
186+
})
187+
188+
t.Run("valid response", func(t *testing.T) {
189+
token := "next-page"
190+
expectedItems := []registry.Artifactory{
191+
{Id: "art-1", Name: "name-1"},
192+
}
193+
resp := &artifactoryPageResponse{
194+
resp: &registry.ArtifactoryList{
195+
Artifactories: expectedItems,
196+
NextPageToken: &token,
197+
},
198+
}
199+
200+
if diff := cmp.Diff(resp.GetItems(), expectedItems); diff != "" {
201+
t.Errorf("items mismatch (-got +want):\n%s", diff)
202+
}
203+
if gotToken := resp.GetNextPageToken(); gotToken != token {
204+
t.Errorf("token mismatch: got %q, want %q", gotToken, token)
205+
}
206+
})
207+
}
208+
177209
func TestOutputResult(t *testing.T) {
178210
type args struct {
179211
projectLabel string

‎internal/cmd/container-registry/replication-adapter/list/list.go‎

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,9 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
6262
}
6363

6464
// Call API
65-
req := buildRequest(ctx, model, apiClient)
66-
resp, err := req.Execute()
65+
adapters, err := fetchReplicationAdapters(ctx, model, apiClient)
6766
if err != nil {
68-
return fmt.Errorf("list replication adapters: %w", err)
69-
}
70-
71-
var adapters []registry.ReplicationAdapter
72-
if resp != nil {
73-
adapters = resp.ReplicationAdapters
74-
}
75-
76-
if model.Limit != nil && len(adapters) > int(*model.Limit) {
77-
adapters = adapters[:*model.Limit]
67+
return err
7868
}
7969

8070
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
@@ -122,6 +112,30 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *registry.AP
122112
return apiClient.DefaultAPI.ListReplicationAdapters(ctx, model.ProjectId, model.Region)
123113
}
124114

115+
func fetchReplicationAdapters(ctx context.Context, model *inputModel, apiClient *registry.APIClient) ([]registry.ReplicationAdapter, error) {
116+
// Note: the ListReplicationAdapters endpoint does not accept page_token/page_size
117+
// query parameters (the SDK only exposes Execute()), so the next page token of the
118+
// response cannot be forwarded. The list of adapter types is small and always
119+
// fits in a single page.
120+
resp, err := buildRequest(ctx, model, apiClient).Execute()
121+
if err != nil {
122+
return nil, fmt.Errorf("list replication adapters: %w", err)
123+
}
124+
if resp == nil {
125+
return nil, fmt.Errorf("list replication adapters: empty response")
126+
}
127+
128+
adapters := resp.GetReplicationAdapters()
129+
if adapters == nil {
130+
adapters = []registry.ReplicationAdapter{}
131+
}
132+
if model.Limit != nil && len(adapters) > int(*model.Limit) {
133+
adapters = adapters[:*model.Limit]
134+
}
135+
136+
return adapters, nil
137+
}
138+
125139
func outputResult(p *print.Printer, outputFormat, projectLabel string, adapters []registry.ReplicationAdapter) error {
126140
return p.OutputResult(outputFormat, adapters, func() error {
127141
if len(adapters) == 0 {
@@ -140,7 +154,11 @@ func outputResult(p *print.Printer, outputFormat, projectLabel string, adapters
140154
)
141155
}
142156

143-
p.Outputln(table.Render())
157+
err := table.Display(p)
158+
if err != nil {
159+
return fmt.Errorf("render table: %w", err)
160+
}
161+
144162
return nil
145163
})
146164
}

0 commit comments

Comments
 (0)