2020-12-15 10:07:03 +00:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/remote/deserializer"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource/aws"
|
|
|
|
awsdeserializer "github.com/cloudskiff/driftctl/pkg/resource/aws/deserializer"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/terraform"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
type VPCSupplier struct {
|
|
|
|
reader terraform.ResourceReader
|
|
|
|
defaultVPCDeserializer deserializer.CTYDeserializer
|
2021-01-11 17:18:55 +00:00
|
|
|
vpcDeserializer deserializer.CTYDeserializer
|
2020-12-15 10:07:03 +00:00
|
|
|
client ec2iface.EC2API
|
|
|
|
defaultVPCRunner *terraform.ParallelResourceReader
|
2021-01-11 17:18:55 +00:00
|
|
|
vpcRunner *terraform.ParallelResourceReader
|
2020-12-15 10:07:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewVPCSupplier(runner *pkg.ParallelRunner, client ec2iface.EC2API) *VPCSupplier {
|
|
|
|
return &VPCSupplier{
|
|
|
|
terraform.Provider(terraform.AWS),
|
|
|
|
awsdeserializer.NewDefaultVPCDeserializer(),
|
|
|
|
awsdeserializer.NewVPCDeserializer(),
|
|
|
|
client,
|
|
|
|
terraform.NewParallelResourceReader(runner.SubRunner()),
|
|
|
|
terraform.NewParallelResourceReader(runner.SubRunner()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s VPCSupplier) Resources() ([]resource.Resource, error) {
|
|
|
|
input := ec2.DescribeVpcsInput{}
|
|
|
|
var VPCs []*ec2.Vpc
|
|
|
|
var defaultVPCs []*ec2.Vpc
|
|
|
|
err := s.client.DescribeVpcsPages(&input,
|
|
|
|
func(resp *ec2.DescribeVpcsOutput, lastPage bool) bool {
|
|
|
|
for _, vpc := range resp.Vpcs {
|
|
|
|
if vpc.IsDefault != nil && *vpc.IsDefault {
|
|
|
|
defaultVPCs = append(defaultVPCs, vpc)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
VPCs = append(VPCs, vpc)
|
|
|
|
}
|
|
|
|
return !lastPage
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range VPCs {
|
|
|
|
res := *item
|
2021-01-11 17:18:55 +00:00
|
|
|
s.vpcRunner.Run(func() (cty.Value, error) {
|
2020-12-15 10:07:03 +00:00
|
|
|
return s.readVPC(res)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range defaultVPCs {
|
|
|
|
res := *item
|
|
|
|
s.defaultVPCRunner.Run(func() (cty.Value, error) {
|
|
|
|
return s.readVPC(res)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve results from terraform provider
|
|
|
|
defaultVPCResources, err := s.defaultVPCRunner.Wait()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-11 17:18:55 +00:00
|
|
|
VPCResources, err := s.vpcRunner.Wait()
|
2020-12-15 10:07:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deserialize
|
|
|
|
deserializedDefaultVPCs, err := s.defaultVPCDeserializer.Deserialize(defaultVPCResources)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-01-11 17:18:55 +00:00
|
|
|
deserializedVPCs, err := s.vpcDeserializer.Deserialize(VPCResources)
|
2020-12-15 10:07:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resources := make([]resource.Resource, 0, len(VPCResources)+len(deserializedDefaultVPCs))
|
|
|
|
resources = append(resources, deserializedDefaultVPCs...)
|
|
|
|
resources = append(resources, deserializedVPCs...)
|
|
|
|
|
|
|
|
return resources, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s VPCSupplier) readVPC(vpc ec2.Vpc) (cty.Value, error) {
|
|
|
|
var Ty resource.ResourceType = aws.AwsVpcResourceType
|
|
|
|
if vpc.IsDefault != nil && *vpc.IsDefault {
|
|
|
|
Ty = aws.AwsDefaultVpcResourceType
|
|
|
|
}
|
|
|
|
val, err := s.reader.ReadResource(terraform.ReadResourceArgs{
|
|
|
|
ID: *vpc.VpcId,
|
|
|
|
Ty: Ty,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
return cty.NilVal, err
|
|
|
|
}
|
|
|
|
return *val, nil
|
|
|
|
}
|