Merge branch 'main' into feat/cacheLambdarepo
commit
7a12dd869f
|
@ -6,6 +6,7 @@ import (
|
|||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/cache"
|
||||
testresource "github.com/cloudskiff/driftctl/test/resource"
|
||||
|
||||
"github.com/aws/aws-sdk-go/service/cloudfront"
|
||||
|
@ -76,7 +77,7 @@ func TestCloudfrontDistributionSupplier_Resources(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
supplierLibrary.AddSupplier(NewCloudfrontDistributionSupplier(provider, deserializer, repository.NewCloudfrontClient(provider.session)))
|
||||
supplierLibrary.AddSupplier(NewCloudfrontDistributionSupplier(provider, deserializer, repository.NewCloudfrontClient(provider.session, cache.New(0))))
|
||||
}
|
||||
|
||||
t.Run(c.test, func(tt *testing.T) {
|
||||
|
|
|
@ -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"
|
||||
|
@ -112,7 +113,7 @@ func TestDBInstanceSupplier_Resources(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
supplierLibrary.AddSupplier(NewDBInstanceSupplier(provider, deserializer, repository.NewRDSRepository(provider.session)))
|
||||
supplierLibrary.AddSupplier(NewDBInstanceSupplier(provider, deserializer, repository.NewRDSRepository(provider.session, cache.New(0))))
|
||||
}
|
||||
|
||||
t.Run(tt.test, func(t *testing.T) {
|
||||
|
|
|
@ -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"
|
||||
|
@ -85,7 +86,7 @@ func TestDBSubnetGroupSupplier_Resources(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
supplierLibrary.AddSupplier(NewDBInstanceSupplier(provider, deserializer, repository.NewRDSRepository(provider.session)))
|
||||
supplierLibrary.AddSupplier(NewDBInstanceSupplier(provider, deserializer, repository.NewRDSRepository(provider.session, cache.New(0))))
|
||||
}
|
||||
|
||||
t.Run(tt.test, func(t *testing.T) {
|
||||
|
|
|
@ -7,6 +7,7 @@ import (
|
|||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"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"
|
||||
|
@ -82,7 +83,7 @@ func TestDynamoDBTableSupplier_Resources(t *testing.T) {
|
|||
t.Fatal(err)
|
||||
}
|
||||
|
||||
supplierLibrary.AddSupplier(NewDynamoDBTableSupplier(provider, deserializer, repository.NewDynamoDBRepository(provider.session)))
|
||||
supplierLibrary.AddSupplier(NewDynamoDBTableSupplier(provider, deserializer, repository.NewDynamoDBRepository(provider.session, cache.New(0))))
|
||||
}
|
||||
|
||||
t.Run(c.test, func(tt *testing.T) {
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/aws/aws-sdk-go/service/ecr"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/cache"
|
||||
testresource "github.com/cloudskiff/driftctl/test/resource"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
|
@ -75,7 +76,7 @@ func TestEcrRepositorySupplier_Resources(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
supplierLibrary.AddSupplier(NewECRRepositorySupplier(provider, deserializer, repository.NewECRRepository(provider.session)))
|
||||
supplierLibrary.AddSupplier(NewECRRepositorySupplier(provider, deserializer, repository.NewECRRepository(provider.session, cache.New(0))))
|
||||
}
|
||||
|
||||
t.Run(c.test, func(tt *testing.T) {
|
||||
|
|
|
@ -40,13 +40,13 @@ func Init(version string, alerter *alerter.Alerter,
|
|||
ec2repository := repository.NewEC2Repository(provider.session, repositoryCache)
|
||||
route53repository := repository.NewRoute53Repository(provider.session)
|
||||
lambdaRepository := repository.NewLambdaRepository(provider.session, repositoryCache)
|
||||
rdsRepository := repository.NewRDSRepository(provider.session)
|
||||
sqsRepository := repository.NewSQSClient(provider.session)
|
||||
rdsRepository := repository.NewRDSRepository(provider.session, repositoryCache)
|
||||
sqsRepository := repository.NewSQSClient(provider.session, repositoryCache)
|
||||
snsRepository := repository.NewSNSClient(provider.session)
|
||||
dynamoDBRepository := repository.NewDynamoDBRepository(provider.session)
|
||||
cloudfrontRepository := repository.NewCloudfrontClient(provider.session)
|
||||
kmsRepository := repository.NewKMSRepository(provider.session)
|
||||
ecrRepository := repository.NewECRRepository(provider.session)
|
||||
cloudfrontRepository := repository.NewCloudfrontClient(provider.session, repositoryCache)
|
||||
dynamoDBRepository := repository.NewDynamoDBRepository(provider.session, repositoryCache)
|
||||
ecrRepository := repository.NewECRRepository(provider.session, repositoryCache)
|
||||
kmsRepository := repository.NewKMSRepository(provider.session, repositoryCache)
|
||||
iamRepository := repository.NewIAMRepository(provider.session, repositoryCache)
|
||||
|
||||
deserializer := resource.NewDeserializer(factory)
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/aws/aws-sdk-go/service/kms"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/cache"
|
||||
testresource "github.com/cloudskiff/driftctl/test/resource"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
|
@ -77,7 +78,7 @@ func TestKMSAliasSupplier_Resources(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
supplierLibrary.AddSupplier(NewKMSAliasSupplier(provider, deserializer, repository.NewKMSRepository(provider.session)))
|
||||
supplierLibrary.AddSupplier(NewKMSAliasSupplier(provider, deserializer, repository.NewKMSRepository(provider.session, cache.New(0))))
|
||||
}
|
||||
|
||||
t.Run(c.test, func(tt *testing.T) {
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/aws/aws-sdk-go/service/kms"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/cache"
|
||||
testresource "github.com/cloudskiff/driftctl/test/resource"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
|
@ -77,7 +78,7 @@ func TestKMSKeySupplier_Resources(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
supplierLibrary.AddSupplier(NewKMSKeySupplier(provider, deserializer, repository.NewKMSRepository(provider.session)))
|
||||
supplierLibrary.AddSupplier(NewKMSKeySupplier(provider, deserializer, repository.NewKMSRepository(provider.session, cache.New(0))))
|
||||
}
|
||||
|
||||
t.Run(c.test, func(tt *testing.T) {
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"github.com/aws/aws-sdk-go/aws/session"
|
||||
"github.com/aws/aws-sdk-go/service/cloudfront"
|
||||
"github.com/aws/aws-sdk-go/service/cloudfront/cloudfrontiface"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/cache"
|
||||
)
|
||||
|
||||
type CloudfrontRepository interface {
|
||||
|
@ -12,15 +13,21 @@ type CloudfrontRepository interface {
|
|||
|
||||
type cloudfrontRepository struct {
|
||||
client cloudfrontiface.CloudFrontAPI
|
||||
cache cache.Cache
|
||||
}
|
||||
|
||||
func NewCloudfrontClient(session *session.Session) *cloudfrontRepository {
|
||||
func NewCloudfrontClient(session *session.Session, c cache.Cache) *cloudfrontRepository {
|
||||
return &cloudfrontRepository{
|
||||
cloudfront.New(session),
|
||||
c,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *cloudfrontRepository) ListAllDistributions() ([]*cloudfront.DistributionSummary, error) {
|
||||
if v := r.cache.Get("cloudfrontListAllDistributions"); v != nil {
|
||||
return v.([]*cloudfront.DistributionSummary), nil
|
||||
}
|
||||
|
||||
var distributions []*cloudfront.DistributionSummary
|
||||
input := cloudfront.ListDistributionsInput{}
|
||||
err := r.client.ListDistributionsPages(&input,
|
||||
|
@ -34,5 +41,7 @@ func (r *cloudfrontRepository) ListAllDistributions() ([]*cloudfront.Distributio
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
r.cache.Put("cloudfrontListAllDistributions", distributions)
|
||||
return distributions, nil
|
||||
}
|
||||
|
|
|
@ -4,10 +4,10 @@ import (
|
|||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/aws/aws-sdk-go/service/cloudfront"
|
||||
awstest "github.com/cloudskiff/driftctl/test/aws"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/service/cloudfront"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/cache"
|
||||
awstest "github.com/cloudskiff/driftctl/test/aws"
|
||||
|
||||
"github.com/stretchr/testify/mock"
|
||||
|
||||
|
@ -47,7 +47,7 @@ func Test_cloudfrontRepository_ListAllDistributions(t *testing.T) {
|
|||
},
|
||||
}, true)
|
||||
return true
|
||||
})).Return(nil)
|
||||
})).Return(nil).Once()
|
||||
},
|
||||
want: []*cloudfront.DistributionSummary{
|
||||
{Id: aws.String("distribution1")},
|
||||
|
@ -61,13 +61,24 @@ func Test_cloudfrontRepository_ListAllDistributions(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
store := cache.New(1)
|
||||
client := awstest.MockFakeCloudFront{}
|
||||
tt.mocks(&client)
|
||||
r := &cloudfrontRepository{
|
||||
client: &client,
|
||||
cache: store,
|
||||
}
|
||||
got, err := r.ListAllDistributions()
|
||||
assert.Equal(t, tt.wantErr, err)
|
||||
|
||||
if err == nil {
|
||||
// Check that results were cached
|
||||
cachedData, err := r.ListAllDistributions()
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, got, cachedData)
|
||||
assert.IsType(t, []*cloudfront.DistributionSummary{}, store.Get("cloudfrontListAllDistributions"))
|
||||
}
|
||||
|
||||
changelog, err := diff.Diff(got, tt.want)
|
||||
assert.Nil(t, err)
|
||||
if len(changelog) > 0 {
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"github.com/aws/aws-sdk-go/aws/session"
|
||||
"github.com/aws/aws-sdk-go/service/dynamodb"
|
||||
"github.com/aws/aws-sdk-go/service/dynamodb/dynamodbiface"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/cache"
|
||||
)
|
||||
|
||||
type DynamoDBRepository interface {
|
||||
|
@ -12,15 +13,21 @@ type DynamoDBRepository interface {
|
|||
|
||||
type dynamoDBRepository struct {
|
||||
client dynamodbiface.DynamoDBAPI
|
||||
cache cache.Cache
|
||||
}
|
||||
|
||||
func NewDynamoDBRepository(session *session.Session) *dynamoDBRepository {
|
||||
func NewDynamoDBRepository(session *session.Session, c cache.Cache) *dynamoDBRepository {
|
||||
return &dynamoDBRepository{
|
||||
dynamodb.New(session),
|
||||
c,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *dynamoDBRepository) ListAllTables() ([]*string, error) {
|
||||
if v := r.cache.Get("dynamodbListAllTables"); v != nil {
|
||||
return v.([]*string), nil
|
||||
}
|
||||
|
||||
var tables []*string
|
||||
input := &dynamodb.ListTablesInput{}
|
||||
err := r.client.ListTablesPages(input, func(res *dynamodb.ListTablesOutput, lastPage bool) bool {
|
||||
|
@ -30,5 +37,7 @@ func (r *dynamoDBRepository) ListAllTables() ([]*string, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
r.cache.Put("dynamodbListAllTables", tables)
|
||||
return tables, nil
|
||||
}
|
||||
|
|
|
@ -4,10 +4,10 @@ import (
|
|||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/aws/aws-sdk-go/service/dynamodb"
|
||||
awstest "github.com/cloudskiff/driftctl/test/aws"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/service/dynamodb"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/cache"
|
||||
awstest "github.com/cloudskiff/driftctl/test/aws"
|
||||
|
||||
"github.com/stretchr/testify/mock"
|
||||
|
||||
|
@ -44,7 +44,7 @@ func Test_dynamoDBRepository_ListAllTopics(t *testing.T) {
|
|||
},
|
||||
}, true)
|
||||
return true
|
||||
})).Return(nil)
|
||||
})).Return(nil).Once()
|
||||
},
|
||||
want: []*string{
|
||||
aws.String("1"),
|
||||
|
@ -58,13 +58,24 @@ func Test_dynamoDBRepository_ListAllTopics(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
store := cache.New(1)
|
||||
client := awstest.MockFakeDynamoDB{}
|
||||
tt.mocks(&client)
|
||||
r := &dynamoDBRepository{
|
||||
client: &client,
|
||||
cache: store,
|
||||
}
|
||||
got, err := r.ListAllTables()
|
||||
assert.Equal(t, tt.wantErr, err)
|
||||
|
||||
if err == nil {
|
||||
// Check that results were cached
|
||||
cachedData, err := r.ListAllTables()
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, got, cachedData)
|
||||
assert.IsType(t, []*string{}, store.Get("dynamodbListAllTables"))
|
||||
}
|
||||
|
||||
changelog, err := diff.Diff(got, tt.want)
|
||||
assert.Nil(t, err)
|
||||
if len(changelog) > 0 {
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"github.com/aws/aws-sdk-go/aws/session"
|
||||
"github.com/aws/aws-sdk-go/service/ecr"
|
||||
"github.com/aws/aws-sdk-go/service/ecr/ecriface"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/cache"
|
||||
)
|
||||
|
||||
type ECRRepository interface {
|
||||
|
@ -12,15 +13,21 @@ type ECRRepository interface {
|
|||
|
||||
type ecrRepository struct {
|
||||
client ecriface.ECRAPI
|
||||
cache cache.Cache
|
||||
}
|
||||
|
||||
func NewECRRepository(session *session.Session) *ecrRepository {
|
||||
func NewECRRepository(session *session.Session, c cache.Cache) *ecrRepository {
|
||||
return &ecrRepository{
|
||||
ecr.New(session),
|
||||
c,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *ecrRepository) ListAllRepositories() ([]*ecr.Repository, error) {
|
||||
if v := r.cache.Get("ecrListAllRepositories"); v != nil {
|
||||
return v.([]*ecr.Repository), nil
|
||||
}
|
||||
|
||||
var repositories []*ecr.Repository
|
||||
input := &ecr.DescribeRepositoriesInput{}
|
||||
err := r.client.DescribeRepositoriesPages(input, func(res *ecr.DescribeRepositoriesOutput, lastPage bool) bool {
|
||||
|
@ -30,5 +37,7 @@ func (r *ecrRepository) ListAllRepositories() ([]*ecr.Repository, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
r.cache.Put("ecrListAllRepositories", repositories)
|
||||
return repositories, nil
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/aws/aws-sdk-go/service/ecr"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/cache"
|
||||
awstest "github.com/cloudskiff/driftctl/test/aws"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
|
@ -44,7 +45,7 @@ func Test_ecrRepository_ListAllRepositories(t *testing.T) {
|
|||
},
|
||||
}, true)
|
||||
return true
|
||||
})).Return(nil)
|
||||
})).Return(nil).Once()
|
||||
},
|
||||
want: []*ecr.Repository{
|
||||
{RepositoryName: aws.String("1")},
|
||||
|
@ -58,13 +59,24 @@ func Test_ecrRepository_ListAllRepositories(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
store := cache.New(1)
|
||||
client := awstest.MockFakeECR{}
|
||||
tt.mocks(&client)
|
||||
r := &ecrRepository{
|
||||
client: &client,
|
||||
cache: store,
|
||||
}
|
||||
got, err := r.ListAllRepositories()
|
||||
assert.Equal(t, tt.wantErr, err)
|
||||
|
||||
if err == nil {
|
||||
// Check that results were cached
|
||||
cachedData, err := r.ListAllRepositories()
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, got, cachedData)
|
||||
assert.IsType(t, []*ecr.Repository{}, store.Get("ecrListAllRepositories"))
|
||||
}
|
||||
|
||||
changelog, err := diff.Diff(got, tt.want)
|
||||
assert.Nil(t, err)
|
||||
if len(changelog) > 0 {
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
"github.com/aws/aws-sdk-go/aws/session"
|
||||
"github.com/aws/aws-sdk-go/service/kms"
|
||||
"github.com/aws/aws-sdk-go/service/kms/kmsiface"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/cache"
|
||||
)
|
||||
|
||||
type KMSRepository interface {
|
||||
|
@ -15,15 +16,21 @@ type KMSRepository interface {
|
|||
|
||||
type kmsRepository struct {
|
||||
client kmsiface.KMSAPI
|
||||
cache cache.Cache
|
||||
}
|
||||
|
||||
func NewKMSRepository(session *session.Session) *kmsRepository {
|
||||
func NewKMSRepository(session *session.Session, c cache.Cache) *kmsRepository {
|
||||
return &kmsRepository{
|
||||
kms.New(session),
|
||||
c,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *kmsRepository) ListAllKeys() ([]*kms.KeyListEntry, error) {
|
||||
if v := r.cache.Get("kmsListAllKeys"); v != nil {
|
||||
return v.([]*kms.KeyListEntry), nil
|
||||
}
|
||||
|
||||
var keys []*kms.KeyListEntry
|
||||
input := kms.ListKeysInput{}
|
||||
err := r.client.ListKeysPages(&input,
|
||||
|
@ -39,10 +46,16 @@ func (r *kmsRepository) ListAllKeys() ([]*kms.KeyListEntry, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
r.cache.Put("kmsListAllKeys", customerKeys)
|
||||
return customerKeys, nil
|
||||
}
|
||||
|
||||
func (r *kmsRepository) ListAllAliases() ([]*kms.AliasListEntry, error) {
|
||||
if v := r.cache.Get("kmsListAllAliases"); v != nil {
|
||||
return v.([]*kms.AliasListEntry), nil
|
||||
}
|
||||
|
||||
var aliases []*kms.AliasListEntry
|
||||
input := kms.ListAliasesInput{}
|
||||
err := r.client.ListAliasesPages(&input,
|
||||
|
@ -54,7 +67,10 @@ func (r *kmsRepository) ListAllAliases() ([]*kms.AliasListEntry, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return r.filterAliases(aliases), nil
|
||||
|
||||
result := r.filterAliases(aliases)
|
||||
r.cache.Put("kmsListAllAliases", result)
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (r *kmsRepository) filterKeys(keys []*kms.KeyListEntry) ([]*kms.KeyListEntry, error) {
|
||||
|
|
|
@ -4,11 +4,10 @@ import (
|
|||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/aws/aws-sdk-go/service/kms"
|
||||
awstest "github.com/cloudskiff/driftctl/test/aws"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
|
||||
"github.com/aws/aws-sdk-go/service/kms"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/cache"
|
||||
awstest "github.com/cloudskiff/driftctl/test/aws"
|
||||
"github.com/stretchr/testify/mock"
|
||||
|
||||
"github.com/r3labs/diff/v2"
|
||||
|
@ -36,7 +35,7 @@ func Test_KMSRepository_ListAllKeys(t *testing.T) {
|
|||
},
|
||||
}, true)
|
||||
return true
|
||||
})).Return(nil)
|
||||
})).Return(nil).Once()
|
||||
client.On("DescribeKey",
|
||||
&kms.DescribeKeyInput{
|
||||
KeyId: aws.String("1"),
|
||||
|
@ -45,7 +44,7 @@ func Test_KMSRepository_ListAllKeys(t *testing.T) {
|
|||
KeyId: aws.String("1"),
|
||||
KeyManager: aws.String("CUSTOMER"),
|
||||
},
|
||||
}, nil)
|
||||
}, nil).Once()
|
||||
client.On("DescribeKey",
|
||||
&kms.DescribeKeyInput{
|
||||
KeyId: aws.String("2"),
|
||||
|
@ -54,7 +53,7 @@ func Test_KMSRepository_ListAllKeys(t *testing.T) {
|
|||
KeyId: aws.String("2"),
|
||||
KeyManager: aws.String("AWS"),
|
||||
},
|
||||
}, nil)
|
||||
}, nil).Once()
|
||||
client.On("DescribeKey",
|
||||
&kms.DescribeKeyInput{
|
||||
KeyId: aws.String("3"),
|
||||
|
@ -63,7 +62,7 @@ func Test_KMSRepository_ListAllKeys(t *testing.T) {
|
|||
KeyId: aws.String("3"),
|
||||
KeyManager: aws.String("AWS"),
|
||||
},
|
||||
}, nil)
|
||||
}, nil).Once()
|
||||
},
|
||||
want: []*kms.KeyListEntry{
|
||||
{KeyId: aws.String("1")},
|
||||
|
@ -72,13 +71,24 @@ func Test_KMSRepository_ListAllKeys(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
store := cache.New(1)
|
||||
client := awstest.MockFakeKMS{}
|
||||
tt.mocks(&client)
|
||||
r := &kmsRepository{
|
||||
client: &client,
|
||||
cache: store,
|
||||
}
|
||||
got, err := r.ListAllKeys()
|
||||
assert.Equal(t, tt.wantErr, err)
|
||||
|
||||
if err == nil {
|
||||
// Check that results were cached
|
||||
cachedData, err := r.ListAllKeys()
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, got, cachedData)
|
||||
assert.IsType(t, []*kms.KeyListEntry{}, store.Get("kmsListAllKeys"))
|
||||
}
|
||||
|
||||
changelog, err := diff.Diff(got, tt.want)
|
||||
assert.Nil(t, err)
|
||||
if len(changelog) > 0 {
|
||||
|
@ -116,7 +126,7 @@ func Test_KMSRepository_ListAllAliases(t *testing.T) {
|
|||
},
|
||||
}, true)
|
||||
return true
|
||||
})).Return(nil)
|
||||
})).Return(nil).Once()
|
||||
},
|
||||
want: []*kms.AliasListEntry{
|
||||
{AliasName: aws.String("alias/1")},
|
||||
|
@ -129,13 +139,24 @@ func Test_KMSRepository_ListAllAliases(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
store := cache.New(1)
|
||||
client := awstest.MockFakeKMS{}
|
||||
tt.mocks(&client)
|
||||
r := &kmsRepository{
|
||||
client: &client,
|
||||
cache: store,
|
||||
}
|
||||
got, err := r.ListAllAliases()
|
||||
assert.Equal(t, tt.wantErr, err)
|
||||
|
||||
if err == nil {
|
||||
// Check that results were cached
|
||||
cachedData, err := r.ListAllAliases()
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, got, cachedData)
|
||||
assert.IsType(t, []*kms.AliasListEntry{}, store.Get("kmsListAllAliases"))
|
||||
}
|
||||
|
||||
changelog, err := diff.Diff(got, tt.want)
|
||||
assert.Nil(t, err)
|
||||
if len(changelog) > 0 {
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"github.com/aws/aws-sdk-go/aws/session"
|
||||
"github.com/aws/aws-sdk-go/service/rds"
|
||||
"github.com/aws/aws-sdk-go/service/rds/rdsiface"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/cache"
|
||||
)
|
||||
|
||||
type RDSClient interface {
|
||||
|
@ -17,15 +18,21 @@ type RDSRepository interface {
|
|||
|
||||
type rdsRepository struct {
|
||||
client rdsiface.RDSAPI
|
||||
cache cache.Cache
|
||||
}
|
||||
|
||||
func NewRDSRepository(session *session.Session) *rdsRepository {
|
||||
func NewRDSRepository(session *session.Session, c cache.Cache) *rdsRepository {
|
||||
return &rdsRepository{
|
||||
rds.New(session),
|
||||
c,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *rdsRepository) ListAllDBInstances() ([]*rds.DBInstance, error) {
|
||||
if v := r.cache.Get("rdsListAllDBInstances"); v != nil {
|
||||
return v.([]*rds.DBInstance), nil
|
||||
}
|
||||
|
||||
var result []*rds.DBInstance
|
||||
input := &rds.DescribeDBInstancesInput{}
|
||||
err := r.client.DescribeDBInstancesPages(input, func(res *rds.DescribeDBInstancesOutput, lastPage bool) bool {
|
||||
|
@ -35,10 +42,16 @@ func (r *rdsRepository) ListAllDBInstances() ([]*rds.DBInstance, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
r.cache.Put("rdsListAllDBInstances", result)
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (r *rdsRepository) ListAllDbSubnetGroups() ([]*rds.DBSubnetGroup, error) {
|
||||
if v := r.cache.Get("rdsListAllDbSubnetGroups"); v != nil {
|
||||
return v.([]*rds.DBSubnetGroup), nil
|
||||
}
|
||||
|
||||
var subnetGroups []*rds.DBSubnetGroup
|
||||
input := rds.DescribeDBSubnetGroupsInput{}
|
||||
err := r.client.DescribeDBSubnetGroupsPages(&input,
|
||||
|
@ -47,5 +60,7 @@ func (r *rdsRepository) ListAllDbSubnetGroups() ([]*rds.DBSubnetGroup, error) {
|
|||
return !lastPage
|
||||
},
|
||||
)
|
||||
|
||||
r.cache.Put("rdsListAllDbSubnetGroups", subnetGroups)
|
||||
return subnetGroups, err
|
||||
}
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/aws/aws-sdk-go/service/rds"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/cache"
|
||||
"github.com/r3labs/diff/v2"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/mock"
|
||||
|
@ -39,7 +40,7 @@ func Test_rdsRepository_ListAllDBInstances(t *testing.T) {
|
|||
},
|
||||
}, true)
|
||||
return true
|
||||
})).Return(nil)
|
||||
})).Return(nil).Once()
|
||||
},
|
||||
want: []*rds.DBInstance{
|
||||
{DBInstanceIdentifier: aws.String("1")},
|
||||
|
@ -53,13 +54,24 @@ func Test_rdsRepository_ListAllDBInstances(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
store := cache.New(1)
|
||||
client := &MockRDSClient{}
|
||||
tt.mocks(client)
|
||||
r := &rdsRepository{
|
||||
client: client,
|
||||
cache: store,
|
||||
}
|
||||
got, err := r.ListAllDBInstances()
|
||||
assert.Equal(t, tt.wantErr, err)
|
||||
|
||||
if err == nil {
|
||||
// Check that results were cached
|
||||
cachedData, err := r.ListAllDBInstances()
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, got, cachedData)
|
||||
assert.IsType(t, []*rds.DBInstance{}, store.Get("rdsListAllDBInstances"))
|
||||
}
|
||||
|
||||
changelog, err := diff.Diff(got, tt.want)
|
||||
assert.Nil(t, err)
|
||||
if len(changelog) > 0 {
|
||||
|
@ -100,7 +112,7 @@ func Test_rdsRepository_ListAllDbSubnetGroups(t *testing.T) {
|
|||
},
|
||||
}, true)
|
||||
return true
|
||||
})).Return(nil)
|
||||
})).Return(nil).Once()
|
||||
},
|
||||
want: []*rds.DBSubnetGroup{
|
||||
{DBSubnetGroupName: aws.String("1")},
|
||||
|
@ -114,13 +126,24 @@ func Test_rdsRepository_ListAllDbSubnetGroups(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
store := cache.New(1)
|
||||
client := &MockRDSClient{}
|
||||
tt.mocks(client)
|
||||
r := &rdsRepository{
|
||||
client: client,
|
||||
cache: store,
|
||||
}
|
||||
got, err := r.ListAllDbSubnetGroups()
|
||||
assert.Equal(t, tt.wantErr, err)
|
||||
|
||||
if err == nil {
|
||||
// Check that results were cached
|
||||
cachedData, err := r.ListAllDbSubnetGroups()
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, got, cachedData)
|
||||
assert.IsType(t, []*rds.DBSubnetGroup{}, store.Get("rdsListAllDbSubnetGroups"))
|
||||
}
|
||||
|
||||
changelog, err := diff.Diff(got, tt.want)
|
||||
assert.Nil(t, err)
|
||||
if len(changelog) > 0 {
|
||||
|
|
|
@ -4,6 +4,7 @@ import (
|
|||
"github.com/aws/aws-sdk-go/aws/session"
|
||||
"github.com/aws/aws-sdk-go/service/sqs"
|
||||
"github.com/aws/aws-sdk-go/service/sqs/sqsiface"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/cache"
|
||||
)
|
||||
|
||||
type SQSRepository interface {
|
||||
|
@ -12,15 +13,21 @@ type SQSRepository interface {
|
|||
|
||||
type sqsRepository struct {
|
||||
client sqsiface.SQSAPI
|
||||
cache cache.Cache
|
||||
}
|
||||
|
||||
func NewSQSClient(session *session.Session) *sqsRepository {
|
||||
func NewSQSClient(session *session.Session, c cache.Cache) *sqsRepository {
|
||||
return &sqsRepository{
|
||||
sqs.New(session),
|
||||
c,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *sqsRepository) ListAllQueues() ([]*string, error) {
|
||||
if v := r.cache.Get("sqsListAllQueues"); v != nil {
|
||||
return v.([]*string), nil
|
||||
}
|
||||
|
||||
var queues []*string
|
||||
input := sqs.ListQueuesInput{}
|
||||
err := r.client.ListQueuesPages(&input,
|
||||
|
@ -32,5 +39,7 @@ func (r *sqsRepository) ListAllQueues() ([]*string, error) {
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
r.cache.Put("sqsListAllQueues", queues)
|
||||
return queues, nil
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import (
|
|||
"testing"
|
||||
|
||||
awssdk "github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/cache"
|
||||
awstest "github.com/cloudskiff/driftctl/test/aws"
|
||||
|
||||
"github.com/aws/aws-sdk-go/service/sqs"
|
||||
|
@ -38,7 +39,7 @@ func Test_sqsRepository_ListAllQueues(t *testing.T) {
|
|||
},
|
||||
}, true)
|
||||
return true
|
||||
})).Return(nil)
|
||||
})).Return(nil).Once()
|
||||
},
|
||||
want: []*string{
|
||||
awssdk.String("https://sqs.eu-west-3.amazonaws.com/047081014315/bar.fifo"),
|
||||
|
@ -49,13 +50,24 @@ func Test_sqsRepository_ListAllQueues(t *testing.T) {
|
|||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
store := cache.New(1)
|
||||
client := &awstest.MockFakeSQS{}
|
||||
tt.mocks(client)
|
||||
r := &sqsRepository{
|
||||
client: client,
|
||||
cache: store,
|
||||
}
|
||||
got, err := r.ListAllQueues()
|
||||
assert.Equal(t, tt.wantErr, err)
|
||||
|
||||
if err == nil {
|
||||
// Check that results were cached
|
||||
cachedData, err := r.ListAllQueues()
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, got, cachedData)
|
||||
assert.IsType(t, []*string{}, store.Get("sqsListAllQueues"))
|
||||
}
|
||||
|
||||
changelog, err := diff.Diff(got, tt.want)
|
||||
assert.Nil(t, err)
|
||||
if len(changelog) > 0 {
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
|
||||
"github.com/aws/aws-sdk-go/aws/awserr"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/cache"
|
||||
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
|
||||
resourceaws "github.com/cloudskiff/driftctl/pkg/resource/aws"
|
||||
testresource "github.com/cloudskiff/driftctl/test/resource"
|
||||
|
@ -77,7 +78,7 @@ func TestSqsQueuePolicySupplier_Resources(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
supplierLibrary.AddSupplier(NewSqsQueuePolicySupplier(provider, deserializer, repository.NewSQSClient(provider.session)))
|
||||
supplierLibrary.AddSupplier(NewSqsQueuePolicySupplier(provider, deserializer, repository.NewSQSClient(provider.session, cache.New(0))))
|
||||
}
|
||||
|
||||
t.Run(c.test, func(tt *testing.T) {
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
|
||||
awssdk "github.com/aws/aws-sdk-go/aws"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/cache"
|
||||
testresource "github.com/cloudskiff/driftctl/test/resource"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws/awserr"
|
||||
|
@ -74,7 +75,7 @@ func TestSqsQueueSupplier_Resources(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
supplierLibrary.AddSupplier(NewSqsQueueSupplier(provider, deserializer, repository.NewSQSClient(provider.session)))
|
||||
supplierLibrary.AddSupplier(NewSqsQueueSupplier(provider, deserializer, repository.NewSQSClient(provider.session, cache.New(0))))
|
||||
}
|
||||
|
||||
t.Run(c.test, func(tt *testing.T) {
|
||||
|
|
Loading…
Reference in New Issue