Skip to content

Commit c30cdc6

Browse files
committed
feat(lb) add support for path-begin route matching
1 parent af5e786 commit c30cdc6

File tree

5 files changed

+2315
-5
lines changed

5 files changed

+2315
-5
lines changed

docs/data-sources/lb_routes.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,5 @@ In addition to all arguments above, the following attributes are exported:
4040
- `update_at` - The date on which the route was last updated (RFC 3339 format).
4141
- `match_subdomains` - If true, all subdomains will match.
4242
- `match_sni` - Server Name Indication TLS extension field from an incoming connection made via an SSL/TLS transport layer.
43-
- `match_host_header` - Specifies the host of the server to which the request is being sent.
43+
- `match_host_header` - Specifies the host of the server to which the request is being sent.
44+
- `match_path_begin` - The value to match in the URL beginning path from an incoming request.

docs/resources/lb_route.md

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,50 @@ resource "scaleway_lb_route" "rt01" {
7373
}
7474
```
7575

76+
# With path-begin matching for HTTP backends
77+
78+
```terraform
79+
resource "scaleway_lb_ip" "ip" {}
80+
81+
resource "scaleway_lb" "lb" {
82+
ip_id = scaleway_lb_ip.ip.id
83+
name = "my-lb"
84+
type = "lb-s"
85+
}
86+
87+
resource "scaleway_lb_backend" "app" {
88+
lb_id = scaleway_lb.lb.id
89+
forward_protocol = "http"
90+
forward_port = 80
91+
proxy_protocol = "none"
92+
}
93+
94+
resource "scaleway_lb_backend" "admin" {
95+
lb_id = scaleway_lb.lb.id
96+
forward_protocol = "http"
97+
forward_port = 8080
98+
proxy_protocol = "none"
99+
}
100+
101+
resource "scaleway_lb_frontend" "frontend" {
102+
lb_id = scaleway_lb.lb.id
103+
backend_id = scaleway_lb_backend.app.id
104+
inbound_port = 80
105+
}
106+
107+
resource "scaleway_lb_route" "admin_route" {
108+
frontend_id = scaleway_lb_frontend.frontend.id
109+
backend_id = scaleway_lb_backend.admin.id
110+
match_path_begin = "/admin"
111+
}
112+
113+
resource "scaleway_lb_route" "default_route" {
114+
frontend_id = scaleway_lb_frontend.frontend.id
115+
backend_id = scaleway_lb_backend.app.id
116+
match_path_begin = "/"
117+
}
118+
```
119+
76120
## Argument Reference
77121

78122
The following arguments are supported:
@@ -81,15 +125,17 @@ The following arguments are supported:
81125
- `frontend_id` - (Required) The ID of the frontend the route is associated with.
82126
- `match_subdomains` - (Default: `false`) If true, all subdomains will match.
83127
- `match_sni` - The Server Name Indication (SNI) value to match. Value to match in the Server Name Indication TLS extension (SNI) field from an incoming connection made via an SSL/TLS transport layer.
84-
Only one of `match_sni` and `match_host_header` should be specified.
128+
Only one of `match_sni`, `match_host_header` and `match_path_begin` should be specified.
85129

86130
~> **Important:** This field should be set for routes on TCP Load Balancers.
87131

88132
- `match_host_header` - The HTTP host header to match. Value to match in the HTTP Host request header from an incoming connection.
89-
Only one of `match_sni` and `match_host_header` should be specified.
133+
Only one of `match_sni`, `match_host_header` and `match_path_begin` should be specified.
90134

91135
~> **Important:** This field should be set for routes on HTTP Load Balancers.
92136

137+
- `match_path_begin` - The value to match in the URL beginning path from an incoming request.
138+
Only one of `match_sni`, `match_host_header` and `match_path_begin` should be specified.
93139
- `zone` - (Defaults to [provider](../index.md#zone) `zone`) The [zone](../guides/regions_and_zones.md#zones) in which the Load Balancer was created.
94140

95141
## Attributes Reference

internal/services/lb/route.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,19 @@ func ResourceRoute() *schema.Resource {
4747
Type: schema.TypeString,
4848
Optional: true,
4949
Description: "Server Name Indication TLS extension field from an incoming connection made via an SSL/TLS transport layer",
50-
ConflictsWith: []string{"match_host_header"},
50+
ConflictsWith: []string{"match_host_header", "match_path_begin"},
5151
},
5252
"match_host_header": {
5353
Type: schema.TypeString,
5454
Optional: true,
5555
Description: "Specifies the host of the server to which the request is being sent",
56-
ConflictsWith: []string{"match_sni"},
56+
ConflictsWith: []string{"match_sni", "match_path_begin"},
57+
},
58+
"match_path_begin": {
59+
Type: schema.TypeString,
60+
Optional: true,
61+
Description: "Value to match in the URL beginning path from an incoming request",
62+
ConflictsWith: []string{"match_sni", "match_host_header"},
5763
},
5864
"match_subdomains": {
5965
Type: schema.TypeBool,
@@ -102,6 +108,7 @@ func resourceLbRouteCreate(ctx context.Context, d *schema.ResourceData, m interf
102108
Match: &lbSDK.RouteMatch{
103109
Sni: types.ExpandStringPtr(d.Get("match_sni")),
104110
HostHeader: types.ExpandStringPtr(d.Get("match_host_header")),
111+
PathBegin: types.ExpandStringPtr(d.Get("match_path_begin")),
105112
MatchSubdomains: d.Get("match_subdomains").(bool),
106113
},
107114
}
@@ -140,6 +147,7 @@ func resourceLbRouteRead(ctx context.Context, d *schema.ResourceData, m interfac
140147
_ = d.Set("backend_id", zonal.NewIDString(zone, route.BackendID))
141148
_ = d.Set("match_sni", types.FlattenStringPtr(route.Match.Sni))
142149
_ = d.Set("match_host_header", types.FlattenStringPtr(route.Match.HostHeader))
150+
_ = d.Set("match_path_begin", types.FlattenStringPtr(route.Match.PathBegin))
143151
_ = d.Set("match_subdomains", route.Match.MatchSubdomains)
144152
_ = d.Set("created_at", types.FlattenTime(route.CreatedAt))
145153
_ = d.Set("updated_at", types.FlattenTime(route.UpdatedAt))
@@ -169,6 +177,7 @@ func resourceLbRouteUpdate(ctx context.Context, d *schema.ResourceData, m interf
169177
Match: &lbSDK.RouteMatch{
170178
Sni: types.ExpandStringPtr(d.Get("match_sni")),
171179
HostHeader: types.ExpandStringPtr(d.Get("match_host_header")),
180+
PathBegin: types.ExpandStringPtr(d.Get("match_path_begin")),
172181
MatchSubdomains: d.Get("match_subdomains").(bool),
173182
},
174183
}

internal/services/lb/route_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,50 @@ func TestAccRoute_WithHostHeader(t *testing.T) {
135135
})
136136
}
137137

138+
func TestAccRoute_WithPathBegin(t *testing.T) {
139+
tt := acctest.NewTestTools(t)
140+
defer tt.Cleanup()
141+
resource.ParallelTest(t, resource.TestCase{
142+
PreCheck: func() { acctest.PreCheck(t) },
143+
ProviderFactories: tt.ProviderFactories,
144+
CheckDestroy: isRouteDestroyed(tt),
145+
Steps: []resource.TestStep{
146+
{
147+
Config: `
148+
resource scaleway_lb_ip ip01 {}
149+
resource scaleway_lb lb01 {
150+
ip_id = scaleway_lb_ip.ip01.id
151+
name = "test-lb"
152+
type = "lb-s"
153+
}
154+
resource scaleway_lb_backend bkd01 {
155+
lb_id = scaleway_lb.lb01.id
156+
forward_protocol = "http"
157+
forward_port = 80
158+
proxy_protocol = "none"
159+
}
160+
resource scaleway_lb_frontend frt01 {
161+
lb_id = scaleway_lb.lb01.id
162+
backend_id = scaleway_lb_backend.bkd01.id
163+
inbound_port = 80
164+
}
165+
resource scaleway_lb_route rt01 {
166+
frontend_id = scaleway_lb_frontend.frt01.id
167+
backend_id = scaleway_lb_backend.bkd01.id
168+
match_path_begin = "/api"
169+
}
170+
`,
171+
Check: resource.ComposeTestCheckFunc(
172+
isRoutePresent(tt, "scaleway_lb_route.rt01"),
173+
resource.TestCheckResourceAttr("scaleway_lb_route.rt01", "match_path_begin", "/api"),
174+
resource.TestCheckResourceAttrSet("scaleway_lb_route.rt01", "created_at"),
175+
resource.TestCheckResourceAttrSet("scaleway_lb_route.rt01", "updated_at"),
176+
),
177+
},
178+
},
179+
})
180+
}
181+
138182
func isRoutePresent(tt *acctest.TestTools, n string) resource.TestCheckFunc {
139183
return func(s *terraform.State) error {
140184
rs, ok := s.RootModule().Resources[n]

0 commit comments

Comments
 (0)