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

Commit 31c7929

Browse files
authored
added support for basic cdn resources (#26)
* added support for basic cdn resources * apply terraform crud best practices * fixed tests
1 parent 994b4cf commit 31c7929

19 files changed

+1122
-17
lines changed

GNUmakefile

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
TEST?=$$(go list ./... |grep -v 'vendor')
22
GOFMT_FILES?=$$(find . -name '*.go' |grep -v vendor)
33
WEBSITE_REPO=github.com/hashicorp/terraform-website
4-
TAG=$$(git describe --tags)
54
BINARY_NAME=terraform-provider-gcore
6-
PLUGIN_PATH=~/.terraform.d/plugins/local.gcorelabs.com/repo/gcore/$(TAG)/linux_amd64
75
PKG_NAME=gcorelabs
86

7+
TAG_PREFIX="v"
8+
TAG=$(shell git describe --tags)
9+
VERSION=$(shell git describe --tags $(LAST_TAG_COMMIT) | sed "s/^$(TAG_PREFIX)//")
10+
OS=$$(go env GOOS)
11+
ARCH=$$(go env GOARCH)
12+
PLUGIN_PATH=~/.terraform.d/plugins/local.gcorelabs.com/repo/gcore/$(VERSION)/$(OS)_$(ARCH)
13+
914
default: build
1015

1116
build: fmtcheck
1217
mkdir -p $(PLUGIN_PATH)
13-
go build -o $(PLUGIN_PATH)/$(BINARY_NAME)_v$(TAG)
18+
go build -o $(PLUGIN_PATH)/$(BINARY_NAME)_v$(VERSION)
1419

1520
test: fmtcheck
1621
go test -i $(TEST) || exit 1

docs/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ resource "gcore_lbmember" "lbm2" {
245245
### Optional
246246

247247
- **gcore_api** (String) Region API
248+
- **gcore_cdn_api** (String) CDN API
248249
- **gcore_client_id** (String) Client id
249250
- **gcore_platform** (String) Platform ulr is used for generate jwt
250251
- **password** (String)
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "gcore_cdn_origingroup Resource - terraform-provider-gcorelabs"
4+
subcategory: ""
5+
description: |-
6+
Represent origin group
7+
---
8+
9+
# gcore_cdn_origingroup (Resource)
10+
11+
Represent origin group
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+
resource "gcore_cdn_origingroup" "origin_group_1" {
24+
name = "origin_group_1"
25+
use_next = true
26+
origin {
27+
source = "example.com"
28+
enabled = false
29+
}
30+
origin {
31+
source = "mirror.example.com"
32+
enabled = true
33+
backup = true
34+
}
35+
}
36+
```
37+
38+
<!-- schema generated by tfplugindocs -->
39+
## Schema
40+
41+
### Required
42+
43+
- **name** (String) Name of the origin group
44+
- **origin** (Block Set, Min: 1) Contains information about all IP address or Domain names of your origin and the port if custom (see [below for nested schema](#nestedblock--origin))
45+
- **use_next** (Boolean) This options have two possible values: true — The option is active. In case the origin responds with 4XX or 5XX codes, use the next origin from the list. false — The option is disabled.
46+
47+
### Optional
48+
49+
- **id** (String) The ID of this resource.
50+
51+
<a id="nestedblock--origin"></a>
52+
### Nested Schema for `origin`
53+
54+
Required:
55+
56+
- **source** (String) IP address or Domain name of your origin and the port if custom
57+
58+
Optional:
59+
60+
- **backup** (Boolean) true — The option is active. The origin will not be used until one of active origins become unavailable. false — The option is disabled.
61+
- **enabled** (Boolean) The setting allows to enable or disable an Origin source in the Origins group
62+
63+
Read-Only:
64+
65+
- **id** (Number) The ID of this resource.
66+
67+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "gcore_cdn_resource Resource - terraform-provider-gcorelabs"
4+
subcategory: ""
5+
description: |-
6+
Represent cdn resource
7+
---
8+
9+
# gcore_cdn_resource (Resource)
10+
11+
Represent cdn resource
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+
24+
resource "gcore_cdn_resource" "cdn_example_com" {
25+
cname = "cdn.example.com"
26+
origin_group = 11
27+
origin_protocol = "MATCH"
28+
secondary_hostnames = ["cdn2.example.com"]
29+
}
30+
```
31+
32+
<!-- schema generated by tfplugindocs -->
33+
## Schema
34+
35+
### Required
36+
37+
- **cname** (String) A CNAME that will be used to deliver content though a CDN
38+
39+
### Optional
40+
41+
- **active** (Boolean) The setting allows to enable or disable a CDN Resource
42+
- **id** (String) The ID of this resource.
43+
- **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.
44+
- **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.
45+
- **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.
46+
- **secondary_hostnames** (Set of String) List of additional CNAMEs.
47+
48+
### Read-Only
49+
50+
- **last_updated** (String)
51+
- **status** (String) Status of a CDN resource content availability. Possible values are: Active, Suspended, Processed.
52+
53+

docs/resources/gcore_cdn_rule.md

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "gcore_cdn_rule Resource - terraform-provider-gcorelabs"
4+
subcategory: ""
5+
description: |-
6+
Represent cdn resource rule
7+
---
8+
9+
# gcore_cdn_rule (Resource)
10+
11+
Represent cdn resource rule
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+
resource "gcore_cdn_origingroup" "origin_group_1" {
24+
name = "origin_group_1"
25+
use_next = true
26+
origin {
27+
source = "example.com"
28+
enabled = false
29+
}
30+
origin {
31+
source = "mirror.example.com"
32+
enabled = true
33+
backup = true
34+
}
35+
}
36+
37+
resource "gcore_cdn_resource" "cdn_example_com" {
38+
cname = "cdn.example.com"
39+
origin_group = gcore_cdn_origingroup.origin_group_1.id
40+
origin_protocol = "MATCH"
41+
secondary_hostnames = ["cdn2.example.com"]
42+
}
43+
44+
resource "gcore_cdn_rule" "cdn_example_com_rule_1" {
45+
resource_id = gcore_cdn_resource.cdn_example_com.id
46+
name = "All images"
47+
rule = "/folder/images/*.png"
48+
rule_type = 0
49+
}
50+
51+
resource "gcore_cdn_rule" "cdn_example_com_rule_2" {
52+
resource_id = gcore_cdn_resource.cdn_example_com.id
53+
name = "All scripts"
54+
rule = "/folder/images/*.js"
55+
rule_type = 0
56+
}
57+
```
58+
59+
<!-- schema generated by tfplugindocs -->
60+
## Schema
61+
62+
### Required
63+
64+
- **name** (String) Rule name
65+
- **resource_id** (Number)
66+
- **rule** (String) A pattern that defines when the rule is triggered. By default, we add a leading forward slash to any rule pattern. Specify a pattern without a forward slash.
67+
- **rule_type** (Number) Type of rule. The rule is applied if the requested URI matches the rule pattern. It has two possible values: Type 0 — RegEx. Must start with '^/' or '/'. Type 1 — RegEx. Legacy type. Note that for this rule type we automatically add / to each rule pattern before your regular expression. Please use Type 0.
68+
69+
### Optional
70+
71+
- **id** (String) The ID of this resource.
72+
73+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
resource "gcore_cdn_origingroup" "origin_group_1" {
9+
name = "origin_group_1"
10+
use_next = true
11+
origin {
12+
source = "example.com"
13+
enabled = false
14+
}
15+
origin {
16+
source = "mirror.example.com"
17+
enabled = true
18+
backup = true
19+
}
20+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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+
9+
resource "gcore_cdn_resource" "cdn_example_com" {
10+
cname = "cdn.example.com"
11+
origin_group = 11
12+
origin_protocol = "MATCH"
13+
secondary_hostnames = ["cdn2.example.com"]
14+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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+
resource "gcore_cdn_origingroup" "origin_group_1" {
9+
name = "origin_group_1"
10+
use_next = true
11+
origin {
12+
source = "example.com"
13+
enabled = false
14+
}
15+
origin {
16+
source = "mirror.example.com"
17+
enabled = true
18+
backup = true
19+
}
20+
}
21+
22+
resource "gcore_cdn_resource" "cdn_example_com" {
23+
cname = "cdn.example.com"
24+
origin_group = gcore_cdn_origingroup.origin_group_1.id
25+
origin_protocol = "MATCH"
26+
secondary_hostnames = ["cdn2.example.com"]
27+
}
28+
29+
resource "gcore_cdn_rule" "cdn_example_com_rule_1" {
30+
resource_id = gcore_cdn_resource.cdn_example_com.id
31+
name = "All images"
32+
rule = "/folder/images/*.png"
33+
rule_type = 0
34+
}
35+
36+
resource "gcore_cdn_rule" "cdn_example_com_rule_2" {
37+
resource_id = gcore_cdn_resource.cdn_example_com.id
38+
name = "All scripts"
39+
rule = "/folder/images/*.js"
40+
rule_type = 0
41+
}

gcore/provider.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ package gcore
22

33
import (
44
"context"
5+
"net/http"
56

7+
gcdn "github.com/G-Core/gcorelabscdn-go"
8+
gcdnProvider "github.com/G-Core/gcorelabscdn-go/gcore/provider"
69
gcorecloud "github.com/G-Core/gcorelabscloud-go"
710
gc "github.com/G-Core/gcorelabscloud-go/gcore"
811
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
@@ -34,6 +37,12 @@ func Provider() *schema.Provider {
3437
Description: "Region API",
3538
DefaultFunc: schema.EnvDefaultFunc("GCORE_API", ""),
3639
},
40+
"gcore_cdn_api": &schema.Schema{
41+
Type: schema.TypeString,
42+
Optional: true,
43+
Description: "CDN API",
44+
DefaultFunc: schema.EnvDefaultFunc("GCORE_CDN_API", ""),
45+
},
3746
"gcore_client_id": &schema.Schema{
3847
Type: schema.TypeString,
3948
Optional: true,
@@ -58,6 +67,9 @@ func Provider() *schema.Provider {
5867
"gcore_baremetal": resourceBmInstance(),
5968
"gcore_snapshot": resourceSnapshot(),
6069
"gcore_servergroup": resourceServerGroup(),
70+
"gcore_cdn_resource": resourceCDNResource(),
71+
"gcore_cdn_origingroup": resourceCDNOriginGroup(),
72+
"gcore_cdn_rule": resourceCDNRule(),
6173
},
6274
DataSourcesMap: map[string]*schema.Resource{
6375
"gcore_project": dataSourceProject(),
@@ -84,6 +96,7 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}
8496
username := d.Get("user_name").(string)
8597
password := d.Get("password").(string)
8698
api := d.Get("gcore_api").(string)
99+
cdnAPI := d.Get("gcore_cdn_api").(string)
87100
platform := d.Get("gcore_platform").(string)
88101
clientID := d.Get("gcore_client_id").(string)
89102

@@ -101,8 +114,15 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}
101114
return nil, diag.FromErr(err)
102115
}
103116

117+
cdnProvider := gcdnProvider.NewClient(cdnAPI, gcdnProvider.WithSignerFunc(func(req *http.Request) error {
118+
req.Header.Set("Authorization", "Bearer "+provider.AccessToken())
119+
return nil
120+
}))
121+
cdnService := gcdn.NewService(cdnProvider)
122+
104123
config := Config{
105-
Provider: provider,
124+
Provider: provider,
125+
CDNClient: cdnService,
106126
}
107127

108128
return &config, diags

0 commit comments

Comments
 (0)