Skip to content

Commit

Permalink
fix(rdb): add missing fields on backup builder (#2586)
Browse files Browse the repository at this point in the history
Co-authored-by: Rémy Léone <rleone@scaleway.com>
  • Loading branch information
Monitob and remyleone authored Nov 18, 2022
1 parent d19e6ad commit 1953a69
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 29 deletions.
2 changes: 1 addition & 1 deletion internal/namespaces/rdb/v1/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func GetCommands() *core.Commands {

human.RegisterMarshalerFunc(rdb.Instance{}, instanceMarshalerFunc)
human.RegisterMarshalerFunc(rdb.BackupSchedule{}, backupScheduleMarshalerFunc)
human.RegisterMarshalerFunc(backupDownloadResult{}, backupResultMarshalerFunc)
human.RegisterMarshalerFunc(backupDownloadResult{}, backupResultMarshallerFunc)

human.RegisterMarshalerFunc(rdb.InstanceStatus(""), human.EnumMarshalFunc(instanceStatusMarshalSpecs))
human.RegisterMarshalerFunc(rdb.DatabaseBackupStatus(""), human.EnumMarshalFunc(backupStatusMarshalSpecs))
Expand Down
90 changes: 71 additions & 19 deletions internal/namespaces/rdb/v1/custom_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,20 @@ func backupRestoreBuilder(c *core.Command) *core.Command {

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"`
ID string `json:"ID"`
InstanceID string `json:"instance_ID"`
DatabaseName string `json:"database_name"`
Name string `json:"name"`
Status rdb.DatabaseBackupStatus `json:"status"`
Size *scw.Size `json:"size"`
ExpiresAt *time.Time `json:"expires_at"`
CreatedAt *time.Time `json:"created_at"`
UpdatedAt *time.Time `json:"updated_at"`
InstanceName string `json:"instance_name"`
DownloadURL string `json:"download_url"`
URLExpired bool `json:"url_expired"`
Region scw.Region `json:"region"`
SameRegion bool `json:"same_region"`
}

c.View = &core.View{
Expand All @@ -138,6 +147,14 @@ func backupListBuilder(c *core.Command) *core.Command {
Label: "Name",
FieldName: "Name",
},
{
Label: "Database Name",
FieldName: "DatabaseName",
},
{
Label: "Size",
FieldName: "Size",
},
{
Label: "Status",
FieldName: "Status",
Expand All @@ -147,8 +164,32 @@ func backupListBuilder(c *core.Command) *core.Command {
FieldName: "InstanceID",
},
{
Label: "Exported",
FieldName: "Exported",
Label: "URL Expired",
FieldName: "URLExpired",
},
{
Label: "Download URL",
FieldName: "DownloadURL",
},
{
Label: "Expires At",
FieldName: "ExpiresAt",
},
{
Label: "Created At",
FieldName: "CreatedAt",
},
{
Label: "Updated At",
FieldName: "UpdatedAt",
},
{
Label: "Region",
FieldName: "Region",
},
{
Label: "Same Region",
FieldName: "SameRegion",
},
},
}
Expand All @@ -161,12 +202,25 @@ func backupListBuilder(c *core.Command) *core.Command {
backupList := listBackupResp.([]*rdb.DatabaseBackup)
var res []customBackup
for _, backup := range backupList {
downloadURL := ""
if backup.DownloadURL != nil {
downloadURL = *backup.DownloadURL
}
res = append(res, customBackup{
ID: backup.ID,
Name: backup.Name,
Status: backup.Status,
InstanceID: backup.InstanceID,
Exported: isExported(backup.DownloadURLExpiresAt),
ID: backup.ID,
InstanceID: backup.InstanceID,
DatabaseName: backup.DatabaseName,
Name: backup.Name,
Status: backup.Status,
Size: backup.Size,
ExpiresAt: backup.ExpiresAt,
CreatedAt: backup.CreatedAt,
UpdatedAt: backup.UpdatedAt,
InstanceName: backup.InstanceName,
DownloadURL: downloadURL,
URLExpired: urlExpired(backup.DownloadURLExpiresAt),
Region: backup.Region,
SameRegion: backup.SameRegion,
})
}
return res, nil
Expand All @@ -175,14 +229,12 @@ func backupListBuilder(c *core.Command) *core.Command {
return c
}

func isExported(expirationDate *time.Time) bool {
var exported bool
// urlExpired: indicates if the backup url is still valid after the indicated date.
func urlExpired(expirationDate *time.Time) bool {
if expirationDate == nil {
exported = false
} else {
exported = time.Now().Before(*expirationDate)
return true
}
return exported
return time.Now().After(*expirationDate)
}

func getDefaultFileName(rawURL string) (string, error) {
Expand All @@ -200,7 +252,7 @@ type backupDownloadResult struct {
FileName string `json:"file_name"`
}

func backupResultMarshalerFunc(i interface{}, opt *human.MarshalOpt) (string, error) {
func backupResultMarshallerFunc(i interface{}, opt *human.MarshalOpt) (string, error) {
backupResult := i.(backupDownloadResult)
sizeStr, err := human.Marshal(backupResult.Size, nil)
if err != nil {
Expand Down
36 changes: 27 additions & 9 deletions internal/namespaces/rdb/v1/testdata/test-list-backup-simple.golden
Original file line number Diff line number Diff line change
@@ -1,22 +1,40 @@
🎲🎲🎲 EXIT CODE: 0 🎲🎲🎲
🟩🟩🟩 STDOUT️ 🟩🟩🟩️
ID Name Status Instance ID Exported
da73454a-8232-4d91-b773-8f16ad50a7e1 will_be_exported ready e3678a86-fc68-43ae-90b1-d267b2369576 true
65ce3352-def6-4376-a9d2-cb063a3ce973 will_not_be_exported ready e3678a86-fc68-43ae-90b1-d267b2369576 false
ID Name Database Name Size Status Instance ID URL Expired Download URL Expires At Created At Updated At Region Same Region
da73454a-8232-4d91-b773-8f16ad50a7e1 will_be_exported rdb 2.1 kB ready e3678a86-fc68-43ae-90b1-d267b2369576 false https://s3.nl-ams.scw.cloud/65940610-0e5e-4a98-9306-568aa4eb3673/e3678a86-fc68-43ae-90b1-d267b2369576/da73454a-8232-4d91-b773-8f16ad50a7e1.custom?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=SCWBTK7RYYS1750DS37K%2F20220901%2Fnl-ams%2Fs3%2Faws4_request&X-Amz-Date=20220901T134254Z&X-Amz-Expires=86400&X-Amz-SignedHeaders=host&X-Amz-Signature=4c1622ddf93fa720d38e9a7ba365c16ae07e9d731ba6f6182c95847214d7e21d few seconds ago few seconds ago few seconds ago fr-par false
65ce3352-def6-4376-a9d2-cb063a3ce973 will_not_be_exported rdb 2.1 kB ready e3678a86-fc68-43ae-90b1-d267b2369576 true - few seconds ago few seconds ago few seconds ago fr-par false
🟩🟩🟩 JSON STDOUT 🟩🟩🟩
[
{
"ID": "da73454a-8232-4d91-b773-8f16ad50a7e1",
"name": "will_be_exported",
"instance_ID": "e3678a86-fc68-43ae-90b1-d267b2369576",
"exported": true,
"status": "ready"
"database_name": "rdb",
"name": "will_be_exported",
"status": "ready",
"size": 2107,
"expires_at": "2999-01-02T22:04:05Z",
"created_at": "1970-01-01T00:00:00.0Z",
"updated_at": "1970-01-01T00:00:00.0Z",
"instance_name": "cli-test",
"download_url": "https://s3.nl-ams.scw.cloud/65940610-0e5e-4a98-9306-568aa4eb3673/e3678a86-fc68-43ae-90b1-d267b2369576/da73454a-8232-4d91-b773-8f16ad50a7e1.custom?X-Amz-Algorithm=AWS4-HMAC-SHA256\u0026X-Amz-Credential=SCWBTK7RYYS1750DS37K%2F20220901%2Fnl-ams%2Fs3%2Faws4_request\u0026X-Amz-Date=20220901T134254Z\u0026X-Amz-Expires=86400\u0026X-Amz-SignedHeaders=host\u0026X-Amz-Signature=4c1622ddf93fa720d38e9a7ba365c16ae07e9d731ba6f6182c95847214d7e21d",
"url_expired": false,
"region": "fr-par",
"same_region": false
},
{
"ID": "65ce3352-def6-4376-a9d2-cb063a3ce973",
"name": "will_not_be_exported",
"instance_ID": "e3678a86-fc68-43ae-90b1-d267b2369576",
"exported": false,
"status": "ready"
"database_name": "rdb",
"name": "will_not_be_exported",
"status": "ready",
"size": 2107,
"expires_at": "2999-01-02T22:04:05Z",
"created_at": "1970-01-01T00:00:00.0Z",
"updated_at": "1970-01-01T00:00:00.0Z",
"instance_name": "cli-test",
"download_url": "",
"url_expired": true,
"region": "fr-par",
"same_region": false
}
]

0 comments on commit 1953a69

Please sign in to comment.