Merge pull request #767 from cloudskiff/refact/route53_record_deepmode

Split route53_record_supplier
main
Elie 2021-07-06 15:19:40 +02:00 committed by GitHub
commit 554fe88a8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 288 additions and 314 deletions

View File

@ -109,6 +109,8 @@ func Init(version string, alerter *alerter.Alerter,
remoteLibrary.AddDetailsFetcher(aws.AwsRoute53HealthCheckResourceType, common.NewGenericDetailsFetcher(aws.AwsRoute53HealthCheckResourceType, provider, deserializer))
remoteLibrary.AddEnumerator(NewRoute53ZoneEnumerator(route53repository, factory))
remoteLibrary.AddDetailsFetcher(aws.AwsRoute53ZoneResourceType, common.NewGenericDetailsFetcher(aws.AwsRoute53ZoneResourceType, provider, deserializer))
remoteLibrary.AddEnumerator(NewRoute53RecordEnumerator(route53repository, factory))
remoteLibrary.AddDetailsFetcher(aws.AwsRoute53RecordResourceType, common.NewGenericDetailsFetcher(aws.AwsRoute53RecordResourceType, provider, deserializer))
remoteLibrary.AddEnumerator(NewVPCSecurityGroupEnumerator(ec2repository, factory))
remoteLibrary.AddDetailsFetcher(aws.AwsSecurityGroupResourceType, common.NewGenericDetailsFetcher(aws.AwsSecurityGroupResourceType, provider, deserializer))
@ -117,7 +119,6 @@ func Init(version string, alerter *alerter.Alerter,
remoteLibrary.AddDetailsFetcher(aws.AwsDefaultSecurityGroupResourceType, common.NewGenericDetailsFetcher(aws.AwsDefaultSecurityGroupResourceType, provider, deserializer))
supplierLibrary.AddSupplier(NewS3BucketAnalyticSupplier(provider, s3Repository, deserializer))
supplierLibrary.AddSupplier(NewRoute53RecordSupplier(provider, deserializer, route53repository))
supplierLibrary.AddSupplier(NewLambdaFunctionSupplier(provider, deserializer, lambdaRepository))
supplierLibrary.AddSupplier(NewDBSubnetGroupSupplier(provider, deserializer, rdsRepository))
supplierLibrary.AddSupplier(NewDBInstanceSupplier(provider, deserializer, rdsRepository))

View File

@ -0,0 +1,85 @@
package aws
import (
"strings"
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
"github.com/cloudskiff/driftctl/pkg/resource"
resourceaws "github.com/cloudskiff/driftctl/pkg/resource/aws"
)
type Route53RecordEnumerator struct {
client repository.Route53Repository
factory resource.ResourceFactory
}
func NewRoute53RecordEnumerator(repo repository.Route53Repository, factory resource.ResourceFactory) *Route53RecordEnumerator {
return &Route53RecordEnumerator{
repo,
factory,
}
}
func (e *Route53RecordEnumerator) SupportedType() resource.ResourceType {
return resourceaws.AwsRoute53RecordResourceType
}
func (e *Route53RecordEnumerator) Enumerate() ([]resource.Resource, error) {
zones, err := e.client.ListAllZones()
if err != nil {
return nil, remoteerror.NewResourceEnumerationErrorWithType(err, resourceaws.AwsRoute53RecordResourceType, resourceaws.AwsRoute53ZoneResourceType)
}
results := make([]resource.Resource, len(zones))
for _, hostedZone := range zones {
records, err := e.listRecordsForZone(strings.TrimPrefix(*hostedZone.Id, "/hostedzone/"))
if err != nil {
return nil, remoteerror.NewResourceEnumerationError(err, resourceaws.AwsRoute53RecordResourceType)
}
results = append(results, records...)
}
return results, err
}
func (e *Route53RecordEnumerator) listRecordsForZone(zoneId string) ([]resource.Resource, error) {
records, err := e.client.ListRecordsForZone(zoneId)
if err != nil {
return nil, err
}
results := make([]resource.Resource, len(records))
for _, raw := range records {
rawType := *raw.Type
rawName := *raw.Name
rawSetIdentifier := raw.SetIdentifier
vars := []string{
zoneId,
strings.ToLower(strings.TrimSuffix(rawName, ".")),
rawType,
}
if rawSetIdentifier != nil {
vars = append(vars, *rawSetIdentifier)
}
results = append(
results,
e.factory.CreateAbstractResource(
string(e.SupportedType()),
strings.Join(vars, "_"),
map[string]interface{}{},
),
)
}
return results, nil
}

View File

@ -1,103 +0,0 @@
package aws
import (
"strings"
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
"github.com/cloudskiff/driftctl/pkg/resource"
resourceaws "github.com/cloudskiff/driftctl/pkg/resource/aws"
"github.com/cloudskiff/driftctl/pkg/terraform"
"github.com/zclconf/go-cty/cty"
)
type Route53RecordSupplier struct {
reader terraform.ResourceReader
deserializer *resource.Deserializer
repository repository.Route53Repository
runner *terraform.ParallelResourceReader
}
func NewRoute53RecordSupplier(provider *AWSTerraformProvider, deserializer *resource.Deserializer, repository repository.Route53Repository) *Route53RecordSupplier {
return &Route53RecordSupplier{
provider,
deserializer,
repository,
terraform.NewParallelResourceReader(provider.Runner().SubRunner())}
}
func (s *Route53RecordSupplier) Resources() ([]resource.Resource, error) {
zones, err := s.listZones()
if err != nil {
return nil, remoteerror.NewResourceEnumerationErrorWithType(err, resourceaws.AwsRoute53RecordResourceType, resourceaws.AwsRoute53ZoneResourceType)
}
for _, zone := range zones {
if err := s.listRecordsForZone(zone[0], zone[1]); err != nil {
return nil, remoteerror.NewResourceEnumerationError(err, resourceaws.AwsRoute53RecordResourceType)
}
}
results, err := s.runner.Wait()
if err != nil {
return nil, err
}
return s.deserializer.Deserialize(resourceaws.AwsRoute53RecordResourceType, results)
}
func (s *Route53RecordSupplier) listZones() ([][2]string, error) {
results := make([][2]string, 0)
zones, err := s.repository.ListAllZones()
if err != nil {
return nil, err
}
for _, hostedZone := range zones {
results = append(results, [2]string{strings.TrimPrefix(*hostedZone.Id, "/hostedzone/"), *hostedZone.Name})
}
return results, nil
}
func (s *Route53RecordSupplier) listRecordsForZone(zoneId string, zoneName string) error {
records, err := s.repository.ListRecordsForZone(zoneId)
if err != nil {
return err
}
for _, raw := range records {
rawType := *raw.Type
rawName := *raw.Name
rawSetIdentifier := raw.SetIdentifier
s.runner.Run(func() (cty.Value, error) {
vars := []string{
zoneId,
strings.ToLower(strings.TrimSuffix(rawName, ".")),
rawType,
}
if rawSetIdentifier != nil {
vars = append(vars, *rawSetIdentifier)
}
record, err := s.reader.ReadResource(
terraform.ReadResourceArgs{
Ty: resourceaws.AwsRoute53RecordResourceType,
ID: strings.Join(vars, "_"),
},
)
if err != nil {
return cty.NilVal, err
}
return *record, nil
})
}
return nil
}

View File

@ -1,210 +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"
testmocks "github.com/cloudskiff/driftctl/test/mocks"
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"
awssdk "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/route53"
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/terraform"
"github.com/cloudskiff/driftctl/test"
)
func TestRoute53RecordSupplier_Resources(t *testing.T) {
tests := []struct {
test string
dirName string
mocks func(client *repository.MockRoute53Repository)
err error
}{
{
test: "no records",
dirName: "route53_zone_with_no_record",
mocks: func(client *repository.MockRoute53Repository) {
client.On("ListAllZones").Return(
[]*route53.HostedZone{
{
Id: awssdk.String("Z1035360GLIB82T1EH2G"),
Name: awssdk.String("foo-0.com"),
},
},
nil,
)
client.On("ListRecordsForZone", "Z1035360GLIB82T1EH2G").Return([]*route53.ResourceRecordSet{}, nil)
},
err: nil,
},
{
test: "multiples records in multiples zones",
dirName: "route53_record_multiples",
mocks: func(client *repository.MockRoute53Repository) {
client.On("ListAllZones").Return(
[]*route53.HostedZone{
{
Id: awssdk.String("Z1035360GLIB82T1EH2G"),
Name: awssdk.String("foo-0.com"),
},
{
Id: awssdk.String("Z10347383HV75H96J919W"),
Name: awssdk.String("foo-1.com"),
},
},
nil,
)
client.On("ListRecordsForZone", "Z1035360GLIB82T1EH2G").Return([]*route53.ResourceRecordSet{
{
Name: awssdk.String("foo-0.com"),
Type: awssdk.String("NS"),
},
{
Name: awssdk.String("test0"),
Type: awssdk.String("A"),
},
{
Name: awssdk.String("test1"),
Type: awssdk.String("A"),
},
{
Name: awssdk.String("test2"),
Type: awssdk.String("A"),
},
{
Name: awssdk.String("test3"),
Type: awssdk.String("A"),
},
}, nil)
client.On("ListRecordsForZone", "Z10347383HV75H96J919W").Return([]*route53.ResourceRecordSet{
{
Name: awssdk.String("test2"),
Type: awssdk.String("A"),
},
}, nil)
},
err: nil,
},
{
test: "explicit subdomain records",
dirName: "route53_record_explicit_subdomain",
mocks: func(client *repository.MockRoute53Repository) {
client.On("ListAllZones").Return(
[]*route53.HostedZone{
{
Id: awssdk.String("Z06486383UC8WYSBZTWFM"),
Name: awssdk.String("foo-2.com"),
},
},
nil,
)
client.On("ListRecordsForZone", "Z06486383UC8WYSBZTWFM").Return([]*route53.ResourceRecordSet{
{
Name: awssdk.String("test0"),
Type: awssdk.String("TXT"),
},
{
Name: awssdk.String("test0"),
Type: awssdk.String("A"),
},
{
Name: awssdk.String("test1.foo-2.com"),
Type: awssdk.String("TXT"),
},
{
Name: awssdk.String("test1.foo-2.com"),
Type: awssdk.String("A"),
},
{
Name: awssdk.String("_test2.foo-2.com"),
Type: awssdk.String("TXT"),
},
{
Name: awssdk.String("_test2.foo-2.com"),
Type: awssdk.String("A"),
},
}, nil)
},
err: nil,
},
{
test: "cannot list zones",
dirName: "route53_zone_with_no_record",
mocks: func(client *repository.MockRoute53Repository) {
client.On("ListAllZones").Return(
[]*route53.HostedZone{},
awserr.NewRequestFailure(nil, 403, ""))
},
err: remoteerror.NewResourceEnumerationErrorWithType(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsRoute53RecordResourceType, resourceaws.AwsRoute53ZoneResourceType),
},
{
test: "cannot list records",
dirName: "route53_zone_with_no_record",
mocks: func(client *repository.MockRoute53Repository) {
client.On("ListAllZones").Return(
[]*route53.HostedZone{
{
Id: awssdk.String("Z06486383UC8WYSBZTWFM"),
Name: awssdk.String("foo-2.com"),
},
},
nil)
client.On("ListRecordsForZone", "Z06486383UC8WYSBZTWFM").Return(
[]*route53.ResourceRecordSet{},
awserr.NewRequestFailure(nil, 403, ""))
},
err: remoteerror.NewResourceEnumerationError(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsRoute53RecordResourceType),
},
}
for _, tt := range tests {
t.Run(tt.test, func(t *testing.T) {
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(NewRoute53RecordSupplier(provider, deserializer, repository.NewRoute53Repository(provider.session, cache.New(0))))
}
provider := testmocks.NewMockedGoldenTFProvider(tt.dirName, providerLibrary.Provider(terraform.AWS), shouldUpdate)
client := &repository.MockRoute53Repository{}
tt.mocks(client)
s := &Route53RecordSupplier{
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)
})
}
}

