Merge branch 'main' into feat/cacheCloudfrontRepo

main
Raphaël 2021-06-09 15:42:21 +02:00 committed by GitHub
commit 6473d8b807
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 107 additions and 25 deletions

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

@ -43,10 +43,10 @@ func Init(version string, alerter *alerter.Alerter,
rdsRepository := repository.NewRDSRepository(provider.session)
sqsRepository := repository.NewSQSClient(provider.session)
snsRepository := repository.NewSNSClient(provider.session)
dynamoDBRepository := repository.NewDynamoDBRepository(provider.session)
cloudfrontRepository := repository.NewCloudfrontClient(provider.session, repositoryCache)
kmsRepository := repository.NewKMSRepository(provider.session)
ecrRepository := repository.NewECRRepository(provider.session)
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/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 {