Skip to content
3 changes: 3 additions & 0 deletions docs/data-sources/cdn_distribution.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ Optional:
Read-Only:

- `backend` (Attributes) The configured backend for the distribution (see [below for nested schema](#nestedatt--config--backend))
- `blocked_ips` (List of String) Restricts access to your content by specifying a list of blocked IPv4 addresses. This feature enhances security and privacy by preventing these addresses from accessing your distribution. Note: once a value is set, removing the attribute from your configuration will retain the last known value in state; to clear it explicitly, set it to an empty list.
- `default_cache_duration` (String) Sets the default cache duration for the distribution. The default cache duration is applied when a 'Cache-Control' header is not presented in the origin's response. We use ISO8601 duration format for cache duration (e.g. P1DT2H30M). Note: once a value is set, removing the attribute from your configuration will retain the last known value in state.
- `forward_host_header` (Boolean) Enable this allows the 'Host' header to be passed through to the origin.
- `monthly_limit_bytes` (Number) Sets the monthly limit of bandwidth in bytes that the pullzone is allowed to use. Note: once a value is set, removing the attribute from your configuration will retain the last known value in state.
- `optimizer` (Attributes) Configuration for the Image Optimizer. This is a paid feature that automatically optimizes images to reduce their file size for faster delivery, leading to improved website performance and a better user experience. (see [below for nested schema](#nestedatt--config--optimizer))
- `redirects` (Attributes) A wrapper for a list of redirect rules that allows for redirect settings on a distribution (see [below for nested schema](#nestedatt--config--redirects))
- `regions` (List of String) The configured regions where content will be hosted
Expand Down
11 changes: 11 additions & 0 deletions docs/resources/cdn_distribution.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ resource "stackit_cdn_distribution" "example_distribution" {
}
regions = ["EU", "US", "ASIA", "AF", "SA"]
blocked_countries = ["DE", "AT", "CH"]
blocked_ips = ["1.1.1.1"]

default_cache_duration = "P1DT2H30M"
monthly_limit_bytes = 1048576

optimizer = {
enabled = true
Expand All @@ -52,6 +56,10 @@ resource "stackit_cdn_distribution" "example_bucket_distribution" {
}
regions = ["EU", "US"]
blocked_countries = ["CN", "RU"]
blocked_ips = ["1.1.1.1"]

default_cache_duration = "P1DT2H30M"
monthly_limit_bytes = 1048576

optimizer = {
enabled = false
Expand Down Expand Up @@ -147,7 +155,10 @@ Required:
Optional:

- `blocked_countries` (List of String) The configured countries where distribution of content is blocked
- `blocked_ips` (List of String) Restricts access to your content by specifying a list of blocked IPv4 addresses. This feature enhances security and privacy by preventing these addresses from accessing your distribution. Note: once a value is set, removing the attribute from your configuration will retain the last known value in state; to clear it explicitly, set it to an empty list.
- `default_cache_duration` (String) Sets the default cache duration for the distribution. The default cache duration is applied when a 'Cache-Control' header is not presented in the origin's response. We use ISO8601 duration format for cache duration (e.g. P1DT2H30M). Note: once a value is set, removing the attribute from your configuration will retain the last known value in state.
- `forward_host_header` (Boolean) Enable this allows the 'Host' header to be passed through to the origin.
- `monthly_limit_bytes` (Number) Sets the monthly limit of bandwidth in bytes that the pullzone is allowed to use. Note: once a value is set, removing the attribute from your configuration will retain the last known value in state.
- `optimizer` (Attributes) Configuration for the Image Optimizer. This is a paid feature that automatically optimizes images to reduce their file size for faster delivery, leading to improved website performance and a better user experience. (see [below for nested schema](#nestedatt--config--optimizer))
- `redirects` (Attributes) A wrapper for a list of redirect rules that allows for redirect settings on a distribution (see [below for nested schema](#nestedatt--config--redirects))
- `strip_response_cookies` (Boolean) Enable this to prevent origin-level cookies from being forwarded to the end user.
Expand Down
8 changes: 8 additions & 0 deletions examples/resources/stackit_cdn_distribution/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ resource "stackit_cdn_distribution" "example_distribution" {
}
regions = ["EU", "US", "ASIA", "AF", "SA"]
blocked_countries = ["DE", "AT", "CH"]
blocked_ips = ["1.1.1.1"]

default_cache_duration = "P1DT2H30M"
monthly_limit_bytes = 1048576

optimizer = {
enabled = true
Expand All @@ -34,6 +38,10 @@ resource "stackit_cdn_distribution" "example_bucket_distribution" {
}
regions = ["EU", "US"]
blocked_countries = ["CN", "RU"]
blocked_ips = ["1.1.1.1"]

default_cache_duration = "P1DT2H30M"
monthly_limit_bytes = 1048576

optimizer = {
enabled = false
Expand Down
51 changes: 45 additions & 6 deletions stackit/internal/services/cdn/cdn_acc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ var testConfigVarsHttp = config.Variables{
"optimizer": config.BoolVariable(true),
"backend_http_type": config.StringVariable("http"),
"blocked_countries": config.ListVariable(config.StringVariable("CU")),
"blocked_ips": config.ListVariable(config.StringVariable("1.1.1.1")),
"backend_origin_url": config.StringVariable("https://test-backend-1.cdn-dev.runs.onstackit.cloud"),
"geofencing_list": config.ListVariable(config.StringVariable("DE")),
"origin_request_headers_name": config.StringVariable("X-Custom-Header"),
Expand All @@ -129,17 +130,32 @@ var testConfigVarsHttp = config.Variables{
"redirect_rule_enabled": config.BoolVariable(true),
"redirect_rule_match_condition": config.StringVariable("ANY"),
"redirect_matcher_condition": config.StringVariable("ANY"),
"waf": wafConfigVariable("ENABLED", "FREE", "L2", "GET", "application/json", "HTTP/1.0", wafRule1, wafRule2, wafRule3),
"tls_enable_tls_10": config.BoolVariable(true),
"tls_enable_tls_11": config.BoolVariable(true),
"strip_response_cookies": config.BoolVariable(false),
"forward_host_header": config.BoolVariable(true),
"monthly_limit_bytes": config.IntegerVariable(104857600),
"default_cache_duration": config.StringVariable("PT2H"),
"waf": wafConfigVariable(
"ENABLED",
"FREE",
"L2",
"GET",
"application/json",
"HTTP/1.0",
wafRule1,
wafRule2,
wafRule3,
),
}

func configVarsHttpUpdated() config.Variables {
updatedConfig := maps.Clone(testConfigVarsHttp)
updatedConfig["regions"] = config.ListVariable(config.StringVariable("EU"), config.StringVariable("US"), config.StringVariable("ASIA"))
updatedConfig["redirect_target_url"] = config.StringVariable("https://example.com/updated")
updatedConfig["monthly_limit_bytes"] = config.IntegerVariable(209715200)
updatedConfig["default_cache_duration"] = config.StringVariable("PT4H")
updatedConfig["blocked_ips"] = config.ListVariable(config.StringVariable("1.1.1.1"), config.StringVariable("2.2.2.2"))

// Update WAF configuration to test mutation
updatedConfig["waf"] = wafConfigVariable("LOG_ONLY", "PREMIUM", "L3", "POST", "text/plain", "HTTP/1.1", wafRule3, wafRule2, wafRule1)
Expand Down Expand Up @@ -220,8 +236,13 @@ func TestAccCDNDistributionHttp(t *testing.T) {
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "config.redirects.rules.0.matchers.#", "1"),
testutil.CheckListAttr("stackit_cdn_distribution.distribution", "config.redirects.rules.0.matchers.0.values", testConfigVarsHttp["redirect_matcher_values"]),
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "config.redirects.rules.0.matchers.0.value_match_condition", testutil.ConvertConfigVariable(testConfigVarsHttp["redirect_matcher_condition"])),
testutil.CheckListAttr("stackit_cdn_distribution.distribution", "config.regions", testConfigVarsHttp["regions"]),
testutil.CheckListAttr("stackit_cdn_distribution.distribution", "config.blocked_countries", testConfigVarsHttp["blocked_countries"]),
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "config.regions.#", "2"),
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "config.regions.0", "EU"),
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "config.regions.1", "US"),
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "config.blocked_countries.#", "1"),
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "config.blocked_countries.0", "CU"),
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "config.blocked_ips.#", "1"),
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "config.blocked_ips.0", "1.1.1.1"),
resource.TestCheckResourceAttr(
"stackit_cdn_distribution.distribution",
fmt.Sprintf("config.backend.origin_request_headers.%s", testutil.ConvertConfigVariable(testConfigVarsHttp["origin_request_headers_name"])),
Expand All @@ -239,6 +260,8 @@ func TestAccCDNDistributionHttp(t *testing.T) {
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "config.tls.enable_tls_11", testutil.ConvertConfigVariable(testConfigVarsHttp["tls_enable_tls_11"])),
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "config.forward_host_header", testutil.ConvertConfigVariable(testConfigVarsHttp["forward_host_header"])),
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "config.strip_response_cookies", testutil.ConvertConfigVariable(testConfigVarsHttp["strip_response_cookies"])),
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "config.monthly_limit_bytes", testutil.ConvertConfigVariable(testConfigVarsHttp["monthly_limit_bytes"])),
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "config.default_cache_duration", testutil.ConvertConfigVariable(testConfigVarsHttp["default_cache_duration"])),

// WAF Checks
testutil.CheckObjectAttr("stackit_cdn_distribution.distribution", "config.waf", testConfigVarsHttp["waf"]),
Expand Down Expand Up @@ -340,14 +363,21 @@ func TestAccCDNDistributionHttp(t *testing.T) {
fmt.Sprintf("config.backend.geofencing.%s", testutil.ConvertConfigVariable(testConfigVarsHttp["backend_origin_url"])),
testConfigVarsHttp["geofencing_list"],
),
testutil.CheckListAttr("data.stackit_cdn_distribution.distribution", "config.blocked_countries", testConfigVarsHttp["blocked_countries"]),
resource.TestCheckResourceAttr("data.stackit_cdn_distribution.distribution", "config.regions.0", "EU"),
resource.TestCheckResourceAttr("data.stackit_cdn_distribution.distribution", "config.regions.1", "US"),
resource.TestCheckResourceAttr("data.stackit_cdn_distribution.distribution", "config.blocked_countries.#", "1"),
resource.TestCheckResourceAttr("data.stackit_cdn_distribution.distribution", "config.blocked_countries.0", "CU"),
resource.TestCheckResourceAttr("data.stackit_cdn_distribution.distribution", "config.blocked_ips.#", "1"),
resource.TestCheckResourceAttr("data.stackit_cdn_distribution.distribution", "config.blocked_ips.0", "1.1.1.1"),
resource.TestCheckResourceAttr("data.stackit_cdn_distribution.distribution", "config.optimizer.enabled", testutil.ConvertConfigVariable(testConfigVarsHttp["optimizer"])),

// TLS Checks inside Data Source
resource.TestCheckResourceAttr("data.stackit_cdn_distribution.distribution", "config.tls.enable_tls_10", testutil.ConvertConfigVariable(testConfigVarsHttp["tls_enable_tls_10"])),
resource.TestCheckResourceAttr("data.stackit_cdn_distribution.distribution", "config.tls.enable_tls_11", testutil.ConvertConfigVariable(testConfigVarsHttp["tls_enable_tls_11"])),
resource.TestCheckResourceAttr("data.stackit_cdn_distribution.distribution", "config.forward_host_header", testutil.ConvertConfigVariable(testConfigVarsHttp["forward_host_header"])),
resource.TestCheckResourceAttr("data.stackit_cdn_distribution.distribution", "config.strip_response_cookies", testutil.ConvertConfigVariable(testConfigVarsHttp["strip_response_cookies"])),
resource.TestCheckResourceAttr("data.stackit_cdn_distribution.distribution", "config.monthly_limit_bytes", testutil.ConvertConfigVariable(testConfigVarsHttp["monthly_limit_bytes"])),
resource.TestCheckResourceAttr("data.stackit_cdn_distribution.distribution", "config.default_cache_duration", testutil.ConvertConfigVariable(testConfigVarsHttp["default_cache_duration"])),

// WAF Checks inside Data Source
testutil.CheckObjectAttr("data.stackit_cdn_distribution.distribution", "config.waf", testConfigVarsHttp["waf"]),
Expand Down Expand Up @@ -384,15 +414,24 @@ func TestAccCDNDistributionHttp(t *testing.T) {
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "domains.1.status", "ACTIVE"),
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "domains.0.type", "managed"),
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "domains.1.type", "custom"),
testutil.CheckListAttr("stackit_cdn_distribution.distribution", "config.regions", configVarsHttpUpdated()["regions"]),
testutil.CheckListAttr("stackit_cdn_distribution.distribution", "config.blocked_countries", configVarsHttpUpdated()["blocked_countries"]),
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "config.regions.#", "3"),
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "config.regions.0", "EU"),
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "config.regions.1", "US"),
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "config.regions.2", "ASIA"),
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "config.blocked_countries.#", "1"),
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "config.blocked_countries.0", "CU"),
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "config.blocked_ips.#", "2"),
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "config.blocked_ips.0", "1.1.1.1"),
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "config.blocked_ips.1", "2.2.2.2"),
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "config.optimizer.enabled", testutil.ConvertConfigVariable(testConfigVarsHttp["optimizer"])),

