From f6a48d8edf43093b8d2b3d6ecf2aeb9e9e33b3b5 Mon Sep 17 00:00:00 2001 From: sundowndev Date: Tue, 6 Jul 2021 11:23:52 +0200 Subject: [PATCH 1/3] refactor: split route53 record supplier --- pkg/remote/aws/init.go | 3 +- pkg/remote/aws/route53_record_enumerator.go | 99 +++++++++ pkg/remote/aws/route53_record_supplier.go | 103 --------- .../aws/route53_record_supplier_test.go | 210 ------------------ pkg/remote/route53_scanner_test.go | 200 +++++++++++++++++ ...SBZTWFM__test2.foo-2.com_A.res.golden.json | 0 ...ZTWFM__test2.foo-2.com_TXT.res.golden.json | 0 ...486383UC8WYSBZTWFM_test0_A.res.golden.json | 0 ...6383UC8WYSBZTWFM_test0_TXT.res.golden.json | 0 ...YSBZTWFM_test1.foo-2.com_A.res.golden.json | 0 ...BZTWFM_test1.foo-2.com_TXT.res.golden.json | 0 .../route53_record_explicit_subdomain/main.tf | 0 .../results.golden.json | 0 .../schema.golden.json | 0 ...347383HV75H96J919W_test2_A.res.golden.json | 0 ...0GLIB82T1EH2G_foo-0.com_NS.res.golden.json | 0 ...035360GLIB82T1EH2G_test0_A.res.golden.json | 0 ...035360GLIB82T1EH2G_test1_A.res.golden.json | 0 ...035360GLIB82T1EH2G_test2_A.res.golden.json | 0 ...035360GLIB82T1EH2G_test3_A.res.golden.json | 0 .../results.golden.json | 0 .../schema.golden.json | 0 22 files changed, 301 insertions(+), 314 deletions(-) create mode 100644 pkg/remote/aws/route53_record_enumerator.go delete mode 100644 pkg/remote/aws/route53_record_supplier.go delete mode 100644 pkg/remote/aws/route53_record_supplier_test.go rename pkg/remote/{aws => }/test/route53_record_explicit_subdomain/aws_route53_record-Z06486383UC8WYSBZTWFM__test2.foo-2.com_A.res.golden.json (100%) rename pkg/remote/{aws => }/test/route53_record_explicit_subdomain/aws_route53_record-Z06486383UC8WYSBZTWFM__test2.foo-2.com_TXT.res.golden.json (100%) rename pkg/remote/{aws => }/test/route53_record_explicit_subdomain/aws_route53_record-Z06486383UC8WYSBZTWFM_test0_A.res.golden.json (100%) rename pkg/remote/{aws => }/test/route53_record_explicit_subdomain/aws_route53_record-Z06486383UC8WYSBZTWFM_test0_TXT.res.golden.json (100%) rename pkg/remote/{aws => }/test/route53_record_explicit_subdomain/aws_route53_record-Z06486383UC8WYSBZTWFM_test1.foo-2.com_A.res.golden.json (100%) rename pkg/remote/{aws => }/test/route53_record_explicit_subdomain/aws_route53_record-Z06486383UC8WYSBZTWFM_test1.foo-2.com_TXT.res.golden.json (100%) rename pkg/remote/{aws => }/test/route53_record_explicit_subdomain/main.tf (100%) rename pkg/remote/{aws => }/test/route53_record_explicit_subdomain/results.golden.json (100%) rename pkg/remote/{aws => }/test/route53_record_explicit_subdomain/schema.golden.json (100%) rename pkg/remote/{aws => }/test/route53_record_multiples/aws_route53_record-Z10347383HV75H96J919W_test2_A.res.golden.json (100%) rename pkg/remote/{aws => }/test/route53_record_multiples/aws_route53_record-Z1035360GLIB82T1EH2G_foo-0.com_NS.res.golden.json (100%) rename pkg/remote/{aws => }/test/route53_record_multiples/aws_route53_record-Z1035360GLIB82T1EH2G_test0_A.res.golden.json (100%) rename pkg/remote/{aws => }/test/route53_record_multiples/aws_route53_record-Z1035360GLIB82T1EH2G_test1_A.res.golden.json (100%) rename pkg/remote/{aws => }/test/route53_record_multiples/aws_route53_record-Z1035360GLIB82T1EH2G_test2_A.res.golden.json (100%) rename pkg/remote/{aws => }/test/route53_record_multiples/aws_route53_record-Z1035360GLIB82T1EH2G_test3_A.res.golden.json (100%) rename pkg/remote/{aws => }/test/route53_record_multiples/results.golden.json (100%) rename pkg/remote/{aws => }/test/route53_record_multiples/schema.golden.json (100%) diff --git a/pkg/remote/aws/init.go b/pkg/remote/aws/init.go index 293bc588..d9808fc4 100644 --- a/pkg/remote/aws/init.go +++ b/pkg/remote/aws/init.go @@ -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)) diff --git a/pkg/remote/aws/route53_record_enumerator.go b/pkg/remote/aws/route53_record_enumerator.go new file mode 100644 index 00000000..859b2cc7 --- /dev/null +++ b/pkg/remote/aws/route53_record_enumerator.go @@ -0,0 +1,99 @@ +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.listZones() + if err != nil { + return nil, remoteerror.NewResourceEnumerationErrorWithType(err, resourceaws.AwsRoute53RecordResourceType, resourceaws.AwsRoute53ZoneResourceType) + } + + results := make([]resource.Resource, len(zones)) + + for _, zone := range zones { + records, err := e.listRecordsForZone(zone[0], zone[1]) + if err != nil { + return nil, remoteerror.NewResourceEnumerationError(err, resourceaws.AwsRoute53RecordResourceType) + } + + results = append(results, records...) + } + + return results, err +} + +func (e *Route53RecordEnumerator) listZones() ([][2]string, error) { + results := make([][2]string, 0) + zones, err := e.client.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 (e *Route53RecordEnumerator) listRecordsForZone(zoneId string, _ 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 +} diff --git a/pkg/remote/aws/route53_record_supplier.go b/pkg/remote/aws/route53_record_supplier.go deleted file mode 100644 index b4b8a86c..00000000 --- a/pkg/remote/aws/route53_record_supplier.go +++ /dev/null @@ -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 -} diff --git a/pkg/remote/aws/route53_record_supplier_test.go b/pkg/remote/aws/route53_record_supplier_test.go deleted file mode 100644 index 426e3fa0..00000000 --- a/pkg/remote/aws/route53_record_supplier_test.go +++ /dev/null @@ -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) - }) - } -} diff --git a/pkg/remote/route53_scanner_test.go b/pkg/remote/route53_scanner_test.go index 03d24fe7..3b5c32bb 100644 --- a/pkg/remote/route53_scanner_test.go +++ b/pkg/remote/route53_scanner_test.go @@ -243,3 +243,203 @@ 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: 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), + }, + } + + schemaRepository := testresource.InitFakeSchemaRepository("aws", "3.19.0") + resourceaws.InitResourcesMetadata(schemaRepository) + factory := terraform.NewTerraformResourceFactory(schemaRepository) + deserializer := resource.NewDeserializer(factory) + alerter := &mocks.AlerterInterface{} + + 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 + 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) + }) + } +} diff --git a/pkg/remote/aws/test/route53_record_explicit_subdomain/aws_route53_record-Z06486383UC8WYSBZTWFM__test2.foo-2.com_A.res.golden.json b/pkg/remote/test/route53_record_explicit_subdomain/aws_route53_record-Z06486383UC8WYSBZTWFM__test2.foo-2.com_A.res.golden.json similarity index 100% rename from pkg/remote/aws/test/route53_record_explicit_subdomain/aws_route53_record-Z06486383UC8WYSBZTWFM__test2.foo-2.com_A.res.golden.json rename to pkg/remote/test/route53_record_explicit_subdomain/aws_route53_record-Z06486383UC8WYSBZTWFM__test2.foo-2.com_A.res.golden.json diff --git a/pkg/remote/aws/test/route53_record_explicit_subdomain/aws_route53_record-Z06486383UC8WYSBZTWFM__test2.foo-2.com_TXT.res.golden.json b/pkg/remote/test/route53_record_explicit_subdomain/aws_route53_record-Z06486383UC8WYSBZTWFM__test2.foo-2.com_TXT.res.golden.json similarity index 100% rename from pkg/remote/aws/test/route53_record_explicit_subdomain/aws_route53_record-Z06486383UC8WYSBZTWFM__test2.foo-2.com_TXT.res.golden.json rename to pkg/remote/test/route53_record_explicit_subdomain/aws_route53_record-Z06486383UC8WYSBZTWFM__test2.foo-2.com_TXT.res.golden.json diff --git a/pkg/remote/aws/test/route53_record_explicit_subdomain/aws_route53_record-Z06486383UC8WYSBZTWFM_test0_A.res.golden.json b/pkg/remote/test/route53_record_explicit_subdomain/aws_route53_record-Z06486383UC8WYSBZTWFM_test0_A.res.golden.json similarity index 100% rename from pkg/remote/aws/test/route53_record_explicit_subdomain/aws_route53_record-Z06486383UC8WYSBZTWFM_test0_A.res.golden.json rename to pkg/remote/test/route53_record_explicit_subdomain/aws_route53_record-Z06486383UC8WYSBZTWFM_test0_A.res.golden.json diff --git a/pkg/remote/aws/test/route53_record_explicit_subdomain/aws_route53_record-Z06486383UC8WYSBZTWFM_test0_TXT.res.golden.json b/pkg/remote/test/route53_record_explicit_subdomain/aws_route53_record-Z06486383UC8WYSBZTWFM_test0_TXT.res.golden.json similarity index 100% rename from pkg/remote/aws/test/route53_record_explicit_subdomain/aws_route53_record-Z06486383UC8WYSBZTWFM_test0_TXT.res.golden.json rename to pkg/remote/test/route53_record_explicit_subdomain/aws_route53_record-Z06486383UC8WYSBZTWFM_test0_TXT.res.golden.json diff --git a/pkg/remote/aws/test/route53_record_explicit_subdomain/aws_route53_record-Z06486383UC8WYSBZTWFM_test1.foo-2.com_A.res.golden.json b/pkg/remote/test/route53_record_explicit_subdomain/aws_route53_record-Z06486383UC8WYSBZTWFM_test1.foo-2.com_A.res.golden.json similarity index 100% rename from pkg/remote/aws/test/route53_record_explicit_subdomain/aws_route53_record-Z06486383UC8WYSBZTWFM_test1.foo-2.com_A.res.golden.json rename to pkg/remote/test/route53_record_explicit_subdomain/aws_route53_record-Z06486383UC8WYSBZTWFM_test1.foo-2.com_A.res.golden.json diff --git a/pkg/remote/aws/test/route53_record_explicit_subdomain/aws_route53_record-Z06486383UC8WYSBZTWFM_test1.foo-2.com_TXT.res.golden.json b/pkg/remote/test/route53_record_explicit_subdomain/aws_route53_record-Z06486383UC8WYSBZTWFM_test1.foo-2.com_TXT.res.golden.json similarity index 100% rename from pkg/remote/aws/test/route53_record_explicit_subdomain/aws_route53_record-Z06486383UC8WYSBZTWFM_test1.foo-2.com_TXT.res.golden.json rename to pkg/remote/test/route53_record_explicit_subdomain/aws_route53_record-Z06486383UC8WYSBZTWFM_test1.foo-2.com_TXT.res.golden.json diff --git a/pkg/remote/aws/test/route53_record_explicit_subdomain/main.tf b/pkg/remote/test/route53_record_explicit_subdomain/main.tf similarity index 100% rename from pkg/remote/aws/test/route53_record_explicit_subdomain/main.tf rename to pkg/remote/test/route53_record_explicit_subdomain/main.tf diff --git a/pkg/remote/aws/test/route53_record_explicit_subdomain/results.golden.json b/pkg/remote/test/route53_record_explicit_subdomain/results.golden.json similarity index 100% rename from pkg/remote/aws/test/route53_record_explicit_subdomain/results.golden.json rename to pkg/remote/test/route53_record_explicit_subdomain/results.golden.json diff --git a/pkg/remote/aws/test/route53_record_explicit_subdomain/schema.golden.json b/pkg/remote/test/route53_record_explicit_subdomain/schema.golden.json similarity index 100% rename from pkg/remote/aws/test/route53_record_explicit_subdomain/schema.golden.json rename to pkg/remote/test/route53_record_explicit_subdomain/schema.golden.json diff --git a/pkg/remote/aws/test/route53_record_multiples/aws_route53_record-Z10347383HV75H96J919W_test2_A.res.golden.json b/pkg/remote/test/route53_record_multiples/aws_route53_record-Z10347383HV75H96J919W_test2_A.res.golden.json similarity index 100% rename from pkg/remote/aws/test/route53_record_multiples/aws_route53_record-Z10347383HV75H96J919W_test2_A.res.golden.json rename to pkg/remote/test/route53_record_multiples/aws_route53_record-Z10347383HV75H96J919W_test2_A.res.golden.json diff --git a/pkg/remote/aws/test/route53_record_multiples/aws_route53_record-Z1035360GLIB82T1EH2G_foo-0.com_NS.res.golden.json b/pkg/remote/test/route53_record_multiples/aws_route53_record-Z1035360GLIB82T1EH2G_foo-0.com_NS.res.golden.json similarity index 100% rename from pkg/remote/aws/test/route53_record_multiples/aws_route53_record-Z1035360GLIB82T1EH2G_foo-0.com_NS.res.golden.json rename to pkg/remote/test/route53_record_multiples/aws_route53_record-Z1035360GLIB82T1EH2G_foo-0.com_NS.res.golden.json diff --git a/pkg/remote/aws/test/route53_record_multiples/aws_route53_record-Z1035360GLIB82T1EH2G_test0_A.res.golden.json b/pkg/remote/test/route53_record_multiples/aws_route53_record-Z1035360GLIB82T1EH2G_test0_A.res.golden.json similarity index 100% rename from pkg/remote/aws/test/route53_record_multiples/aws_route53_record-Z1035360GLIB82T1EH2G_test0_A.res.golden.json rename to pkg/remote/test/route53_record_multiples/aws_route53_record-Z1035360GLIB82T1EH2G_test0_A.res.golden.json diff --git a/pkg/remote/aws/test/route53_record_multiples/aws_route53_record-Z1035360GLIB82T1EH2G_test1_A.res.golden.json b/pkg/remote/test/route53_record_multiples/aws_route53_record-Z1035360GLIB82T1EH2G_test1_A.res.golden.json similarity index 100% rename from pkg/remote/aws/test/route53_record_multiples/aws_route53_record-Z1035360GLIB82T1EH2G_test1_A.res.golden.json rename to pkg/remote/test/route53_record_multiples/aws_route53_record-Z1035360GLIB82T1EH2G_test1_A.res.golden.json diff --git a/pkg/remote/aws/test/route53_record_multiples/aws_route53_record-Z1035360GLIB82T1EH2G_test2_A.res.golden.json b/pkg/remote/test/route53_record_multiples/aws_route53_record-Z1035360GLIB82T1EH2G_test2_A.res.golden.json similarity index 100% rename from pkg/remote/aws/test/route53_record_multiples/aws_route53_record-Z1035360GLIB82T1EH2G_test2_A.res.golden.json rename to pkg/remote/test/route53_record_multiples/aws_route53_record-Z1035360GLIB82T1EH2G_test2_A.res.golden.json diff --git a/pkg/remote/aws/test/route53_record_multiples/aws_route53_record-Z1035360GLIB82T1EH2G_test3_A.res.golden.json b/pkg/remote/test/route53_record_multiples/aws_route53_record-Z1035360GLIB82T1EH2G_test3_A.res.golden.json similarity index 100% rename from pkg/remote/aws/test/route53_record_multiples/aws_route53_record-Z1035360GLIB82T1EH2G_test3_A.res.golden.json rename to pkg/remote/test/route53_record_multiples/aws_route53_record-Z1035360GLIB82T1EH2G_test3_A.res.golden.json diff --git a/pkg/remote/aws/test/route53_record_multiples/results.golden.json b/pkg/remote/test/route53_record_multiples/results.golden.json similarity index 100% rename from pkg/remote/aws/test/route53_record_multiples/results.golden.json rename to pkg/remote/test/route53_record_multiples/results.golden.json diff --git a/pkg/remote/aws/test/route53_record_multiples/schema.golden.json b/pkg/remote/test/route53_record_multiples/schema.golden.json similarity index 100% rename from pkg/remote/aws/test/route53_record_multiples/schema.golden.json rename to pkg/remote/test/route53_record_multiples/schema.golden.json From 336c7062bd85ca8b7fa21b6a6d2996e7f2a2398c Mon Sep 17 00:00:00 2001 From: sundowndev Date: Tue, 6 Jul 2021 11:31:51 +0200 Subject: [PATCH 2/3] refactor: simplify code --- pkg/remote/aws/route53_record_enumerator.go | 22 ++++----------------- 1 file changed, 4 insertions(+), 18 deletions(-) diff --git a/pkg/remote/aws/route53_record_enumerator.go b/pkg/remote/aws/route53_record_enumerator.go index 859b2cc7..9f861065 100644 --- a/pkg/remote/aws/route53_record_enumerator.go +++ b/pkg/remote/aws/route53_record_enumerator.go @@ -29,15 +29,15 @@ func (e *Route53RecordEnumerator) SupportedType() resource.ResourceType { func (e *Route53RecordEnumerator) Enumerate() ([]resource.Resource, error) { - zones, err := e.listZones() + zones, err := e.client.ListAllZones() if err != nil { return nil, remoteerror.NewResourceEnumerationErrorWithType(err, resourceaws.AwsRoute53RecordResourceType, resourceaws.AwsRoute53ZoneResourceType) } results := make([]resource.Resource, len(zones)) - for _, zone := range zones { - records, err := e.listRecordsForZone(zone[0], zone[1]) + for _, hostedZone := range zones { + records, err := e.listRecordsForZone(strings.TrimPrefix(*hostedZone.Id, "/hostedzone/")) if err != nil { return nil, remoteerror.NewResourceEnumerationError(err, resourceaws.AwsRoute53RecordResourceType) } @@ -48,21 +48,7 @@ func (e *Route53RecordEnumerator) Enumerate() ([]resource.Resource, error) { return results, err } -func (e *Route53RecordEnumerator) listZones() ([][2]string, error) { - results := make([][2]string, 0) - zones, err := e.client.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 (e *Route53RecordEnumerator) listRecordsForZone(zoneId string, _ string) ([]resource.Resource, error) { +func (e *Route53RecordEnumerator) listRecordsForZone(zoneId string) ([]resource.Resource, error) { records, err := e.client.ListRecordsForZone(zoneId) if err != nil { From 500748f787cc8c03a095c0968c333200fe546936 Mon Sep 17 00:00:00 2001 From: sundowndev Date: Tue, 6 Jul 2021 15:09:14 +0200 Subject: [PATCH 3/3] test: route53 scanner --- pkg/remote/route53_scanner_test.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/pkg/remote/route53_scanner_test.go b/pkg/remote/route53_scanner_test.go index 3b5c32bb..e3a97790 100644 --- a/pkg/remote/route53_scanner_test.go +++ b/pkg/remote/route53_scanner_test.go @@ -367,7 +367,7 @@ func TestRoute53_Record(t *testing.T) { []*route53.HostedZone{}, awserr.NewRequestFailure(nil, 403, "")) }, - err: remoteerror.NewResourceEnumerationErrorWithType(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsRoute53RecordResourceType, resourceaws.AwsRoute53ZoneResourceType), + err: nil, }, { test: "cannot list records", @@ -386,7 +386,7 @@ func TestRoute53_Record(t *testing.T) { awserr.NewRequestFailure(nil, 403, "")) }, - err: remoteerror.NewResourceEnumerationError(awserr.NewRequestFailure(nil, 403, ""), resourceaws.AwsRoute53RecordResourceType), + err: nil, }, } @@ -394,7 +394,6 @@ func TestRoute53_Record(t *testing.T) { resourceaws.InitResourcesMetadata(schemaRepository) factory := terraform.NewTerraformResourceFactory(schemaRepository) deserializer := resource.NewDeserializer(factory) - alerter := &mocks.AlerterInterface{} for _, c := range tests { t.Run(c.test, func(tt *testing.T) { @@ -409,6 +408,8 @@ func TestRoute53_Record(t *testing.T) { 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