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

Commit 13eea8a

Browse files
authored
added support for ssl certs and several options (#28)
1 parent 21064ae commit 13eea8a

12 files changed

+649
-4
lines changed

docs/resources/gcore_cdn_resource.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,47 @@ resource "gcore_cdn_resource" "cdn_example_com" {
4040

4141
- **active** (Boolean) The setting allows to enable or disable a CDN Resource
4242
- **id** (String) The ID of this resource.
43+
- **options** (Block List, Max: 1) Each option in CDN resource settings. Each option added to CDN resource settings should have the following mandatory request fields: enabled, value. (see [below for nested schema](#nestedblock--options))
4344
- **origin** (String) A domain name or IP of your origin source. Specify a port if custom. You can use either 'origin' parameter or 'originGroup' in the resource definition.
4445
- **origin_group** (Number) ID of the Origins Group. Use one of your Origins Group or create a new one. You can use either 'origin' parameter or 'originGroup' in the resource definition.
4546
- **origin_protocol** (String) This option defines the protocol that will be used by CDN servers to request content from an origin source. If not specified, we will use HTTP to connect to an origin server. Possible values are: HTTPS, HTTP, MATCH.
4647
- **secondary_hostnames** (Set of String) List of additional CNAMEs.
48+
- **ssl_data** (Number)
49+
- **ssl_enabled** (Boolean)
4750

4851
### Read-Only
4952

5053
- **last_updated** (String)
5154
- **status** (String) Status of a CDN resource content availability. Possible values are: Active, Suspended, Processed.
5255

56+
<a id="nestedblock--options"></a>
57+
### Nested Schema for `options`
58+
59+
Optional:
60+
61+
- **edge_cache_settings** (Block List, Max: 1) The cache expiration time for CDN servers. (see [below for nested schema](#nestedblock--options--edge_cache_settings))
62+
- **host_header** (Block List, Max: 1) Specify the Host header that CDN servers use when request content from an origin server. Your server must be able to process requests with the chosen header. If the option is in NULL state Host Header value is taken from the CNAME field. (see [below for nested schema](#nestedblock--options--host_header))
63+
64+
<a id="nestedblock--options--edge_cache_settings"></a>
65+
### Nested Schema for `options.edge_cache_settings`
66+
67+
Required:
68+
69+
- **enabled** (Boolean)
70+
71+
Optional:
72+
73+
- **custom_values** (Map of String) Caching time for a response with specific codes. These settings have a higher priority than the value field. Response code ('304', '404' for example). Use 'any' to specify caching time for all response codes. Caching time in seconds ('0s', '600s' for example). Use '0s' to disable caching for a specific response code.
74+
- **default** (String) Content will be cached according to origin cache settings. The value applies for a response with codes 200, 201, 204, 206, 301, 302, 303, 304, 307, 308 if an origin server does not have caching HTTP headers. Responses with other codes will not be cached.
75+
- **value** (String) Caching time for a response with codes 200, 206, 301, 302. Responses with codes 4xx, 5xx will not be cached. Use '0s' disable to caching. Use custom_values field to specify a custom caching time for a response with specific codes.
76+
77+
78+
<a id="nestedblock--options--host_header"></a>
79+
### Nested Schema for `options.host_header`
80+
81+
Required:
82+
83+
- **enabled** (Boolean)
84+
- **value** (String)
85+
5386

docs/resources/gcore_cdn_rule.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,36 @@ resource "gcore_cdn_rule" "cdn_example_com_rule_2" {
6969
### Optional
7070

7171
- **id** (String) The ID of this resource.
72+
- **options** (Block List, Max: 1) Each option in CDN resource settings. Each option added to CDN resource settings should have the following mandatory request fields: enabled, value. (see [below for nested schema](#nestedblock--options))
73+
74+
<a id="nestedblock--options"></a>
75+
### Nested Schema for `options`
76+
77+
Optional:
78+
79+
- **edge_cache_settings** (Block List, Max: 1) The cache expiration time for CDN servers. (see [below for nested schema](#nestedblock--options--edge_cache_settings))
80+
- **host_header** (Block List, Max: 1) Specify the Host header that CDN servers use when request content from an origin server. Your server must be able to process requests with the chosen header. If the option is in NULL state Host Header value is taken from the CNAME field. (see [below for nested schema](#nestedblock--options--host_header))
81+
82+
<a id="nestedblock--options--edge_cache_settings"></a>
83+
### Nested Schema for `options.edge_cache_settings`
84+
85+
Required:
86+
87+
- **enabled** (Boolean)
88+
89+
Optional:
90+
91+
- **custom_values** (Map of String) Caching time for a response with specific codes. These settings have a higher priority than the value field. Response code ('304', '404' for example). Use 'any' to specify caching time for all response codes. Caching time in seconds ('0s', '600s' for example). Use '0s' to disable caching for a specific response code.
92+
- **default** (String) Content will be cached according to origin cache settings. The value applies for a response with codes 200, 201, 204, 206, 301, 302, 303, 304, 307, 308 if an origin server does not have caching HTTP headers. Responses with other codes will not be cached.
93+
- **value** (String) Caching time for a response with codes 200, 206, 301, 302. Responses with codes 4xx, 5xx will not be cached. Use '0s' disable to caching. Use custom_values field to specify a custom caching time for a response with specific codes.
94+
95+
96+
<a id="nestedblock--options--host_header"></a>
97+
### Nested Schema for `options.host_header`
98+
99+
Required:
100+
101+
- **enabled** (Boolean)
102+
- **value** (String)
72103

73104

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "gcore_cdn_sslcert Resource - terraform-provider-gcorelabs"
4+
subcategory: ""
5+
description: |-
6+
7+
---
8+
9+
# gcore_cdn_sslcert (Resource)
10+
11+
12+
13+
## Example Usage
14+
15+
```terraform
16+
provider gcore {
17+
user_name = "test"
18+
password = "test"
19+
gcore_platform = "https://api.gcdn.co"
20+
gcore_cdn_api = "https://api.gcdn.co"
21+
}
22+
23+
variable "cert" {
24+
type = string
25+
sensitive = true
26+
}
27+
28+
variable "private_key" {
29+
type = string
30+
sensitive = true
31+
}
32+
33+
resource "gcore_cdn_sslcert" "cdnopt_cert" {
34+
name = "Test cert for cdnopt_bookatest_by"
35+
cert = var.cert
36+
private_key = var.private_key
37+
}
38+
```
39+
40+
<!-- schema generated by tfplugindocs -->
41+
## Schema
42+
43+
### Required
44+
45+
- **cert** (String, Sensitive) The public part of the SSL certificate. All chain of the SSL certificate should be added.
46+
- **name** (String) Name of the SSL certificate. Must be unique.
47+
- **private_key** (String, Sensitive) The private key of the SSL certificate.
48+
49+
### Optional
50+
51+
- **id** (String) The ID of this resource.
52+
53+
### Read-Only
54+
55+
- **automated** (Boolean) The way SSL certificate was issued.
56+
- **has_related_resources** (Boolean) It shows if the SSL certificate is used by a CDN resource.
57+
58+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
provider gcore {
2+
user_name = "test"
3+
password = "test"
4+
gcore_platform = "https://api.gcdn.co"
5+
gcore_cdn_api = "https://api.gcdn.co"
6+
}
7+
8+
variable "cert" {
9+
type = string
10+
sensitive = true
11+
}
12+
13+
variable "private_key" {
14+
type = string
15+
sensitive = true
16+
}
17+
18+
resource "gcore_cdn_sslcert" "cdnopt_cert" {
19+
name = "Test cert for cdnopt_bookatest_by"
20+
cert = var.cert
21+
private_key = var.private_key
22+
}
23+

gcore/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ func Provider() *schema.Provider {
7070
"gcore_cdn_resource": resourceCDNResource(),
7171
"gcore_cdn_origingroup": resourceCDNOriginGroup(),
7272
"gcore_cdn_rule": resourceCDNRule(),
73+
"gcore_cdn_sslcert": resourceCDNCert(),
7374
},
7475
DataSourcesMap: map[string]*schema.Resource{
7576
"gcore_project": dataSourceProject(),

gcore/resource_gcore_cdn_resource.go

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"context"
55
"fmt"
66
"log"
7+
"reflect"
78
"strconv"
89

10+
gcdn "github.com/G-Core/gcorelabscdn-go/gcore"
911
"github.com/G-Core/gcorelabscdn-go/resources"
1012
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1113
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
@@ -51,6 +53,75 @@ func resourceCDNResource() *schema.Resource {
5153
Elem: &schema.Schema{Type: schema.TypeString},
5254
Description: "List of additional CNAMEs.",
5355
},
56+
"ssl_enabled": {
57+
Type: schema.TypeBool,
58+
Optional: true,
59+
},
60+
"ssl_data": {
61+
Type: schema.TypeInt,
62+
Optional: true,
63+
RequiredWith: []string{"ssl_enabled"},
64+
},
65+
"options": {
66+
Type: schema.TypeList,
67+
MaxItems: 1,
68+
Optional: true,
69+
Computed: true,
70+
Description: "Each option in CDN resource settings. Each option added to CDN resource settings should have the following mandatory request fields: enabled, value.",
71+
Elem: &schema.Resource{
72+
Schema: map[string]*schema.Schema{
73+
"edge_cache_settings": {
74+
Type: schema.TypeList,
75+
MaxItems: 1,
76+
Optional: true,
77+
Computed: true,
78+
Description: "The cache expiration time for CDN servers.",
79+
Elem: &schema.Resource{
80+
Schema: map[string]*schema.Schema{
81+
"enabled": {
82+
Type: schema.TypeBool,
83+
Required: true,
84+
},
85+
"value": {
86+
Type: schema.TypeString,
87+
Optional: true,
88+
Description: "Caching time for a response with codes 200, 206, 301, 302. Responses with codes 4xx, 5xx will not be cached. Use '0s' disable to caching. Use custom_values field to specify a custom caching time for a response with specific codes.",
89+
},
90+
"default": {
91+
Type: schema.TypeString,
92+
Optional: true,
93+
Description: "Content will be cached according to origin cache settings. The value applies for a response with codes 200, 201, 204, 206, 301, 302, 303, 304, 307, 308 if an origin server does not have caching HTTP headers. Responses with other codes will not be cached.",
94+
},
95+
"custom_values": {
96+
Type: schema.TypeMap,
97+
Optional: true,
98+
Elem: schema.TypeString,
99+
Description: "Caching time for a response with specific codes. These settings have a higher priority than the value field. Response code ('304', '404' for example). Use 'any' to specify caching time for all response codes. Caching time in seconds ('0s', '600s' for example). Use '0s' to disable caching for a specific response code.",
100+
},
101+
},
102+
},
103+
},
104+
"host_header": {
105+
Type: schema.TypeList,
106+
MaxItems: 1,
107+
Optional: true,
108+
Description: "Specify the Host header that CDN servers use when request content from an origin server. Your server must be able to process requests with the chosen header. If the option is in NULL state Host Header value is taken from the CNAME field.",
109+
Elem: &schema.Resource{
110+
Schema: map[string]*schema.Schema{
111+
"enabled": {
112+
Type: schema.TypeBool,
113+
Required: true,
114+
},
115+
"value": {
116+
Type: schema.TypeString,
117+
Required: true,
118+
},
119+
},
120+
},
121+
},
122+
},
123+
},
124+
},
54125
"active": {
55126
Type: schema.TypeBool,
56127
Optional: true,
@@ -121,8 +192,13 @@ func resourceCDNResourceRead(ctx context.Context, d *schema.ResourceData, m inte
121192
d.Set("origin_group", result.OriginGroup)
122193
d.Set("origin_protocol", result.OriginProtocol)
123194
d.Set("secondary_hostnames", result.SecondaryHostnames)
195+
d.Set("ssl_enabled", result.SSlEnabled)
196+
d.Set("ssl_data", result.SSLData)
124197
d.Set("status", result.Status)
125198
d.Set("active", result.Active)
199+
if err := d.Set("options", optionsToList(result.Options)); err != nil {
200+
return diag.FromErr(err)
201+
}
126202

127203
log.Println("[DEBUG] Finish CDN Resource reading")
128204
return nil
@@ -141,8 +217,11 @@ func resourceCDNResourceUpdate(ctx context.Context, d *schema.ResourceData, m in
141217

142218
var req resources.UpdateRequest
143219
req.Active = d.Get("active").(bool)
220+
req.SSlEnabled = d.Get("ssl_enabled").(bool)
221+
req.SSLData = d.Get("ssl_data").(int)
144222
req.OriginGroup = d.Get("origin_group").(int)
145223
req.OriginProtocol = resources.Protocol(d.Get("origin_protocol").(string))
224+
req.Options = listToOptions(d.Get("options").([]interface{}))
146225
for _, hostname := range d.Get("secondary_hostnames").(*schema.Set).List() {
147226
req.SecondaryHostnames = append(req.SecondaryHostnames, hostname.(string))
148227
}
@@ -178,3 +257,96 @@ func resourceCDNRresourceDelete(ctx context.Context, d *schema.ResourceData, m i
178257
log.Println("[DEBUG] Finish CDN Resource deleting")
179258
return nil
180259
}
260+
261+
func listToOptions(l []interface{}) *gcdn.Options {
262+
if len(l) == 0 {
263+
return nil
264+
}
265+
266+
var opts gcdn.Options
267+
fields := l[0].(map[string]interface{})
268+
if opt, ok := getOptByName(fields, "edge_cache_settings"); ok {
269+
rawCustomVals := opt["custom_values"].(map[string]interface{})
270+
customVals := make(map[string]string, len(rawCustomVals))
271+
for key, value := range rawCustomVals {
272+
customVals[key] = value.(string)
273+
}
274+
275+
opts.EdgeCacheSettings = &gcdn.EdgeCacheSettings{
276+
Enabled: opt["enabled"].(bool),
277+
Value: opt["value"].(string),
278+
CustomValues: customVals,
279+
Default: opt["default"].(string),
280+
}
281+
}
282+
if opt, ok := getOptByName(fields, "host_header"); ok {
283+
opts.HostHeader = &gcdn.HostHeader{
284+
Enabled: opt["enabled"].(bool),
285+
Value: opt["value"].(string),
286+
}
287+
}
288+
289+
return &opts
290+
}
291+
292+
func getOptByName(fields map[string]interface{}, name string) (map[string]interface{}, bool) {
293+
if _, ok := fields[name]; !ok {
294+
return nil, false
295+
}
296+
297+
container, ok := fields[name].([]interface{})
298+
if !ok {
299+
return nil, false
300+
}
301+
302+
if len(container) == 0 {
303+
return nil, false
304+
}
305+
306+
opt, ok := container[0].(map[string]interface{})
307+
if !ok {
308+
return nil, false
309+
}
310+
311+
return opt, true
312+
}
313+
314+
func optionsToList(options *gcdn.Options) []interface{} {
315+
result := make(map[string][]interface{})
316+
if options.EdgeCacheSettings != nil {
317+
m := structToMap(options.EdgeCacheSettings)
318+
result["edge_cache_settings"] = []interface{}{m}
319+
}
320+
if options.HostHeader != nil {
321+
m := structToMap(options.HostHeader)
322+
result["host_header"] = []interface{}{m}
323+
}
324+
return []interface{}{result}
325+
}
326+
327+
func structToMap(item interface{}) map[string]interface{} {
328+
329+
res := map[string]interface{}{}
330+
if item == nil {
331+
return res
332+
}
333+
v := reflect.TypeOf(item)
334+
reflectValue := reflect.ValueOf(item)
335+
reflectValue = reflect.Indirect(reflectValue)
336+
337+
if v.Kind() == reflect.Ptr {
338+
v = v.Elem()
339+
}
340+
for i := 0; i < v.NumField(); i++ {
341+
tag := v.Field(i).Tag.Get("json")
342+
field := reflectValue.Field(i).Interface()
343+
if tag != "" && tag != "-" {
344+
if v.Field(i).Type.Kind() == reflect.Struct {
345+
res[tag] = structToMap(field)
346+
} else {
347+
res[tag] = field
348+
}
349+
}
350+
}
351+
return res
352+
}

0 commit comments

Comments
 (0)