// TLS Configuration
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "config.tls.enable_tls_10", testutil.ConvertConfigVariable(configVarsHttpUpdated()["tls_enable_tls_10"])),
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "config.tls.enable_tls_11", testutil.ConvertConfigVariable(configVarsHttpUpdated()["tls_enable_tls_11"])),
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "config.forward_host_header", testutil.ConvertConfigVariable(configVarsHttpUpdated()["forward_host_header"])),
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "config.strip_response_cookies", testutil.ConvertConfigVariable(configVarsHttpUpdated()["strip_response_cookies"])),
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "config.monthly_limit_bytes", testutil.ConvertConfigVariable(configVarsHttpUpdated()["monthly_limit_bytes"])),
resource.TestCheckResourceAttr("stackit_cdn_distribution.distribution", "config.default_cache_duration", testutil.ConvertConfigVariable(configVarsHttpUpdated()["default_cache_duration"])),

// Checking WAF Mutated Configurations
testutil.CheckObjectAttr("stackit_cdn_distribution.distribution", "config.waf", configVarsHttpUpdated()["waf"]),
Expand Down
52 changes: 49 additions & 3 deletions stackit/internal/services/cdn/distribution/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ var dataSourceBackendTypes = map[string]attr.Type{
}

var dataSourceConfigTypes = map[string]attr.Type{
"backend": types.ObjectType{AttrTypes: dataSourceBackendTypes},
"regions": types.ListType{ElemType: types.StringType},
"blocked_countries": types.ListType{ElemType: types.StringType},
"backend": types.ObjectType{AttrTypes: dataSourceBackendTypes},
"regions": types.ListType{ElemType: types.StringType},
"blocked_countries": types.ListType{ElemType: types.StringType},
"blocked_ips": types.ListType{ElemType: types.StringType},
"default_cache_duration": types.StringType,
"monthly_limit_bytes": types.Int64Type,
"optimizer": types.ObjectType{
AttrTypes: optimizerTypes, // Shared from resource.go
},
Expand Down Expand Up @@ -203,6 +206,19 @@ func (r *distributionDataSource) Schema(_ context.Context, _ datasource.SchemaRe
Description: schemaDescriptions["config_blocked_countries"],
ElementType: types.StringType,
},
"blocked_ips": schema.ListAttribute{
Computed: true,
Description: schemaDescriptions["config_blocked_ips"],
ElementType: types.StringType,
},
"default_cache_duration": schema.StringAttribute{
Computed: true,
Description: schemaDescriptions["config_default_cache_duration"],
},
"monthly_limit_bytes": schema.Int64Attribute{
Computed: true,
Description: schemaDescriptions["config_monthly_limit_bytes"],
},
"optimizer": schema.SingleNestedAttribute{
Description: schemaDescriptions["config_optimizer"],
Computed: true,
Expand Down Expand Up @@ -678,11 +694,41 @@ func mapDataSourceFields(ctx context.Context, distribution *cdnSdk.Distribution,
return core.DiagsToError(diagWaf)
}

// blockedIps
var blockedIps []attr.Value
if distribution.Config.BlockedIps != nil {
for _, ip := range distribution.Config.BlockedIps {
blockedIps = append(blockedIps, types.StringValue(ip))
}
}

modelBlockedIps, diags := types.ListValue(types.StringType, blockedIps)
if diags.HasError() {
return core.DiagsToError(diags)
}

var defaultCacheDuration types.String
if distribution.Config.DefaultCacheDuration.IsSet() {
defaultCacheDuration = types.StringPointerValue(distribution.Config.DefaultCacheDuration.Get())
} else {
defaultCacheDuration = types.StringNull()
}

var monthlyLimitBytes types.Int64
if distribution.Config.MonthlyLimitBytes.IsSet() {
monthlyLimitBytes = types.Int64PointerValue(distribution.Config.MonthlyLimitBytes.Get())
} else {
monthlyLimitBytes = types.Int64Null()
}
Comment thread
cgoetz-inovex marked this conversation as resolved.

// Use dataSourceConfigTypes
cfg, diags := types.ObjectValue(dataSourceConfigTypes, map[string]attr.Value{
"backend": backend,
"regions": modelRegions,
"blocked_countries": modelBlockedCountries,
"blocked_ips": modelBlockedIps,
"default_cache_duration": defaultCacheDuration,
"monthly_limit_bytes": monthlyLimitBytes,
"optimizer": optimizerVal,
"redirects": redirectsVal,
"waf": wafVal,
Expand Down
Loading
Loading