Merge pull request #814 from cloudskiff/db_subnet_group_supplier
Split db_subnet_group_suppliermain
commit
365e3f167f
|
@ -1,65 +0,0 @@
|
|||
package aws
|
||||
|
||||
import (
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
|
||||
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
|
||||
"github.com/cloudskiff/driftctl/pkg/resource/aws"
|
||||
|
||||
"github.com/zclconf/go-cty/cty"
|
||||
|
||||
"github.com/aws/aws-sdk-go/service/rds"
|
||||
|
||||
"github.com/cloudskiff/driftctl/pkg/resource"
|
||||
"github.com/cloudskiff/driftctl/pkg/terraform"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
type DBSubnetGroupSupplier struct {
|
||||
reader terraform.ResourceReader
|
||||
deserializer *resource.Deserializer
|
||||
repository repository.RDSRepository
|
||||
runner *terraform.ParallelResourceReader
|
||||
}
|
||||
|
||||
func NewDBSubnetGroupSupplier(provider *AWSTerraformProvider, deserializer *resource.Deserializer, repository repository.RDSRepository) *DBSubnetGroupSupplier {
|
||||
return &DBSubnetGroupSupplier{
|
||||
provider,
|
||||
deserializer,
|
||||
repository,
|
||||
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *DBSubnetGroupSupplier) Resources() ([]resource.Resource, error) {
|
||||
|
||||
subnetGroups, err := s.repository.ListAllDbSubnetGroups()
|
||||
|
||||
if err != nil {
|
||||
return nil, remoteerror.NewResourceEnumerationError(err, aws.AwsDbSubnetGroupResourceType)
|
||||
}
|
||||
|
||||
for _, subnetGroup := range subnetGroups {
|
||||
sub := *subnetGroup
|
||||
s.runner.Run(func() (cty.Value, error) {
|
||||
return s.readSubnetGroup(sub)
|
||||
})
|
||||
}
|
||||
ctyValues, err := s.runner.Wait()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return s.deserializer.Deserialize(aws.AwsDbSubnetGroupResourceType, ctyValues)
|
||||
}
|
||||
|
||||
func (s *DBSubnetGroupSupplier) readSubnetGroup(subnetGroup rds.DBSubnetGroup) (cty.Value, error) {
|
||||
val, err := s.reader.ReadResource(terraform.ReadResourceArgs{
|
||||
ID: *subnetGroup.DBSubnetGroupName,
|
||||
Ty: aws.AwsDbSubnetGroupResourceType,
|
||||
})
|
||||
if err != nil {
|
||||
logrus.Error(err)
|
||||
return cty.NilVal, err
|
||||
}
|
||||
return *val, nil
|
||||
}
|
|
@ -1,108 +0,0 @@
|
|||
package aws
|
||||
|
||||
import (
|
||||
"context"
|
||||
"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"
|
||||
|
||||
resourceaws "github.com/cloudskiff/driftctl/pkg/resource/aws"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws/awserr"
|
||||
"github.com/cloudskiff/driftctl/pkg/parallel"
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/cloudskiff/driftctl/test/goldenfile"
|
||||
|
||||
"github.com/aws/aws-sdk-go/aws"
|
||||
|
||||
"github.com/aws/aws-sdk-go/service/rds"
|
||||
|
||||
"github.com/cloudskiff/driftctl/pkg/resource"
|
||||
"github.com/cloudskiff/driftctl/pkg/terraform"
|
||||
"github.com/cloudskiff/driftctl/test"
|
||||
"github.com/cloudskiff/driftctl/test/mocks"
|
||||
)
|
||||
|
||||
func TestDBSubnetGroupSupplier_Resources(t *testing.T) {
|
||||
|
||||
tests := []struct {
|
||||
test string
|
||||
dirName string
|
||||
mocks func(client *repository.MockRDSRepository)
|
||||
err error
|
||||
}{
|
||||
{
|
||||
test: "no subnets",
|
||||
dirName: "db_subnet_empty",
|
||||
mocks: func(client *repository.MockRDSRepository) {
|
||||
client.On("ListAllDbSubnetGroups").Return([]*rds.DBSubnetGroup{}, nil)
|
||||
},
|
||||
err: nil,
|
||||
},
|
||||
{
|
||||
test: "multiples db subnets",
|
||||
dirName: "db_subnet_multiples",
|
||||
mocks: func(client *repository.MockRDSRepository) {
|
||||
client.On("ListAllDbSubnetGroups").Return([]*rds.DBSubnetGroup{
|
||||
{
|
||||
DBSubnetGroupName: aws.String("foo"),
|
||||
},
|
||||
{
|
||||
DBSubnetGroupName: aws.String("bar"),
|
||||
},
|
||||
}, nil)
|
||||
},
|
||||
err: nil,
|
||||
},
|
||||
{
|
||||
test: "Cannot list subnet",
|
||||
dirName: "db_subnet_empty",
|
||||
mocks: func(client *repository.MockRDSRepository) {
|
||||
client.On("ListAllDbSubnetGroups").Return([]*rds.DBSubnetGroup{}, awserr.NewRequestFailure(nil, 403, ""))
|
||||
},
|
||||
err: remoteerror.NewResourceEnumerationError(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsDbSubnetGroupResourceType),
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
|
||||
shouldUpdate := tt.dirName == *goldenfile.Update
|
||||
|
||||
providerLibrary := terraform.NewProviderLibrary()
|
||||
supplierLibrary := resource.NewSupplierLibrary()
|
||||
|
||||
repo := testresource.InitFakeSchemaRepository("aws", "3.19.0")
|
||||
resourceaws.InitResourcesMetadata(repo)
|
||||
factory := terraform.NewTerraformResourceFactory(repo)
|
||||
|
||||
deserializer := resource.NewDeserializer(factory)
|
||||
|
||||
if shouldUpdate {
|
||||
provider, err := InitTestAwsProvider(providerLibrary)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
supplierLibrary.AddSupplier(NewDBSubnetGroupSupplier(provider, deserializer, repository.NewRDSRepository(provider.session, cache.New(0))))
|
||||
}
|
||||
|
||||
t.Run(tt.test, func(t *testing.T) {
|
||||
provider := mocks.NewMockedGoldenTFProvider(tt.dirName, providerLibrary.Provider(terraform.AWS), shouldUpdate)
|
||||
client := &repository.MockRDSRepository{}
|
||||
tt.mocks(client)
|
||||
s := &DBSubnetGroupSupplier{
|
||||
provider,
|
||||
deserializer,
|
||||
client,
|
||||
terraform.NewParallelResourceReader(parallel.NewParallelRunner(context.TODO(), 10)),
|
||||
}
|
||||
got, err := s.Resources()
|
||||
assert.Equal(t, tt.err, err)
|
||||
|
||||
test.CtyTestDiff(got, tt.dirName, provider, deserializer, shouldUpdate, t)
|
||||
})
|
||||
}
|
||||
}
|
|
@ -121,6 +121,8 @@ func Init(version string, alerter *alerter.Alerter,
|
|||
|
||||
remoteLibrary.AddEnumerator(NewRDSDBInstanceEnumerator(rdsRepository, factory))
|
||||
remoteLibrary.AddDetailsFetcher(aws.AwsDbInstanceResourceType, common.NewGenericDetailsFetcher(aws.AwsDbInstanceResourceType, provider, deserializer))
|
||||
remoteLibrary.AddEnumerator(NewRDSDBSubnetGroupEnumerator(rdsRepository, factory))
|
||||
remoteLibrary.AddDetailsFetcher(aws.AwsDbSubnetGroupResourceType, common.NewGenericDetailsFetcher(aws.AwsDbSubnetGroupResourceType, provider, deserializer))
|
||||
|
||||
remoteLibrary.AddEnumerator(NewSQSQueueEnumerator(sqsRepository, factory))
|
||||
remoteLibrary.AddDetailsFetcher(aws.AwsSqsQueueResourceType, common.NewGenericDetailsFetcher(aws.AwsSqsQueueResourceType, provider, deserializer))
|
||||
|
@ -139,7 +141,6 @@ func Init(version string, alerter *alerter.Alerter,
|
|||
|
||||
supplierLibrary.AddSupplier(NewS3BucketAnalyticSupplier(provider, s3Repository, deserializer))
|
||||
supplierLibrary.AddSupplier(NewLambdaFunctionSupplier(provider, deserializer, lambdaRepository))
|
||||
supplierLibrary.AddSupplier(NewDBSubnetGroupSupplier(provider, deserializer, rdsRepository))
|
||||
supplierLibrary.AddSupplier(NewIamUserSupplier(provider, deserializer, iamRepository))
|
||||
supplierLibrary.AddSupplier(NewIamUserPolicySupplier(provider, deserializer, iamRepository))
|
||||
supplierLibrary.AddSupplier(NewIamUserPolicyAttachmentSupplier(provider, deserializer, iamRepository))
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
package aws
|
||||
|
||||
import (
|
||||
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
|
||||
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
|
||||
"github.com/cloudskiff/driftctl/pkg/resource"
|
||||
"github.com/cloudskiff/driftctl/pkg/resource/aws"
|
||||
)
|
||||
|
||||
type RDSDBSubnetGroupEnumerator struct {
|
||||
repository repository.RDSRepository
|
||||
factory resource.ResourceFactory
|
||||
}
|
||||
|
||||
func NewRDSDBSubnetGroupEnumerator(repo repository.RDSRepository, factory resource.ResourceFactory) *RDSDBSubnetGroupEnumerator {
|
||||
return &RDSDBSubnetGroupEnumerator{
|
||||
repository: repo,
|
||||
factory: factory,
|
||||
}
|
||||
}
|
||||
|
||||
func (e *RDSDBSubnetGroupEnumerator) SupportedType() resource.ResourceType {
|
||||
return aws.AwsDbSubnetGroupResourceType
|
||||
}
|
||||
|
||||
func (e *RDSDBSubnetGroupEnumerator) Enumerate() ([]resource.Resource, error) {
|
||||
subnetGroups, err := e.repository.ListAllDBSubnetGroups()
|
||||
if err != nil {
|
||||
return nil, remoteerror.NewResourceEnumerationError(err, string(e.SupportedType()))
|
||||
}
|
||||
|
||||
results := make([]resource.Resource, len(subnetGroups))
|
||||
|
||||
for _, subnetGroup := range subnetGroups {
|
||||
results = append(
|
||||
results,
|
||||
e.factory.CreateAbstractResource(
|
||||
string(e.SupportedType()),
|
||||
*subnetGroup.DBSubnetGroupName,
|
||||
map[string]interface{}{},
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
return results, err
|
||||
}
|
|
@ -35,8 +35,8 @@ func (_m *MockRDSRepository) ListAllDBInstances() ([]*rds.DBInstance, error) {
|
|||
return r0, r1
|
||||
}
|
||||
|
||||
// ListAllDbSubnetGroups provides a mock function with given fields:
|
||||
func (_m *MockRDSRepository) ListAllDbSubnetGroups() ([]*rds.DBSubnetGroup, error) {
|
||||
// ListAllDBSubnetGroups provides a mock function with given fields:
|
||||
func (_m *MockRDSRepository) ListAllDBSubnetGroups() ([]*rds.DBSubnetGroup, error) {
|
||||
ret := _m.Called()
|
||||
|
||||
var r0 []*rds.DBSubnetGroup
|
||||
|
|
|
@ -9,7 +9,7 @@ import (
|
|||
|
||||
type RDSRepository interface {
|
||||
ListAllDBInstances() ([]*rds.DBInstance, error)
|
||||
ListAllDbSubnetGroups() ([]*rds.DBSubnetGroup, error)
|
||||
ListAllDBSubnetGroups() ([]*rds.DBSubnetGroup, error)
|
||||
}
|
||||
|
||||
type rdsRepository struct {
|
||||
|
@ -43,8 +43,8 @@ func (r *rdsRepository) ListAllDBInstances() ([]*rds.DBInstance, error) {
|
|||
return result, nil
|
||||
}
|
||||
|
||||
func (r *rdsRepository) ListAllDbSubnetGroups() ([]*rds.DBSubnetGroup, error) {
|
||||
if v := r.cache.Get("rdsListAllDbSubnetGroups"); v != nil {
|
||||
func (r *rdsRepository) ListAllDBSubnetGroups() ([]*rds.DBSubnetGroup, error) {
|
||||
if v := r.cache.Get("rdsListAllDBSubnetGroups"); v != nil {
|
||||
return v.([]*rds.DBSubnetGroup), nil
|
||||
}
|
||||
|
||||
|
@ -57,6 +57,6 @@ func (r *rdsRepository) ListAllDbSubnetGroups() ([]*rds.DBSubnetGroup, error) {
|
|||
},
|
||||
)
|
||||
|
||||
r.cache.Put("rdsListAllDbSubnetGroups", subnetGroups)
|
||||
r.cache.Put("rdsListAllDBSubnetGroups", subnetGroups)
|
||||
return subnetGroups, err
|
||||
}
|
||||
|
|
|
@ -85,7 +85,7 @@ func Test_rdsRepository_ListAllDBInstances(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func Test_rdsRepository_ListAllDbSubnetGroups(t *testing.T) {
|
||||
func Test_rdsRepository_ListAllDBSubnetGroups(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
mocks func(client *awstest.MockFakeRDS)
|
||||
|
@ -134,15 +134,15 @@ func Test_rdsRepository_ListAllDbSubnetGroups(t *testing.T) {
|
|||
client: client,
|
||||
cache: store,
|
||||
}
|
||||
got, err := r.ListAllDbSubnetGroups()
|
||||
got, err := r.ListAllDBSubnetGroups()
|
||||
assert.Equal(t, tt.wantErr, err)
|
||||
|
||||
if err == nil {
|
||||
// Check that results were cached
|
||||
cachedData, err := r.ListAllDbSubnetGroups()
|
||||
cachedData, err := r.ListAllDBSubnetGroups()
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, got, cachedData)
|
||||
assert.IsType(t, []*rds.DBSubnetGroup{}, store.Get("rdsListAllDbSubnetGroups"))
|
||||
assert.IsType(t, []*rds.DBSubnetGroup{}, store.Get("rdsListAllDBSubnetGroups"))
|
||||
}
|
||||
|
||||
changelog, err := diff.Diff(got, tt.want)
|
||||
|
|
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
@ -120,3 +120,92 @@ func TestRDSDBInstance(t *testing.T) {
|
|||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRDSDBSubnetGroup(t *testing.T) {
|
||||
tests := []struct {
|
||||
test string
|
||||
dirName string
|
||||
mocks func(repository *repository.MockRDSRepository)
|
||||
wantErr error
|
||||
}{
|
||||
{
|
||||
test: "no db subnet groups",
|
||||
dirName: "aws_rds_db_subnet_group_empty",
|
||||
mocks: func(repository *repository.MockRDSRepository) {
|
||||
repository.On("ListAllDBSubnetGroups").Return([]*rds.DBSubnetGroup{}, nil)
|
||||
},
|
||||
},
|
||||
{
|
||||
test: "multiple db subnet groups",
|
||||
dirName: "aws_rds_db_subnet_group_multiple",
|
||||
mocks: func(repository *repository.MockRDSRepository) {
|
||||
repository.On("ListAllDBSubnetGroups").Return([]*rds.DBSubnetGroup{
|
||||
{DBSubnetGroupName: awssdk.String("foo")},
|
||||
{DBSubnetGroupName: awssdk.String("bar")},
|
||||
}, nil)
|
||||
},
|
||||
},
|
||||
{
|
||||
test: "cannot list db subnet groups",
|
||||
dirName: "aws_rds_db_subnet_group_list",
|
||||
mocks: func(repository *repository.MockRDSRepository) {
|
||||
repository.On("ListAllDBSubnetGroups").Return(nil, awserr.NewRequestFailure(nil, 403, ""))
|
||||
},
|
||||
wantErr: nil,
|
||||
},
|
||||
}
|
||||
|
||||
schemaRepository := testresource.InitFakeSchemaRepository("aws", "3.19.0")
|
||||
resourceaws.InitResourcesMetadata(schemaRepository)
|
||||
factory := terraform.NewTerraformResourceFactory(schemaRepository)
|
||||
deserializer := resource.NewDeserializer(factory)
|
||||
|
||||
for _, c := range tests {
|
||||
t.Run(c.test, func(tt *testing.T) {
|
||||
shouldUpdate := c.dirName == *goldenfile.Update
|
||||
|
||||
sess := session.Must(session.NewSessionWithOptions(session.Options{
|
||||
SharedConfigState: session.SharedConfigEnable,
|
||||
}))
|
||||
|
||||
scanOptions := ScannerOptions{Deep: true}
|
||||
providerLibrary := terraform.NewProviderLibrary()
|
||||
remoteLibrary := common.NewRemoteLibrary()
|
||||
|
||||
// Initialize mocks
|
||||
alerter := &mocks.AlerterInterface{}
|
||||
alerter.On("SendAlert", mock.Anything, mock.Anything).Maybe().Return()
|
||||
fakeRepo := &repository.MockRDSRepository{}
|
||||
c.mocks(fakeRepo)
|
||||
var repo repository.RDSRepository = fakeRepo
|
||||
providerVersion := "3.19.0"
|
||||
realProvider, err := terraform2.InitTestAwsProvider(providerLibrary, providerVersion)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
provider := terraform2.NewFakeTerraformProvider(realProvider)
|
||||
provider.WithResponse(c.dirName)
|
||||
|
||||
// Replace mock by real resources if we are in update mode
|
||||
if shouldUpdate {
|
||||
err := realProvider.Init()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
provider.ShouldUpdate()
|
||||
repo = repository.NewRDSRepository(sess, cache.New(0))
|
||||
}
|
||||
|
||||
remoteLibrary.AddEnumerator(aws.NewRDSDBSubnetGroupEnumerator(repo, factory))
|
||||
remoteLibrary.AddDetailsFetcher(resourceaws.AwsDbSubnetGroupResourceType, common.NewGenericDetailsFetcher(resourceaws.AwsDbSubnetGroupResourceType, provider, deserializer))
|
||||
|
||||
s := NewScanner(nil, remoteLibrary, alerter, scanOptions)
|
||||
got, err := s.Resources()
|
||||
assert.Equal(tt, err, c.wantErr)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
test.TestAgainstGoldenFile(got, resourceaws.AwsDbSubnetGroupResourceType, c.dirName, provider, deserializer, shouldUpdate, tt)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
[]
|
Loading…
Reference in New Issue