Skip to content

Commit

Permalink
Fixing a bug with the query parameter in AA database latest-backup lo…
Browse files Browse the repository at this point in the history
…okups. The ? for query params was being encoded (#166)
  • Loading branch information
JohnSharpe authored Jun 4, 2024
1 parent 5b88d15 commit 39114d2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
5 changes: 3 additions & 2 deletions latest_backups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,9 +186,10 @@ func TestGetAALatestBackup(t *testing.T) {
testServer(
"key",
"secret",
getRequest(
getRequestWithQuery(
t,
"/subscriptions/12/databases/34/backup?regionName=eu-west-2",
"/subscriptions/12/databases/34/backup",
map[string][]string{"regionName": {"eu-west-2"}},
`{
"taskId": "ce2cbfea-9b15-4250-a516-f014161a8dd3",
"commandType": "databaseBackupStatusRequest",
Expand Down
35 changes: 31 additions & 4 deletions service/latest_backups/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"context"
"fmt"
"net/http"
"net/url"

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

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

type TaskWaiter interface {
Expand Down Expand Up @@ -52,12 +54,37 @@ func (a *API) GetFixed(ctx context.Context, subscription int, database int) (*La

func (a *API) GetActiveActive(ctx context.Context, subscription int, database int, region string) (*LatestBackupStatus, error) {
message := fmt.Sprintf("get latest backup information for database %d in subscription %d and region %s", subscription, database, region)
address := fmt.Sprintf("/subscriptions/%d/databases/%d/backup?regionName=%s", subscription, database, region)
task, err := a.get(ctx, message, address)
address := fmt.Sprintf("/subscriptions/%d/databases/%d/backup", subscription, database)

q := map[string][]string{
"regionName": {region},
}

var task internal.TaskResponse
err := a.client.GetWithQuery(ctx, message, address, q, &task)
if err != nil {
return nil, wrap404ErrorActiveActive(subscription, database, region, err)
return nil, err
}
return task, nil

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,
)

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
}

func (a *API) get(ctx context.Context, message string, address string) (*LatestBackupStatus, error) {
Expand Down

0 comments on commit 39114d2

Please sign in to comment.