Skip to content

Commit 4f3d099

Browse files
committed
refactoring
1 parent 9c806bd commit 4f3d099

File tree

2 files changed

+12
-21
lines changed

2 files changed

+12
-21
lines changed

internal/cmd/volume/snapshot/list/list.go

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -82,22 +82,7 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
8282
return nil
8383
}
8484

85-
// Filter results by label selector
8685
snapshots := *resp.Items
87-
if model.LabelSelector != nil {
88-
filtered := []iaas.Snapshot{}
89-
for _, s := range snapshots {
90-
if s.Labels != nil {
91-
for k, v := range *s.Labels {
92-
if fmt.Sprintf("%s=%s", k, v) == *model.LabelSelector {
93-
filtered = append(filtered, s)
94-
break
95-
}
96-
}
97-
}
98-
}
99-
snapshots = filtered
100-
}
10186

10287
// Apply limit if specified
10388
if model.Limit != nil && int(*model.Limit) < len(snapshots) {
@@ -125,7 +110,10 @@ func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
125110

126111
limit := flags.FlagToInt64Pointer(p, cmd, limitFlag)
127112
if limit != nil && *limit < 1 {
128-
return nil, fmt.Errorf("limit must be greater than 0")
113+
return nil, &errors.FlagValidationError{
114+
Flag: limitFlag,
115+
Details: "must be greater than 0",
116+
}
129117
}
130118

131119
labelSelector := flags.FlagToStringPointer(p, cmd, labelSelectorFlag)
@@ -149,7 +137,11 @@ func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
149137
}
150138

151139
func buildRequest(ctx context.Context, model *inputModel, apiClient *iaas.APIClient) iaas.ApiListSnapshotsRequest {
152-
return apiClient.ListSnapshots(ctx, model.ProjectId)
140+
req := apiClient.ListSnapshots(ctx, model.ProjectId)
141+
if model.LabelSelector != nil {
142+
req.LabelSelector(*model.LabelSelector)
143+
}
144+
return req
153145
}
154146

155147
func outputResult(p *print.Printer, outputFormat string, snapshots []iaas.Snapshot) error {

internal/cmd/volume/snapshot/update/update.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
1414
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1515
"github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/client"
16+
iaasUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/iaas/utils"
1617
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1718

1819
"github.com/stackitcloud/stackit-sdk-go/services/iaas"
@@ -59,12 +60,10 @@ func NewCmd(params *params.CmdParams) *cobra.Command {
5960
}
6061

6162
// Get snapshot name for label
62-
snapshotLabel := model.SnapshotId
63-
snapshot, err := apiClient.GetSnapshot(ctx, model.ProjectId, model.SnapshotId).Execute()
63+
snapshotLabel, err := iaasUtils.GetSnapshotName(ctx, apiClient, model.ProjectId, model.SnapshotId)
6464
if err != nil {
6565
params.Printer.Debug(print.ErrorLevel, "get snapshot name: %v", err)
66-
} else if snapshot != nil && snapshot.Name != nil {
67-
snapshotLabel = *snapshot.Name
66+
snapshotLabel = model.SnapshotId
6867
}
6968

7069
if !model.AssumeYes {

0 commit comments

Comments
 (0)