View File

@ -243,3 +243,204 @@ func TestRoute53_Zone(t *testing.T) {
})
}
}
func TestRoute53_Record(t *testing.T) {
tests := []struct {
test string
dirName string
mocks func(*repository.MockRoute53Repository)
err error
}{
{
test: "no records",
dirName: "route53_zone_with_no_record",
mocks: func(client *repository.MockRoute53Repository) {
client.On("ListAllZones").Return(
[]*route53.HostedZone{
{
Id: awssdk.String("Z1035360GLIB82T1EH2G"),
Name: awssdk.String("foo-0.com"),
},
},
nil,
)
client.On("ListRecordsForZone", "Z1035360GLIB82T1EH2G").Return([]*route53.ResourceRecordSet{}, nil)
},
err: nil,
},
{
test: "multiples records in multiples zones",
dirName: "route53_record_multiples",
mocks: func(client *repository.MockRoute53Repository) {
client.On("ListAllZones").Return(
[]*route53.HostedZone{
{
Id: awssdk.String("Z1035360GLIB82T1EH2G"),
Name: awssdk.String("foo-0.com"),
},
{
Id: awssdk.String("Z10347383HV75H96J919W"),
Name: awssdk.String("foo-1.com"),
},
},
nil,
)
client.On("ListRecordsForZone", "Z1035360GLIB82T1EH2G").Return([]*route53.ResourceRecordSet{
{
Name: awssdk.String("foo-0.com"),
Type: awssdk.String("NS"),
},
{
Name: awssdk.String("test0"),
Type: awssdk.String("A"),
},
{
Name: awssdk.String("test1"),
Type: awssdk.String("A"),
},
{
Name: awssdk.String("test2"),
Type: awssdk.String("A"),
},
{
Name: awssdk.String("test3"),
Type: awssdk.String("A"),
},
}, nil)
client.On("ListRecordsForZone", "Z10347383HV75H96J919W").Return([]*route53.ResourceRecordSet{
{
Name: awssdk.String("test2"),
Type: awssdk.String("A"),
},
}, nil)
},
err: nil,
},
{
test: "explicit subdomain records",
dirName: "route53_record_explicit_subdomain",
mocks: func(client *repository.MockRoute53Repository) {
client.On("ListAllZones").Return(
[]*route53.HostedZone{
{
Id: awssdk.String("Z06486383UC8WYSBZTWFM"),
Name: awssdk.String("foo-2.com"),
},
},
nil,
)
client.On("ListRecordsForZone", "Z06486383UC8WYSBZTWFM").Return([]*route53.ResourceRecordSet{
{
Name: awssdk.String("test0"),
Type: awssdk.String("TXT"),
},
{
Name: awssdk.String("test0"),
Type: awssdk.String("A"),
},
{
Name: awssdk.String("test1.foo-2.com"),
Type: awssdk.String("TXT"),
},
{
Name: awssdk.String("test1.foo-2.com"),
Type: awssdk.String("A"),
},
{
Name: awssdk.String("_test2.foo-2.com"),
Type: awssdk.String("TXT"),
},
{
Name: awssdk.String("_test2.foo-2.com"),
Type: awssdk.String("A"),
},
}, nil)
},
err: nil,
},
{
test: "cannot list zones",
dirName: "route53_zone_with_no_record",
mocks: func(client *repository.MockRoute53Repository) {
client.On("ListAllZones").Return(
[]*route53.HostedZone{},
awserr.NewRequestFailure(nil, 403, ""))
},
err: nil,
},
{
test: "cannot list records",
dirName: "route53_zone_with_no_record",
mocks: func(client *repository.MockRoute53Repository) {
client.On("ListAllZones").Return(
[]*route53.HostedZone{
{
Id: awssdk.String("Z06486383UC8WYSBZTWFM"),
Name: awssdk.String("foo-2.com"),
},
},
nil)
client.On("ListRecordsForZone", "Z06486383UC8WYSBZTWFM").Return(
[]*route53.ResourceRecordSet{},
awserr.NewRequestFailure(nil, 403, ""))
},
err: 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
session := 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.MockRoute53Repository{}
c.mocks(fakeRepo)
var repo repository.Route53Repository = 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.NewRoute53Repository(session, cache.New(0))
}
remoteLibrary.AddEnumerator(aws.NewRoute53RecordEnumerator(repo, factory))
remoteLibrary.AddDetailsFetcher(resourceaws.AwsRoute53RecordResourceType, common.NewGenericDetailsFetcher(resourceaws.AwsRoute53RecordResourceType, provider, deserializer))
s := NewScanner(nil, remoteLibrary, alerter, scanOptions)
got, err := s.Resources()
assert.Equal(tt, c.err, err)
if err != nil {
return
}
test.TestAgainstGoldenFile(got, resourceaws.AwsRoute53RecordResourceType, c.dirName, provider, deserializer, shouldUpdate, tt)
})
}
}