2020-12-09 15:31:34 +00:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/aws/aws-sdk-go/service/s3"
|
2021-03-17 16:23:36 +00:00
|
|
|
|
2021-03-17 18:38:58 +00:00
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
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"
|
2021-03-17 18:38:58 +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"
|
|
|
|
awsdeserializer "github.com/cloudskiff/driftctl/pkg/resource/aws/deserializer"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
type S3BucketSupplier struct {
|
2021-03-17 18:38:58 +00:00
|
|
|
reader terraform.ResourceReader
|
|
|
|
deserializer deserializer.CTYDeserializer
|
|
|
|
repository repository.S3Repository
|
|
|
|
runner *terraform.ParallelResourceReader
|
|
|
|
providerConfig tf.TerraformProviderConfig
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
2021-02-16 17:42:46 +00:00
|
|
|
func NewS3BucketSupplier(provider *AWSTerraformProvider, repository repository.S3Repository) *S3BucketSupplier {
|
2021-01-20 13:01:57 +00:00
|
|
|
return &S3BucketSupplier{
|
2021-01-22 17:06:17 +00:00
|
|
|
provider,
|
2021-01-20 13:01:57 +00:00
|
|
|
awsdeserializer.NewS3BucketDeserializer(),
|
2021-02-16 17:42:46 +00:00
|
|
|
repository,
|
2021-01-22 17:06:17 +00:00
|
|
|
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
|
2021-03-17 18:38:58 +00:00
|
|
|
provider.Config,
|
2021-01-20 13:01:57 +00:00
|
|
|
}
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
2021-03-17 15:54:53 +00:00
|
|
|
func (s *S3BucketSupplier) 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.NewResourceEnumerationError(err, aws.AwsS3BucketResourceType)
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
2021-02-16 17:42:46 +00:00
|
|
|
for _, bucket := range buckets {
|
2020-12-09 15:31:34 +00:00
|
|
|
b := *bucket
|
|
|
|
s.runner.Run(func() (cty.Value, error) {
|
2021-02-16 17:42:46 +00:00
|
|
|
return s.readBucket(b)
|
2020-12-09 15:31:34 +00:00
|
|
|
})
|
|
|
|
}
|
2021-02-16 17:42:46 +00:00
|
|
|
values, err := s.runner.Wait()
|
2020-12-09 15:31:34 +00:00
|
|
|
if err != nil {
|
2021-02-16 17:42:46 +00:00
|
|
|
return nil, err
|
2021-02-18 16:28:24 +00:00
|
|
|
}
|
|
|
|
|
2021-02-16 17:42:46 +00:00
|
|
|
return s.deserializer.Deserialize(values)
|
2020-12-09 15:31:34 +00:00
|
|
|
}
|
|
|
|
|
2021-02-16 17:42:46 +00:00
|
|
|
func (s *S3BucketSupplier) readBucket(bucket s3.Bucket) (cty.Value, error) {
|
|
|
|
region, err := s.repository.GetBucketLocation(&bucket)
|
2020-12-09 15:31:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return cty.NilVal, err
|
|
|
|
}
|
2021-03-17 18:38:58 +00:00
|
|
|
if region == "" || region != s.providerConfig.DefaultAlias {
|
2020-12-09 15:31:34 +00:00
|
|
|
return cty.NilVal, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
s3Bucket, err := s.reader.ReadResource(terraform.ReadResourceArgs{
|
|
|
|
Ty: aws.AwsS3BucketResourceType,
|
2021-02-16 17:42:46 +00:00
|
|
|
ID: *bucket.Name,
|
2020-12-09 15:31:34 +00:00
|
|
|
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 *s3Bucket, err
|
|
|
|
}
|