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"
|
2020-12-09 15:31:34 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/remote/deserializer"
|
2021-02-16 17:42:46 +00:00
|
|
|
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
|
2020-12-09 15:31:34 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource/aws"
|
2021-02-16 17:42:46 +00:00
|
|
|
awsdeserializer "github.com/cloudskiff/driftctl/pkg/resource/aws/deserializer"
|
2020-12-09 15:31:34 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/terraform"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
)
|
|
|
|
|
|
|
|
type S3BucketMetricSupplier struct {
|
|
|
|
reader terraform.ResourceReader
|
|
|
|
deserializer deserializer.CTYDeserializer
|
2021-02-16 17:42:46 +00:00
|
|
|
repository repository.S3Repository
|
2020-12-09 15:31:34 +00:00
|
|
|
runner *terraform.ParallelResourceReader
|
|
|
|
}
|
|
|
|
|
2021-02-16 17:42:46 +00:00
|
|
|
func NewS3BucketMetricSupplier(provider *AWSTerraformProvider, repository repository.S3Repository) *S3BucketMetricSupplier {
|
2021-01-20 13:01:57 +00:00
|
|
|
return &S3BucketMetricSupplier{
|
2021-01-22 17:06:17 +00:00
|
|
|
provider,
|
2021-01-20 13:01:57 +00:00
|
|
|
awsdeserializer.NewS3BucketMetricDeserializer(),
|
2021-02-16 17:42:46 +00:00
|
|
|
repository,
|
2021-01-22 17:06:17 +00:00
|
|
|
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
|
2021-01-20 13:01:57 +00:00
|
|
|
}
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *S3BucketMetricSupplier) 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.AwsS3BucketMetricResourceType, aws.AwsS3BucketResourceType)
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
2021-02-16 17:42:46 +00:00
|
|
|
for _, bucket := range buckets {
|
|
|
|
bucket := *bucket
|
|
|
|
region, err := s.repository.GetBucketLocation(&bucket)
|
2020-12-09 15:31:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if region == "" {
|
|
|
|
continue
|
|
|
|
}
|
2021-02-16 17:42:46 +00:00
|
|
|
if err := s.listBucketMetricConfiguration(&bucket, region); err != nil {
|
2021-01-20 13:01:57 +00:00
|
|
|
return nil, remoteerror.NewResourceEnumerationError(err, aws.AwsS3BucketMetricResourceType)
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ctyVals, err := s.runner.Wait()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.deserializer.Deserialize(ctyVals)
|
|
|
|
}
|
|
|
|
|
2021-02-16 17:42:46 +00:00
|
|
|
func (s *S3BucketMetricSupplier) listBucketMetricConfiguration(bucket *s3.Bucket, region string) error {
|
2020-12-09 15:31:34 +00:00
|
|
|
|
2021-02-16 17:42:46 +00:00
|
|
|
metricsConfigurationList, err := s.repository.ListBucketMetricsConfigurations(bucket, region)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, config := range metricsConfigurationList {
|
2021-02-16 17:42:46 +00:00
|
|
|
id := fmt.Sprintf("%s:%s", *bucket.Name, *config.Id)
|
2020-12-09 15:31:34 +00:00
|
|
|
s.runner.Run(func() (cty.Value, error) {
|
|
|
|
s3BucketMetric, err := s.reader.ReadResource(
|
|
|
|
terraform.ReadResourceArgs{
|
|
|
|
Ty: aws.AwsS3BucketMetricResourceType,
|
|
|
|
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 *s3BucketMetric, err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|