Skip to content
This repository was archived by the owner on Mar 1, 2023. It is now read-only.

Commit dddcfc9

Browse files
author
Vitaly Velikodny
committed
add options for resource: browser_cache_settings, cors, rewrite, webp
1 parent ad838ef commit dddcfc9

File tree

3 files changed

+168
-4
lines changed

3 files changed

+168
-4
lines changed

gcore/resource_gcore_cdn_resource.go

Lines changed: 165 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ func resourceCDNResource() *schema.Resource {
8787
Schema: map[string]*schema.Schema{
8888
"enabled": {
8989
Type: schema.TypeBool,
90-
Required: true,
90+
Optional: true,
91+
Default: true,
9192
},
9293
"value": {
9394
Type: schema.TypeString,
@@ -108,6 +109,27 @@ func resourceCDNResource() *schema.Resource {
108109
},
109110
},
110111
},
112+
"browser_cache_settings": {
113+
Type: schema.TypeList,
114+
MaxItems: 1,
115+
Optional: true,
116+
Computed: true,
117+
Description: "",
118+
Elem: &schema.Resource{
119+
Schema: map[string]*schema.Schema{
120+
"enabled": {
121+
Type: schema.TypeBool,
122+
Optional: true,
123+
Default: true,
124+
},
125+
"value": {
126+
Type: schema.TypeString,
127+
Optional: true,
128+
Description: "",
129+
},
130+
},
131+
},
132+
},
111133
"host_header": {
112134
Type: schema.TypeList,
113135
MaxItems: 1,
@@ -117,7 +139,8 @@ func resourceCDNResource() *schema.Resource {
117139
Schema: map[string]*schema.Schema{
118140
"enabled": {
119141
Type: schema.TypeBool,
120-
Required: true,
142+
Optional: true,
143+
Default: true,
121144
},
122145
"value": {
123146
Type: schema.TypeString,
@@ -164,6 +187,78 @@ func resourceCDNResource() *schema.Resource {
164187
},
165188
},
166189
},
190+
"cors": {
191+
Type: schema.TypeList,
192+
MaxItems: 1,
193+
Optional: true,
194+
Description: "",
195+
Elem: &schema.Resource{
196+
Schema: map[string]*schema.Schema{
197+
"enabled": {
198+
Type: schema.TypeBool,
199+
Optional: true,
200+
Default: true,
201+
},
202+
"value": {
203+
Type: schema.TypeSet,
204+
Elem: &schema.Schema{Type: schema.TypeString},
205+
Required: true,
206+
},
207+
},
208+
},
209+
},
210+
"rewrite": {
211+
Type: schema.TypeList,
212+
MaxItems: 1,
213+
Optional: true,
214+
Description: "",
215+
Elem: &schema.Resource{
216+
Schema: map[string]*schema.Schema{
217+
"enabled": {
218+
Type: schema.TypeBool,
219+
Optional: true,
220+
Default: true,
221+
},
222+
"body": {
223+
Type: schema.TypeString,
224+
Required: true,
225+
},
226+
"flag": {
227+
Type: schema.TypeString,
228+
Optional: true,
229+
Default: "break",
230+
},
231+
},
232+
},
233+
},
234+
"webp": {
235+
Type: schema.TypeList,
236+
MaxItems: 1,
237+
Optional: true,
238+
Description: "",
239+
Elem: &schema.Resource{
240+
Schema: map[string]*schema.Schema{
241+
"enabled": {
242+
Type: schema.TypeBool,
243+
Optional: true,
244+
Default: true,
245+
},
246+
"jpg_quality": {
247+
Type: schema.TypeInt,
248+
Required: true,
249+
},
250+
"png_quality": {
251+
Type: schema.TypeInt,
252+
Required: true,
253+
},
254+
"png_lossless": {
255+
Type: schema.TypeBool,
256+
Optional: true,
257+
Default: false,
258+
},
259+
},
260+
},
261+
},
167262
},
168263
},
169264
},
@@ -198,6 +293,8 @@ func resourceCDNResourceCreate(ctx context.Context, d *schema.ResourceData, m in
198293
req.Origin = d.Get("origin").(string)
199294
req.OriginGroup = d.Get("origin_group").(int)
200295

296+
req.Options = listToOptions(d.Get("options").([]interface{}))
297+
201298
for _, hostname := range d.Get("secondary_hostnames").(*schema.Set).List() {
202299
req.SecondaryHostnames = append(req.SecondaryHostnames, hostname.(string))
203300
}
@@ -323,15 +420,29 @@ func listToOptions(l []interface{}) *gcdn.Options {
323420
Default: opt["default"].(string),
324421
}
325422
}
423+
if opt, ok := getOptByName(fields, "browser_cache_settings"); ok {
424+
enabled := true
425+
if _, ok := opt["enabled"]; ok {
426+
enabled = opt["enabled"].(bool)
427+
}
428+
opts.BrowserCacheSettings = &gcdn.BrowserCacheSettings{
429+
Enabled: enabled,
430+
Value: opt["value"].(string),
431+
}
432+
}
326433
if opt, ok := getOptByName(fields, "host_header"); ok {
327434
opts.HostHeader = &gcdn.HostHeader{
328435
Enabled: opt["enabled"].(bool),
329436
Value: opt["value"].(string),
330437
}
331438
}
332439
if opt, ok := getOptByName(fields, "redirect_http_to_https"); ok {
440+
enabled := true
441+
if _, ok := opt["enabled"]; ok {
442+
enabled = opt["enabled"].(bool)
443+
}
333444
opts.RedirectHttpToHttps = &gcdn.RedirectHttpToHttps{
334-
Enabled: opt["enabled"].(bool),
445+
Enabled: enabled,
335446
Value: opt["value"].(bool),
336447
}
337448
}
@@ -345,6 +456,41 @@ func listToOptions(l []interface{}) *gcdn.Options {
345456
Value: opt["value"].(bool),
346457
}
347458
}
459+
if opt, ok := getOptByName(fields, "cors"); ok {
460+
enabled := true
461+
if _, ok := opt["enabled"]; ok {
462+
enabled = opt["enabled"].(bool)
463+
}
464+
opts.Cors = &gcdn.Cors{
465+
Enabled: enabled,
466+
}
467+
for _, v := range opt["value"].(*schema.Set).List() {
468+
opts.Cors.Value = append(opts.Cors.Value, v.(string))
469+
}
470+
}
471+
if opt, ok := getOptByName(fields, "rewrite"); ok {
472+
enabled := true
473+
if _, ok := opt["enabled"]; ok {
474+
enabled = opt["enabled"].(bool)
475+
}
476+
opts.Rewrite = &gcdn.Rewrite{
477+
Enabled: enabled,
478+
Body: opt["body"].(string),
479+
Flag: opt["flag"].(string),
480+
}
481+
}
482+
if opt, ok := getOptByName(fields, "webp"); ok {
483+
enabled := true
484+
if _, ok := opt["enabled"]; ok {
485+
enabled = opt["enabled"].(bool)
486+
}
487+
opts.Webp = &gcdn.Webp{
488+
Enabled: enabled,
489+
JPGQuality: opt["jpg_quality"].(int),
490+
PNGQuality: opt["png_quality"].(int),
491+
PNGLossless: opt["png_lossless"].(bool),
492+
}
493+
}
348494
return &opts
349495
}
350496

