Skip to content

Commit

Permalink
Individual NotFound structs are used by the terraform provider
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnSharpe committed Jun 22, 2023
1 parent 9c85e2b commit 2b52f8b
Show file tree
Hide file tree
Showing 11 changed files with 116 additions and 43 deletions.
4 changes: 1 addition & 3 deletions cloud_account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"net/http/httptest"
"testing"

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

"github.com/RedisLabs/rediscloud-go-api/redis"
"github.com/RedisLabs/rediscloud-go-api/service/cloud_accounts"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -158,7 +156,7 @@ func TestCloudAccount_Get_wraps404(t *testing.T) {
actual, err := subject.CloudAccount.Get(context.TODO(), 98765)

assert.Nil(t, actual)
assert.IsType(t, &internal.NotFound{}, err)
assert.IsType(t, &cloud_accounts.NotFound{}, err)
}

func TestCloudAccount_List(t *testing.T) {
Expand Down
17 changes: 0 additions & 17 deletions internal/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"math"
"net/http"
"net/url"
"time"

Expand Down Expand Up @@ -167,19 +166,3 @@ func (e *taskNotFoundError) Error() string {
func (e taskNotFoundError) Unwrap() error {
return e.wrapped
}

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

type NotFound struct {
ID int
EntityType string
}

func (f *NotFound) Error() string {
return fmt.Sprintf("%s %d not found", f.EntityType, f.ID)
}
20 changes: 18 additions & 2 deletions service/access_control_lists/redis_rules/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package redis_rules
import (
"context"
"fmt"
"net/http"

"github.com/RedisLabs/rediscloud-go-api/internal"
)
Expand Down Expand Up @@ -69,7 +70,7 @@ func (a *API) Update(ctx context.Context, id int, redisRule CreateRedisRuleReque
var task internal.TaskResponse
err := a.client.Put(ctx, fmt.Sprintf("update redisRule %d", id), fmt.Sprintf("/acl/redisRules/%d", id), redisRule, &task)
if err != nil {
return internal.Wrap404Error(id, "redisRule", err)
return wrap404Error(id, err)
}

a.logger.Printf("Waiting for task %s to finish updating the redisRule", task)
Expand All @@ -87,7 +88,7 @@ func (a *API) Delete(ctx context.Context, id int) error {
var task internal.TaskResponse
err := a.client.Delete(ctx, fmt.Sprintf("delete redisRule %d", id), fmt.Sprintf("/acl/redisRules/%d", id), &task)
if err != nil {
return internal.Wrap404Error(id, "redisRule", err)
return wrap404Error(id, err)
}

a.logger.Printf("Waiting for redisRule %d to finish being deleted", id)
Expand All @@ -99,3 +100,18 @@ func (a *API) Delete(ctx context.Context, id int) error {

return nil
}

type NotFound struct {
ID int
}

func (f *NotFound) Error() string {
return fmt.Sprintf("redisRule %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 err
}
18 changes: 17 additions & 1 deletion service/access_control_lists/roles/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package roles
import (
"context"
"fmt"
"net/http"

"github.com/RedisLabs/rediscloud-go-api/internal"
)
Expand Down Expand Up @@ -87,7 +88,7 @@ func (a *API) Delete(ctx context.Context, id int) error {
var task internal.TaskResponse
err := a.client.Delete(ctx, fmt.Sprintf("delete role %d", id), fmt.Sprintf("/acl/roles/%d", id), &task)
if err != nil {
return internal.Wrap404Error(id, "role", err)
return wrap404Error(id, err)
}

a.logger.Printf("Waiting for role %d to finish being deleted", id)
Expand All @@ -99,3 +100,18 @@ func (a *API) Delete(ctx context.Context, id int) error {

return nil
}

type NotFound struct {
ID int
}

func (f *NotFound) Error() string {
return fmt.Sprintf("role %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 err
}
20 changes: 18 additions & 2 deletions service/access_control_lists/users/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package users
import (
"context"
"fmt"
"net/http"

"github.com/RedisLabs/rediscloud-go-api/internal"
)
Expand Down Expand Up @@ -49,7 +50,7 @@ func (a *API) Get(ctx context.Context, id int) (*GetUserResponse, error) {
var response GetUserResponse
err := a.client.Get(ctx, fmt.Sprintf("get user %d", id), fmt.Sprintf("/acl/users/%d", id), &response)
if err != nil {
return nil, internal.Wrap404Error(id, "user", err)
return nil, wrap404Error(id, err)
}

return &response, nil
Expand Down Expand Up @@ -96,7 +97,7 @@ func (a *API) Delete(ctx context.Context, id int) error {
var task internal.TaskResponse
err := a.client.Delete(ctx, fmt.Sprintf("delete user %d", id), fmt.Sprintf("/acl/users/%d", id), &task)
if err != nil {
return internal.Wrap404Error(id, "user", err)
return wrap404Error(id, err)
}

a.logger.Printf("Waiting for user %d to finish being deleted", id)
Expand All @@ -108,3 +109,18 @@ func (a *API) Delete(ctx context.Context, id int) error {

return nil
}

type NotFound struct {
ID int
}

func (f *NotFound) Error() string {
return fmt.Sprintf("user %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 err
}
10 changes: 10 additions & 0 deletions service/cloud_accounts/model.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cloud_accounts

import (
"fmt"

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

Expand Down Expand Up @@ -31,6 +33,14 @@ func (o UpdateCloudAccount) String() string {
return internal.ToString(o)
}

type NotFound struct {
id int
}

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

type listCloudAccounts struct {
CloudAccounts []*CloudAccount `json:"cloudAccounts"`
}
Expand Down
14 changes: 11 additions & 3 deletions service/cloud_accounts/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cloud_accounts
import (
"context"
"fmt"
"net/http"

"github.com/RedisLabs/rediscloud-go-api/internal"
)
Expand Down Expand Up @@ -63,7 +64,7 @@ func (a API) List(ctx context.Context) ([]*CloudAccount, error) {
func (a *API) Get(ctx context.Context, id int) (*CloudAccount, error) {
var response CloudAccount
if err := a.client.Get(ctx, fmt.Sprintf("retrieve cloud account %d", id), fmt.Sprintf("/cloud-accounts/%d", id), &response); err != nil {
return nil, internal.Wrap404Error(id, "cloud account", err)
return nil, wrap404Error(id, err)
}

return &response, nil
Expand All @@ -73,7 +74,7 @@ func (a *API) Get(ctx context.Context, id int) (*CloudAccount, error) {
func (a *API) Update(ctx context.Context, id int, account UpdateCloudAccount) error {
var response internal.TaskResponse
if err := a.client.Put(ctx, fmt.Sprintf("update cloud account %d", id), fmt.Sprintf("/cloud-accounts/%d", id), account, &response); err != nil {
return internal.Wrap404Error(id, "cloud account", err)
return wrap404Error(id, err)
}

a.logger.Printf("Waiting for cloud account %d to finish being updated", id)
Expand All @@ -90,7 +91,7 @@ func (a *API) Update(ctx context.Context, id int, account UpdateCloudAccount) er
func (a *API) Delete(ctx context.Context, id int) error {
var response internal.TaskResponse
if err := a.client.Delete(ctx, fmt.Sprintf("delete cloud account %d", id), fmt.Sprintf("/cloud-accounts/%d", id), &response); err != nil {
return internal.Wrap404Error(id, "cloud account", err)
return wrap404Error(id, err)
}

a.logger.Printf("Waiting for cloud account %d to finish being deleted", id)
Expand All @@ -101,3 +102,10 @@ func (a *API) Delete(ctx context.Context, id int) error {

return nil
}

func wrap404Error(id int, err error) error {
if v, ok := err.(*internal.HTTPError); ok && v.StatusCode == http.StatusNotFound {
return &NotFound{id: id}
}
return err
}
16 changes: 13 additions & 3 deletions service/regions/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package regions
import (
"context"
"fmt"
"net/http"

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

"github.com/RedisLabs/rediscloud-go-api/internal"
)
Expand Down Expand Up @@ -39,7 +42,7 @@ func (a *API) Create(ctx context.Context, subId int, region CreateRegion) (int,
var task internal.TaskResponse
err := a.client.Post(ctx, "create subscription region", fmt.Sprintf("/subscriptions/%d/regions", subId), region, &task)
if err != nil {
return 0, internal.Wrap404Error(subId, "subscription", err)
return 0, wrap404Error(subId, err)
}

a.logger.Printf("Waiting for task %s to finish creating the subscription region", task)
Expand All @@ -57,7 +60,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, internal.Wrap404Error(subId, "subscription", err)
return nil, wrap404Error(subId, err)
}

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

a.logger.Printf("Waiting for region %d to finish being deleted", id)
Expand All @@ -79,3 +82,10 @@ func (a *API) DeleteWithQuery(ctx context.Context, id int, regions DeleteRegions

return nil
}

func wrap404Error(id int, err error) error {
if v, ok := err.(*internal.HTTPError); ok && v.StatusCode == http.StatusNotFound {
return &subscriptions.NotFound{ID: id}
}
return err
}
10 changes: 10 additions & 0 deletions service/subscriptions/model.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package subscriptions

import (
"fmt"

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

Expand Down Expand Up @@ -273,6 +275,14 @@ type listSubscriptionResponse struct {
Subscriptions []*Subscription `json:"subscriptions"`
}

type NotFound struct {
ID int
}

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

const (
// Active value of the `Status` field in `Subscription`
SubscriptionStatusActive = "active"
Expand Down
Loading

0 comments on commit 2b52f8b

Please sign in to comment.