Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CDI-468: add origin shielding #75

Merged
merged 12 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions docs/data-sources/cdn_shielding_location.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "gcore_cdn_shielding_location Data Source - terraform-provider-gcore"
subcategory: ""
description: |-
Represent shielding locations data
---

# gcore_cdn_shielding_location (Data Source)

Represent shielding locations data

## Example Usage

```terraform
provider gcore {
permanent_api_token = "251$d3361.............1b35f26d8"
}

data "gcore_cdn_shielding_location" "sl" {
city = "Luxembourg"
}

resource "gcore_cdn_originshielding" "origin_shielding_1" {
resource_id = 1
shielding_pop = data.gcore_cdn_shielding_location.sl.id
}
```

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

### Required

- `city` (String) Displayed shielding location point of present

### Read-Only

- `id` (String) The ID of this resource.
96 changes: 96 additions & 0 deletions docs/resources/cdn_originshielding.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "gcore_cdn_originshielding Resource - terraform-provider-gcore"
subcategory: ""
description: |-
Represent origin shielding
---

# gcore_cdn_originshielding (Resource)

Represent origin shielding

## Example Usage

```terraform
provider gcore {
permanent_api_token = "251$d3361.............1b35f26d8"
}

resource "gcore_cdn_origingroup" "origin_group_1" {
name = "origin_group_1"
use_next = true
origin {
source = "example.com"
enabled = 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"]

options {
edge_cache_settings {
default = "8d"
}
browser_cache_settings {
value = "1d"
}
redirect_http_to_https {
value = true
}
request_limiter {
rate_unit = "r/s"
rate = 5
burst = 1
}
gzip_on {
value = true
}
cors {
value = [
"*"
]
}
rewrite {
body = "/(.*) /$1"
}

tls_versions {
enabled = true
value = [
"TLSv1.2",
]
}

force_return {
code = 200
body = "OK"
}
}
}

data "gcore_cdn_shielding_location" "sl" {
city = "Luxembourg"
}

resource "gcore_cdn_originshielding" "origin_shielding_1" {
resource_id = gcore_cdn_resource.cdn_example_com.id
shielding_pop = data.gcore_cdn_shielding_location.sl.id
}
```

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

### Required

- `resource_id` (Number) ID of CDN resource for which shielding will be applied
- `shielding_pop` (Number) ID of the shielding point of present
andrei-lukyanchyk marked this conversation as resolved.
Show resolved Hide resolved

### Read-Only

- `id` (String) The ID of this resource.
12 changes: 12 additions & 0 deletions examples/data-sources/gcore_cdn_shielding_location/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
provider gcore {
permanent_api_token = "251$d3361.............1b35f26d8"
}

data "gcore_cdn_shielding_location" "sl" {
city = "Luxembourg"
}

resource "gcore_cdn_originshielding" "origin_shielding_1" {
resource_id = 1
shielding_pop = data.gcore_cdn_shielding_location.sl.id
}
68 changes: 68 additions & 0 deletions examples/resources/gcore_cdn_originshielding/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
provider gcore {
permanent_api_token = "251$d3361.............1b35f26d8"
}

