2021-01-20 16:28:56 +00:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
|
2021-05-18 16:00:43 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/remote/deserializer"
|
2021-01-20 13:01:57 +00:00
|
|
|
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
|
2021-01-20 16:28:56 +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 16:28:56 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/terraform"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
)
|
|
|
|
|
|
|
|
type NatGatewaySupplier struct {
|
|
|
|
reader terraform.ResourceReader
|
2021-05-21 14:09:45 +00:00
|
|
|
deserializer *resource.Deserializer
|
2021-05-18 16:00:43 +00:00
|
|
|
client repository.EC2Repository
|
2021-01-20 16:28:56 +00:00
|
|
|
runner *terraform.ParallelResourceReader
|
|
|
|
}
|
|
|
|
|
2021-05-21 14:09:45 +00:00
|
|
|
func NewNatGatewaySupplier(provider *AWSTerraformProvider, deserializer *resource.Deserializer) *NatGatewaySupplier {
|
2021-01-20 16:28:56 +00:00
|
|
|
return &NatGatewaySupplier{
|
2021-01-22 17:06:17 +00:00
|
|
|
provider,
|
2021-05-21 14:09:45 +00:00
|
|
|
deserializer,
|
2021-05-18 16:00:43 +00:00
|
|
|
repository.NewEC2Repository(provider.session),
|
2021-01-22 17:06:17 +00:00
|
|
|
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
|
2021-01-20 16:28:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-17 15:54:53 +00:00
|
|
|
func (s *NatGatewaySupplier) Resources() ([]resource.Resource, error) {
|
2021-05-18 16:00:43 +00:00
|
|
|
retrievedNatGateways, err := s.client.ListAllNatGateways()
|
2021-01-20 16:28:56 +00:00
|
|
|
if err != nil {
|
2021-01-20 13:01:57 +00:00
|
|
|
return nil, remoteerror.NewResourceEnumerationError(err, aws.AwsNatGatewayResourceType)
|
2021-01-20 16:28:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, gateway := range retrievedNatGateways {
|
|
|
|
res := *gateway
|
|
|
|
s.runner.Run(func() (cty.Value, error) {
|
|
|
|
return s.readNatGateway(res)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve results from terraform provider
|
|
|
|
natGatewayResources, err := s.runner.Wait()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deserialize
|
2021-05-21 14:09:45 +00:00
|
|
|
resources, err := s.deserializer.Deserialize(aws.AwsNatGatewayResourceType, natGatewayResources)
|
2021-01-20 16:28:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return resources, nil
|
|
|
|
}
|
|
|
|
|
2021-03-17 15:54:53 +00:00
|
|
|
func (s *NatGatewaySupplier) readNatGateway(gateway ec2.NatGateway) (cty.Value, error) {
|
2021-01-20 16:28:56 +00:00
|
|
|
var Ty resource.ResourceType = aws.AwsNatGatewayResourceType
|
|
|
|
val, err := s.reader.ReadResource(terraform.ReadResourceArgs{
|
|
|
|
ID: *gateway.NatGatewayId,
|
|
|
|
Ty: Ty,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"type": Ty,
|
|
|
|
}).Error(err)
|
|
|
|
return cty.NilVal, err
|
|
|
|
}
|
|
|
|
return *val, nil
|
|
|
|
}
|