Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tags #179

Merged
merged 1 commit into from
Aug 28, 2024
Merged

Tags #179

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
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
3 changes: 3 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/RedisLabs/rediscloud-go-api/service/pricing"
"github.com/RedisLabs/rediscloud-go-api/service/regions"
"github.com/RedisLabs/rediscloud-go-api/service/subscriptions"
"github.com/RedisLabs/rediscloud-go-api/service/tags"
"github.com/RedisLabs/rediscloud-go-api/service/transit_gateway/attachments"
)

Expand All @@ -42,6 +43,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 +91,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