2021-01-07 16:53:17 +00:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2021-05-18 15:47:28 +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-07 16:53:17 +00:00
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource/aws"
|
2021-05-21 14:09:45 +00:00
|
|
|
|
2021-01-07 16:53:17 +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 SubnetSupplier struct {
|
2021-05-21 14:09:45 +00:00
|
|
|
reader terraform.ResourceReader
|
|
|
|
deserializer *resource.Deserializer
|
2021-06-07 13:02:12 +00:00
|
|
|
repo repository.EC2Repository
|
2021-05-21 14:09:45 +00:00
|
|
|
defaultSubnetRunner *terraform.ParallelResourceReader
|
|
|
|
subnetRunner *terraform.ParallelResourceReader
|
2021-01-07 16:53:17 +00:00
|
|
|
}
|
|
|
|
|
2021-06-07 09:04:46 +00:00
|
|
|
func NewSubnetSupplier(provider *AWSTerraformProvider, deserializer *resource.Deserializer, repo repository.EC2Repository) *SubnetSupplier {
|
2021-01-07 16:53:17 +00:00
|
|
|
return &SubnetSupplier{
|
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()),
|
|
|
|
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
|
2021-01-07 16:53:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-17 15:54:53 +00:00
|
|
|
func (s *SubnetSupplier) Resources() ([]resource.Resource, error) {
|
2021-06-07 13:02:12 +00:00
|
|
|
subnets, defaultSubnets, err := s.repo.ListAllSubnets()
|
2021-01-07 16:53:17 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2021-01-20 13:01:57 +00:00
|
|
|
return nil, remoteerror.NewResourceEnumerationError(err, aws.AwsSubnetResourceType)
|
2021-01-07 16:53:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, item := range subnets {
|
|
|
|
res := *item
|
|
|
|
s.subnetRunner.Run(func() (cty.Value, error) {
|
|
|
|
return s.readSubnet(res)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-03-17 15:54:53 +00:00
|
|
|
subnetResources, err := s.subnetRunner.Wait()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-01-07 16:53:17 +00:00
|
|
|
for _, item := range defaultSubnets {
|
|
|
|
res := *item
|
|
|
|
s.defaultSubnetRunner.Run(func() (cty.Value, error) {
|
|
|
|
return s.readSubnet(res)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve results from terraform provider
|
|
|
|
defaultSubnetResources, err := s.defaultSubnetRunner.Wait()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deserialize
|
2021-05-21 14:09:45 +00:00
|
|
|
deserializedDefaultSubnets, err := s.deserializer.Deserialize(aws.AwsDefaultSubnetResourceType, defaultSubnetResources)
|
2021-01-07 16:53:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-05-21 14:09:45 +00:00
|
|
|
deserializedSubnets, err := s.deserializer.Deserialize(aws.AwsSubnetResourceType, subnetResources)
|
2021-01-07 16:53:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resources := make([]resource.Resource, 0, len(subnetResources)+len(deserializedDefaultSubnets))
|
|
|
|
resources = append(resources, deserializedDefaultSubnets...)
|
|
|
|
resources = append(resources, deserializedSubnets...)
|
|
|
|
|
|
|
|
return resources, nil
|
|
|
|
}
|
|
|
|
|
2021-03-17 15:54:53 +00:00
|
|
|
func (s *SubnetSupplier) readSubnet(subnet ec2.Subnet) (cty.Value, error) {
|
2021-01-07 16:53:17 +00:00
|
|
|
var Ty resource.ResourceType = aws.AwsSubnetResourceType
|
|
|
|
if subnet.DefaultForAz != nil && *subnet.DefaultForAz {
|
|
|
|
Ty = aws.AwsDefaultSubnetResourceType
|
|
|
|
}
|
|
|
|
val, err := s.reader.ReadResource(terraform.ReadResourceArgs{
|
|
|
|
ID: *subnet.SubnetId,
|
|
|
|
Ty: Ty,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
return cty.NilVal, err
|
|
|
|
}
|
|
|
|
return *val, nil
|
|
|
|
}
|