Skip to content

Commit

Permalink
Added backup resource detail
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnSharpe committed May 21, 2024
1 parent d1d71b5 commit bfe514c
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 53 deletions.
100 changes: 93 additions & 7 deletions latest_backups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ import (
"net/http/httptest"
"testing"

"github.com/RedisLabs/rediscloud-go-api/redis"
"github.com/RedisLabs/rediscloud-go-api/service/latest_backups"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -54,6 +57,29 @@ func TestGetLatestBackup(t *testing.T) {
]
}`,
),
getRequest(
t,
"/tasks/50ec6172-8475-4ef6-8b3c-d61e688d8fe5",
`{
"taskId": "50ec6172-8475-4ef6-8b3c-d61e688d8fe5",
"commandType": "databaseBackupStatusRequest",
"status": "processing-completed",
"description": "Request processing completed successfully and its resources are now being provisioned / de-provisioned.",
"timestamp": "2024-04-15T09:08:07.537915Z",
"response": {
"resourceId": 51051292,
"additionalResourceId": 12,
"resource": {}
},
"links": [
{
"href": "https://api-staging.qa.redislabs.com/v1/tasks/50ec6172-8475-4ef6-8b3c-d61e688d8fe5",
"type": "GET",
"rel": "self"
}
]
}`,
),
))

subject, err := clientFromTestServer(server, "key", "secret")
Expand Down Expand Up @@ -92,14 +118,35 @@ func TestGetFixedLatestBackup(t *testing.T) {
`{
"taskId": "ce2cbfea-9b15-4250-a516-f014161a8dd3",
"commandType": "databaseBackupStatusRequest",
"status": "processing-error",
"description": "Task request failed during processing. See error information for failure details.",
"status": "processing-completed",
"description": "Request processing completed successfully and its resources are now being provisioned / de-provisioned.",
"timestamp": "2024-04-15T09:52:26.101936Z",
"response": {
"error": {
"type": "DATABASE_BACKUP_DISABLED",
"status": "400 BAD_REQUEST",
"description": "Database backup is disabled"
"resource": {
"status": "success"
}
},
"links": [
{
"href": "https://api-staging.qa.redislabs.com/v1/tasks/ce2cbfea-9b15-4250-a516-f014161a8dd3",
"type": "GET",
"rel": "self"
}
]
}`,
),
getRequest(
t,
"/tasks/ce2cbfea-9b15-4250-a516-f014161a8dd3",
`{
"taskId": "ce2cbfea-9b15-4250-a516-f014161a8dd3",
"commandType": "databaseBackupStatusRequest",
"status": "processing-completed",
"description": "Request processing completed successfully and its resources are now being provisioned / de-provisioned.",
"timestamp": "2024-04-15T09:52:26.101936Z",
"response": {
"resource": {
"status": "success"
}
},
"links": [
Expand All @@ -116,8 +163,22 @@ func TestGetFixedLatestBackup(t *testing.T) {
subject, err := clientFromTestServer(server, "key", "secret")
require.NoError(t, err)

_, err = subject.LatestBackup.GetFixed(context.TODO(), 12, 34)
actual, err := subject.LatestBackup.GetFixed(context.TODO(), 12, 34)
require.NoError(t, err)

assert.Equal(t, &latest_backups.LatestBackupStatus{
CommandType: redis.String("databaseBackupStatusRequest"),
Description: redis.String("Request processing completed successfully and its resources are now being provisioned / de-provisioned."),
Status: redis.String("processing-completed"),
ID: redis.String("ce2cbfea-9b15-4250-a516-f014161a8dd3"),
Response: &latest_backups.Response{
Resource: &latest_backups.Resource{
Status: redis.String("success"),
},
Error: nil,
},
}, actual)

}

func TestGetAALatestBackup(t *testing.T) {
Expand Down Expand Up @@ -168,6 +229,31 @@ func TestGetAALatestBackup(t *testing.T) {
]
}`,
),
getRequest(
t,
"/tasks/ce2cbfea-9b15-4250-a516-f014161a8dd3",
`{
"taskId": "ce2cbfea-9b15-4250-a516-f014161a8dd3",
"commandType": "databaseBackupStatusRequest",
"status": "processing-error",
"description": "Task request failed during processing. See error information for failure details.",
"timestamp": "2024-04-15T09:52:26.101936Z",
"response": {
"error": {
"type": "DATABASE_BACKUP_DISABLED",
"status": "400 BAD_REQUEST",
"description": "Database backup is disabled"
}
},
"links": [
{
"href": "https://api-staging.qa.redislabs.com/v1/tasks/ce2cbfea-9b15-4250-a516-f014161a8dd3",
"type": "GET",
"rel": "self"
}
]
}`,
),
))

subject, err := clientFromTestServer(server, "key", "secret")
Expand Down
52 changes: 16 additions & 36 deletions service/latest_backups/model.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package latest_backups

import (
"encoding/json"
"fmt"
"regexp"
"time"

"github.com/RedisLabs/rediscloud-go-api/internal"
"github.com/RedisLabs/rediscloud-go-api/redis"
Expand All @@ -22,23 +22,33 @@ func (o LatestBackupStatus) String() string {
}

type Response struct {
ID *int `json:"resourceId,omitempty"`
Resource *json.RawMessage `json:"resource,omitempty"`
Error *Error `json:"error,omitempty"`
ID *int `json:"resourceId,omitempty"`
Resource *Resource `json:"resource,omitempty"`
Error *Error `json:"error,omitempty"`
}

func (o Response) String() string {
return internal.ToString(o)
}

type Resource struct {
Status *string `json:"status,omitempty"`
LastBackupTime *time.Time `json:"lastBackupTime,omitempty"`
FailureReason *string `json:"failureReason,omitempty"`
}

func (o Resource) String() string {
return internal.ToString(o)
}

type Error struct {
Type *string `json:"type,omitempty"`
Description *string `json:"description,omitempty"`
Status *string `json:"status,omitempty"`
}

func (o Error) String() string {
return internal.ToString(o)
func (e *Error) String() string {
return internal.ToString(e)
}

func (e *Error) StatusCode() string {
Expand All @@ -55,36 +65,6 @@ func (e *Error) Error() string {

var errorStatusCode = regexp.MustCompile("^(\\d*).*$")

func NewLatestBackupStatus(task *internal.Task) *LatestBackupStatus {
latestBackupStatus := LatestBackupStatus{
CommandType: task.CommandType,
Description: task.Description,
Status: task.Status,
ID: task.ID,
}

if task.Response != nil {
r := Response{
ID: task.Response.ID,
Resource: task.Response.Resource,
}

if task.Response.Error != nil {
e := Error{
Type: task.Response.Error.Type,
Description: task.Response.Error.Description,
Status: task.Response.Error.Status,
}

r.Error = &e
}

latestBackupStatus.Response = &r
}

return &latestBackupStatus
}

type NotFound struct {
subId int
dbId int
Expand Down
33 changes: 24 additions & 9 deletions service/latest_backups/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ type HttpClient interface {
}

type TaskWaiter interface {
WaitForTask(ctx context.Context, id string) (*internal.Task, error)
Wait(ctx context.Context, id string) error
}

type Log interface {
Expand All @@ -37,7 +37,7 @@ func (a *API) Get(ctx context.Context, subscription int, database int) (*LatestB
if err != nil {
return nil, wrap404Error(subscription, database, err)
}
return NewLatestBackupStatus(task), nil
return task, nil
}

func (a *API) GetFixed(ctx context.Context, subscription int, database int) (*LatestBackupStatus, error) {
Expand All @@ -47,7 +47,7 @@ func (a *API) GetFixed(ctx context.Context, subscription int, database int) (*La
if err != nil {
return nil, wrap404Error(subscription, database, err)
}
return NewLatestBackupStatus(task), nil
return task, nil
}

func (a *API) GetActiveActive(ctx context.Context, subscription int, database int, region string) (*LatestBackupStatus, error) {
Expand All @@ -57,19 +57,34 @@ func (a *API) GetActiveActive(ctx context.Context, subscription int, database in
if err != nil {
return nil, wrap404ErrorActiveActive(subscription, database, region, err)
}
return NewLatestBackupStatus(task), nil
return task, nil
}

func (a *API) get(ctx context.Context, message string, address string) (*internal.Task, error) {
var taskResponse internal.TaskResponse
err := a.client.Get(ctx, message, address, &taskResponse)
func (a *API) get(ctx context.Context, message string, address string) (*LatestBackupStatus, error) {
var task internal.TaskResponse
err := a.client.Get(ctx, message, address, &task)
if err != nil {
return nil, err
}

a.logger.Printf("Waiting for backup status request %d to complete", taskResponse.ID)
a.logger.Printf("Waiting for backup status request %d to complete", task.ID)

return a.taskWaiter.WaitForTask(ctx, *taskResponse.ID)
err = a.taskWaiter.Wait(ctx, *task.ID)

a.logger.Printf("Backup status request %d completed, possibly with error", task.ID, err)

var backupStatusTask *LatestBackupStatus
err = a.client.Get(ctx,
fmt.Sprintf("retrieve completed backup status task %d", task.ID),
"/tasks/"+*task.ID,
&backupStatusTask,
)

if err != nil {
return nil, fmt.Errorf("failed to retrieve completed backup status %d: %w", task.ID, err)
}

return backupStatusTask, nil
}

func wrap404Error(subId int, dbId int, err error) error {
Expand Down
2 changes: 1 addition & 1 deletion service/latest_imports/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ type Error struct {
Status *string `json:"status,omitempty"`
}

func (e Error) String() string {
func (e *Error) String() string {
return internal.ToString(e)
}

Expand Down

0 comments on commit bfe514c

Please sign in to comment.