2021-02-17 14:33:08 +00:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/aws/aws-sdk-go/service/cloudfront"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
|
|
|
|
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource/aws"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/terraform"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CloudfrontDistributionSupplier struct {
|
|
|
|
reader terraform.ResourceReader
|
2021-05-21 14:09:45 +00:00
|
|
|
deserializer *resource.Deserializer
|
2021-02-17 14:33:08 +00:00
|
|
|
client repository.CloudfrontRepository
|
|
|
|
runner *terraform.ParallelResourceReader
|
|
|
|
}
|
|
|
|
|
2021-05-31 15:39:16 +00:00
|
|
|
func NewCloudfrontDistributionSupplier(provider *AWSTerraformProvider, deserializer *resource.Deserializer, repo repository.CloudfrontRepository) *CloudfrontDistributionSupplier {
|
2021-02-17 14:33:08 +00:00
|
|
|
return &CloudfrontDistributionSupplier{
|
|
|
|
provider,
|
2021-05-21 14:09:45 +00:00
|
|
|
deserializer,
|
2021-05-31 15:39:16 +00:00
|
|
|
repo,
|
2021-02-17 14:33:08 +00:00
|
|
|
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-17 15:54:53 +00:00
|
|
|
func (s *CloudfrontDistributionSupplier) Resources() ([]resource.Resource, error) {
|
2021-02-17 14:33:08 +00:00
|
|
|
distributions, err := s.client.ListAllDistributions()
|
|
|
|
if err != nil {
|
|
|
|
return nil, remoteerror.NewResourceEnumerationError(err, aws.AwsCloudfrontDistributionResourceType)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, distribution := range distributions {
|
|
|
|
d := *distribution
|
|
|
|
s.runner.Run(func() (cty.Value, error) {
|
|
|
|
return s.readCloudfrontDistribution(d)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
resources, err := s.runner.Wait()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-21 14:09:45 +00:00
|
|
|
return s.deserializer.Deserialize(aws.AwsCloudfrontDistributionResourceType, resources)
|
2021-02-17 14:33:08 +00:00
|
|
|
}
|
|
|
|
|
2021-03-17 15:54:53 +00:00
|
|
|
func (s *CloudfrontDistributionSupplier) readCloudfrontDistribution(distribution cloudfront.DistributionSummary) (cty.Value, error) {
|
2021-02-17 14:33:08 +00:00
|
|
|
val, err := s.reader.ReadResource(terraform.ReadResourceArgs{
|
|
|
|
ID: *distribution.Id,
|
|
|
|
Ty: aws.AwsCloudfrontDistributionResourceType,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
return cty.NilVal, err
|
|
|
|
}
|
|
|
|
return *val, nil
|
|
|
|
}
|