Skip to content

Commit ed1abf5

Browse files
authored
feat(vpc): add custom route resource (#2702)
* add vpc route resource * fixes and add test * update test * fix broken links * remove nolint ireturn * fix
1 parent 4191ad1 commit ed1abf5

File tree

9 files changed

+6472
-3
lines changed

9 files changed

+6472
-3
lines changed

docs/resources/vpc_private_network.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ The following arguments are supported:
5959
In addition to all arguments above, the following attributes are exported:
6060

6161
- `id` - The ID of the Private Network.
62+
- `created_at` - The date and time of the creation of the Private Network (RFC 3339 format).
63+
- `updated_at` - The date and time of the creation of the Private Network (RFC 3339 format).
6264
- `ipv4_subnet` - The IPv4 subnet associated with the Private Network.
6365
- `subnet` - The subnet CIDR.
6466
- `id` - The subnet ID.
@@ -84,5 +86,5 @@ In addition to all arguments above, the following attributes are exported:
8486
Private Networks can be imported using `{region}/{id}`, e.g.
8587

8688
```bash
87-
terraform import scaleway_vpc_private_network.vpc_demo fr-par/11111111-1111-1111-1111-111111111111
89+
terraform import scaleway_vpc_private_network.main fr-par/11111111-1111-1111-1111-111111111111
8890
```

docs/resources/vpc_route.md

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
---
2+
subcategory: "VPC"
3+
page_title: "Scaleway: scaleway_vpc_route"
4+
---
5+
6+
# Resource: scaleway_vpc_route
7+
8+
Creates and manages Scaleway VPC Routes.
9+
For more information, see [the main documentation](https://www.scaleway.com/en/docs/network/vpc/concepts/).
10+
11+
## Example Usage
12+
13+
### Basic
14+
15+
```terraform
16+
resource "scaleway_vpc" "vpc01" {
17+
name = "tf-vpc-vpn"
18+
}
19+
20+
resource "scaleway_vpc_private_network" "pn01" {
21+
name = "tf-pn-vpn"
22+
ipv4_subnet {
23+
subnet = "172.16.64.0/22"
24+
}
25+
vpc_id = scaleway_vpc.vpc01.id
26+
}
27+
28+
resource "scaleway_instance_server" "server01" {
29+
name = "tf-server-vpn"
30+
type = "PLAY2-MICRO"
31+
image = "openvpn"
32+
}
33+
34+
resource "scaleway_instance_private_nic" "pnic01" {
35+
private_network_id = scaleway_vpc_private_network.pn01.id
36+
server_id = scaleway_instance_server.server01.id
37+
}
38+
39+
resource "scaleway_vpc_route" "rt01" {
40+
vpc_id = scaleway_vpc.vpc01.id
41+
description = "tf-route-vpn"
42+
tags = ["tf", "route"]
43+
destination = "10.0.0.0/24"
44+
nexthop_resource_id = scaleway_instance_private_nic.pnic01.id
45+
}
46+
```
47+
48+
## Argument Reference
49+
50+
The following arguments are supported:
51+
52+
- `vpc_id` - (Required) The VPC ID the route belongs to.
53+
- `description` - (Optional) The route description.
54+
- `tags` - (Optional) The tags to associate with the route.
55+
- `destination` - (Optional) The destination of the route.
56+
- `nexthop_resource_id` - (Optional) The ID of the nexthop resource.
57+
- `nexthop_private_network_id` - (Optional) The ID of the nexthop private network.
58+
- `region` - (Defaults to [provider](../index.md#region) `region`) The [region](../guides/regions_and_zones.md#regions) of the route.
59+
- `project_id` - (Defaults to [provider](../index.md#project_id) `project_id`) The ID of the Project the route is associated with.
60+
61+
## Attributes Reference
62+
63+
In addition to all arguments above, the following attributes are exported:
64+
65+
- `id` - The ID of the route.
66+
- `created_at` - The date and time of the creation of the route (RFC 3339 format).
67+
- `updated_at` - The date and time of the creation of the route (RFC 3339 format).
68+
69+
~> **Important:** routes' IDs are [regional](../guides/regions_and_zones.md#resource-ids), which means they are of the form `{region}/{id}`, e.g. `fr-par/11111111-1111-1111-1111-111111111111
70+
71+
## Import
72+
73+
Routes can be imported using `{region}/{id}`, e.g.
74+
75+
```bash
76+
terraform import scaleway_vpc_route.main fr-par/11111111-1111-1111-1111-111111111111
77+
```

internal/provider/provider.go

+1
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ func Provider(config *Config) plugin.ProviderFunc {
223223
"scaleway_vpc_public_gateway_ip": vpcgw.ResourceIP(),
224224
"scaleway_vpc_public_gateway_ip_reverse_dns": vpcgw.ResourceIPReverseDNS(),
225225
"scaleway_vpc_public_gateway_pat_rule": vpcgw.ResourcePATRule(),
226+
"scaleway_vpc_route": vpc.ResourceRoute(),
226227
"scaleway_webhosting": webhosting.ResourceWebhosting(),
227228
},
228229

internal/services/mnq/helpers_mnq_queue.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ func NewSQSClient(httpClient *http.Client, region string, endpoint string, acces
6868
return sqs.New(s), nil
6969
}
7070

71-
func NATSClientWithRegion(d *schema.ResourceData, m interface{}) (nats.JetStreamContext, scw.Region, error) { //nolint:ireturn
71+
func NATSClientWithRegion( //nolint:ireturn
72+
d *schema.ResourceData,
73+
m interface{},
74+
) (nats.JetStreamContext, scw.Region, error) {
7275
region, err := meta.ExtractRegion(d, m)
7376
if err != nil {
7477
return nil, "", err
@@ -84,7 +87,11 @@ func NATSClientWithRegion(d *schema.ResourceData, m interface{}) (nats.JetStream
8487
return js, region, err
8588
}
8689

87-
func newNATSJetStreamClient(region string, endpoint string, credentials string) (nats.JetStreamContext, error) { //nolint:ireturn
90+
func newNATSJetStreamClient( //nolint:ireturn
91+
region string,
92+
endpoint string,
93+
credentials string,
94+
) (nats.JetStreamContext, error) {
8895
jwt, seed, err := splitNATSJWTAndSeed(credentials)
8996
if err != nil {
9097
return nil, err

internal/services/vpc/helpers.go

+39
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"fmt"
7+
"strings"
78
"time"
89

910
"github.com/hashicorp/go-cty/cty"
@@ -94,3 +95,41 @@ func vpcPrivateNetworkUpgradeV1ZonalToRegionalID(element string) (string, error)
9495

9596
return fmt.Sprintf("%s/%s", fetchRegion.String(), id), nil
9697
}
98+
99+
func vpcRouteExpandResourceID(id string) (string, error) {
100+
parts := strings.Split(id, "/")
101+
partCount := len(parts)
102+
103+
switch partCount {
104+
case 1:
105+
return id, nil
106+
case 2:
107+
_, ID, err := locality.ParseLocalizedID(id)
108+
if err != nil {
109+
return "", fmt.Errorf("failed to parse localized ID: %w", err)
110+
}
111+
return ID, nil
112+
case 3:
113+
// Parse as a nested ID and return the outerID
114+
_, _, ID, err := locality.ParseLocalizedNestedID(id)
115+
if err != nil {
116+
return "", fmt.Errorf("failed to parse nested ID: %w", err)
117+
}
118+
return ID, nil
119+
default:
120+
return "", fmt.Errorf("unrecognized ID format: %s", id)
121+
}
122+
}
123+
124+
func diffSuppressFuncRouteResourceID(_, oldValue, newValue string, _ *schema.ResourceData) bool {
125+
oldResourceID, err := vpcRouteExpandResourceID(oldValue)
126+
if err != nil {
127+
return false
128+
}
129+
newResourceID, err := vpcRouteExpandResourceID(newValue)
130+
if err != nil {
131+
return false
132+
}
133+
134+
return oldResourceID == newResourceID
135+
}

0 commit comments

Comments
 (0)