Skip to content

Commit 2f07640

Browse files
authored
Merge pull request #619 from cloudskiff/deep_mode
Split suppliers to add deep mode
2 parents 1fde6a0 + 4f44039 commit 2f07640

28 files changed

Lines changed: 352 additions & 304 deletions

pkg/analyser/analyzer.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,22 @@ func (c *ComputedDiffAlert) ShouldIgnoreResource() bool {
3636
return false
3737
}
3838

39+
type AnalyzerOptions struct {
40+
Deep bool
41+
}
42+
3943
type Analyzer struct {
4044
alerter *alerter.Alerter
45+
options AnalyzerOptions
4146
}
4247

4348
type Filter interface {
4449
IsResourceIgnored(res resource.Resource) bool
4550
IsFieldIgnored(res resource.Resource, path []string) bool
4651
}
4752

48-
func NewAnalyzer(alerter *alerter.Alerter) Analyzer {
49-
return Analyzer{alerter}
53+
func NewAnalyzer(alerter *alerter.Alerter, options AnalyzerOptions) Analyzer {
54+
return Analyzer{alerter, options}
5055
}
5156

5257
func (a Analyzer) Analyze(remoteResources, resourcesFromState []resource.Resource, filter Filter) (Analysis, error) {
@@ -78,6 +83,11 @@ func (a Analyzer) Analyze(remoteResources, resourcesFromState []resource.Resourc
7883
filteredRemoteResource = removeResourceByIndex(i, filteredRemoteResource)
7984
analysis.AddManaged(stateRes)
8085

86+
// Stop there if we are not in deep mode, we do not want to compute diffs
87+
if !a.options.Deep {
88+
continue
89+
}
90+
8191
var delta diff.Changelog
8292
delta, _ = diff.Diff(stateRes.Attributes(), remoteRes.Attributes())
8393

pkg/analyser/analyzer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ func TestAnalyze(t *testing.T) {
955955
repo := testresource.InitFakeSchemaRepository("aws", "3.19.0")
956956
aws.InitResourcesMetadata(repo)
957957

958-
analyzer := NewAnalyzer(al)
958+
analyzer := NewAnalyzer(al, AnalyzerOptions{Deep: true})
959959

960960
for _, res := range c.cloud {
961961
addSchemaToRes(res, repo)

pkg/cmd/scan.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"syscall"
1010
"time"
1111

12+
"github.com/cloudskiff/driftctl/pkg/remote/common"
1213
"github.com/cloudskiff/driftctl/pkg/telemetry"
1314
"github.com/fatih/color"
1415
"github.com/mitchellh/go-homedir"
@@ -31,7 +32,7 @@ import (
3132
)
3233

3334
func NewScanCmd() *cobra.Command {
34-
opts := &pkg.ScanOptions{}
35+
opts := &pkg.ScanOptions{Deep: true}
3536
opts.BackendOptions = &backend.Options{}
3637

3738
cmd := &cobra.Command{
@@ -184,8 +185,10 @@ func scanRun(opts *pkg.ScanOptions) error {
184185
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
185186

186187
alerter := alerter.NewAlerter()
188+
187189
providerLibrary := terraform.NewProviderLibrary()
188190
supplierLibrary := resource.NewSupplierLibrary()
191+
remoteLibrary := common.NewRemoteLibrary()
189192

190193
iacProgress := globaloutput.NewProgress("Scanning states", "Scanned states", true)
191194
scanProgress := globaloutput.NewProgress("Scanning resources", "Scanned resources", false)
@@ -194,7 +197,7 @@ func scanRun(opts *pkg.ScanOptions) error {
194197

195198
resFactory := terraform.NewTerraformResourceFactory(resourceSchemaRepository)
196199

197-
err := remote.Activate(opts.To, opts.ProviderVersion, alerter, providerLibrary, supplierLibrary, scanProgress, resourceSchemaRepository, resFactory, opts.ConfigDir)
200+
err := remote.Activate(opts.To, opts.ProviderVersion, alerter, providerLibrary, supplierLibrary, remoteLibrary, scanProgress, resourceSchemaRepository, resFactory, opts.ConfigDir)
198201
if err != nil {
199202
return err
200203
}
@@ -206,7 +209,7 @@ func scanRun(opts *pkg.ScanOptions) error {
206209
logrus.Trace("Exited")
207210
}()
208211

209-
scanner := pkg.NewScanner(supplierLibrary.Suppliers(), alerter)
212+
scanner := pkg.NewScanner(supplierLibrary.Suppliers(), remoteLibrary, alerter, pkg.ScannerOptions{Deep: opts.Deep})
210213

211214
iacSupplier, err := supplier.GetIACSupplier(opts.From, providerLibrary, opts.BackendOptions, iacProgress, resFactory)
212215
if err != nil {

pkg/driftctl.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ type ScanOptions struct {
3232
ProviderVersion string
3333
ConfigDir string
3434
DriftignorePath string
35+
Deep bool
3536
}
3637

3738
type DriftCTL struct {
@@ -58,7 +59,7 @@ func NewDriftCTL(remoteSupplier resource.Supplier,
5859
remoteSupplier,
5960
iacSupplier,
6061
alerter,
61-
analyser.NewAnalyzer(alerter),
62+
analyser.NewAnalyzer(alerter, analyser.AnalyzerOptions{Deep: opts.Deep}),
6263
resFactory,
6364
scanProgress,
6465
iacProgress,

pkg/driftctl_test.go

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ func runTest(t *testing.T, cases TestCases) {
8787
}
8888

8989
if c.options == nil {
90-
c.options = &pkg.ScanOptions{}
90+
c.options = &pkg.ScanOptions{Deep: true}
9191
}
9292

9393
scanProgress := &output.MockProgress{}
@@ -140,7 +140,7 @@ func TestDriftctlRun_BasicBehavior(t *testing.T) {
140140
result.AssertInfrastructureIsInSync()
141141
},
142142
options: func(t *testing.T) *pkg.ScanOptions {
143-
return &pkg.ScanOptions{}
143+
return &pkg.ScanOptions{Deep: true}
144144
}(t),
145145
},
146146
{
@@ -152,9 +152,6 @@ func TestDriftctlRun_BasicBehavior(t *testing.T) {
152152
assert: func(result *test.ScanResult, err error) {
153153
result.AssertDeletedCount(1)
154154
},
155-
options: func(t *testing.T) *pkg.ScanOptions {
156-
return &pkg.ScanOptions{}
157-
}(t),
158155
},
159156
{
160157
name: "we should have unmanaged resource",
@@ -165,9 +162,6 @@ func TestDriftctlRun_BasicBehavior(t *testing.T) {
165162
assert: func(result *test.ScanResult, err error) {
166163
result.AssertUnmanagedCount(1)
167164
},
168-
options: func(t *testing.T) *pkg.ScanOptions {
169-
return &pkg.ScanOptions{}
170-
}(t),
171165
},
172166
{
173167
name: "we should have changes of field update",
@@ -199,9 +193,6 @@ func TestDriftctlRun_BasicBehavior(t *testing.T) {
199193
Computed: false,
200194
})
201195
},
202-
options: func(t *testing.T) *pkg.ScanOptions {
203-
return &pkg.ScanOptions{}
204-
}(t),
205196
},
206197
{
207198
name: "we should have changes on computed field",
@@ -235,9 +226,6 @@ func TestDriftctlRun_BasicBehavior(t *testing.T) {
235226
Computed: true,
236227
})
237228
},
238-
options: func(t *testing.T) *pkg.ScanOptions {
239-
return &pkg.ScanOptions{}
240-
}(t),
241229
},
242230
{
243231
name: "we should have changes on deleted field",
@@ -271,9 +259,6 @@ func TestDriftctlRun_BasicBehavior(t *testing.T) {
271259
Computed: false,
272260
})
273261
},
274-
options: func(t *testing.T) *pkg.ScanOptions {
275-
return &pkg.ScanOptions{}
276-
}(t),
277262
},
278263
{
279264
name: "we should have changes of added field",
@@ -307,9 +292,6 @@ func TestDriftctlRun_BasicBehavior(t *testing.T) {
307292
Computed: false,
308293
})
309294
},
310-
options: func(t *testing.T) *pkg.ScanOptions {
311-
return &pkg.ScanOptions{}
312-
}(t),
313295
},
314296
{
315297
name: "we should ignore default AWS IAM role when strict mode is disabled",
@@ -396,6 +378,7 @@ func TestDriftctlRun_BasicBehavior(t *testing.T) {
396378
options: func(t *testing.T) *pkg.ScanOptions {
397379
return &pkg.ScanOptions{
398380
StrictMode: false,
381+
Deep: true,
399382
}
400383
}(t),
401384
},
@@ -484,6 +467,7 @@ func TestDriftctlRun_BasicBehavior(t *testing.T) {
484467
options: func(t *testing.T) *pkg.ScanOptions {
485468
return &pkg.ScanOptions{
486469
StrictMode: true,
470+
Deep: true,
487471
}
488472
}(t),
489473
},
@@ -581,6 +565,7 @@ func TestDriftctlRun_BasicBehavior(t *testing.T) {
581565
return &pkg.ScanOptions{
582566
Filter: f,
583567
StrictMode: true,
568+
Deep: true,
584569
}
585570
}(t),
586571
},
@@ -617,7 +602,7 @@ func TestDriftctlRun_BasicFilter(t *testing.T) {
617602
t.Fatalf("Unable to build filter expression: %s\n%s", filterStr, err)
618603
}
619604

620-
return &pkg.ScanOptions{Filter: f}
605+
return &pkg.ScanOptions{Filter: f, Deep: true}
621606
}(t),
622607
},
623608
{
@@ -646,7 +631,7 @@ func TestDriftctlRun_BasicFilter(t *testing.T) {
646631
t.Fatalf("Unable to build filter expression: %s\n%s", filterStr, err)
647632
}
648633

649-
return &pkg.ScanOptions{Filter: f}
634+
return &pkg.ScanOptions{Filter: f, Deep: true}
650635
}(t),
651636
},
652637
{
@@ -677,7 +662,7 @@ func TestDriftctlRun_BasicFilter(t *testing.T) {
677662
t.Fatalf("Unable to build filter expression: %s\n%s", filterStr, err)
678663
}
679664

680-
return &pkg.ScanOptions{Filter: f}
665+
return &pkg.ScanOptions{Filter: f, Deep: true}
681666
}(t),
682667
},
683668
}
@@ -751,7 +736,7 @@ func TestDriftctlRun_Middlewares(t *testing.T) {
751736
t.Fatalf("Unable to build filter expression: %s\n%s", filterStr, err)
752737
}
753738

