Skip to content

Commit

Permalink
feat(rdb): add a boolean to reveal if a backup is exported (#2484)
Browse files Browse the repository at this point in the history
  • Loading branch information
yfodil authored Sep 2, 2022
1 parent 432cfad commit 212374c
Show file tree
Hide file tree
Showing 5 changed files with 1,120 additions and 0 deletions.
2 changes: 2 additions & 0 deletions internal/namespaces/rdb/v1/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,7 @@ func GetCommands() *core.Commands {

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

cmds.MustFind("rdb", "backup", "list").Override(backupListBuilder)

return cmds
}
66 changes: 66 additions & 0 deletions internal/namespaces/rdb/v1/custom_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,72 @@ func backupRestoreBuilder(c *core.Command) *core.Command {
return c
}

func backupListBuilder(c *core.Command) *core.Command {
type customBackup struct {
ID string `json:"ID"`
Name string `json:"name"`
InstanceID string `json:"instance_ID"`
Exported bool `json:"exported"`
Status rdb.DatabaseBackupStatus `json:"status"`
}

c.View = &core.View{
Fields: []*core.ViewField{
{
Label: "ID",
FieldName: "ID",
},
{
Label: "Name",
FieldName: "Name",
},
{
Label: "Status",
FieldName: "Status",
},
{
Label: "Instance ID",
FieldName: "InstanceID",
},
{
Label: "Exported",
FieldName: "Exported",
},
},
}

c.AddInterceptors(func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (i interface{}, err error) {
listBackupResp, err := runner(ctx, argsI)
if err != nil {
return listBackupResp, err
}
backupList := listBackupResp.([]*rdb.DatabaseBackup)
var res []customBackup
for _, backup := range backupList {
res = append(res, customBackup{
ID: backup.ID,
Name: backup.Name,
Status: backup.Status,
InstanceID: backup.InstanceID,
Exported: isExported(backup.DownloadURLExpiresAt),
})
}
return res, nil
})

return c
}

func isExported(expirationDate *time.Time) bool {
var exported bool
if expirationDate == nil {
exported = false
} else {
exported = time.Now().Before(*expirationDate)
}
return exported
}

func getDefaultFileName(rawURL string) (string, error) {
u, err := url.Parse(rawURL)
if err != nil {
Expand Down
27 changes: 27 additions & 0 deletions internal/namespaces/rdb/v1/custom_backup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,30 @@ func Test_DownloadBackup(t *testing.T) {
TmpHomeDir: true,
}))
}

func Test_ListBackup(t *testing.T) {
t.Run("Simple", core.Test(&core.TestConfig{
Commands: GetCommands(),
BeforeFunc: core.BeforeFuncCombine(
createInstance(engine),
core.ExecStoreBeforeCmd(
"BackupA",
"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",
),
core.ExecStoreBeforeCmd(
"BackupB",
"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",
),
core.ExecStoreBeforeCmd(
"BackupExport",
"scw rdb backup export {{ .BackupA.ID }} --wait",
),
),
Cmd: "scw rdb backup list instance-id={{ .Instance.ID }}",
Check: core.TestCheckCombine(
core.TestCheckGolden(),
core.TestCheckExitCode(0),
),
AfterFunc: deleteInstance(),
}))
}
Loading

0 comments on commit 212374c

Please sign in to comment.