Skip to content

Commit

Permalink
Getting a detailed import status request (yet to do failureReasonPara…
Browse files Browse the repository at this point in the history
…ms and backups)
  • Loading branch information
JohnSharpe committed May 21, 2024
1 parent c6c217f commit e64689d
Show file tree
Hide file tree
Showing 3 changed files with 180 additions and 48 deletions.
145 changes: 141 additions & 4 deletions latest_imports_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ package rediscloud_api

import (
"context"
"github.com/RedisLabs/rediscloud-go-api/redis"
"github.com/RedisLabs/rediscloud-go-api/service/latest_imports"
"github.com/stretchr/testify/assert"
"net/http/httptest"
"testing"
"time"

"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -56,6 +60,31 @@ func TestGetLatestImportTooEarly(t *testing.T) {
]
}`,
),
getRequest(
t,
"/tasks/1dfd6084-21df-40c6-829c-e9b4790e207e",
`{
"taskId": "1dfd6084-21df-40c6-829c-e9b4790e207e",
"commandType": "databaseImportStatusRequest",
"status": "processing-error",
"description": "Task request failed during processing. See error information for failure details.",
"timestamp": "2024-04-15T10:19:07.331898Z",
"response": {
"error": {
"type": "SUBSCRIPTION_NOT_ACTIVE",
"status": "403 FORBIDDEN",
"description": "Cannot preform any actions for subscription that is not in an active state"
}
},
"links": [
{
"href": "https://api-staging.qa.redislabs.com/v1/tasks/1dfd6084-21df-40c6-829c-e9b4790e207e",
"type": "GET",
"rel": "self"
}
]
}`,
),
))

subject, err := clientFromTestServer(server, "key", "secret")
Expand Down Expand Up @@ -100,7 +129,34 @@ func TestGetFixedLatestImport(t *testing.T) {
"response": {
"resourceId": 51051302,
"additionalResourceId": 110777,
"resource": {}
"resource": {
"status": "importing"
}
},
"links": [
{
"href": "https://api-staging.qa.redislabs.com/v1/tasks/e9232e43-3781-4263-a38e-f4d150e03475",
"type": "GET",
"rel": "self"
}
]
}`,
),
getRequest(
t,
"/tasks/e9232e43-3781-4263-a38e-f4d150e03475",
`{
"taskId": "e9232e43-3781-4263-a38e-f4d150e03475",
"commandType": "databaseImportStatusRequest",
"status": "processing-completed",
"description": "Request processing completed successfully and its resources are now being provisioned / de-provisioned.",
"timestamp": "2024-04-15T10:44:35.225468Z",
"response": {
"resourceId": 51051302,
"additionalResourceId": 110777,
"resource": {
"status": "importing"
}
},
"links": [
{
Expand All @@ -116,8 +172,22 @@ func TestGetFixedLatestImport(t *testing.T) {
subject, err := clientFromTestServer(server, "key", "secret")
require.NoError(t, err)

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

assert.Equal(t, &latest_imports.LatestImportStatus{
CommandType: redis.String("databaseImportStatusRequest"),
Description: redis.String("Request processing completed successfully and its resources are now being provisioned / de-provisioned."),
Status: redis.String("processing-completed"),
ID: redis.String("e9232e43-3781-4263-a38e-f4d150e03475"),
Response: &latest_imports.Response{
ID: redis.Int(51051302),
Resource: &latest_imports.Resource{
Status: redis.String("importing"),
},
Error: nil,
},
}, actual)
}

func TestGetLatestImport(t *testing.T) {
Expand Down Expand Up @@ -155,7 +225,58 @@ func TestGetLatestImport(t *testing.T) {
"response": {
"resourceId": 51051302,
"additionalResourceId": 110777,
"resource": {}
"resource": {
"failureReason": "file-corrupted",
"failureReasonParams": [
{
"key": "bytes_configured_bdb_limit",
"value": "1234"
},
{
"key": "bytes_of_expected_dataset",
"value": "5678"
}
],
"lastImportTime": "2024-05-21T10:36:26Z",
"status": "failed"
}
},
"links": [
{
"href": "https://api-staging.qa.redislabs.com/v1/tasks/e9232e43-3781-4263-a38e-f4d150e03475",
"type": "GET",
"rel": "self"
}
]
}`,
),
getRequest(
t,
"/tasks/e9232e43-3781-4263-a38e-f4d150e03475",
`{
"taskId": "e9232e43-3781-4263-a38e-f4d150e03475",
"commandType": "databaseImportStatusRequest",
"status": "processing-completed",
"description": "Request processing completed successfully and its resources are now being provisioned / de-provisioned.",
"timestamp": "2024-04-15T10:44:35.225468Z",
"response": {
"resourceId": 51051302,
"additionalResourceId": 110777,
"resource": {
"failureReason": "file-corrupted",
"failureReasonParams": [
{
"key": "bytes_configured_bdb_limit",
"value": "1234"
},
{
"key": "bytes_of_expected_dataset",
"value": "5678"
}
],
"lastImportTime": "2024-05-21T10:36:26Z",
"status": "failed"
}
},
"links": [
{
Expand All @@ -171,6 +292,22 @@ func TestGetLatestImport(t *testing.T) {
subject, err := clientFromTestServer(server, "key", "secret")
require.NoError(t, err)

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

assert.Equal(t, &latest_imports.LatestImportStatus{
CommandType: redis.String("databaseImportStatusRequest"),
Description: redis.String("Request processing completed successfully and its resources are now being provisioned / de-provisioned."),
Status: redis.String("processing-completed"),
ID: redis.String("e9232e43-3781-4263-a38e-f4d150e03475"),
Response: &latest_imports.Response{
ID: redis.Int(51051302),
Resource: &latest_imports.Resource{
Status: redis.String("failed"),
LastImportTime: redis.Time(time.Date(2024, 5, 21, 10, 36, 26, 0, time.UTC)),
FailureReason: redis.String("file-corrupted"),
},
Error: nil,
},
}, actual)
}
49 changes: 15 additions & 34 deletions service/latest_imports/model.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package latest_imports

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

"github.com/RedisLabs/rediscloud-go-api/internal"
"github.com/RedisLabs/rediscloud-go-api/redis"
Expand All @@ -22,15 +22,26 @@ func (o LatestImportStatus) 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"`
LastImportTime *time.Time `json:"lastImportTime,omitempty"`
FailureReason *string `json:"failureReason,omitempty"`
// FailureReasonParams // Type unknown
}

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

