Merge branch 'main' into feat/cacheRDSrepo

main
Raphaël 2021-06-09 16:06:27 +02:00 committed by GitHub
commit 237eaba9ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 163 additions and 37 deletions

View File

@ -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) {

View File

@ -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) {

View File

@ -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) {

View File

@ -41,12 +41,12 @@ func Init(version string, alerter *alerter.Alerter,
route53repository := repository.NewRoute53Repository(provider.session)
lambdaRepository := repository.NewLambdaRepository(provider.session)
rdsRepository := repository.NewRDSRepository(provider.session, repositoryCache)
sqsRepository := repository.NewSQSClient(provider.session)
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)

View File

@ -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) {

View File

@ -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) {

View File

@ -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
}

View File

@ -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 {

View File

@ -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
}

View File

@ -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 {

View File

@ -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
}

View File

@ -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 {

View File

@ -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) {

View File

@ -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 {

View File

@ -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
}

View File

@ -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 {

View File

@ -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) {

View File

@ -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) {