driftctl/pkg/remote/aws/vpc_supplier.go

100 lines
2.6 KiB
Go
Raw Normal View History

2020-12-15 10:07:03 +00:00
package aws
import (
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
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
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{
provider,
2021-05-21 14:09:45 +00:00
deserializer,
repository.NewEC2Repository(provider.session),
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
2020-12-15 10:07:03 +00:00
}
}
func (s *VPCSupplier) Resources() ([]resource.Resource, error) {
VPCs, defaultVPCs, err := s.client.ListAllVPCs()
2020-12-15 10:07:03 +00:00
if err != nil {
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)
})
}
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
}
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
}