Skip to content

Commit 212374c

Browse files
authored
feat(rdb): add a boolean to reveal if a backup is exported (#2484)
1 parent 432cfad commit 212374c

5 files changed

Lines changed: 1120 additions & 0 deletions

File tree

internal/namespaces/rdb/v1/custom.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,7 @@ func GetCommands() *core.Commands {
5353

5454
cmds.MustFind("rdb", "user", "list").Override(userListBuilder)
5555

56+
cmds.MustFind("rdb", "backup", "list").Override(backupListBuilder)
57+
5658
return cmds
5759
}

internal/namespaces/rdb/v1/custom_backup.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,72 @@ func backupRestoreBuilder(c *core.Command) *core.Command {
119119
return c
120120
}
121121

122+
func backupListBuilder(c *core.Command) *core.Command {
123+
type customBackup struct {
124+
ID string `json:"ID"`
125+
Name string `json:"name"`
126+
InstanceID string `json:"instance_ID"`
127+
Exported bool `json:"exported"`
128+
Status rdb.DatabaseBackupStatus `json:"status"`
129+
}
130+
131+
c.View = &core.View{
132+
Fields: []*core.ViewField{
133+
{
134+
Label: "ID",
135+
FieldName: "ID",
136+
},
137+
{
138+
Label: "Name",
139+
FieldName: "Name",
140+
},
141+
{
142+
Label: "Status",
143+
FieldName: "Status",
144+
},
145+
{
146+
Label: "Instance ID",
147+
FieldName: "InstanceID",
148+
},
149+
{
150+
Label: "Exported",
151+
FieldName: "Exported",
152+
},
153+
},
154+
}
155+
156+
c.AddInterceptors(func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (i interface{}, err error) {
157+
listBackupResp, err := runner(ctx, argsI)
158+
if err != nil {
159+
return listBackupResp, err
160+
}
161+
backupList := listBackupResp.([]*rdb.DatabaseBackup)
162+
var res []customBackup
163+
for _, backup := range backupList {
164+
res = append(res, customBackup{
165+
ID: backup.ID,
166+
Name: backup.Name,
167+
Status: backup.Status,
168+
InstanceID: backup.InstanceID,
169+
Exported: isExported(backup.DownloadURLExpiresAt),
170+
})
171+
}
172+
return res, nil
173+
})
174+
175+
return c
176+
}
177+
178+
func isExported(expirationDate *time.Time) bool {
179+
var exported bool
180+
if expirationDate == nil {
181+
exported = false
182+
} else {
183+
exported = time.Now().Before(*expirationDate)
184+
}
185+
return exported
186+
}
187+
122188
func getDefaultFileName(rawURL string) (string, error) {
123189
u, err := url.Parse(rawURL)
124190
if err != nil {

internal/namespaces/rdb/v1/custom_backup_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,30 @@ func Test_DownloadBackup(t *testing.T) {
111111
TmpHomeDir: true,
112112
}))
113113
}
114+
115+
func Test_ListBackup(t *testing.T) {
116+
t.Run("Simple", core.Test(&core.TestConfig{
117+
Commands: GetCommands(),
118+
BeforeFunc: core.BeforeFuncCombine(
119+
createInstance(engine),
120+
core.ExecStoreBeforeCmd(
121+
"BackupA",
122+
"scw rdb backup create name=will_be_exported expires-at=2999-01-02T15:04:05-07:00 instance-id={{ .Instance.ID }} database-name=rdb --wait",
123+
),
124+
core.ExecStoreBeforeCmd(
125+
"BackupB",
126+
"scw rdb backup create name=will_not_be_exported expires-at=2999-01-02T15:04:05-07:00 instance-id={{ .Instance.ID }} database-name=rdb --wait",
127+
),
128+
core.ExecStoreBeforeCmd(
129+
"BackupExport",
130+
"scw rdb backup export {{ .BackupA.ID }} --wait",
131+
),
132+
),
133+
Cmd: "scw rdb backup list instance-id={{ .Instance.ID }}",
134+
Check: core.TestCheckCombine(
135+
core.TestCheckGolden(),
136+
core.TestCheckExitCode(0),
137+
),
138+
AfterFunc: deleteInstance(),
139+
}))
140+
}

0 commit comments

Comments
 (0)