2021-01-14 17:11:02 +00:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
2021-01-20 13:01:57 +00:00
|
|
|
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
|
|
|
|
|
2021-01-14 17:11:02 +00:00
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/remote/deserializer"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/resource/aws"
|
|
|
|
awsdeserializer "github.com/cloudskiff/driftctl/pkg/resource/aws/deserializer"
|
|
|
|
"github.com/cloudskiff/driftctl/pkg/terraform"
|
|
|
|
"github.com/hashicorp/terraform/flatmap"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
)
|
|
|
|
|
|
|
|
type RouteTableSupplier struct {
|
|
|
|
reader terraform.ResourceReader
|
|
|
|
defaultRouteTableDeserializer deserializer.CTYDeserializer
|
|
|
|
routeTableDeserializer deserializer.CTYDeserializer
|
|
|
|
client ec2iface.EC2API
|
|
|
|
defaultRouteTableRunner *terraform.ParallelResourceReader
|
|
|
|
routeTableRunner *terraform.ParallelResourceReader
|
|
|
|
}
|
|
|
|
|
2021-02-09 13:56:11 +00:00
|
|
|
func NewRouteTableSupplier(provider *AWSTerraformProvider) *RouteTableSupplier {
|
2021-01-14 17:11:02 +00:00
|
|
|
return &RouteTableSupplier{
|
2021-01-22 17:06:17 +00:00
|
|
|
provider,
|
2021-01-14 17:11:02 +00:00
|
|
|
awsdeserializer.NewDefaultRouteTableDeserializer(),
|
|
|
|
awsdeserializer.NewRouteTableDeserializer(),
|
2021-01-22 17:06:17 +00:00
|
|
|
ec2.New(provider.session),
|
|
|
|
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
|
|
|
|
terraform.NewParallelResourceReader(provider.Runner().SubRunner()),
|
2021-01-14 17:11:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-17 15:54:53 +00:00
|
|
|
func (s *RouteTableSupplier) Resources() ([]resource.Resource, error) {
|
2021-01-14 17:11:02 +00:00
|
|
|
|
2021-03-17 15:54:53 +00:00
|
|
|
results, err := listRouteTables(s.client, aws.AwsRouteTableResourceType)
|
2021-01-14 17:11:02 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-17 15:54:53 +00:00
|
|
|
retrievedDefaultRouteTables := []*ec2.RouteTable{}
|
|
|
|
retrievedRouteTables := []*ec2.RouteTable{}
|
|
|
|
|
|
|
|
for _, routeTable := range results {
|
2021-01-14 17:11:02 +00:00
|
|
|
res := *routeTable
|
|
|
|
var isMain bool
|
|
|
|
for _, assoc := range res.Associations {
|
|
|
|
if assoc.Main != nil && *assoc.Main {
|
|
|
|
isMain = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if isMain {
|
2021-03-17 15:54:53 +00:00
|
|
|
retrievedDefaultRouteTables = append(retrievedDefaultRouteTables, &res)
|
2021-01-14 17:11:02 +00:00
|
|
|
continue
|
|
|
|
}
|
2021-03-17 15:54:53 +00:00
|
|
|
retrievedRouteTables = append(retrievedRouteTables, &res)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, routeTable := range retrievedDefaultRouteTables {
|
|
|
|
res := *routeTable
|
|
|
|
s.defaultRouteTableRunner.Run(func() (cty.Value, error) {
|
|
|
|
return s.readRouteTable(res, true)
|
2021-01-14 17:11:02 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Retrieve results from terraform provider
|
|
|
|
defaultRouteTableResources, err := s.defaultRouteTableRunner.Wait()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-03-17 15:54:53 +00:00
|
|
|
|
|
|
|
for _, routeTable := range retrievedRouteTables {
|
|
|
|
res := *routeTable
|
|
|
|
s.routeTableRunner.Run(func() (cty.Value, error) {
|
|
|
|
return s.readRouteTable(res, false)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-01-14 17:11:02 +00:00
|
|
|
routeTableResources, err := s.routeTableRunner.Wait()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deserialize
|
|
|
|
deserializedDefaultRouteTables, err := s.defaultRouteTableDeserializer.Deserialize(defaultRouteTableResources)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
deserializedRouteTables, err := s.routeTableDeserializer.Deserialize(routeTableResources)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resources := make([]resource.Resource, 0, len(routeTableResources)+len(defaultRouteTableResources))
|
|
|
|
resources = append(resources, deserializedDefaultRouteTables...)
|
|
|
|
resources = append(resources, deserializedRouteTables...)
|
|
|
|
|
|
|
|
return resources, nil
|
|
|
|
}
|
|
|
|
|
2021-03-17 15:54:53 +00:00
|
|
|
func (s *RouteTableSupplier) readRouteTable(routeTable ec2.RouteTable, isMain bool) (cty.Value, error) {
|
2021-01-14 17:11:02 +00:00
|
|
|
var Ty resource.ResourceType = aws.AwsRouteTableResourceType
|
|
|
|
attributes := map[string]interface{}{}
|
|
|
|
if isMain {
|
|
|
|
if routeTable.VpcId == nil {
|
|
|
|
return cty.NilVal, errors.New("a default route table does not have a VpcId")
|
|
|
|
}
|
|
|
|
Ty = aws.AwsDefaultRouteTableResourceType
|
|
|
|
attributes["vpc_id"] = *routeTable.VpcId
|
|
|
|
}
|
|
|
|
val, err := s.reader.ReadResource(terraform.ReadResourceArgs{
|
|
|
|
ID: *routeTable.RouteTableId,
|
|
|
|
Ty: Ty,
|
|
|
|
Attributes: flatmap.Flatten(attributes),
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"type": Ty,
|
|
|
|
}).Error(err)
|
|
|
|
return cty.NilVal, err
|
|
|
|
}
|
|
|
|
return *val, nil
|
|
|
|
}
|
|
|
|
|
2021-01-20 13:01:57 +00:00
|
|
|
func listRouteTables(client ec2iface.EC2API, supplierType string) ([]*ec2.RouteTable, error) {
|
2021-01-14 17:11:02 +00:00
|
|
|
var routeTables []*ec2.RouteTable
|
|
|
|
input := ec2.DescribeRouteTablesInput{}
|
|
|
|
err := client.DescribeRouteTablesPages(&input,
|
|
|
|
func(resp *ec2.DescribeRouteTablesOutput, lastPage bool) bool {
|
|
|
|
routeTables = append(routeTables, resp.RouteTables...)
|
|
|
|
return !lastPage
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
2021-01-20 13:01:57 +00:00
|
|
|
return nil, remoteerror.NewResourceEnumerationErrorWithType(err, supplierType, aws.AwsRouteTableResourceType)
|
2021-01-14 17:11:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return routeTables, nil
|
|
|
|
}
|