type Error struct {
Type *string `json:"type,omitempty"`
Description *string `json:"description,omitempty"`
Expand All @@ -55,36 +66,6 @@ func (e *Error) Error() string {

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

func NewLatestImportStatus(task *internal.Task) *LatestImportStatus {
latestImportStatus := LatestImportStatus{
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
}

latestImportStatus.Response = &r
}

return &latestImportStatus
}

type NotFound struct {
subId int
dbId int
Expand Down
34 changes: 24 additions & 10 deletions service/latest_imports/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@ package latest_imports
import (
"context"
"fmt"
"net/http"

"github.com/RedisLabs/rediscloud-go-api/internal"
"net/http"
)

type HttpClient interface {
Get(ctx context.Context, name, path string, responseBody interface{}) error
}

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 +36,7 @@ func (a *API) Get(ctx context.Context, subscription int, database int) (*LatestI
if err != nil {
return nil, wrap404Error(subscription, database, err)
}
return NewLatestImportStatus(task), nil
return task, nil
}

func (a *API) GetFixed(ctx context.Context, subscription int, database int) (*LatestImportStatus, error) {
Expand All @@ -47,19 +46,34 @@ func (a *API) GetFixed(ctx context.Context, subscription int, database int) (*La
if err != nil {
return nil, wrap404Error(subscription, database, err)
}
return NewLatestImportStatus(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) (*LatestImportStatus, 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 import status request %d to complete", task.ID)

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

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

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

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

return a.taskWaiter.WaitForTask(ctx, *taskResponse.ID)
return importStatusTask, nil
}

func wrap404Error(subId int, dbId int, err error) error {
Expand Down

0 comments on commit e64689d

Please sign in to comment.