Skip to content

Commit

Permalink
Merge pull request #619 from cloudskiff/deep_mode
Browse files Browse the repository at this point in the history
Split suppliers to add deep mode
  • Loading branch information
eliecharra authored Jun 23, 2021
2 parents 1fde6a0 + 4f44039 commit 2f07640
Show file tree
Hide file tree
Showing 28 changed files with 352 additions and 304 deletions.
14 changes: 12 additions & 2 deletions pkg/analyser/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,22 @@ func (c *ComputedDiffAlert) ShouldIgnoreResource() bool {
return false
}

type AnalyzerOptions struct {
Deep bool
}

type Analyzer struct {
alerter *alerter.Alerter
options AnalyzerOptions
}

type Filter interface {
IsResourceIgnored(res resource.Resource) bool
IsFieldIgnored(res resource.Resource, path []string) bool
}

func NewAnalyzer(alerter *alerter.Alerter) Analyzer {
return Analyzer{alerter}
func NewAnalyzer(alerter *alerter.Alerter, options AnalyzerOptions) Analyzer {
return Analyzer{alerter, options}
}

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

// Stop there if we are not in deep mode, we do not want to compute diffs
if !a.options.Deep {
continue
}

var delta diff.Changelog
delta, _ = diff.Diff(stateRes.Attributes(), remoteRes.Attributes())

Expand Down
2 changes: 1 addition & 1 deletion pkg/analyser/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -955,7 +955,7 @@ func TestAnalyze(t *testing.T) {
repo := testresource.InitFakeSchemaRepository("aws", "3.19.0")
aws.InitResourcesMetadata(repo)

analyzer := NewAnalyzer(al)
analyzer := NewAnalyzer(al, AnalyzerOptions{Deep: true})

for _, res := range c.cloud {
addSchemaToRes(res, repo)
Expand Down
9 changes: 6 additions & 3 deletions pkg/cmd/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"syscall"
"time"

"github.com/cloudskiff/driftctl/pkg/remote/common"
"github.com/cloudskiff/driftctl/pkg/telemetry"
"github.com/fatih/color"
"github.com/mitchellh/go-homedir"
Expand All @@ -31,7 +32,7 @@ import (
)

func NewScanCmd() *cobra.Command {
opts := &pkg.ScanOptions{}
opts := &pkg.ScanOptions{Deep: true}
opts.BackendOptions = &backend.Options{}

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

alerter := alerter.NewAlerter()

providerLibrary := terraform.NewProviderLibrary()
supplierLibrary := resource.NewSupplierLibrary()
remoteLibrary := common.NewRemoteLibrary()

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

resFactory := terraform.NewTerraformResourceFactory(resourceSchemaRepository)

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

scanner := pkg.NewScanner(supplierLibrary.Suppliers(), alerter)
scanner := pkg.NewScanner(supplierLibrary.Suppliers(), remoteLibrary, alerter, pkg.ScannerOptions{Deep: opts.Deep})

iacSupplier, err := supplier.GetIACSupplier(opts.From, providerLibrary, opts.BackendOptions, iacProgress, resFactory)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion pkg/driftctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type ScanOptions struct {
ProviderVersion string
ConfigDir string
DriftignorePath string
Deep bool
}

type DriftCTL struct {
Expand All @@ -58,7 +59,7 @@ func NewDriftCTL(remoteSupplier resource.Supplier,
remoteSupplier,
iacSupplier,
alerter,
analyser.NewAnalyzer(alerter),
analyser.NewAnalyzer(alerter, analyser.AnalyzerOptions{Deep: opts.Deep}),
resFactory,
scanProgress,
iacProgress,
Expand Down
45 changes: 15 additions & 30 deletions pkg/driftctl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func runTest(t *testing.T, cases TestCases) {
}

if c.options == nil {
c.options = &pkg.ScanOptions{}
c.options = &pkg.ScanOptions{Deep: true}
}

scanProgress := &output.MockProgress{}
Expand Down Expand Up @@ -140,7 +140,7 @@ func TestDriftctlRun_BasicBehavior(t *testing.T) {
result.AssertInfrastructureIsInSync()
},
options: func(t *testing.T) *pkg.ScanOptions {
return &pkg.ScanOptions{}
return &pkg.ScanOptions{Deep: true}
}(t),
},
{
Expand All @@ -152,9 +152,6 @@ func TestDriftctlRun_BasicBehavior(t *testing.T) {
assert: func(result *test.ScanResult, err error) {
result.AssertDeletedCount(1)
},
options: func(t *testing.T) *pkg.ScanOptions {
return &pkg.ScanOptions{}
}(t),
},
{
name: "we should have unmanaged resource",
Expand All @@ -165,9 +162,6 @@ func TestDriftctlRun_BasicBehavior(t *testing.T) {
assert: func(result *test.ScanResult, err error) {
result.AssertUnmanagedCount(1)
},
options: func(t *testing.T) *pkg.ScanOptions {
return &pkg.ScanOptions{}
}(t),
},
{
name: "we should have changes of field update",
Expand Down Expand Up @@ -199,9 +193,6 @@ func TestDriftctlRun_BasicBehavior(t *testing.T) {
Computed: false,
})
},
options: func(t *testing.T) *pkg.ScanOptions {
return &pkg.ScanOptions{}
}(t),
},
{
name: "we should have changes on computed field",
Expand Down Expand Up @@ -235,9 +226,6 @@ func TestDriftctlRun_BasicBehavior(t *testing.T) {
Computed: true,
})
},
options: func(t *testing.T) *pkg.ScanOptions {
return &pkg.ScanOptions{}
}(t),
},
{
name: "we should have changes on deleted field",
Expand Down Expand Up @@ -271,9 +259,6 @@ func TestDriftctlRun_BasicBehavior(t *testing.T) {
Computed: false,
})
},
options: func(t *testing.T) *pkg.ScanOptions {
return &pkg.ScanOptions{}
}(t),
},
{
name: "we should have changes of added field",
Expand Down Expand Up @@ -307,9 +292,6 @@ func TestDriftctlRun_BasicBehavior(t *testing.T) {
Computed: false,
})
},
options: func(t *testing.T) *pkg.ScanOptions {
return &pkg.ScanOptions{}
}(t),
},
{
name: "we should ignore default AWS IAM role when strict mode is disabled",
Expand Down Expand Up @@ -396,6 +378,7 @@ func TestDriftctlRun_BasicBehavior(t *testing.T) {
options: func(t *testing.T) *pkg.ScanOptions {
return &pkg.ScanOptions{
StrictMode: false,
Deep: true,
}
}(t),
},
Expand Down Expand Up @@ -484,6 +467,7 @@ func TestDriftctlRun_BasicBehavior(t *testing.T) {
options: func(t *testing.T) *pkg.ScanOptions {
return &pkg.ScanOptions{
StrictMode: true,
Deep: true,
}
}(t),
},
Expand Down Expand Up @@ -581,6 +565,7 @@ func TestDriftctlRun_BasicBehavior(t *testing.T) {
return &pkg.ScanOptions{
Filter: f,
StrictMode: true,
Deep: true,
}
}(t),
},
Expand Down Expand Up @@ -617,7 +602,7 @@ func TestDriftctlRun_BasicFilter(t *testing.T) {
t.Fatalf("Unable to build filter expression: %s\n%s", filterStr, err)
}

