driftctl/pkg/remote/aws/s3_bucket_analytic_enumerat...

75 lines
2.2 KiB
Go
Raw Normal View History

2021-07-06 13:00:41 +00:00
package aws
import (
"fmt"
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
tf "github.com/cloudskiff/driftctl/pkg/remote/terraform"
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/resource/aws"
"github.com/sirupsen/logrus"
)
type S3BucketAnalyticEnumerator struct {
repository repository.S3Repository
factory resource.ResourceFactory
providerConfig tf.TerraformProviderConfig
}
func NewS3BucketAnalyticEnumerator(repo repository.S3Repository, factory resource.ResourceFactory, providerConfig tf.TerraformProviderConfig) *S3BucketAnalyticEnumerator {
return &S3BucketAnalyticEnumerator{
repository: repo,
factory: factory,
providerConfig: providerConfig,
}
}
func (e *S3BucketAnalyticEnumerator) SupportedType() resource.ResourceType {
return aws.AwsS3BucketAnalyticsConfigurationResourceType
}
func (e *S3BucketAnalyticEnumerator) Enumerate() ([]resource.Resource, error) {
buckets, err := e.repository.ListAllBuckets()
if err != nil {
return nil, remoteerror.NewResourceScanningErrorWithType(err, string(e.SupportedType()), aws.AwsS3BucketResourceType)
2021-07-06 13:00:41 +00:00
}
results := make([]resource.Resource, len(buckets))
for _, bucket := range buckets {
region, err := e.repository.GetBucketLocation(*bucket.Name)
if err != nil {
return nil, remoteerror.NewResourceScanningErrorWithType(err, string(e.SupportedType()), aws.AwsS3BucketResourceType)
2021-07-06 13:00:41 +00:00
}
if region == "" || region != e.providerConfig.DefaultAlias {
logrus.WithFields(logrus.Fields{
"region": region,
"bucket": *bucket.Name,
}).Debug("Skipped bucket analytic")
continue
}
analyticsConfigurationList, err := e.repository.ListBucketAnalyticsConfigurations(bucket, region)
if err != nil {
return nil, remoteerror.NewResourceScanningError(err, string(e.SupportedType()))
2021-07-06 13:00:41 +00:00
}
for _, analytics := range analyticsConfigurationList {
id := fmt.Sprintf("%s:%s", *bucket.Name, *analytics.Id)
results = append(
results,
e.factory.CreateAbstractResource(
string(e.SupportedType()),
id,
map[string]interface{}{
"region": region,
},
),
)
}
}
return results, err
}