driftctl/pkg/remote/aws/ec2_route_enumerator.go

67 lines
2.0 KiB
Go
Raw Normal View History

2021-07-06 09:14:51 +00:00
package aws
import (
"github.com/cloudskiff/driftctl/pkg/remote/aws/repository"
remoteerror "github.com/cloudskiff/driftctl/pkg/remote/error"
"github.com/cloudskiff/driftctl/pkg/resource"
"github.com/cloudskiff/driftctl/pkg/resource/aws"
)
type EC2RouteEnumerator struct {
repository repository.EC2Repository
factory resource.ResourceFactory
}
func NewEC2RouteEnumerator(repo repository.EC2Repository, factory resource.ResourceFactory) *EC2RouteEnumerator {
return &EC2RouteEnumerator{
repository: repo,
factory: factory,
}
}
func (e *EC2RouteEnumerator) SupportedType() resource.ResourceType {
return aws.AwsRouteResourceType
}
2021-08-09 14:03:04 +00:00
func (e *EC2RouteEnumerator) Enumerate() ([]*resource.Resource, error) {
2021-07-06 09:14:51 +00:00
routeTables, err := e.repository.ListAllRouteTables()
if err != nil {
2021-08-03 10:34:36 +00:00
return nil, remoteerror.NewResourceListingErrorWithType(err, string(e.SupportedType()), aws.AwsRouteTableResourceType)
2021-07-06 09:14:51 +00:00
}
2021-08-09 14:03:04 +00:00
var results []*resource.Resource
2021-07-06 09:14:51 +00:00
for _, routeTable := range routeTables {
for _, route := range routeTable.Routes {
2021-07-26 15:02:03 +00:00
routeId := aws.CalculateRouteID(routeTable.RouteTableId, route.DestinationCidrBlock, route.DestinationIpv6CidrBlock, route.DestinationPrefixListId)
2021-07-06 09:14:51 +00:00
data := map[string]interface{}{
"route_table_id": *routeTable.RouteTableId,
"origin": *route.Origin,
2021-07-06 09:14:51 +00:00
}
if route.DestinationCidrBlock != nil && *route.DestinationCidrBlock != "" {
data["destination_cidr_block"] = *route.DestinationCidrBlock
}
if route.DestinationIpv6CidrBlock != nil && *route.DestinationIpv6CidrBlock != "" {
data["destination_ipv6_cidr_block"] = *route.DestinationIpv6CidrBlock
}
2021-07-26 15:02:03 +00:00
if route.DestinationPrefixListId != nil && *route.DestinationPrefixListId != "" {
data["destination_prefix_list_id"] = *route.DestinationPrefixListId
}
if route.GatewayId != nil && *route.GatewayId != "" {
data["gateway_id"] = *route.GatewayId
}
2021-07-06 09:14:51 +00:00
results = append(
results,
e.factory.CreateAbstractResource(
string(e.SupportedType()),
routeId,
data,
),
)
}
}
return results, err
}