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

Commit

Permalink
added support for basic cdn resources (#26)
Browse files Browse the repository at this point in the history
* added support for basic cdn resources

* apply terraform crud best practices

* fixed tests
  • Loading branch information
ivishnevs authored Apr 29, 2021
1 parent f6792c1 commit 5af14c1
Show file tree
Hide file tree
Showing 19 changed files with 1,122 additions and 17 deletions.
11 changes: 8 additions & 3 deletions GNUmakefile
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
TEST?=$$(go list ./... |grep -v 'vendor')
GOFMT_FILES?=$$(find . -name '*.go' |grep -v vendor)
WEBSITE_REPO=github.com/hashicorp/terraform-website
TAG=$$(git describe --tags)
BINARY_NAME=terraform-provider-gcore
PLUGIN_PATH=~/.terraform.d/plugins/local.gcorelabs.com/repo/gcore/$(TAG)/linux_amd64
PKG_NAME=gcorelabs

TAG_PREFIX="v"
TAG=$(shell git describe --tags)
VERSION=$(shell git describe --tags $(LAST_TAG_COMMIT) | sed "s/^$(TAG_PREFIX)//")
OS=$$(go env GOOS)
ARCH=$$(go env GOARCH)
PLUGIN_PATH=~/.terraform.d/plugins/local.gcorelabs.com/repo/gcore/$(VERSION)/$(OS)_$(ARCH)

default: build

build: fmtcheck
mkdir -p $(PLUGIN_PATH)
go build -o $(PLUGIN_PATH)/$(BINARY_NAME)_v$(TAG)
go build -o $(PLUGIN_PATH)/$(BINARY_NAME)_v$(VERSION)

test: fmtcheck
go test -i $(TEST) || exit 1
Expand Down
1 change: 1 addition & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ resource "gcore_lbmember" "lbm2" {
### Optional

- **gcore_api** (String) Region API
- **gcore_cdn_api** (String) CDN API
- **gcore_client_id** (String) Client id
- **gcore_platform** (String) Platform ulr is used for generate jwt
- **password** (String)
Expand Down
67 changes: 67 additions & 0 deletions docs/resources/gcore_cdn_origingroup.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "gcore_cdn_origingroup Resource - terraform-provider-gcorelabs"
subcategory: ""
description: |-
Represent origin group
---

# gcore_cdn_origingroup (Resource)

Represent origin group

## Example Usage

```terraform
provider gcore {
user_name = "test"
password = "test"
gcore_platform = "https://api.gcdn.co"
gcore_cdn_api = "https://api.gcdn.co"
}
resource "gcore_cdn_origingroup" "origin_group_1" {
name = "origin_group_1"
use_next = true
origin {
source = "example.com"
enabled = false
}
origin {
source = "mirror.example.com"
enabled = true
backup = true
}
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- **name** (String) Name of the origin group
- **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))
- **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.

### Optional

- **id** (String) The ID of this resource.

<a id="nestedblock--origin"></a>
### Nested Schema for `origin`

Required:

- **source** (String) IP address or Domain name of your origin and the port if custom

Optional:

- **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.
- **enabled** (Boolean) The setting allows to enable or disable an Origin source in the Origins group

Read-Only:

- **id** (Number) The ID of this resource.


53 changes: 53 additions & 0 deletions docs/resources/gcore_cdn_resource.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "gcore_cdn_resource Resource - terraform-provider-gcorelabs"
subcategory: ""
description: |-
Represent cdn resource
---

# gcore_cdn_resource (Resource)

Represent cdn resource

## Example Usage

```terraform
provider gcore {
user_name = "test"
password = "test"
gcore_platform = "https://api.gcdn.co"
gcore_cdn_api = "https://api.gcdn.co"
}
resource "gcore_cdn_resource" "cdn_example_com" {
cname = "cdn.example.com"
origin_group = 11
origin_protocol = "MATCH"
secondary_hostnames = ["cdn2.example.com"]
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- **cname** (String) A CNAME that will be used to deliver content though a CDN

### Optional

- **active** (Boolean) The setting allows to enable or disable a CDN Resource
- **id** (String) The ID of this resource.
- **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.
- **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.
- **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.
- **secondary_hostnames** (Set of String) List of additional CNAMEs.

### Read-Only

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


73 changes: 73 additions & 0 deletions docs/resources/gcore_cdn_rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "gcore_cdn_rule Resource - terraform-provider-gcorelabs"
subcategory: ""
description: |-
Represent cdn resource rule
---

# gcore_cdn_rule (Resource)

Represent cdn resource rule

## Example Usage

```terraform
provider gcore {
user_name = "test"
password = "test"
gcore_platform = "https://api.gcdn.co"
gcore_cdn_api = "https://api.gcdn.co"
}
resource "gcore_cdn_origingroup" "origin_group_1" {
name = "origin_group_1"
use_next = true
origin {
source = "example.com"
enabled = false
}
origin {
source = "mirror.example.com"
enabled = true
backup = true
}
}
resource "gcore_cdn_resource" "cdn_example_com" {
cname = "cdn.example.com"
origin_group = gcore_cdn_origingroup.origin_group_1.id
origin_protocol = "MATCH"
secondary_hostnames = ["cdn2.example.com"]
}
resource "gcore_cdn_rule" "cdn_example_com_rule_1" {
resource_id = gcore_cdn_resource.cdn_example_com.id
name = "All images"
rule = "/folder/images/*.png"
rule_type = 0
}
resource "gcore_cdn_rule" "cdn_example_com_rule_2" {
resource_id = gcore_cdn_resource.cdn_example_com.id
name = "All scripts"
rule = "/folder/images/*.js"
rule_type = 0
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- **name** (String) Rule name
- **resource_id** (Number)
- **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.
- **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.

### Optional

- **id** (String) The ID of this resource.


20 changes: 20 additions & 0 deletions examples/resources/gcore_cdn_origingroup/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
provider gcore {
user_name = "test"
password = "test"
gcore_platform = "https://api.gcdn.co"
gcore_cdn_api = "https://api.gcdn.co"
}

resource "gcore_cdn_origingroup" "origin_group_1" {
name = "origin_group_1"
use_next = true
origin {
source = "example.com"
enabled = false
}
origin {
source = "mirror.example.com"
enabled = true
backup = true
}
}
14 changes: 14 additions & 0 deletions examples/resources/gcore_cdn_resource/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
provider gcore {
user_name = "test"
password = "test"
gcore_platform = "https://api.gcdn.co"
gcore_cdn_api = "https://api.gcdn.co"
}


resource "gcore_cdn_resource" "cdn_example_com" {
cname = "cdn.example.com"
origin_group = 11
origin_protocol = "MATCH"
secondary_hostnames = ["cdn2.example.com"]
}
41 changes: 41 additions & 0 deletions examples/resources/gcore_cdn_rule/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
provider gcore {
user_name = "test"
password = "test"
gcore_platform = "https://api.gcdn.co"
gcore_cdn_api = "https://api.gcdn.co"
}

resource "gcore_cdn_origingroup" "origin_group_1" {
name = "origin_group_1"
use_next = true
origin {
source = "example.com"
enabled = false
}
origin {
source = "mirror.example.com"
enabled = true
backup = true
}
}

resource "gcore_cdn_resource" "cdn_example_com" {
cname = "cdn.example.com"
origin_group = gcore_cdn_origingroup.origin_group_1.id
origin_protocol = "MATCH"
secondary_hostnames = ["cdn2.example.com"]
}

resource "gcore_cdn_rule" "cdn_example_com_rule_1" {
resource_id = gcore_cdn_resource.cdn_example_com.id
name = "All images"
rule = "/folder/images/*.png"
rule_type = 0
}

resource "gcore_cdn_rule" "cdn_example_com_rule_2" {
resource_id = gcore_cdn_resource.cdn_example_com.id
name = "All scripts"
rule = "/folder/images/*.js"
rule_type = 0
}
22 changes: 21 additions & 1 deletion gcore/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package gcore

import (
"context"
"net/http"

gcdn "github.com/G-Core/gcorelabscdn-go"
gcdnProvider "github.com/G-Core/gcorelabscdn-go/gcore/provider"
gcorecloud "github.com/G-Core/gcorelabscloud-go"
gc "github.com/G-Core/gcorelabscloud-go/gcore"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
Expand Down Expand Up @@ -34,6 +37,12 @@ func Provider() *schema.Provider {
Description: "Region API",
DefaultFunc: schema.EnvDefaultFunc("GCORE_API", ""),
},
"gcore_cdn_api": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Description: "CDN API",
DefaultFunc: schema.EnvDefaultFunc("GCORE_CDN_API", ""),
},
"gcore_client_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Expand All @@ -58,6 +67,9 @@ func Provider() *schema.Provider {
"gcore_baremetal": resourceBmInstance(),
"gcore_snapshot": resourceSnapshot(),
"gcore_servergroup": resourceServerGroup(),
"gcore_cdn_resource": resourceCDNResource(),
"gcore_cdn_origingroup": resourceCDNOriginGroup(),
"gcore_cdn_rule": resourceCDNRule(),
},
DataSourcesMap: map[string]*schema.Resource{
"gcore_project": dataSourceProject(),
Expand All @@ -84,6 +96,7 @@ func providerConfigure(ctx context.Context, d *schema.ResourceData) (interface{}
username := d.Get("user_name").(string)
password := d.Get("password").(string)
api := d.Get("gcore_api").(string)
cdnAPI := d.Get("gcore_cdn_api").(string)
platform := d.Get("gcore_platform").(string)
clientID := d.Get("gcore_client_id").(string)

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

cdnProvider := gcdnProvider.NewClient(cdnAPI, gcdnProvider.WithSignerFunc(func(req *http.Request) error {
req.Header.Set("Authorization", "Bearer "+provider.AccessToken())
return nil
}))
cdnService := gcdn.NewService(cdnProvider)

config := Config{
Provider: provider,
Provider: provider,
CDNClient: cdnService,
}

return &config, diags
Expand Down
Loading

0 comments on commit 5af14c1

Please sign in to comment.