Skip to content

Commit

Permalink
CDN-5952 add request limiter option (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarianaStrix authored Apr 19, 2023
1 parent bc73b5b commit 9852535
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 1 deletion.
21 changes: 21 additions & 0 deletions docs/resources/gcore_cdn_resource.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ resource "gcore_cdn_resource" "cdn_example_com" {
redirect_http_to_https {
value = true
}
request_limiter {
rate_unit = "r/s"
rate = 5
burst = 1
}
gzip_on {
value = true
}
Expand Down Expand Up @@ -101,6 +106,7 @@ Optional:
- `query_params_blacklist` (Block List, Max: 1) (see [below for nested schema](#nestedblock--options--query_params_blacklist))
- `query_params_whitelist` (Block List, Max: 1) (see [below for nested schema](#nestedblock--options--query_params_whitelist))
- `redirect_http_to_https` (Block List, Max: 1) Sets redirect from HTTP protocol to HTTPS for all resource requests. (see [below for nested schema](#nestedblock--options--redirect_http_to_https))
- `request_limiter` (Block List, Max: 1) It allows to limit the amount of HTTP requests. (see [below for nested schema](#nestedblock--options--request_limiter))
- `rewrite` (Block List, Max: 1) (see [below for nested schema](#nestedblock--options--rewrite))
- `sni` (Block List, Max: 1) (see [below for nested schema](#nestedblock--options--sni))
- `static_headers` (Block List, Max: 1) (see [below for nested schema](#nestedblock--options--static_headers))
Expand Down Expand Up @@ -213,6 +219,21 @@ Optional:
- `enabled` (Boolean)


<a id="nestedblock--options--request_limiter"></a>
### Nested Schema for `options.request_limiter`

Required:

- `rate` (Number)
- `burst` (Number)

Optional:

- `enabled` (Boolean)
- `rate_unit` (String) Available values 'r/s' or 'r/m'
- `delay` (Number)


<a id="nestedblock--options--rewrite"></a>
### Nested Schema for `options.rewrite`

Expand Down
21 changes: 21 additions & 0 deletions docs/resources/gcore_cdn_rule.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ resource "gcore_cdn_rule" "cdn_example_com_rule_1" {
redirect_http_to_https {
value = true
}
request_limiter {
rate_unit = "r/s"
rate = 5
burst = 1
}
gzip_on {
value = true
}
Expand Down Expand Up @@ -132,6 +137,7 @@ Optional:
- `query_params_blacklist` (Block List, Max: 1) (see [below for nested schema](#nestedblock--options--query_params_blacklist))
- `query_params_whitelist` (Block List, Max: 1) (see [below for nested schema](#nestedblock--options--query_params_whitelist))
- `redirect_http_to_https` (Block List, Max: 1) Sets redirect from HTTP protocol to HTTPS for all resource requests. (see [below for nested schema](#nestedblock--options--redirect_http_to_https))
- `request_limiter` (Block List, Max: 1) It allows to limit the amount of HTTP requests. (see [below for nested schema](#nestedblock--options--request_limiter))
- `rewrite` (Block List, Max: 1) (see [below for nested schema](#nestedblock--options--rewrite))
- `sni` (Block List, Max: 1) (see [below for nested schema](#nestedblock--options--sni))
- `static_headers` (Block List, Max: 1) (see [below for nested schema](#nestedblock--options--static_headers))
Expand Down Expand Up @@ -244,6 +250,21 @@ Optional:
- `enabled` (Boolean)


<a id="nestedblock--options--request_limiter"></a>
### Nested Schema for `options.request_limiter`

Required:

- `rate` (Number)
- `burst` (Number)

Optional:

- `enabled` (Boolean)
- `rate_unit` (String) Available values 'r/s' or 'r/m'
- `delay` (Number)


<a id="nestedblock--options--rewrite"></a>
### Nested Schema for `options.rewrite`

Expand Down
5 changes: 5 additions & 0 deletions examples/resources/gcore_cdn_resource/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ resource "gcore_cdn_resource" "cdn_example_com" {
redirect_http_to_https {
value = true
}
request_limiter {
rate_unit = "r/s"
rate = 5
burst = 1
}
gzip_on {
value = true
}
Expand Down
5 changes: 5 additions & 0 deletions examples/resources/gcore_cdn_rule/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ resource "gcore_cdn_rule" "cdn_example_com_rule_1" {
redirect_http_to_https {
value = true
}
request_limiter {
rate_unit = "r/s"
rate = 5
burst = 1
}
gzip_on {
value = true
}
Expand Down
50 changes: 50 additions & 0 deletions gcore/resource_gcore_cdn_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,39 @@ var (
},
},
},
"request_limiter": {
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Description: "It allows to limit the amount of HTTP requests",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"rate": {
Type: schema.TypeInt,
Required: true,
},
"burst": {
Type: schema.TypeInt,
Required: true,
},
"rate_unit": {
Type: schema.TypeInt,
Optional: true,
Default: "r/s",
},
"delay": {
Type: schema.TypeInt,
Optional: true,
Default: 0,
},
},
},
},
"gzip_on": {
Type: schema.TypeList,
MaxItems: 1,
Expand Down Expand Up @@ -625,6 +658,19 @@ func listToOptions(l []interface{}) *gcdn.Options {
Value: opt["value"].(bool),
}
}
if opt, ok := getOptByName(fields, "request_limiter"); ok {
enabled := true
if _, ok := opt["enabled"]; ok {
enabled = opt["enabled"].(bool)
}
opts.RequestLimiter = &gcdn.RequestLimiter{
Enabled: enabled,
Rate: opt["rate"].(int),
Burst: opt["burst"].(int),
RateUnit: opt["rate_unit"].(string),
Delay: opt["delay"].(int),
}
}
if opt, ok := getOptByName(fields, "gzip_on"); ok {
enabled := true
if _, ok := opt["enabled"]; ok {
Expand Down Expand Up @@ -806,6 +852,10 @@ func optionsToList(options *gcdn.Options) []interface{} {
m := structToMap(options.RedirectHttpToHttps)
result["redirect_http_to_https"] = []interface{}{m}
}
if options.RequestLimiter != nil {
m := structToMap(options.RequestLimiter)
result["request_limiter"] = []interface{}{m}
}
if options.GzipOn != nil {
m := structToMap(options.GzipOn)
result["gzip_on"] = []interface{}{m}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/AlekSi/pointer v1.2.0
github.com/G-Core/gcore-dns-sdk-go v0.2.3
github.com/G-Core/gcore-storage-sdk-go v0.1.34
github.com/G-Core/gcorelabscdn-go v0.1.27
github.com/G-Core/gcorelabscdn-go v0.1.28
github.com/G-Core/gcorelabscloud-go v0.5.31
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ github.com/G-Core/gcore-storage-sdk-go v0.1.34 h1:0GPQfz1kA6mQi6fiisGsh0Um4H9PZe
github.com/G-Core/gcore-storage-sdk-go v0.1.34/go.mod h1:BUAEZZZJJt/+luRFunqziv3+JnbVMLbQXDWz9kV8Te8=
github.com/G-Core/gcorelabscdn-go v0.1.27 h1:ZekODS/la0tA24fpW5jvpCK0TA2gGCJvn/fCXgbQ3YI=
github.com/G-Core/gcorelabscdn-go v0.1.27/go.mod h1:iSGXaTvZBzDHQW+rKFS918BgFVpONcyLEijwh8WsXpE=
github.com/G-Core/gcorelabscdn-go v0.1.28 h1:gmPXzyYSPOayG+3h+HWSgredv/zv2U/559ivJ9weCJk=
github.com/G-Core/gcorelabscdn-go v0.1.28/go.mod h1:iSGXaTvZBzDHQW+rKFS918BgFVpONcyLEijwh8WsXpE=
github.com/G-Core/gcorelabscloud-go v0.5.31 h1:cbGJVaBf4zUK/CXM8rKEGhfl68sq4QJpGwEwmAM4tV4=
github.com/G-Core/gcorelabscloud-go v0.5.31/go.mod h1:tizV2NaASUpPI6NfJVIOM5yBI8MOriAzSHA/5K1m6aU=
github.com/Masterminds/goutils v1.1.0/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
Expand Down

0 comments on commit 9852535

Please sign in to comment.