driftctl/pkg/scanner.go

70 lines
1.7 KiB
Go
Raw Normal View History

package pkg
import (
"context"
2021-03-26 08:44:55 +00:00
"github.com/cloudskiff/driftctl/pkg/alerter"
"github.com/cloudskiff/driftctl/pkg/parallel"
"github.com/cloudskiff/driftctl/pkg/remote"
2021-03-26 08:44:55 +00:00
"github.com/cloudskiff/driftctl/pkg/resource"
2021-02-09 18:43:39 +00:00
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)
type Scanner struct {
2021-05-21 14:09:45 +00:00
resourceSuppliers []resource.Supplier
runner *parallel.ParallelRunner
alerter *alerter.Alerter
}
2021-05-21 14:09:45 +00:00
func NewScanner(resourceSuppliers []resource.Supplier, alerter *alerter.Alerter) *Scanner {
return &Scanner{
2021-05-21 14:09:45 +00:00
resourceSuppliers: resourceSuppliers,
runner: parallel.NewParallelRunner(context.TODO(), 10),
alerter: alerter,
}
}
func (s *Scanner) Resources() ([]resource.Resource, error) {
for _, resourceProvider := range s.resourceSuppliers {
supplier := resourceProvider
s.runner.Run(func() (interface{}, error) {
res, err := supplier.Resources()
if err != nil {
err := remote.HandleResourceEnumerationError(err, s.alerter)
if err == nil {
return []resource.Resource{}, nil
}
return nil, err
}
for _, resource := range res {
logrus.WithFields(logrus.Fields{
"id": resource.TerraformId(),
"type": resource.TerraformType(),
}).Debug("Found cloud resource")
}
return res, nil
})
}
results := make([]resource.Resource, 0)
loop:
for {
select {
case resources, ok := <-s.runner.Read():
if !ok || resources == nil {
break loop
}
2021-05-21 14:09:45 +00:00
results = append(results, resources.([]resource.Resource)...)
case <-s.runner.DoneChan():
break loop
}
}
return results, s.runner.Err()
}
func (s *Scanner) Stop() {
logrus.Debug("Stopping scanner")
2021-02-09 18:43:39 +00:00
s.runner.Stop(errors.New("interrupted"))
}