2021-01-20 21:54:41 +00:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2021-05-17 20:51:36 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
|
2021-01-20 13:01:57 +00:00
|
|
|
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
|
2021-01-20 21:54:41 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource/aws"
|
2021-05-21 14:09:45 +00:00
|
|
|
|
2021-01-20 21:54:41 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/terraform"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
)
|
|
|
|
|
|
|
|
type InternetGatewaySupplier struct {
|
|
|
|
reader terraform.ResourceReader
|
2021-05-21 14:09:45 +00:00
|
|
|
deserializer *resource.Deserializer
|
2021-06-07 13:02:12 +00:00
|
|
|
repo repository.EC2Repository
|
2021-01-20 21:54:41 +00:00
|
|
|
runner *terraform.ParallelResourceReader
|
|
|
|
}
|
|
|
|
|
2021-06-07 09:04:46 +00:00
|
|
|
func NewInternetGatewaySupplier(provider *AWSTerraformProvider, deserializer *resource.Deserializer, repo repository.EC2Repository) *InternetGatewaySupplier {
|
2021-01-20 21:54:41 +00:00
|
|
|
return &InternetGatewaySupplier{
|
2021-01-22 17:06:17 +00:00
|
|
|
provider,
|
2021-05-21 14:09:45 +00:00
|
|
|
deserializer,
|
2021-06-07 09:04:46 +00:00
|
|
|
repo,
|
2021-01-22 17:06:17 +00:00
|
|
|
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
|
2021-01-20 21:54:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-17 15:54:53 +00:00
|
|
|
func (s *InternetGatewaySupplier) Resources() ([]resource.Resource, error) {
|
2021-06-07 13:02:12 +00:00
|
|
|
internetGateways, err := s.repo.ListAllInternetGateways()
|
2021-01-20 21:54:41 +00:00
|
|
|
if err != nil {
|
2021-01-20 13:01:57 +00:00
|
|
|
return nil, remoteerror.NewResourceEnumerationError(err, aws.AwsInternetGatewayResourceType)
|
2021-01-20 21:54:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, internetGateway := range internetGateways {
|
|
|
|
gtw := *internetGateway
|
|
|
|
s.runner.Run(func() (cty.Value, error) {
|
|
|
|
return s.readInternetGateway(gtw)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
resources, err := s.runner.Wait()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-21 14:09:45 +00:00
|
|
|
return s.deserializer.Deserialize(aws.AwsInternetGatewayResourceType, resources)
|
2021-01-20 21:54:41 +00:00
|
|
|
}
|
|
|
|
|
2021-03-17 15:54:53 +00:00
|
|
|
func (s *InternetGatewaySupplier) readInternetGateway(internetGateway ec2.InternetGateway) (cty.Value, error) {
|
2021-01-20 21:54:41 +00:00
|
|
|
var Ty resource.ResourceType = aws.AwsInternetGatewayResourceType
|
|
|
|
val, err := s.reader.ReadResource(terraform.ReadResourceArgs{
|
|
|
|
Ty: Ty,
|
|
|
|
ID: *internetGateway.InternetGatewayId,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"type": Ty,
|
|
|
|
}).Error(err)
|
|
|
|
return cty.NilVal, err
|
|
|
|
}
|
|
|
|
return *val, nil
|
|
|
|
}
|