Skip to content

Commit

Permalink
feat!: add aws-sdk-go-v2 support (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
nabeken authored Apr 20, 2024
2 parents b55b7c2 + 926988c commit 483cf34
Show file tree
Hide file tree
Showing 12 changed files with 271 additions and 192 deletions.
21 changes: 11 additions & 10 deletions cache.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package psadm

import (
"context"
"fmt"
"time"

Expand All @@ -15,14 +16,14 @@ type CachedClient struct {

var _ client = &CachedClient{}

func (c *CachedClient) GetParameterWithDescription(key string) (*Parameter, error) {
func (c *CachedClient) GetParameterWithDescription(ctx context.Context, key string) (*Parameter, error) {
ck := buildCacheKey("GetParameterWithDescription", key)

if v, found := c.cache.Get(ck); found {
return v.(*Parameter), nil
}

param, err := c.client.GetParameterWithDescription(key)
param, err := c.client.GetParameterWithDescription(ctx, key)
if err != nil {
return nil, err
}
Expand All @@ -31,14 +32,14 @@ func (c *CachedClient) GetParameterWithDescription(key string) (*Parameter, erro
return param, nil
}

func (c *CachedClient) GetParameter(key string) (string, error) {
func (c *CachedClient) GetParameter(ctx context.Context, key string) (string, error) {
ck := buildCacheKey("GetParameter", key)

if v, found := c.cache.Get(ck); found {
return v.(string), nil
}

param, err := c.client.GetParameter(key)
param, err := c.client.GetParameter(ctx, key)
if err != nil {
return "", err
}
Expand All @@ -47,14 +48,14 @@ func (c *CachedClient) GetParameter(key string) (string, error) {
return param, nil
}

func (c *CachedClient) GetParameterByTime(key string, at time.Time) (*Parameter, error) {
func (c *CachedClient) GetParameterByTime(ctx context.Context, key string, at time.Time) (*Parameter, error) {
ck := fmt.Sprintf("%s/%s/%d", "GetParameterByTime", key, at.Unix())

if v, found := c.cache.Get(ck); found {
return v.(*Parameter), nil
}

param, err := c.client.GetParameterByTime(key, at)
param, err := c.client.GetParameterByTime(ctx, key, at)
if err != nil {
return nil, err
}
Expand All @@ -63,14 +64,14 @@ func (c *CachedClient) GetParameterByTime(key string, at time.Time) (*Parameter,
return param, nil
}

func (c *CachedClient) GetParametersByPath(pathPrefix string) ([]*Parameter, error) {
func (c *CachedClient) GetParametersByPath(ctx context.Context, pathPrefix string) ([]*Parameter, error) {
ck := buildCacheKey("GetParametersByPath", pathPrefix)

if v, found := c.cache.Get(ck); found {
return v.([]*Parameter), nil
}

params, err := c.client.GetParametersByPath(pathPrefix)
params, err := c.client.GetParametersByPath(ctx, pathPrefix)
if err != nil {
return nil, err
}
Expand All @@ -80,8 +81,8 @@ func (c *CachedClient) GetParametersByPath(pathPrefix string) ([]*Parameter, err
}

// PutParameter forwards a call to the underlying client. It doesn't do any caching.
func (c *CachedClient) PutParameter(p *Parameter, overrite bool) error {
return c.client.PutParameter(p, overrite)
func (c *CachedClient) PutParameter(ctx context.Context, p *Parameter, overwrite bool) error {
return c.client.PutParameter(ctx, p, overwrite)
}

func buildCacheKey(prefix, key string) string {
Expand Down
14 changes: 8 additions & 6 deletions cache_test.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package psadm

import (
"context"
"testing"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ssm"
gomock "github.com/golang/mock/gomock"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ssm"
"github.com/aws/aws-sdk-go-v2/service/ssm/types"
"github.com/patrickmn/go-cache"
"github.com/stretchr/testify/assert"
gomock "go.uber.org/mock/gomock"
)

func TestCachedClient(t *testing.T) {
Expand All @@ -18,20 +20,20 @@ func TestCachedClient(t *testing.T) {
t.Run("GetParameter", func(t *testing.T) {
mockSSM := NewMockssmClient(mockctrl)
mockSSM.EXPECT().
GetParameter(&ssm.GetParameterInput{
GetParameter(gomock.Any(), &ssm.GetParameterInput{
Name: aws.String("key/1/2/3"),
WithDecryption: aws.Bool(true),
}).
Return(&ssm.GetParameterOutput{
Parameter: &ssm.Parameter{
Parameter: &types.Parameter{
Value: aws.String("value"),
},
}, nil)

c := cache.New(time.Minute, 10*time.Minute)
client := (&Client{SSM: mockSSM}).CachedClient(c)

v, err := client.GetParameter("key/1/2/3")
v, err := client.GetParameter(context.TODO(), "key/1/2/3")
assert.Equal("value", v)
assert.NoError(err)

Expand Down
Loading

0 comments on commit 483cf34

Please sign in to comment.