Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 8 additions & 15 deletions service/regions/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"

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

type Log interface {
Expand Down Expand Up @@ -35,15 +36,15 @@ func NewAPI(client HttpClient, task Task, logger Log) *API {
return &API{client: client, task: task, logger: logger}
}

// Create will create a new subscription.
// Create will create a new region
func (a *API) Create(ctx context.Context, subId int, region CreateRegion) (int, error) {
var task taskResponse
err := a.client.Post(ctx, "create subscription", fmt.Sprintf("/subscriptions/%d/regions", subId), region, &task)
err := a.client.Post(ctx, "create subscription region", fmt.Sprintf("/subscriptions/%d/regions", subId), region, &task)
if err != nil {
return 0, err
return 0, wrap404Error(subId, err)
}

a.logger.Printf("Waiting for task %s to finish creating the subscription", task)
a.logger.Printf("Waiting for task %s to finish creating the subscription region", task)

id, err := a.task.WaitForResourceId(ctx, *task.ID)
if err != nil {
Expand All @@ -58,7 +59,7 @@ func (a API) List(ctx context.Context, subId int) (*Regions, error) {
var response Regions
err := a.client.Get(ctx, "list regions", fmt.Sprintf("/subscriptions/%d/regions", subId), &response)
if err != nil {
return nil, err
return nil, wrap404Error(subId, err)
}

return &response, nil
Expand All @@ -68,7 +69,7 @@ func (a *API) DeleteWithQuery(ctx context.Context, id int, regions DeleteRegions
var task taskResponse
err := a.client.DeleteWithQuery(ctx, fmt.Sprintf("delete region %d", id), fmt.Sprintf("/subscriptions/%d/regions/", id), regions, &task)
if err != nil {
return err
return wrap404Error(id, err)
}

a.logger.Printf("Waiting for region %d to finish being deleted", id)
Expand All @@ -81,17 +82,9 @@ func (a *API) DeleteWithQuery(ctx context.Context, id int, regions DeleteRegions
return nil
}

type NotFound struct {
id int
}

func (f *NotFound) Error() string {
return fmt.Sprintf("subscription %d not found", f.id)
}

func wrap404Error(id int, err error) error {
if v, ok := err.(*internal.HTTPError); ok && v.StatusCode == http.StatusNotFound {
return &NotFound{id: id}
return &subscriptions.NotFound{ID: id}
}
return err
}
4 changes: 2 additions & 2 deletions service/subscriptions/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,11 @@ func (o taskResponse) String() string {
}

type NotFound struct {
id int
ID int
}

func (f *NotFound) Error() string {
return fmt.Sprintf("subscription %d not found", f.id)
return fmt.Sprintf("subscription %d not found", f.ID)
}

const (
Expand Down
2 changes: 1 addition & 1 deletion service/subscriptions/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func (a *API) DeleteActiveActiveVPCPeering(ctx context.Context, subscription int

func wrap404Error(id int, err error) error {
if v, ok := err.(*internal.HTTPError); ok && v.StatusCode == http.StatusNotFound {
return &NotFound{id: id}
return &NotFound{ID: id}
}
return err
}