Merge pull request #581 from cloudskiff/feat/cacheLambdarepo

Implement cache in AWS lambda repository
main
Elie 2021-06-09 16:26:32 +02:00 committed by GitHub
commit 2785f54a6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 46 additions and 7 deletions

View File

@ -39,7 +39,7 @@ func Init(version string, alerter *alerter.Alerter,
s3Repository := repository.NewS3Repository(client.NewAWSClientFactory(provider.session))
ec2repository := repository.NewEC2Repository(provider.session, repositoryCache)
route53repository := repository.NewRoute53Repository(provider.session)
lambdaRepository := repository.NewLambdaRepository(provider.session)
lambdaRepository := repository.NewLambdaRepository(provider.session, repositoryCache)
rdsRepository := repository.NewRDSRepository(provider.session, repositoryCache)
sqsRepository := repository.NewSQSClient(provider.session, repositoryCache)
snsRepository := repository.NewSNSClient(provider.session)

View File

@ -9,6 +9,7 @@ import (
"github.com/aws/aws-sdk-go/service/lambda"
"github.com/cloudskiff/driftctl/pkg/parallel"
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
"github.com/cloudskiff/driftctl/pkg/remote/cache"
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
"github.com/cloudskiff/driftctl/pkg/resource"
resourceaws "github.com/cloudskiff/driftctl/pkg/resource/aws"
@ -88,7 +89,7 @@ func TestEventLambdaSourceMappingSupplier_Resources(t *testing.T) {
if err != nil {
t.Fatal(err)
}
supplierLibrary.AddSupplier(NewLambdaEventSourceMappingSupplier(provider, deserializer, repository.NewLambdaRepository(provider.session)))
supplierLibrary.AddSupplier(NewLambdaEventSourceMappingSupplier(provider, deserializer, repository.NewLambdaRepository(provider.session, cache.New(0))))
}
t.Run(tt.test, func(t *testing.T) {

View File

@ -5,6 +5,7 @@ import (
"testing"
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
"github.com/cloudskiff/driftctl/pkg/remote/cache"
testresource "github.com/cloudskiff/driftctl/test/resource"
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
@ -94,7 +95,7 @@ func TestLambdaFunctionSupplier_Resources(t *testing.T) {
if err != nil {
t.Fatal(err)
}
supplierLibrary.AddSupplier(NewLambdaFunctionSupplier(provider, deserializer, repository.NewLambdaRepository(provider.session)))
supplierLibrary.AddSupplier(NewLambdaFunctionSupplier(provider, deserializer, repository.NewLambdaRepository(provider.session, cache.New(0))))
}
t.Run(tt.test, func(t *testing.T) {

View File

@ -4,6 +4,7 @@ import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/lambda"
"github.com/aws/aws-sdk-go/service/lambda/lambdaiface"
"github.com/cloudskiff/driftctl/pkg/remote/cache"
)
type LambdaClient interface {
@ -17,15 +18,21 @@ type LambdaRepository interface {
type lambdaRepository struct {
client lambdaiface.LambdaAPI
cache cache.Cache
}
func NewLambdaRepository(session *session.Session) *lambdaRepository {
func NewLambdaRepository(session *session.Session, c cache.Cache) *lambdaRepository {
return &lambdaRepository{
lambda.New(session),
c,
}
}
func (r *lambdaRepository) ListAllLambdaFunctions() ([]*lambda.FunctionConfiguration, error) {
if v := r.cache.Get("lambdaListAllLambdaFunctions"); v != nil {
return v.([]*lambda.FunctionConfiguration), nil
}
var functions []*lambda.FunctionConfiguration
input := &lambda.ListFunctionsInput{}
err := r.client.ListFunctionsPages(input, func(res *lambda.ListFunctionsOutput, lastPage bool) bool {
@ -35,10 +42,16 @@ func (r *lambdaRepository) ListAllLambdaFunctions() ([]*lambda.FunctionConfigura
if err != nil {
return nil, err
}
r.cache.Put("lambdaListAllLambdaFunctions", functions)
return functions, nil
}
func (r *lambdaRepository) ListAllLambdaEventSourceMappings() ([]*lambda.EventSourceMappingConfiguration, error) {
if v := r.cache.Get("lambdaListAllLambdaEventSourceMappings"); v != nil {
return v.([]*lambda.EventSourceMappingConfiguration), nil
}
var eventSourceMappingConfigurations []*lambda.EventSourceMappingConfiguration
input := &lambda.ListEventSourceMappingsInput{}
err := r.client.ListEventSourceMappingsPages(input, func(res *lambda.ListEventSourceMappingsOutput, lastPage bool) bool {
@ -48,5 +61,7 @@ func (r *lambdaRepository) ListAllLambdaEventSourceMappings() ([]*lambda.EventSo
if err != nil {
return nil, err
}
r.cache.Put("lambdaListAllLambdaEventSourceMappings", eventSourceMappingConfigurations)
return eventSourceMappingConfigurations, nil
}

View File

@ -5,7 +5,7 @@ import (
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/cloudskiff/driftctl/pkg/remote/cache"
"github.com/stretchr/testify/mock"
"github.com/aws/aws-sdk-go/service/lambda"
@ -43,7 +43,7 @@ func Test_lambdaRepository_ListAllLambdaFunctions(t *testing.T) {
},
}, true)
return true
})).Return(nil)
})).Return(nil).Once()
},
want: []*lambda.FunctionConfiguration{
{FunctionName: aws.String("1")},
@ -60,13 +60,24 @@ func Test_lambdaRepository_ListAllLambdaFunctions(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
store := cache.New(1)
client := &MockLambdaClient{}
tt.mocks(client)
r := &lambdaRepository{
client: client,
cache: store,
}
got, err := r.ListAllLambdaFunctions()
assert.Equal(t, tt.wantErr, err)
if err == nil {
// Check that results were cached
cachedData, err := r.ListAllLambdaFunctions()
assert.NoError(t, err)
assert.Equal(t, got, cachedData)
assert.IsType(t, []*lambda.FunctionConfiguration{}, store.Get("lambdaListAllLambdaFunctions"))
}
changelog, err := diff.Diff(got, tt.want)
assert.Nil(t, err)
if len(changelog) > 0 {
@ -109,7 +120,7 @@ func Test_lambdaRepository_ListAllLambdaEventSourceMappings(t *testing.T) {
},
}, true)
return true
})).Return(nil)
})).Return(nil).Once()
},
want: []*lambda.EventSourceMappingConfiguration{
{UUID: aws.String("1")},
@ -126,13 +137,24 @@ func Test_lambdaRepository_ListAllLambdaEventSourceMappings(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
store := cache.New(1)
client := &MockLambdaClient{}
tt.mocks(client)
r := &lambdaRepository{
client: client,
cache: store,
}
got, err := r.ListAllLambdaEventSourceMappings()
assert.Equal(t, tt.wantErr, err)
if err == nil {
// Check that results were cached
cachedData, err := r.ListAllLambdaEventSourceMappings()
assert.NoError(t, err)
assert.Equal(t, got, cachedData)
assert.IsType(t, []*lambda.EventSourceMappingConfiguration{}, store.Get("lambdaListAllLambdaEventSourceMappings"))
}
changelog, err := diff.Diff(got, tt.want)
assert.Nil(t, err)
if len(changelog) > 0 {