driftctl/pkg/resource/aws/aws_route.go

78 lines
2.7 KiB
Go
Raw Normal View History

package aws
import (
2021-05-21 14:09:45 +00:00
"fmt"
"github.com/cloudskiff/driftctl/pkg/resource"
2021-05-21 14:09:45 +00:00
"github.com/hashicorp/terraform/helper/hashcode"
)
2021-03-25 11:13:52 +00:00
const AwsRouteResourceType = "aws_route"
func initAwsRouteMetaData(resourceSchemaRepository resource.SchemaRepositoryInterface) {
2021-08-09 15:15:35 +00:00
resourceSchemaRepository.SetResolveReadAttributesFunc(AwsRouteResourceType, func(res *resource.Resource) map[string]string {
attributes := map[string]string{
"route_table_id": *res.Attributes().GetString("route_table_id"),
}
if ipv4 := res.Attributes().GetString("destination_cidr_block"); ipv4 != nil && *ipv4 != "" {
attributes["destination_cidr_block"] = *ipv4
}
if ipv6 := res.Attributes().GetString("destination_ipv6_cidr_block"); ipv6 != nil && *ipv6 != "" {
attributes["destination_ipv6_cidr_block"] = *ipv6
}
return attributes
})
2021-08-09 14:03:04 +00:00
resourceSchemaRepository.SetNormalizeFunc(AwsRouteResourceType, func(res *resource.Resource) {
val := res.Attrs
val.SafeDelete([]string{"timeouts"})
2021-05-18 15:31:51 +00:00
val.DeleteIfDefault("vpc_endpoint_id")
val.DeleteIfDefault("local_gateway_id")
val.DeleteIfDefault("destination_cidr_block")
val.DeleteIfDefault("destination_ipv6_cidr_block")
val.DeleteIfDefault("egress_only_gateway_id")
val.DeleteIfDefault("nat_gateway_id")
val.DeleteIfDefault("instance_id")
val.DeleteIfDefault("network_interface_id")
val.DeleteIfDefault("transit_gateway_id")
val.DeleteIfDefault("vpc_peering_connection_id")
val.DeleteIfDefault("destination_prefix_list_id")
val.DeleteIfDefault("instance_owner_id")
val.DeleteIfDefault("carrier_gateway_id")
})
2021-08-09 14:03:04 +00:00
resourceSchemaRepository.SetHumanReadableAttributesFunc(AwsRouteResourceType, func(res *resource.Resource) map[string]string {
2021-05-21 14:09:45 +00:00
val := res.Attrs
attrs := make(map[string]string)
if rtID := val.GetString("route_table_id"); rtID != nil && *rtID != "" {
attrs["Table"] = *rtID
}
if ipv4 := val.GetString("destination_cidr_block"); ipv4 != nil && *ipv4 != "" {
attrs["Destination"] = *ipv4
}
if ipv6 := val.GetString("destination_ipv6_cidr_block"); ipv6 != nil && *ipv6 != "" {
attrs["Destination"] = *ipv6
}
2021-07-26 15:02:03 +00:00
if prefix := val.GetString("destination_prefix_list_id"); prefix != nil && *prefix != "" {
attrs["Destination"] = *prefix
}
2021-05-21 14:09:45 +00:00
return attrs
})
2021-09-17 15:16:06 +00:00
resourceSchemaRepository.SetFlags(AwsRouteResourceType, resource.FlagDeepMode)
2021-05-21 14:09:45 +00:00
}
2021-07-26 15:02:03 +00:00
func CalculateRouteID(tableId, CidrBlock, Ipv6CidrBlock, PrefixListId *string) string {
2021-05-21 14:09:45 +00:00
if CidrBlock != nil && *CidrBlock != "" {
2021-07-26 15:02:03 +00:00
return fmt.Sprintf("r-%s%d", *tableId, hashcode.String(*CidrBlock))
2021-05-21 14:09:45 +00:00
}
if Ipv6CidrBlock != nil && *Ipv6CidrBlock != "" {
2021-07-26 15:02:03 +00:00
return fmt.Sprintf("r-%s%d", *tableId, hashcode.String(*Ipv6CidrBlock))
}
if PrefixListId != nil && *PrefixListId != "" {
return fmt.Sprintf("r-%s%d", *tableId, hashcode.String(*PrefixListId))
2021-05-21 14:09:45 +00:00
}
2021-07-26 15:02:03 +00:00
return ""
}