resource "gcore_cdn_origingroup" "origin_group_1" {
name = "origin_group_1"
use_next = true
origin {
source = "example.com"
enabled = 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"]

options {
edge_cache_settings {
default = "8d"
}
browser_cache_settings {
value = "1d"
}
redirect_http_to_https {
value = true
}
request_limiter {
rate_unit = "r/s"
rate = 5
burst = 1
}
gzip_on {
value = true
}
cors {
value = [
"*"
]
}
rewrite {
body = "/(.*) /$1"
}

tls_versions {
enabled = true
value = [
"TLSv1.2",
]
}

force_return {
code = 200
body = "OK"
}
}
}

data "gcore_cdn_shielding_location" "sl" {
city = "Luxembourg"
anton-sharonov marked this conversation as resolved.
Show resolved Hide resolved
}

resource "gcore_cdn_originshielding" "origin_shielding_1" {
resource_id = gcore_cdn_resource.cdn_example_com.id
shielding_pop = data.gcore_cdn_shielding_location.sl.id
}
59 changes: 59 additions & 0 deletions gcore/data_source_gcore_cdn_origin_shielding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package gcore

import (
"context"
"fmt"
"log"
"strconv"

"github.com/G-Core/gcorelabscdn-go/originshielding"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataOriginShieldingLocation() *schema.Resource {
return &schema.Resource{
ReadContext: dataOriginShieldingLocationRead,
Description: "Represent shielding locations data",
Schema: map[string]*schema.Schema{
"city": {
Type: schema.TypeString,
Description: "Displayed shielding location point of present",
Required: true,
},
},
}
}

func getLocationByCity(arr []originshielding.OriginShieldingLocations, city string) (int, error) {
for _, el := range arr {
if el.City == city {
return el.ID, nil
}
}
return 0, fmt.Errorf("shielding location for city %s not found", city)
}

func dataOriginShieldingLocationRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
log.Println("[DEBUG] Start reading origin shielding locations")

city := d.Get("city").(string)
config := m.(*Config)
client := config.CDNClient

result, err := client.OriginShielding().GetLocations(ctx)
if err != nil {
return diag.FromErr(err)
}
log.Printf("[DEBUG] Shielding locations: %v", result)
locationID, err := getLocationByCity(result, city)
if err != nil {
return diag.FromErr(err)
}
d.SetId(strconv.Itoa(locationID))
d.Set("city", city)

log.Println("[DEBUG] Finish reading origin shielding locations")
return nil

}
74 changes: 38 additions & 36 deletions gcore/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,42 +123,43 @@ func Provider() *schema.Provider {
},
},
ResourcesMap: map[string]*schema.Resource{
"gcore_ai_cluster": resourceAICluster(),
"gcore_volume": resourceVolume(),
"gcore_network": resourceNetwork(),
"gcore_subnet": resourceSubnet(),
"gcore_router": resourceRouter(),
"gcore_instance": resourceInstance(),
"gcore_keypair": resourceKeypair(),
"gcore_reservedfixedip": resourceReservedFixedIP(),
"gcore_floatingip": resourceFloatingIP(),
"gcore_loadbalancer": resourceLoadBalancer(),
"gcore_loadbalancerv2": resourceLoadBalancerV2(),
"gcore_lblistener": resourceLbListener(),
"gcore_lbpool": resourceLBPool(),
"gcore_lbmember": resourceLBMember(),
"gcore_securitygroup": resourceSecurityGroup(),
"gcore_baremetal": resourceBmInstance(),
"gcore_snapshot": resourceSnapshot(),
"gcore_servergroup": resourceServerGroup(),
"gcore_k8sv2": resourceK8sV2(),
"gcore_secret": resourceSecret(),
"gcore_laas_topic": resourceLaaSTopic(),
"gcore_faas_namespace": resourceFaaSNamespace(),
"gcore_faas_function": resourceFaaSFunction(),
"gcore_faas_key": resourceFaaSKey(),
"gcore_storage_s3": resourceStorageS3(),
"gcore_storage_s3_bucket": resourceStorageS3Bucket(),
DNSZoneResource: resourceDNSZone(),
DNSZoneRecordResource: resourceDNSZoneRecord(),
"gcore_storage_sftp": resourceStorageSFTP(),
"gcore_storage_sftp_key": resourceStorageSFTPKey(),
"gcore_cdn_resource": resourceCDNResource(),
"gcore_cdn_origingroup": resourceCDNOriginGroup(),
"gcore_cdn_rule": resourceCDNRule(),
"gcore_cdn_sslcert": resourceCDNCert(),
lifecyclePolicyResource: resourceLifecyclePolicy(),
"gcore_ddos_protection": resourceDDoSProtection(),
"gcore_ai_cluster": resourceAICluster(),
"gcore_volume": resourceVolume(),
"gcore_network": resourceNetwork(),
"gcore_subnet": resourceSubnet(),
"gcore_router": resourceRouter(),
"gcore_instance": resourceInstance(),
"gcore_keypair": resourceKeypair(),
"gcore_reservedfixedip": resourceReservedFixedIP(),
"gcore_floatingip": resourceFloatingIP(),
"gcore_loadbalancer": resourceLoadBalancer(),
"gcore_loadbalancerv2": resourceLoadBalancerV2(),
"gcore_lblistener": resourceLbListener(),
"gcore_lbpool": resourceLBPool(),
"gcore_lbmember": resourceLBMember(),
"gcore_securitygroup": resourceSecurityGroup(),
"gcore_baremetal": resourceBmInstance(),
"gcore_snapshot": resourceSnapshot(),
"gcore_servergroup": resourceServerGroup(),
"gcore_k8sv2": resourceK8sV2(),
"gcore_secret": resourceSecret(),
"gcore_laas_topic": resourceLaaSTopic(),
"gcore_faas_namespace": resourceFaaSNamespace(),
"gcore_faas_function": resourceFaaSFunction(),
"gcore_faas_key": resourceFaaSKey(),
"gcore_storage_s3": resourceStorageS3(),
"gcore_storage_s3_bucket": resourceStorageS3Bucket(),
DNSZoneResource: resourceDNSZone(),
DNSZoneRecordResource: resourceDNSZoneRecord(),
"gcore_storage_sftp": resourceStorageSFTP(),
"gcore_storage_sftp_key": resourceStorageSFTPKey(),
"gcore_cdn_resource": resourceCDNResource(),
"gcore_cdn_origingroup": resourceCDNOriginGroup(),
"gcore_cdn_originshielding": resourceCDNOriginShielding(),
"gcore_cdn_rule": resourceCDNRule(),
"gcore_cdn_sslcert": resourceCDNCert(),
lifecyclePolicyResource: resourceLifecyclePolicy(),
"gcore_ddos_protection": resourceDDoSProtection(),
},
DataSourcesMap: map[string]*schema.Resource{
"gcore_ai_cluster": dataSourceAICluster(),
Expand Down Expand Up @@ -191,6 +192,7 @@ func Provider() *schema.Provider {
"gcore_faas_key": dataSourceFaaSKey(),
"gcore_faas_function": dataSourceFaaSFunction(),
"gcore_ddos_profile_template": dataSourceDDoSProfileTemplate(),
"gcore_cdn_shielding_location": dataOriginShieldingLocation(),
},
ConfigureContextFunc: providerConfigure,
}
Expand Down
Loading
Loading