2020-12-15 10:07:03 +00:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2021-05-20 15: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"
|
|
|
|
|
2020-12-15 10:07:03 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource/aws"
|
2021-05-21 14:09:45 +00:00
|
|
|
|
2020-12-15 10:07:03 +00:00
|
|
|
"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 {
|
2021-05-21 14:09:45 +00:00
|
|
|
reader terraform.ResourceReader
|
|
|
|
deserializer *resource.Deserializer
|
2021-05-20 15:51:36 +00:00
|
|
|
client repository.EC2Repository
|
2021-05-21 14:09:45 +00:00
|
|
|
defaultVPCRunner *terraform.ParallelResourceReader
|
|
|
|
vpcRunner *terraform.ParallelResourceReader
|
2020-12-15 10:07:03 +00:00
|
|
|
}
|
|
|
|
|
2021-05-21 14:09:45 +00:00
|
|
|
func NewVPCSupplier(provider *AWSTerraformProvider, deserializer *resource.Deserializer) *VPCSupplier {
|
2020-12-15 10:07:03 +00:00
|
|
|
return &VPCSupplier{
|
2021-01-22 17:06:17 +00:00
|
|
|
provider,
|
2021-05-21 14:09:45 +00:00
|
|
|
deserializer,
|
2021-05-20 15:51:36 +00:00
|
|
|
repository.NewEC2Repository(provider.session),
|
2021-01-22 17:06:17 +00:00
|
|
|
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
|
|
|
|
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
|
2020-12-15 10:07:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-17 15:54:53 +00:00
|
|
|
func (s *VPCSupplier) Resources() ([]resource.Resource, error) {
|
2021-05-20 15:51:36 +00:00
|
|
|
VPCs, defaultVPCs, err := s.client.ListAllVPCs()
|
2020-12-15 10:07:03 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2021-01-20 13:01:57 +00:00
|
|
|
return nil, remoteerror.NewResourceEnumerationError(err, aws.AwsVpcResourceType)
|
2020-12-15 10:07:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-17 15:54:53 +00:00
|
|
|
VPCResources, err := s.vpcRunner.Wait()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-12-15 10:07:03 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deserialize
|
2021-05-21 14:09:45 +00:00
|
|
|
deserializedDefaultVPCs, err := s.deserializer.Deserialize(aws.AwsDefaultVpcResourceType, defaultVPCResources)
|
2020-12-15 10:07:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-21 14:09:45 +00:00
|
|
|
deserializedVPCs, err := s.deserializer.Deserialize(aws.AwsVpcResourceType, 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
|
|
|
|
}
|
|
|
|
|
2021-03-17 15:54:53 +00:00
|
|
|
func (s *VPCSupplier) readVPC(vpc ec2.Vpc) (cty.Value, error) {
|
2020-12-15 10:07:03 +00:00
|
|
|
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
|
|
|
|
}
|