Skip to content

Commit f4faaee

Browse files
committed
feat(postgresflex): migrate to v3 API
relates to STACKITCLI-415
1 parent be300f0 commit f4faaee

45 files changed

Lines changed: 2268 additions & 1809 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

internal/cmd/postgresflex/backup/backup.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
2626
func addSubcommands(cmd *cobra.Command, params *types.CmdParams) {
2727
cmd.AddCommand(list.NewCmd(params))
2828
cmd.AddCommand(describe.NewCmd(params))
29-
cmd.AddCommand(updateschedule.NewCmd(params))
29+
cmd.AddCommand(updateschedule.NewCmd(params)) //nolint:staticcheck // Command is deprecated but must be kept for backward compatibility
3030
}

internal/cmd/postgresflex/backup/describe/describe.go

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ package describe
33
import (
44
"context"
55
"fmt"
6-
"time"
6+
"strconv"
77

88
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
99

1010
"github.com/spf13/cobra"
11-
postgresflex "github.com/stackitcloud/stackit-sdk-go/services/postgresflex/v2api"
11+
postgresflex "github.com/stackitcloud/stackit-sdk-go/services/postgresflex/v3api"
1212

1313
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
1414
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
@@ -18,24 +18,19 @@ import (
1818
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1919
"github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresflex/client"
2020
"github.com/stackitcloud/stackit-cli/internal/pkg/tables"
21-
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
2221
)
2322

2423
const (
2524
backupIdArg = "BACKUP_ID"
2625

2726
instanceIdFlag = "instance-id"
28-
29-
backupExpireYearOffset = 0
30-
backupExpireMonthOffset = 0
31-
backupExpireDayOffset = 30
3227
)
3328

3429
type inputModel struct {
3530
*globalflags.GlobalFlagModel
3631

3732
InstanceId string
38-
BackupId string
33+
BackupId int64
3934
}
4035

4136
func NewCmd(params *types.CmdParams) *cobra.Command {
@@ -73,7 +68,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
7368
return fmt.Errorf("describe backup for PostgreSQL Flex instance: %w", err)
7469
}
7570

76-
return outputResult(params.Printer, model.OutputFormat, resp.Item)
71+
return outputResult(params.Printer, model.OutputFormat, resp)
7772
},
7873
}
7974
configureFlags(cmd)
@@ -88,13 +83,18 @@ func configureFlags(cmd *cobra.Command) {
8883
}
8984

