|
| 1 | +package vpc |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 8 | + "github.com/scaleway/scaleway-sdk-go/api/vpc/v2" |
| 9 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 10 | + "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality" |
| 11 | + "github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional" |
| 12 | + "github.com/scaleway/terraform-provider-scaleway/v2/internal/types" |
| 13 | + "github.com/scaleway/terraform-provider-scaleway/v2/internal/verify" |
| 14 | +) |
| 15 | + |
| 16 | +func DataSourceRoutes() *schema.Resource { |
| 17 | + return &schema.Resource{ |
| 18 | + ReadContext: DataSourceRoutesRead, |
| 19 | + Schema: map[string]*schema.Schema{ |
| 20 | + "vpc_id": { |
| 21 | + Type: schema.TypeString, |
| 22 | + Optional: true, |
| 23 | + Description: "Only routes within this VPC will be returned", |
| 24 | + }, |
| 25 | + "nexthop_resource_id": { |
| 26 | + Type: schema.TypeString, |
| 27 | + Optional: true, |
| 28 | + Description: "Only routes with a matching next hop resource ID will be returned", |
| 29 | + }, |
| 30 | + "nexthop_private_network_id": { |
| 31 | + Type: schema.TypeString, |
| 32 | + Optional: true, |
| 33 | + Description: "Only routes with a matching next hop private network ID will be returned", |
| 34 | + }, |
| 35 | + "nexthop_resource_type": { |
| 36 | + Type: schema.TypeString, |
| 37 | + Optional: true, |
| 38 | + Description: "Only Routes with a matching next hop resource type will be returned", |
| 39 | + ValidateDiagFunc: verify.ValidateEnum[vpc.RouteWithNexthopResourceType](), |
| 40 | + }, |
| 41 | + "is_ipv6": { |
| 42 | + Type: schema.TypeBool, |
| 43 | + Optional: true, |
| 44 | + Description: "Only routes with an IPv6 destination will be returned", |
| 45 | + }, |
| 46 | + "tags": { |
| 47 | + Type: schema.TypeList, |
| 48 | + Elem: &schema.Schema{ |
| 49 | + Type: schema.TypeString, |
| 50 | + }, |
| 51 | + Optional: true, |
| 52 | + Description: "Routes with these exact tags are listed.", |
| 53 | + }, |
| 54 | + "routes": { |
| 55 | + Type: schema.TypeList, |
| 56 | + Computed: true, |
| 57 | + Elem: &schema.Resource{ |
| 58 | + Schema: map[string]*schema.Schema{ |
| 59 | + "id": { |
| 60 | + Computed: true, |
| 61 | + Type: schema.TypeString, |
| 62 | + Description: "The associated route ID", |
| 63 | + }, |
| 64 | + "vpc_id": { |
| 65 | + Computed: true, |
| 66 | + Type: schema.TypeString, |
| 67 | + Description: "The VPC ID associated with the route", |
| 68 | + }, |
| 69 | + "tags": { |
| 70 | + Computed: true, |
| 71 | + Type: schema.TypeList, |
| 72 | + Elem: &schema.Schema{ |
| 73 | + Type: schema.TypeString, |
| 74 | + }, |
| 75 | + Description: "The tags associated with the route", |
| 76 | + }, |
| 77 | + "created_at": { |
| 78 | + Computed: true, |
| 79 | + Type: schema.TypeString, |
| 80 | + Description: "Date and time of route's creation (RFC 3339 format)", |
| 81 | + }, |
| 82 | + "description": { |
| 83 | + Computed: true, |
| 84 | + Type: schema.TypeString, |
| 85 | + Description: "The description of the route", |
| 86 | + }, |
| 87 | + "destination": { |
| 88 | + Computed: true, |
| 89 | + Type: schema.TypeString, |
| 90 | + Description: "The destination IP or IP range of the route", |
| 91 | + }, |
| 92 | + "nexthop_resource_id": { |
| 93 | + Computed: true, |
| 94 | + Type: schema.TypeString, |
| 95 | + Description: "The resource ID of the route's next hop", |
| 96 | + }, |
| 97 | + "nexthop_private_network_id": { |
| 98 | + Computed: true, |
| 99 | + Type: schema.TypeString, |
| 100 | + Description: "The private network ID of the route's next hop", |
| 101 | + }, |
| 102 | + "nexthop_ip": { |
| 103 | + Computed: true, |
| 104 | + Type: schema.TypeString, |
| 105 | + Description: "The IP of the route's next hop", |
| 106 | + }, |
| 107 | + "nexthop_name": { |
| 108 | + Computed: true, |
| 109 | + Type: schema.TypeString, |
| 110 | + Description: "The name of the route's next hop", |
| 111 | + }, |
| 112 | + "nexthop_resource_type": { |
| 113 | + Computed: true, |
| 114 | + Type: schema.TypeString, |
| 115 | + Description: "The resource type of the route's next hop", |
| 116 | + }, |
| 117 | + "region": regional.Schema(), |
| 118 | + }, |
| 119 | + }, |
| 120 | + }, |
| 121 | + "region": regional.Schema(), |
| 122 | + }, |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +func DataSourceRoutesRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 127 | + routesAPI, region, err := routesAPIWithRegion(d, m) |
| 128 | + if err != nil { |
| 129 | + return diag.FromErr(err) |
| 130 | + } |
| 131 | + |
| 132 | + req := &vpc.RoutesWithNexthopAPIListRoutesWithNexthopRequest{ |
| 133 | + Region: region, |
| 134 | + Tags: types.ExpandStrings(d.Get("tags")), |
| 135 | + VpcID: types.ExpandStringPtr(locality.ExpandID(d.Get("vpc_id"))), |
| 136 | + NexthopResourceID: types.ExpandStringPtr(locality.ExpandID(d.Get("nexthop_resource_id"))), |
| 137 | + NexthopPrivateNetworkID: types.ExpandStringPtr(locality.ExpandID(d.Get("nexthop_private_network_id"))), |
| 138 | + NexthopResourceType: vpc.RouteWithNexthopResourceType(d.Get("nexthop_resource_type").(string)), |
| 139 | + } |
| 140 | + |
| 141 | + isipv6, isipv6Exists := d.GetOk("is_ipv6") |
| 142 | + if isipv6Exists { |
| 143 | + req.IsIPv6 = types.ExpandBoolPtr(isipv6) |
| 144 | + } |
| 145 | + |
| 146 | + res, err := routesAPI.ListRoutesWithNexthop(req, scw.WithContext(ctx)) |
| 147 | + if err != nil { |
| 148 | + return diag.FromErr(err) |
| 149 | + } |
| 150 | + |
| 151 | + routes := []interface{}(nil) |
| 152 | + for _, route := range res.Routes { |
| 153 | + rawRoute := make(map[string]interface{}) |
| 154 | + if route.Route != nil { |
| 155 | + rawRoute["id"] = regional.NewIDString(region, route.Route.ID) |
| 156 | + rawRoute["created_at"] = types.FlattenTime(route.Route.CreatedAt) |
| 157 | + rawRoute["vpc_id"] = route.Route.VpcID |
| 158 | + rawRoute["nexthop_resource_id"] = types.FlattenStringPtr(route.Route.NexthopResourceID) |
| 159 | + rawRoute["nexthop_private_network_id"] = types.FlattenStringPtr(route.Route.NexthopPrivateNetworkID) |
| 160 | + rawRoute["description"] = route.Route.Description |
| 161 | + rawRoute["region"] = region.String() |
| 162 | + |
| 163 | + destination, err := types.FlattenIPNet(route.Route.Destination) |
| 164 | + if err != nil { |
| 165 | + return diag.FromErr(err) |
| 166 | + } |
| 167 | + rawRoute["destination"] = destination |
| 168 | + |
| 169 | + if len(route.Route.Tags) > 0 { |
| 170 | + rawRoute["tags"] = route.Route.Tags |
| 171 | + } |
| 172 | + } |
| 173 | + rawRoute["nexthop_ip"] = types.FlattenIPPtr(route.NexthopIP) |
| 174 | + rawRoute["nexthop_name"] = types.FlattenStringPtr(route.NexthopName) |
| 175 | + rawRoute["nexthop_resource_type"] = route.NexthopResourceType.String() |
| 176 | + |
| 177 | + routes = append(routes, rawRoute) |
| 178 | + } |
| 179 | + |
| 180 | + d.SetId(region.String()) |
| 181 | + _ = d.Set("routes", routes) |
| 182 | + |
| 183 | + return nil |
| 184 | +} |
0 commit comments