2020-12-09 15:31:34 +00:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/service/s3"
|
2021-02-16 17:42:46 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
|
|
|
|
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
|
2021-03-18 11:10:46 +00:00
|
|
|
tf "github.com/cloudskiff/driftctl/pkg/remote/terraform"
|
2020-12-09 15:31:34 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource/aws"
|
2021-05-21 14:09:45 +00:00
|
|
|
|
2020-12-09 15:31:34 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/terraform"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
)
|
|
|
|
|
|
|
|
type S3BucketAnalyticSupplier struct {
|
2021-03-18 11:10:46 +00:00
|
|
|
reader terraform.ResourceReader
|
2021-05-21 14:09:45 +00:00
|
|
|
deserializer *resource.Deserializer
|
2021-03-18 11:10:46 +00:00
|
|
|
repository repository.S3Repository
|
|
|
|
runner *terraform.ParallelResourceReader
|
|
|
|
providerConfig tf.TerraformProviderConfig
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
2021-05-21 14:09:45 +00:00
|
|
|
func NewS3BucketAnalyticSupplier(provider *AWSTerraformProvider, repository repository.S3Repository, deserializer *resource.Deserializer) *S3BucketAnalyticSupplier {
|
2021-01-20 13:01:57 +00:00
|
|
|
return &S3BucketAnalyticSupplier{
|
2021-01-22 17:06:17 +00:00
|
|
|
provider,
|
2021-05-21 14:09:45 +00:00
|
|
|
deserializer,
|
2021-02-16 17:42:46 +00:00
|
|
|
repository,
|
2021-01-22 17:06:17 +00:00
|
|
|
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
|
2021-03-18 11:10:46 +00:00
|
|
|
provider.Config,
|
2021-01-20 13:01:57 +00:00
|
|
|
}
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *S3BucketAnalyticSupplier) Resources() ([]resource.Resource, error) {
|
2021-02-16 17:42:46 +00:00
|
|
|
buckets, err := s.repository.ListAllBuckets()
|
2020-12-09 15:31:34 +00:00
|
|
|
if err != nil {
|
2021-01-20 13:01:57 +00:00
|
|
|
return nil, remoteerror.NewResourceEnumerationErrorWithType(err, aws.AwsS3BucketAnalyticsConfigurationResourceType, aws.AwsS3BucketResourceType)
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
2021-02-16 17:42:46 +00:00
|
|
|
for _, bucket := range buckets {
|
|
|
|
bucket := *bucket
|
2021-06-11 15:10:06 +00:00
|
|
|
region, err := s.repository.GetBucketLocation(*bucket.Name)
|
2020-12-09 15:31:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-18 11:10:46 +00:00
|
|
|
if region == "" || region != s.providerConfig.DefaultAlias {
|
2020-12-09 15:31:34 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-02-16 17:42:46 +00:00
|
|
|
if err := s.listBucketAnalyticConfiguration(&bucket, region); err != nil {
|
2021-01-20 13:01:57 +00:00
|
|
|
return nil, remoteerror.NewResourceEnumerationError(err, aws.AwsS3BucketAnalyticsConfigurationResourceType)
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ctyVals, err := s.runner.Wait()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-21 14:09:45 +00:00
|
|
|
return s.deserializer.Deserialize(aws.AwsS3BucketAnalyticsConfigurationResourceType, ctyVals)
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
2021-02-16 17:42:46 +00:00
|
|
|
func (s *S3BucketAnalyticSupplier) listBucketAnalyticConfiguration(bucket *s3.Bucket, region string) error {
|
2020-12-09 15:31:34 +00:00
|
|
|
|
2021-02-16 17:42:46 +00:00
|
|
|
analyticsConfigurationList, err := s.repository.ListBucketAnalyticsConfigurations(bucket, region)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, analytics := range analyticsConfigurationList {
|
2021-02-16 17:42:46 +00:00
|
|
|
id := fmt.Sprintf("%s:%s", *bucket.Name, *analytics.Id)
|
2020-12-09 15:31:34 +00:00
|
|
|
s.runner.Run(func() (cty.Value, error) {
|
|
|
|
s3BucketAnalytic, err := s.reader.ReadResource(
|
|
|
|
terraform.ReadResourceArgs{
|
|
|
|
Ty: aws.AwsS3BucketAnalyticsConfigurationResourceType,
|
|
|
|
ID: id,
|
|
|
|
Attributes: map[string]string{
|
2021-02-09 13:54:56 +00:00
|
|
|
"alias": region,
|
2020-12-09 15:31:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return cty.NilVal, err
|
|
|
|
}
|
|
|
|
return *s3BucketAnalytic, err
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|