diff --git a/latest_backups_test.go b/latest_backups_test.go index ff17e1a..34ca11a 100644 --- a/latest_backups_test.go +++ b/latest_backups_test.go @@ -57,29 +57,6 @@ 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") @@ -135,29 +112,6 @@ func TestGetFixedLatestBackup(t *testing.T) { ] }`, ), - 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": [ - { - "href": "https://api-staging.qa.redislabs.com/v1/tasks/ce2cbfea-9b15-4250-a516-f014161a8dd3", - "type": "GET", - "rel": "self" - } - ] - }`, - ), )) subject, err := clientFromTestServer(server, "key", "secret") @@ -230,31 +184,6 @@ 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") diff --git a/latest_imports_test.go b/latest_imports_test.go index 2c28b15..38f6028 100644 --- a/latest_imports_test.go +++ b/latest_imports_test.go @@ -61,31 +61,6 @@ 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") @@ -143,31 +118,6 @@ func TestGetFixedLatestImport(t *testing.T) { ] }`, ), - 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": [ - { - "href": "https://api-staging.qa.redislabs.com/v1/tasks/e9232e43-3781-4263-a38e-f4d150e03475", - "type": "GET", - "rel": "self" - } - ] - }`, - ), )) subject, err := clientFromTestServer(server, "key", "secret") @@ -251,43 +201,6 @@ func TestGetLatestImport(t *testing.T) { ] }`, ), - 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": [ - { - "href": "https://api-staging.qa.redislabs.com/v1/tasks/e9232e43-3781-4263-a38e-f4d150e03475", - "type": "GET", - "rel": "self" - } - ] - }`, - ), )) subject, err := clientFromTestServer(server, "key", "secret") diff --git a/service/latest_backups/model.go b/service/latest_backups/model.go index bfe6d8b..2a0a8f3 100644 --- a/service/latest_backups/model.go +++ b/service/latest_backups/model.go @@ -1,14 +1,52 @@ package latest_backups import ( + "encoding/json" "fmt" - "regexp" "time" "github.com/RedisLabs/rediscloud-go-api/internal" - "github.com/RedisLabs/rediscloud-go-api/redis" ) +func fromInternal(t internal.Task) LatestBackupStatus { + lbs := LatestBackupStatus{ + CommandType: t.CommandType, + Description: t.Description, + Status: t.Status, + ID: t.ID, + } + if t.Response == nil { + lbs.Response = nil + } else { + response := Response{ + ID: t.Response.ID, + } + + if t.Response.Resource == nil { + response.Resource = nil + } else { + j, err := t.Response.Resource.MarshalJSON() + if err != nil { + panic(nil) + } + + var res Resource + if j != nil && len(j) > 0 { + err = json.Unmarshal(j, &res) + if err != nil { + panic(nil) + } + } + + response.Resource = &res + } + + response.Error = t.Response.Error + lbs.Response = &response + } + return lbs +} + type LatestBackupStatus struct { CommandType *string `json:"commandType,omitempty"` Description *string `json:"description,omitempty"` @@ -22,9 +60,9 @@ func (o LatestBackupStatus) String() string { } type Response struct { - ID *int `json:"resourceId,omitempty"` - Resource *Resource `json:"resource,omitempty"` - Error *Error `json:"error,omitempty"` + ID *int `json:"resourceId,omitempty"` + Resource *Resource `json:"resource,omitempty"` + Error *internal.Error `json:"error,omitempty"` } func (o Response) String() string { @@ -41,30 +79,6 @@ 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 (e *Error) String() string { - return internal.ToString(e) -} - -func (e *Error) StatusCode() string { - matches := errorStatusCode.FindStringSubmatch(redis.StringValue(e.Status)) - if len(matches) == 2 { - return matches[1] - } - return "" -} - -func (e *Error) Error() string { - return fmt.Sprintf("%s - %s: %s", redis.StringValue(e.Status), redis.StringValue(e.Type), redis.StringValue(e.Description)) -} - -var errorStatusCode = regexp.MustCompile("^(\\d*).*$") - type NotFound struct { subId int dbId int diff --git a/service/latest_backups/service.go b/service/latest_backups/service.go index d06013e..cce71b7 100644 --- a/service/latest_backups/service.go +++ b/service/latest_backups/service.go @@ -15,7 +15,7 @@ type HttpClient interface { } type TaskWaiter interface { - Wait(ctx context.Context, id string) error + WaitForTask(ctx context.Context, id string) (*internal.Task, error) } type Log interface { @@ -68,23 +68,16 @@ func (a *API) GetActiveActive(ctx context.Context, subscription int, database in a.logger.Printf("Waiting for backup status request %d to complete", task.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, - ) - + finishedTask, err := a.taskWaiter.WaitForTask(ctx, *task.ID) if err != nil { return nil, wrap404ErrorActiveActive(subscription, database, region, fmt.Errorf("failed to retrieve completed backup status %d: %w", task.ID, err)) } - return backupStatusTask, nil + a.logger.Printf("Backup status request %d completed, possibly with error", task.ID, err) + + lbs := fromInternal(*finishedTask) + return &lbs, nil } func (a *API) get(ctx context.Context, message string, address string) (*LatestBackupStatus, error) { @@ -96,22 +89,12 @@ func (a *API) get(ctx context.Context, message string, address string) (*LatestB a.logger.Printf("Waiting for backup status request %d to complete", task.ID) - err = a.taskWaiter.Wait(ctx, *task.ID) + finishedTask, err := a.taskWaiter.WaitForTask(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 + lbs := fromInternal(*finishedTask) + return &lbs, nil } func wrap404Error(subId int, dbId int, err error) error { diff --git a/service/latest_imports/model.go b/service/latest_imports/model.go index 8b64d35..44b5b5e 100644 --- a/service/latest_imports/model.go +++ b/service/latest_imports/model.go @@ -1,14 +1,52 @@ package latest_imports import ( + "encoding/json" "fmt" - "regexp" "time" "github.com/RedisLabs/rediscloud-go-api/internal" - "github.com/RedisLabs/rediscloud-go-api/redis" ) +func fromInternal(t internal.Task) LatestImportStatus { + lis := LatestImportStatus{ + CommandType: t.CommandType, + Description: t.Description, + Status: t.Status, + ID: t.ID, + } + if t.Response == nil { + lis.Response = nil + } else { + response := Response{ + ID: t.Response.ID, + } + + if t.Response.Resource == nil { + response.Resource = nil + } else { + j, err := t.Response.Resource.MarshalJSON() + if err != nil { + panic(nil) + } + + var res Resource + if j != nil && len(j) > 0 { + err = json.Unmarshal(j, &res) + if err != nil { + panic(nil) + } + } + + response.Resource = &res + } + + response.Error = t.Response.Error + lis.Response = &response + } + return lis +} + type LatestImportStatus struct { CommandType *string `json:"commandType,omitempty"` Description *string `json:"description,omitempty"` @@ -22,9 +60,9 @@ func (o LatestImportStatus) String() string { } type Response struct { - ID *int `json:"resourceId,omitempty"` - Resource *Resource `json:"resource,omitempty"` - Error *Error `json:"error,omitempty"` + ID *int `json:"resourceId,omitempty"` + Resource *Resource `json:"resource,omitempty"` + Error *internal.Error `json:"error,omitempty"` } func (o Response) String() string { @@ -51,30 +89,6 @@ func (o FailureReasonParam) 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 (e *Error) String() string { - return internal.ToString(e) -} - -func (e *Error) StatusCode() string { - matches := errorStatusCode.FindStringSubmatch(redis.StringValue(e.Status)) - if len(matches) == 2 { - return matches[1] - } - return "" -} - -func (e *Error) Error() string { - return fmt.Sprintf("%s - %s: %s", redis.StringValue(e.Status), redis.StringValue(e.Type), redis.StringValue(e.Description)) -} - -var errorStatusCode = regexp.MustCompile("^(\\d*).*$") - type NotFound struct { subId int dbId int diff --git a/service/latest_imports/service.go b/service/latest_imports/service.go index 13a934d..1276989 100644 --- a/service/latest_imports/service.go +++ b/service/latest_imports/service.go @@ -13,7 +13,7 @@ type HttpClient interface { } type TaskWaiter interface { - Wait(ctx context.Context, id string) error + WaitForTask(ctx context.Context, id string) (*internal.Task, error) } type Log interface { @@ -59,22 +59,12 @@ func (a *API) get(ctx context.Context, message string, address string) (*LatestI a.logger.Printf("Waiting for import status request %d to complete", task.ID) - err = a.taskWaiter.Wait(ctx, *task.ID) + finishedTask, err := a.taskWaiter.WaitForTask(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 importStatusTask, nil + lis := fromInternal(*finishedTask) + return &lis, nil } func wrap404Error(subId int, dbId int, err error) error {