754-
return &pkg.ScanOptions{Filter: f}
739+
return &pkg.ScanOptions{Filter: f, Deep: true}
755740
}(t),
756741
},
757742
{
@@ -864,7 +849,7 @@ func TestDriftctlRun_Middlewares(t *testing.T) {
864849
t.Fatalf("Unable to build filter expression: %s\n%s", filterStr, err)
865850
}
866851

867-
return &pkg.ScanOptions{Filter: f}
852+
return &pkg.ScanOptions{Filter: f, Deep: true}
868853
}(t),
869854
},
870855
{
@@ -963,7 +948,7 @@ func TestDriftctlRun_Middlewares(t *testing.T) {
963948
t.Fatalf("Unable to build filter expression: %s\n%s", filterStr, err)
964949
}
965950

966-
return &pkg.ScanOptions{Filter: f}
951+
return &pkg.ScanOptions{Filter: f, Deep: true}
967952
}(t),
968953
},
969954
{
@@ -1026,7 +1011,7 @@ func TestDriftctlRun_Middlewares(t *testing.T) {
10261011
t.Fatalf("Unable to build filter expression: %s\n%s", filterStr, err)
10271012
}
10281013

1029-
return &pkg.ScanOptions{Filter: f}
1014+
return &pkg.ScanOptions{Filter: f, Deep: true}
10301015
}(t),
10311016
},
10321017
{
@@ -1088,7 +1073,7 @@ func TestDriftctlRun_Middlewares(t *testing.T) {
10881073
t.Fatalf("Unable to build filter expression: %s\n%s", filterStr, err)
10891074
}
10901075

1091-
return &pkg.ScanOptions{Filter: f}
1076+
return &pkg.ScanOptions{Filter: f, Deep: true}
10921077
}(t),
10931078
},
10941079
{
@@ -1404,7 +1389,7 @@ func TestDriftctlRun_Middlewares(t *testing.T) {
14041389
t.Fatalf("Unable to build filter expression: %s\n%s", filterStr, err)
14051390
}
14061391

1407-
return &pkg.ScanOptions{Filter: f}
1392+
return &pkg.ScanOptions{Filter: f, Deep: true}
14081393
}(t),
14091394
},
14101395
{
@@ -1542,7 +1527,7 @@ func TestDriftctlRun_Middlewares(t *testing.T) {
15421527
t.Fatalf("Unable to build filter expression: %s\n%s", filterStr, err)
15431528
}
15441529

1545-
return &pkg.ScanOptions{Filter: f}
1530+
return &pkg.ScanOptions{Filter: f, Deep: true}
15461531
}(t),
15471532
},
15481533
{

pkg/remote/aws/init.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/cloudskiff/driftctl/pkg/remote/aws/client"
77
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
88
"github.com/cloudskiff/driftctl/pkg/remote/cache"
9+
"github.com/cloudskiff/driftctl/pkg/remote/common"
910
"github.com/cloudskiff/driftctl/pkg/resource"
1011
"github.com/cloudskiff/driftctl/pkg/resource/aws"
1112
"github.com/cloudskiff/driftctl/pkg/terraform"
@@ -21,6 +22,7 @@ const RemoteAWSTerraform = "aws+tf"
2122
func Init(version string, alerter *alerter.Alerter,
2223
providerLibrary *terraform.ProviderLibrary,
2324
supplierLibrary *resource.SupplierLibrary,
25+
remoteLibrary *common.RemoteLibrary,
2426
progress output.Progress,
2527
resourceSchemaRepository *resource.SchemaRepository,
2628
factory resource.ResourceFactory,
@@ -56,7 +58,9 @@ func Init(version string, alerter *alerter.Alerter,
5658
deserializer := resource.NewDeserializer(factory)
5759
providerLibrary.AddProvider(terraform.AWS, provider)
5860

59-
supplierLibrary.AddSupplier(NewS3BucketSupplier(provider, s3Repository, deserializer))
61+
remoteLibrary.AddEnumerator(NewS3BucketEnumerator(s3Repository, factory, provider.Config))
62+
remoteLibrary.AddDetailsFetcher(aws.AwsS3BucketResourceType, NewS3BucketDetailsFetcher(provider, deserializer))
63+
6064
supplierLibrary.AddSupplier(NewS3BucketAnalyticSupplier(provider, s3Repository, deserializer))
6165
supplierLibrary.AddSupplier(NewS3BucketInventorySupplier(provider, s3Repository, deserializer))
6266
supplierLibrary.AddSupplier(NewS3BucketMetricSupplier(provider, s3Repository, deserializer))

pkg/remote/aws/repository/mock_S3Repository.go

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/remote/aws/repository/s3_repository.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type S3Repository interface {
1717
ListBucketInventoryConfigurations(bucket *s3.Bucket, region string) ([]*s3.InventoryConfiguration, error)
1818
ListBucketMetricsConfigurations(bucket *s3.Bucket, region string) ([]*s3.MetricsConfiguration, error)
1919
ListBucketAnalyticsConfigurations(bucket *s3.Bucket, region string) ([]*s3.AnalyticsConfiguration, error)
20-
GetBucketLocation(bucket *s3.Bucket) (string, error)
20+
GetBucketLocation(bucketName string) (string, error)
2121
}
2222

2323
type s3Repository struct {
@@ -148,19 +148,19 @@ func (s *s3Repository) ListBucketAnalyticsConfigurations(bucket *s3.Bucket, regi
148148
return analyticsConfigurationList, nil
149149
}
150150

151-
func (s *s3Repository) GetBucketLocation(bucket *s3.Bucket) (string, error) {
152-
cacheKey := fmt.Sprintf("s3GetBucketLocation_%s", *bucket.Name)
151+
func (s *s3Repository) GetBucketLocation(bucketName string) (string, error) {
152+
cacheKey := fmt.Sprintf("s3GetBucketLocation_%s", bucketName)
153153
if v := s.cache.Get(cacheKey); v != nil {
154154
return v.(string), nil
155155
}
156156

157-
bucketLocationRequest := s3.GetBucketLocationInput{Bucket: bucket.Name}
157+
bucketLocationRequest := s3.GetBucketLocationInput{Bucket: &bucketName}
158158
bucketLocationResponse, err := s.clientFactory.GetS3Client(nil).GetBucketLocation(&bucketLocationRequest)
159159
if err != nil {
160160
awsErr, ok := err.(awserr.Error)
161161
if ok && awsErr.Code() == s3.ErrCodeNoSuchBucket {
162162
logrus.WithFields(logrus.Fields{
163-
"bucket": *bucket.Name,
163+
"bucket": bucketName,
164164
}).Warning("Unable to retrieve bucket region, this may be an inconsistency in S3 api for fresh deleted bucket, skipping ...")
165165
return "", nil
166166
}

0 commit comments

Comments
 (0)