Skip to content

Commit

Permalink
Added tags to the client and testing
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnSharpe committed Aug 28, 2024
1 parent 81b4873 commit 3e908d3
Show file tree
Hide file tree
Showing 6 changed files with 519 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ Sessionx.vim
# Temporary
.netrwhist
# Auto-generated tag files
tags
# tags
# Persistent undo
[._]*.un~

Expand Down
4 changes: 4 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"regexp"
"strings"

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

"github.com/RedisLabs/rediscloud-go-api/service/access_control_lists/redis_rules"
"github.com/RedisLabs/rediscloud-go-api/service/access_control_lists/roles"
"github.com/RedisLabs/rediscloud-go-api/service/access_control_lists/users"
Expand Down Expand Up @@ -42,6 +44,7 @@ type Client struct {
Maintenance *maintenance.API
Pricing *pricing.API
TransitGatewayAttachments *attachments.API
Tags *tags.API
// fixed
FixedPlans *plans.API
FixedSubscriptions *fixedSubscriptions.API
Expand Down Expand Up @@ -89,6 +92,7 @@ func NewClient(configs ...Option) (*Client, error) {
Maintenance: maintenance.NewAPI(client, t, config.logger),
Pricing: pricing.NewAPI(client),
TransitGatewayAttachments: attachments.NewAPI(client, t, config.logger),
Tags: tags.NewAPI(client),
// fixed
FixedPlans: plans.NewAPI(client, config.logger),
FixedPlanSubscriptions: plan_subscriptions.NewAPI(client, config.logger),
Expand Down
1 change: 0 additions & 1 deletion service/regions/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ type Log interface {
type HttpClient interface {
Get(ctx context.Context, name, path string, responseBody interface{}) error
Post(ctx context.Context, name, path string, requestBody interface{}, responseBody interface{}) error
Put(ctx context.Context, name, path string, requestBody interface{}, responseBody interface{}) error
DeleteWithQuery(ctx context.Context, name, path string, requestBody interface{}, responseBody interface{}) error
}

Expand Down
21 changes: 21 additions & 0 deletions service/tags/model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package tags

import "fmt"

type AllTags struct {
Tags *[]*Tag `json:"tags,omitempty"`
}

type Tag struct {
Key *string `json:"key,omitempty"`
Value *string `json:"value,omitempty"`
}

type NotFound struct {
subId int
dbId int
}

func (f *NotFound) Error() string {
return fmt.Sprintf("database %d in subscription %d not found", f.dbId, f.subId)
}
83 changes: 83 additions & 0 deletions service/tags/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package tags

import (
"context"
"fmt"
"net/http"

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

type HttpClient interface {
Get(ctx context.Context, name, path string, responseBody interface{}) error
Put(ctx context.Context, name, path string, requestBody interface{}, responseBody interface{}) error
}

type API struct {
client HttpClient
}

func NewAPI(client HttpClient) *API {
return &API{client: client}
}

func (a *API) Get(ctx context.Context, subscription int, database int) (*AllTags, error) {
message := fmt.Sprintf("get tags for database %d in subscription %d", subscription, database)
address := fmt.Sprintf("/subscriptions/%d/databases/%d/tags", subscription, database)
tags, err := a.get(ctx, message, address)
if err != nil {
return nil, wrap404Error(subscription, database, err)
}
return tags, nil
}

func (a *API) GetFixed(ctx context.Context, subscription int, database int) (*AllTags, error) {
message := fmt.Sprintf("get tags for fixed database %d in subscription %d", subscription, database)
address := fmt.Sprintf("fixed/subscriptions/%d/databases/%d/tags", subscription, database)
tags, err := a.get(ctx, message, address)
if err != nil {
return nil, wrap404Error(subscription, database, err)
}
return tags, nil
}

func (a *API) Put(ctx context.Context, subscription int, database int, tags AllTags) error {
message := fmt.Sprintf("update tags for database %d in subscription %d", subscription, database)
address := fmt.Sprintf("/subscriptions/%d/databases/%d/tags", subscription, database)
err := a.put(ctx, message, address, tags)
if err != nil {
return wrap404Error(subscription, database, err)
}
return nil
}

func (a *API) PutFixed(ctx context.Context, subscription int, database int, tags AllTags) error {
message := fmt.Sprintf("update tags for fixed database %d in subscription %d", subscription, database)
address := fmt.Sprintf("fixed/subscriptions/%d/databases/%d/tags", subscription, database)
err := a.put(ctx, message, address, tags)
if err != nil {
return wrap404Error(subscription, database, err)
}
return nil
}

func (a *API) get(ctx context.Context, message string, address string) (*AllTags, error) {
var tags AllTags
err := a.client.Get(ctx, message, address, &tags)
if err != nil {
return nil, err
}
return &tags, nil
}

func (a *API) put(ctx context.Context, message string, address string, tags AllTags) error {
var tagsResponse AllTags
return a.client.Put(ctx, message, address, tags, &tagsResponse)
}

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

0 comments on commit 3e908d3

Please sign in to comment.