Skip to content

Commit

Permalink
Added some testing and latest import details
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnSharpe committed Apr 15, 2024
1 parent bb995f4 commit 4c1ba7f
Show file tree
Hide file tree
Showing 8 changed files with 408 additions and 9 deletions.
5 changes: 5 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/RedisLabs/rediscloud-go-api/service/cloud_accounts"
"github.com/RedisLabs/rediscloud-go-api/service/databases"
"github.com/RedisLabs/rediscloud-go-api/service/latest_backups"
"github.com/RedisLabs/rediscloud-go-api/service/latest_imports"
"github.com/RedisLabs/rediscloud-go-api/service/regions"
"github.com/RedisLabs/rediscloud-go-api/service/subscriptions"
)
Expand All @@ -30,6 +31,8 @@ type Client struct {
Subscription *subscriptions.API
Regions *regions.API
LatestBackup *latest_backups.API
LatestImport *latest_imports.API
//Pricing *pricing.API
// acl
RedisRules *redis_rules.API
Roles *roles.API
Expand Down Expand Up @@ -68,6 +71,8 @@ func NewClient(configs ...Option) (*Client, error) {
Subscription: subscriptions.NewAPI(client, t, config.logger),
Regions: regions.NewAPI(client, t, config.logger),
LatestBackup: latest_backups.NewAPI(client, t, config.logger),
LatestImport: latest_imports.NewAPI(client, t, config.logger),
//Pricing: pricing.NewAPI(client),
// acl
RedisRules: redis_rules.NewAPI(client, t, config.logger),
Roles: roles.NewAPI(client, t, config.logger),
Expand Down
3 changes: 1 addition & 2 deletions internal/http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
)
Expand Down Expand Up @@ -85,7 +84,7 @@ func (c *HttpClient) connection(ctx context.Context, method, name, path string,
defer response.Body.Close()

if response.StatusCode > 299 {
body, _ := ioutil.ReadAll(response.Body)
body, _ := io.ReadAll(response.Body)
return &HTTPError{
Name: name,
StatusCode: response.StatusCode,
Expand Down
10 changes: 4 additions & 6 deletions internal/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ type Api interface {
// by cancelling the context.
WaitForResource(ctx context.Context, id string, resource interface{}) error

// WaitForTask will poll the Task, waiting for it to enter a terminal state (i.e Done or Error). This task
// WaitForTask will poll the Task, waiting for it to enter a terminal state (i.e Done or Error). This Task
// will then be returned, or an error in case it cannot be retrieved.
WaitForTask(ctx context.Context, id string) (*Task, error)
}
Expand Down Expand Up @@ -133,10 +133,8 @@ func (a *api) WaitForTask(ctx context.Context, id string) (*Task, error) {
var err error
task, err = a.get(ctx, id)
if err != nil {
if status, ok := err.(*HTTPError); ok && status.StatusCode == 404 {
return &taskNotFoundError{err}
}
return retry.Unrecoverable(err)
// An error is a terminal state (any repeated pre-task 404s will have been exhausted by this point)
return nil
}

status := redis.StringValue(task.Status)
Expand Down Expand Up @@ -180,7 +178,7 @@ func (a *api) get(ctx context.Context, id string) (*Task, error) {
}

if task.Response != nil && task.Response.Error != nil {
return nil, task.Response.Error
return &task, task.Response.Error
}

return &task, nil
Expand Down
121 changes: 121 additions & 0 deletions latest_backups_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package rediscloud_api

import (
"context"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/require"
)

func TestGetLatestBackup(t *testing.T) {
server := httptest.NewServer(
testServer(
"key",
"secret",
getRequest(
t,
"/subscriptions/12/databases/34/backup",
`{
"taskId": "50ec6172-8475-4ef6-8b3c-d61e688d8fe5",
"commandType": "databaseBackupStatusRequest",
"status": "received",
"description": "Task request received and is being queued for processing.",
"timestamp": "2024-04-15T09:08:04.222268Z",
"links": [
{
"href": "https://api-staging.qa.redislabs.com/v1/tasks/50ec6172-8475-4ef6-8b3c-d61e688d8fe5",
"type": "GET",
"rel": "task"
}
]
}`,
),
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")
require.NoError(t, err)

_, err = subject.LatestBackup.Get(context.TODO(), 12, 34)
require.NoError(t, err)
}

func TestGetAALatestBackup(t *testing.T) {
server := httptest.NewServer(
testServer(
"key",
"secret",
getRequest(
t,
"/subscriptions/12/databases/34/backup?regionName=eu-west-2",
`{
"taskId": "ce2cbfea-9b15-4250-a516-f014161a8dd3",
"commandType": "databaseBackupStatusRequest",
"status": "received",
"description": "Task request received and is being queued for processing.",
"timestamp": "2024-04-15T09:52:23.963337Z",
"links": [
{
"href": "https://api-staging.qa.redislabs.com/v1/tasks/ce2cbfea-9b15-4250-a516-f014161a8dd3",
"type": "GET",
"rel": "task"
}
]
}`,
),
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")
require.NoError(t, err)

_, err = subject.LatestBackup.GetActiveActive(context.TODO(), 12, 34, "eu-west-2")
require.NoError(t, err)
}
121 changes: 121 additions & 0 deletions latest_imports_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package rediscloud_api

import (
"context"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/require"
)

func TestGetLatestImportTooEarly(t *testing.T) {
server := httptest.NewServer(
testServer(
"key",
"secret",
getRequest(
t,
"/subscriptions/12/databases/34/import",
`{
"taskId": "1dfd6084-21df-40c6-829c-e9b4790e207e",
"commandType": "databaseImportStatusRequest",
"status": "received",
"description": "Task request received and is being queued for processing.",
"timestamp": "2024-04-15T10:19:06.710686Z",
"links": [
{
"href": "https://api-staging.qa.redislabs.com/v1/tasks/1dfd6084-21df-40c6-829c-e9b4790e207e",
"type": "GET",
"rel": "task"
}
]
}`,
),
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")
require.NoError(t, err)

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

func TestGetLatestImport(t *testing.T) {
server := httptest.NewServer(
testServer(
"key",
"secret",
getRequest(
t,
"/subscriptions/12/databases/34/import",
`{
"taskId": "e9232e43-3781-4263-a38e-f4d150e03475",
"commandType": "databaseImportStatusRequest",
"status": "received",
"description": "Task request received and is being queued for processing.",
"timestamp": "2024-04-15T10:44:34.325298Z",
"links": [
{
"href": "https://api-staging.qa.redislabs.com/v1/tasks/e9232e43-3781-4263-a38e-f4d150e03475",
"type": "GET",
"rel": "task"
}
]
}`,
),
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": {}
},
"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")
require.NoError(t, err)

_, err = subject.LatestImport.Get(context.TODO(), 12, 34)
require.NoError(t, err)
}
2 changes: 1 addition & 1 deletion service/account/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func (a *API) ListDataPersistence(ctx context.Context) ([]*DataPersistence, erro
return body.DataPersistence, nil
}

// ListDataModules will return the list of available data modules that can be applied to a database.
// ListDatabaseModules will return the list of available data modules that can be applied to a database.
func (a *API) ListDatabaseModules(ctx context.Context) ([]*DatabaseModule, error) {
var body databaseModules
if err := a.client.Get(ctx, "list database modules", "/database-modules", &body); err != nil {
Expand Down
Loading

0 comments on commit 4c1ba7f

Please sign in to comment.