@@ -376,6 +522,10 @@ func optionsToList(options *gcdn.Options) []interface{} {
376522
m := structToMap(options.EdgeCacheSettings)
377523
result["edge_cache_settings"] = []interface{}{m}
378524
}
525+
if options.BrowserCacheSettings != nil {
526+
m := structToMap(options.BrowserCacheSettings)
527+
result["browser_cache_settings"] = []interface{}{m}
528+
}
379529
if options.HostHeader != nil {
380530
m := structToMap(options.HostHeader)
381531
result["host_header"] = []interface{}{m}
@@ -388,6 +538,18 @@ func optionsToList(options *gcdn.Options) []interface{} {
388538
m := structToMap(options.GzipOn)
389539
result["gzip_on"] = []interface{}{m}
390540
}
541+
if options.Cors != nil {
542+
m := structToMap(options.Cors)
543+
result["cors"] = []interface{}{m}
544+
}
545+
if options.Rewrite != nil {
546+
m := structToMap(options.Rewrite)
547+
result["rewrite"] = []interface{}{m}
548+
}
549+
if options.Webp != nil {
550+
m := structToMap(options.Webp)
551+
result["webp"] = []interface{}{m}
552+
}
391553
return []interface{}{result}
392554
}
393555

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.16
55
require (
66
github.com/G-Core/g-dns-sdk-go v0.1.2
77
github.com/G-Core/gcorelabs-storage-sdk-go v0.0.9
8-
github.com/G-Core/gcorelabscdn-go v0.1.5
8+
github.com/G-Core/gcorelabscdn-go v0.1.6
99
github.com/G-Core/gcorelabscloud-go v0.4.42
1010
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
1111
github.com/hashicorp/terraform-exec v0.15.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ github.com/G-Core/gcorelabscdn-go v0.1.4 h1:Av65dLjuA8I3lhv2GZkrUz2w1fciJZidpDE9
5151
github.com/G-Core/gcorelabscdn-go v0.1.4/go.mod h1:iSGXaTvZBzDHQW+rKFS918BgFVpONcyLEijwh8WsXpE=
5252
github.com/G-Core/gcorelabscdn-go v0.1.5 h1:+OoMq2/KFdy/NH0/u9CQ+E7s6lmo8ycZQH7Abii3Law=
5353
github.com/G-Core/gcorelabscdn-go v0.1.5/go.mod h1:iSGXaTvZBzDHQW+rKFS918BgFVpONcyLEijwh8WsXpE=
54+
github.com/G-Core/gcorelabscdn-go v0.1.6 h1:Z8vmvJ/0RU7jaGMlLxAID21OtEABoVPwbwbY0oAFPZ0=
55+
github.com/G-Core/gcorelabscdn-go v0.1.6/go.mod h1:iSGXaTvZBzDHQW+rKFS918BgFVpONcyLEijwh8WsXpE=
5456
github.com/G-Core/gcorelabscloud-go v0.4.42 h1:LBH4GnH82eQ5L/Lfnzm8KJovbIHPzt2KMitVN1V9kDQ=
5557
github.com/G-Core/gcorelabscloud-go v0.4.42/go.mod h1:Z1MF80mWagEUrxygtYkvW/MJEYNmIUPsIEYBB3cKjOM=
5658
github.com/Masterminds/goutils v1.1.0/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=

0 commit comments

Comments
 (0)