9085
func parseInput(p *print.Printer, cmd *cobra.Command, inputArgs []string) (*inputModel, error) {
91-
backupId := inputArgs[0]
86+
backupIdStr := inputArgs[0]
9287

9388
globalFlags := globalflags.Parse(p, cmd)
9489
if globalFlags.ProjectId == "" {
9590
return nil, &errors.ProjectIdError{}
9691
}
9792

93+
backupId, err := strconv.ParseInt(backupIdStr, 10, 64)
94+
if err != nil {
95+
return nil, fmt.Errorf("invalid backup id format, must be an integer: %w", err)
96+
}
97+
9898
return &inputModel{
9999
GlobalFlagModel: globalFlags,
100100
InstanceId: flags.FlagToStringValue(p, cmd, instanceIdFlag),
@@ -107,30 +107,20 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *postgresfle
107107
return req
108108
}
109109

110-
func outputResult(p *print.Printer, outputFormat string, backup *postgresflex.Backup) error {
111-
if backup == nil {
112-
return fmt.Errorf("backup is nil")
113-
}
114-
if backup.StartTime == nil || *backup.StartTime == "" {
115-
return fmt.Errorf("start time not defined")
116-
}
117-
backupStartTime, err := time.Parse(time.RFC3339, utils.PtrString(backup.StartTime))
118-
if err != nil {
119-
return fmt.Errorf("parse backup start time: %w", err)
120-
}
121-
backupExpireDate := backupStartTime.AddDate(backupExpireYearOffset, backupExpireMonthOffset, backupExpireDayOffset).Format(time.DateOnly)
122-
110+
func outputResult(p *print.Printer, outputFormat string, backup *postgresflex.BackupData) error {
123111
return p.OutputResult(outputFormat, backup, func() error {
112+
if backup == nil {
113+
return fmt.Errorf("backup is nil")
114+
}
115+
124116
table := tables.NewTable()
125-
table.AddRow("ID", utils.PtrString(backup.Id))
117+
table.AddRow("ID", backup.Id)
126118
table.AddSeparator()
127-
table.AddRow("CREATED AT", utils.PtrString(backup.StartTime))
119+
table.AddRow("COMPLETED AT", backup.CompletionTime)
128120
table.AddSeparator()
129-
table.AddRow("EXPIRES AT", backupExpireDate)
121+
table.AddRow("RETAINED UNTIL", backup.RetainedUntil)
130122
table.AddSeparator()
131-
132-
backupSize := utils.PtrByteSizeDefault(backup.Size, "n/a")
133-
table.AddRow("BACKUP SIZE", backupSize)
123+
table.AddRow("BACKUP SIZE", backup.Size)
134124

135125
err := table.Display(p)
136126
if err != nil {

internal/cmd/postgresflex/backup/describe/describe_test.go

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,19 @@ package describe
22

33
import (
44
"context"
5+
"strconv"
56
"testing"
67
"time"
78

89
"github.com/google/go-cmp/cmp"
910
"github.com/google/go-cmp/cmp/cmpopts"
1011
"github.com/google/uuid"
11-
postgresflex "github.com/stackitcloud/stackit-sdk-go/services/postgresflex/v2api"
12+
postgresflex "github.com/stackitcloud/stackit-sdk-go/services/postgresflex/v3api"
13+
14+
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
1215

1316
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
1417
"github.com/stackitcloud/stackit-cli/internal/pkg/testparams"
15-
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
1618
)
1719

1820
type testCtxKey struct{}
@@ -21,12 +23,15 @@ var testCtx = context.WithValue(context.Background(), testCtxKey{}, "foo")
2123
var testClient = &postgresflex.APIClient{DefaultAPI: &postgresflex.DefaultAPIService{}}
2224
var testProjectId = uuid.NewString()
2325
var testInstanceId = uuid.NewString()
24-
var testBackupId = "backupID"
25-
var testRegion = "eu01"
26+
27+
const (
28+
testBackupId = int64(42)
29+
testRegion = "eu01"
30+
)
2631

2732
func fixtureArgValues(mods ...func(argValues []string)) []string {
2833
argValues := []string{
29-
testBackupId,
34+
strconv.FormatInt(testBackupId, 10),
3035
}
3136
for _, mod := range mods {
3237
mod(argValues)
@@ -243,43 +248,40 @@ func TestBuildRequest(t *testing.T) {
243248
func Test_outputResult(t *testing.T) {
244249
type args struct {
245250
outputFormat string
246-
backup *postgresflex.Backup
251+
backup *postgresflex.BackupData
247252
}
248253
tests := []struct {
249254
name string
250255
args args
251256
wantErr bool
252257
}{
253258
{
254-
name: "empty",
255-
args: args{},
259+
name: "backup is nil",
260+
args: args{
261+
backup: nil,
262+
outputFormat: print.PrettyOutputFormat,
263+
},
256264
wantErr: true,
257265
},
258266
{
259-
name: "standard",
267+
name: "empty backup",
260268
args: args{
261-
outputFormat: "",
262-
backup: &postgresflex.Backup{
263-
StartTime: utils.Ptr(time.Now().Format(time.RFC3339)),
264-
},
269+
outputFormat: print.PrettyOutputFormat,
270+
backup: &postgresflex.BackupData{},
265271
},
266272
wantErr: false,
267273
},
268274
{
269275
name: "complete",
270276
args: args{
271-
outputFormat: "",
272-
backup: &postgresflex.Backup{
273-
EndTime: utils.Ptr(time.Now().Format(time.RFC3339)),
274-
Id: utils.Ptr("id"),
275-
Labels: []string{"foo",
276-
"bar",
277-
"baz",
278-
},
279-
Name: utils.Ptr("name"),
280-
Options: &map[string]string{"test1": "test1", "test2": "test2"},
281-
Size: utils.Ptr(int64(42)),
282-
StartTime: utils.Ptr(time.Now().Format(time.RFC3339)),
277+
outputFormat: print.PrettyOutputFormat,
278+
backup: &postgresflex.BackupData{
279+
CompletionTime: time.Now().Format(time.RFC3339),
280+
Id: int64(1),
281+
Name: "name",
282+
RetainedUntil: time.Now().Format(time.RFC3339),
283+
Size: int64(42),
284+
Type: "type",
283285
},
284286
},
285287
wantErr: false,

internal/cmd/postgresflex/backup/list/list.go

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66

77
"github.com/stackitcloud/stackit-cli/internal/pkg/types"
88

9+
"time"
10+
911
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
1012
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
1113
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
@@ -15,20 +17,14 @@ import (
1517
"github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresflex/client"
1618
postgresflexUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/postgresflex/utils"
1719
"github.com/stackitcloud/stackit-cli/internal/pkg/tables"
18-
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
19-
20-
"time"
2120

2221
"github.com/spf13/cobra"
23-
postgresflex "github.com/stackitcloud/stackit-sdk-go/services/postgresflex/v2api"
22+
postgresflex "github.com/stackitcloud/stackit-sdk-go/services/postgresflex/v3api"
2423
)
2524

2625
const (
27-
instanceIdFlag = "instance-id"
28-
limitFlag = "limit"
29-
backupExpireYearOffset = 0
30-
backupExpireMonthOffset = 0
31-
backupExpireDayOffset = 30
26+
instanceIdFlag = "instance-id"
27+
limitFlag = "limit"
3228
)
3329

3430
type inputModel struct {
@@ -80,7 +76,7 @@ func NewCmd(params *types.CmdParams) *cobra.Command {
8076
if err != nil {
8177
return fmt.Errorf("get backups for PostgreSQL Flex instance %q: %w", instanceLabel, err)
8278
}
83-
backups := resp.Items
79+
backups := resp.Backups
8480

8581
// Truncate output
8682
if model.Limit != nil && len(backups) > int(*model.Limit) {
@@ -129,29 +125,32 @@ func buildRequest(ctx context.Context, model *inputModel, apiClient *postgresfle
129125
return req
130126
}
131127

132-
func outputResult(p *print.Printer, outputFormat, instanceLabel string, backups []postgresflex.Backup) error {
128+
func outputResult(p *print.Printer, outputFormat, instanceLabel string, backups []postgresflex.BackupData) error {
133129
return p.OutputResult(outputFormat, backups, func() error {
134130
if len(backups) == 0 {
135131
p.Outputf("No backups found for instance %q", instanceLabel)
136132
return nil
137133
}
134+
138135
table := tables.NewTable()
139-
table.SetHeader("ID", "CREATED AT", "EXPIRES AT", "BACKUP SIZE")
140-
for i := range backups {
141-
backup := backups[i]
136+
table.SetHeader("ID", "CREATED AT", "RETAINED UNTIL", "BACKUP SIZE")
137+
138+
for _, backup := range backups {
139+
backupCompletionTime, err := time.Parse(time.RFC3339, backup.CompletionTime)
140+
if err != nil {
141+
return fmt.Errorf("parse backup completion time: %w", err)
142+
}
142143

143-
backupStartTime, err := time.Parse(time.RFC3339, utils.PtrString(backup.StartTime))
144+
backupRetainedUntilTime, err := time.Parse(time.RFC3339, backup.RetainedUntil)
144145
if err != nil {
145-
return fmt.Errorf("parse backup start time: %w", err)
146+
return fmt.Errorf("parse backup retained until time: %w", err)
146147
}
147-
backupExpireDate := backupStartTime.AddDate(backupExpireYearOffset, backupExpireMonthOffset, backupExpireDayOffset).Format(time.DateOnly)
148148

149-
backupSize := utils.PtrByteSizeDefault(backup.Size, "n/a")
150149
table.AddRow(
151-
utils.PtrString(backup.Id),
152-
utils.PtrString(backup.StartTime),
153-
backupExpireDate,
154-
backupSize,
150+
backup.Id,
151+
backupCompletionTime,
152+
backupRetainedUntilTime,
153+
backup.Size,
155154
)
156155
}
157156
err := table.Display(p)

internal/cmd/postgresflex/backup/list/list_test.go

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"github.com/google/go-cmp/cmp"
1313
"github.com/google/go-cmp/cmp/cmpopts"
1414
"github.com/google/uuid"
15-
postgresflex "github.com/stackitcloud/stackit-sdk-go/services/postgresflex/v2api"
15+
postgresflex "github.com/stackitcloud/stackit-sdk-go/services/postgresflex/v3api"
1616

1717
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
1818
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
@@ -179,36 +179,53 @@ func Test_outputResult(t *testing.T) {
179179
type args struct {
180180
outputFormat string
181181
instanceLabel string
182-
backups []postgresflex.Backup
182+
backups []postgresflex.BackupData
183183
}
184184
tests := []struct {
185185
name string
186186
args args
187187
wantErr bool
188188
}{
189-
{"empty", args{}, false},
190-
{"standard", args{outputFormat: "", instanceLabel: "label", backups: []postgresflex.Backup{}}, false},
191-
{"complete", args{outputFormat: "", instanceLabel: "label", backups: []postgresflex.Backup{
192-
{
193-
EndTime: utils.Ptr(time.Now().Format(time.RFC3339)),
194-
Id: utils.Ptr("id"),
195-
Labels: []string{"foo", "bar", "baz"},
196-
Name: utils.Ptr("name"),
197-
Options: &map[string]string{"test1": "test1", "test2": "test2"},
198-
Size: utils.Ptr(int64(42)),
199-
StartTime: utils.Ptr(time.Now().Format(time.RFC3339)),
189+
{
190+
name: "empty",
191+
args: args{},
192+
wantErr: false,
193+
},
194+
{
195+
name: "standard",
196+
args: args{
197+
outputFormat: "",
198+
instanceLabel: "label",
199+
backups: []postgresflex.BackupData{},
200200
},
201-
{
202-
EndTime: utils.Ptr(time.Now().Format(time.RFC3339)),
203-
Id: utils.Ptr("id"),
204-
Labels: []string{"foo", "bar", "baz"},
205-
Name: utils.Ptr("name"),
206-
Options: &map[string]string{"test1": "test1", "test2": "test2"},
207-
Size: utils.Ptr(int64(42)),
208-
StartTime: utils.Ptr(time.Now().Format(time.RFC3339)),
201+
wantErr: false,
202+
},
203+
{
204+
name: "complete",
205+
args: args{
206+
outputFormat: "",
207+
instanceLabel: "label",
208+
backups: []postgresflex.BackupData{
209+
{
210+
CompletionTime: time.Now().Format(time.RFC3339),
211+
Id: int64(1),
212+
Name: "name",
213+
RetainedUntil: time.Now().Format(time.RFC3339),
214+
Size: int64(42),
215+
Type: "type",
216+
},
217+
{
218+
CompletionTime: time.Now().Format(time.RFC3339),
219+
Id: int64(2),
220+
Name: "name",
221+
RetainedUntil: time.Now().Format(time.RFC3339),
222+
Size: int64(42),
223+
Type: "type",
224+
},
225+
},
209226
},
227+
wantErr: false,
210228
},
211-
}, false},
212229
}
213230
params := testparams.NewTestParams()
214231
for _, tt := range tests {

0 commit comments

Comments
 (0)