return &pkg.ScanOptions{Filter: f}
return &pkg.ScanOptions{Filter: f, Deep: true}
}(t),
},
{
Expand Down Expand Up @@ -646,7 +631,7 @@ func TestDriftctlRun_BasicFilter(t *testing.T) {
t.Fatalf("Unable to build filter expression: %s\n%s", filterStr, err)
}

return &pkg.ScanOptions{Filter: f}
return &pkg.ScanOptions{Filter: f, Deep: true}
}(t),
},
{
Expand Down Expand Up @@ -677,7 +662,7 @@ func TestDriftctlRun_BasicFilter(t *testing.T) {
t.Fatalf("Unable to build filter expression: %s\n%s", filterStr, err)
}

return &pkg.ScanOptions{Filter: f}
return &pkg.ScanOptions{Filter: f, Deep: true}
}(t),
},
}
Expand Down Expand Up @@ -751,7 +736,7 @@ func TestDriftctlRun_Middlewares(t *testing.T) {
t.Fatalf("Unable to build filter expression: %s\n%s", filterStr, err)
}

return &pkg.ScanOptions{Filter: f}
return &pkg.ScanOptions{Filter: f, Deep: true}
}(t),
},
{
Expand Down Expand Up @@ -864,7 +849,7 @@ func TestDriftctlRun_Middlewares(t *testing.T) {
t.Fatalf("Unable to build filter expression: %s\n%s", filterStr, err)
}

return &pkg.ScanOptions{Filter: f}
return &pkg.ScanOptions{Filter: f, Deep: true}
}(t),
},
{
Expand Down Expand Up @@ -963,7 +948,7 @@ func TestDriftctlRun_Middlewares(t *testing.T) {
t.Fatalf("Unable to build filter expression: %s\n%s", filterStr, err)
}

return &pkg.ScanOptions{Filter: f}
return &pkg.ScanOptions{Filter: f, Deep: true}
}(t),
},
{
Expand Down Expand Up @@ -1026,7 +1011,7 @@ func TestDriftctlRun_Middlewares(t *testing.T) {
t.Fatalf("Unable to build filter expression: %s\n%s", filterStr, err)
}

return &pkg.ScanOptions{Filter: f}
return &pkg.ScanOptions{Filter: f, Deep: true}
}(t),
},
{
Expand Down Expand Up @@ -1088,7 +1073,7 @@ func TestDriftctlRun_Middlewares(t *testing.T) {
t.Fatalf("Unable to build filter expression: %s\n%s", filterStr, err)
}

return &pkg.ScanOptions{Filter: f}
return &pkg.ScanOptions{Filter: f, Deep: true}
}(t),
},
{
Expand Down Expand Up @@ -1404,7 +1389,7 @@ func TestDriftctlRun_Middlewares(t *testing.T) {
t.Fatalf("Unable to build filter expression: %s\n%s", filterStr, err)
}

return &pkg.ScanOptions{Filter: f}
return &pkg.ScanOptions{Filter: f, Deep: true}
}(t),
},
{
Expand Down Expand Up @@ -1542,7 +1527,7 @@ func TestDriftctlRun_Middlewares(t *testing.T) {
t.Fatalf("Unable to build filter expression: %s\n%s", filterStr, err)
}

return &pkg.ScanOptions{Filter: f}
return &pkg.ScanOptions{Filter: f, Deep: true}
}(t),
},
{
Expand Down
6 changes: 5 additions & 1 deletion pkg/remote/aws/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/cloudskiff/driftctl/pkg/remote/aws/client"
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
"github.com/cloudskiff/driftctl/pkg/remote/cache"
"github.com/cloudskiff/driftctl/pkg/remote/common"
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/resource/aws"
"github.com/cloudskiff/driftctl/pkg/terraform"
Expand All @@ -21,6 +22,7 @@ const RemoteAWSTerraform = "aws+tf"
func Init(version string, alerter *alerter.Alerter,
providerLibrary *terraform.ProviderLibrary,
supplierLibrary *resource.SupplierLibrary,
remoteLibrary *common.RemoteLibrary,
progress output.Progress,
resourceSchemaRepository *resource.SchemaRepository,
factory resource.ResourceFactory,
Expand Down Expand Up @@ -56,7 +58,9 @@ func Init(version string, alerter *alerter.Alerter,
deserializer := resource.NewDeserializer(factory)
providerLibrary.AddProvider(terraform.AWS, provider)

supplierLibrary.AddSupplier(NewS3BucketSupplier(provider, s3Repository, deserializer))
remoteLibrary.AddEnumerator(NewS3BucketEnumerator(s3Repository, factory, provider.Config))
remoteLibrary.AddDetailsFetcher(aws.AwsS3BucketResourceType, NewS3BucketDetailsFetcher(provider, deserializer))

supplierLibrary.AddSupplier(NewS3BucketAnalyticSupplier(provider, s3Repository, deserializer))
supplierLibrary.AddSupplier(NewS3BucketInventorySupplier(provider, s3Repository, deserializer))
supplierLibrary.AddSupplier(NewS3BucketMetricSupplier(provider, s3Repository, deserializer))
Expand Down
16 changes: 8 additions & 8 deletions pkg/remote/aws/repository/mock_S3Repository.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions pkg/remote/aws/repository/s3_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type S3Repository interface {
ListBucketInventoryConfigurations(bucket *s3.Bucket, region string) ([]*s3.InventoryConfiguration, error)
ListBucketMetricsConfigurations(bucket *s3.Bucket, region string) ([]*s3.MetricsConfiguration, error)
ListBucketAnalyticsConfigurations(bucket *s3.Bucket, region string) ([]*s3.AnalyticsConfiguration, error)
GetBucketLocation(bucket *s3.Bucket) (string, error)
GetBucketLocation(bucketName string) (string, error)
}

type s3Repository struct {
Expand Down Expand Up @@ -148,19 +148,19 @@ func (s *s3Repository) ListBucketAnalyticsConfigurations(bucket *s3.Bucket, regi
return analyticsConfigurationList, nil
}

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

bucketLocationRequest := s3.GetBucketLocationInput{Bucket: bucket.Name}
bucketLocationRequest := s3.GetBucketLocationInput{Bucket: &bucketName}
bucketLocationResponse, err := s.clientFactory.GetS3Client(nil).GetBucketLocation(&bucketLocationRequest)
if err != nil {
awsErr, ok := err.(awserr.Error)
if ok && awsErr.Code() == s3.ErrCodeNoSuchBucket {
logrus.WithFields(logrus.Fields{
"bucket": *bucket.Name,
"bucket": bucketName,
}).Warning("Unable to retrieve bucket region, this may be an inconsistency in S3 api for fresh deleted bucket, skipping ...")
return "", nil
}
Expand Down
Loading

0 comments on commit 2f07640

Please sign in to comment.