Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
31 changes: 31 additions & 0 deletions docs/data-sources/cdn_distribution.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Read-Only:

- `backend` (Attributes) The configured backend for the distribution (see [below for nested schema](#nestedatt--config--backend))
- `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

<a id="nestedatt--config--backend"></a>
Expand All @@ -74,6 +75,36 @@ Read-Only:
- `enabled` (Boolean)


<a id="nestedatt--config--redirects"></a>
### Nested Schema for `config.redirects`

Read-Only:

- `rules` (Attributes List) A list of redirect rules. The order of rules matters for evaluation (see [below for nested schema](#nestedatt--config--redirects--rules))

<a id="nestedatt--config--redirects--rules"></a>
### Nested Schema for `config.redirects.rules`

Read-Only:

- `description` (String) An optional description for the redirect rule
- `enabled` (Boolean) A toggle to enable or disable the redirect rule. Default to true
- `matchers` (Attributes List) A list of matchers that define when this rule should apply. At least one matcher is required (see [below for nested schema](#nestedatt--config--redirects--rules--matchers))
- `rule_match_condition` (String) Defines how multiple matchers within this rule are combined (ALL, ANY, NONE). Defaults to ANY.
- `status_code` (Number) The HTTP status code for the redirect. Must be one of 301, 302, 303, 307, or 308.
- `target_url` (String) The target URL to redirect to. Must be a valid URI

<a id="nestedatt--config--redirects--rules--matchers"></a>
### Nested Schema for `config.redirects.rules.matchers`

Read-Only:

- `value_match_condition` (String) Defines how multiple matchers within this rule are combined (ALL, ANY, NONE). Defaults to ANY.
- `values` (List of String) A list of glob patterns to match against the request path. At least one value is required. Examples: "/shop/*" or "*/img/*"





<a id="nestedatt--domains"></a>
### Nested Schema for `domains`
Expand Down
37 changes: 37 additions & 0 deletions docs/resources/cdn_distribution.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ Optional:

- `blocked_countries` (List of String) The configured countries where distribution of content is blocked
- `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))

<a id="nestedatt--config--backend"></a>
### Nested Schema for `config.backend`
Expand Down Expand Up @@ -131,6 +132,42 @@ Optional:
- `enabled` (Boolean)


<a id="nestedatt--config--redirects"></a>
### Nested Schema for `config.redirects`

Required:

- `rules` (Attributes List) A list of redirect rules. The order of rules matters for evaluation (see [below for nested schema](#nestedatt--config--redirects--rules))

<a id="nestedatt--config--redirects--rules"></a>
### Nested Schema for `config.redirects.rules`

Required:

- `matchers` (Attributes List) A list of matchers that define when this rule should apply. At least one matcher is required (see [below for nested schema](#nestedatt--config--redirects--rules--matchers))
- `status_code` (Number) The HTTP status code for the redirect. Must be one of 301, 302, 303, 307, or 308.
- `target_url` (String) The target URL to redirect to. Must be a valid URI

Optional:

- `description` (String) An optional description for the redirect rule
- `enabled` (Boolean) A toggle to enable or disable the redirect rule. Default to true
- `rule_match_condition` (String) Defines how multiple matchers within this rule are combined (ALL, ANY, NONE). Defaults to ANY.

<a id="nestedatt--config--redirects--rules--matchers"></a>
### Nested Schema for `config.redirects.rules.matchers`

Required:

- `values` (List of String) A list of glob patterns to match against the request path. At least one value is required. Examples: "/shop/*" or "*/img/*"

Optional:

- `value_match_condition` (String) Defines how multiple matchers within this rule are combined (ALL, ANY, NONE). Defaults to ANY.





<a id="nestedatt--domains"></a>
### Nested Schema for `domains`
Expand Down
18 changes: 18 additions & 0 deletions examples/resources/stackit_cdn_distribution/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@ resource "stackit_cdn_distribution" "example_bucket_distribution" {
optimizer = {
enabled = false
}

redirects = {
rules = [
{
description = "test redirect"
enabled = true
rule_match_condition = "ANY"
status_code = 302
target_url = "https://stackit.de/"
matchers = [
{
values = ["*/otherPath/"]
value_match_condition = "ANY"
}
]
}
]
}
}
}

Expand Down
148 changes: 148 additions & 0 deletions stackit/internal/services/cdn/distribution/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ var dataSourceConfigTypes = map[string]attr.Type{
"optimizer": types.ObjectType{
AttrTypes: optimizerTypes, // Shared from resource.go
},
"redirects": types.ObjectType{
AttrTypes: redirectsTypes, // Shared from resource.go
},
}

type distributionDataSource struct {
Expand Down Expand Up @@ -199,6 +202,57 @@ func (r *distributionDataSource) Schema(_ context.Context, _ datasource.SchemaRe
},
},
},
"redirects": schema.SingleNestedAttribute{
Computed: true,
Description: schemaDescriptions["config_redirects"],
Attributes: map[string]schema.Attribute{
"rules": schema.ListNestedAttribute{
Description: schemaDescriptions["config_redirects_rules"],
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"description": schema.StringAttribute{
Description: schemaDescriptions["config_redirects_rule_description"],
Computed: true,
},
"enabled": schema.BoolAttribute{
Computed: true,
Description: schemaDescriptions["config_redirects_rule_enabled"],
},
"target_url": schema.StringAttribute{
Computed: true,
Description: schemaDescriptions["config_redirects_rule_target_url"],
},
"status_code": schema.Int32Attribute{
Computed: true,
Description: schemaDescriptions["config_redirects_rule_status_code"],
},
"rule_match_condition": schema.StringAttribute{
Computed: true,
Description: schemaDescriptions["config_redirects_rule_match_condition"],
},
"matchers": schema.ListNestedAttribute{
Description: schemaDescriptions["config_redirects_rule_matchers"],
Computed: true,
NestedObject: schema.NestedAttributeObject{
Attributes: map[string]schema.Attribute{
"values": schema.ListAttribute{
Description: schemaDescriptions["config_redirects_rule_matcher_values"],
Computed: true,
ElementType: types.StringType,
},
"value_match_condition": schema.StringAttribute{
Description: schemaDescriptions["config_redirects_rule_match_condition"],
Computed: true,
},
},
},
},
},
},
},
},
},
},
},
},
Expand Down Expand Up @@ -300,6 +354,99 @@ func mapDataSourceFields(ctx context.Context, distribution *cdn.Distribution, mo
return core.DiagsToError(diags)
}

// redirects
redirectsVal := types.ObjectNull(redirectsTypes)
if distribution.Config != nil && distribution.Config.Redirects != nil && distribution.Config.Redirects.Rules != nil {
var tfRules []attr.Value
for _, r := range *distribution.Config.Redirects.Rules {
var tfMatchers []attr.Value
if r.Matchers != nil {
for _, m := range *r.Matchers {
var tfValues []attr.Value
if m.Values != nil {
for _, v := range *m.Values {
tfValues = append(tfValues, types.StringValue(v))
}
}
tfValuesList, diags := types.ListValue(types.StringType, tfValues)
if diags.HasError() {
return core.DiagsToError(diags)
}

tfValMatchCond := types.StringNull()
if m.ValueMatchCondition != nil {
tfValMatchCond = types.StringValue(string(*m.ValueMatchCondition))
}

tfMatcherObj, diags := types.ObjectValue(matcherTypes, map[string]attr.Value{
"values": tfValuesList,
"value_match_condition": tfValMatchCond,
})
if diags.HasError() {
return core.DiagsToError(diags)
}
tfMatchers = append(tfMatchers, tfMatcherObj)
}
}

tfMatchersList, diags := types.ListValue(types.ObjectType{AttrTypes: matcherTypes}, tfMatchers)
if diags.HasError() {
return core.DiagsToError(diags)
}

tfDesc := types.StringNull()
if r.Description != nil {
tfDesc = types.StringValue(*r.Description)
}

tfEnabled := types.BoolNull()
if r.Enabled != nil {
tfEnabled = types.BoolValue(*r.Enabled)
}

tfTargetUrl := types.StringNull()
if r.TargetUrl != nil {
tfTargetUrl = types.StringValue(*r.TargetUrl)
}

tfStatusCode := types.Int32Null()
if r.StatusCode != nil {
tfStatusCode = types.Int32Value(int32(*r.StatusCode))
}

tfRuleMatchCond := types.StringNull()
if r.RuleMatchCondition != nil {
tfRuleMatchCond = types.StringValue(string(*r.RuleMatchCondition))
}

tfRuleObj, diags := types.ObjectValue(redirectRuleTypes, map[string]attr.Value{
"description": tfDesc,
"enabled": tfEnabled,
"target_url": tfTargetUrl,
"status_code": tfStatusCode,
"rule_match_condition": tfRuleMatchCond,
"matchers": tfMatchersList,
})
if diags.HasError() {
return core.DiagsToError(diags)
}
tfRules = append(tfRules, tfRuleObj)
}

tfRulesList, diags := types.ListValue(types.ObjectType{AttrTypes: redirectRuleTypes}, tfRules)
if diags.HasError() {
return core.DiagsToError(diags)
}

var objDiags diag.Diagnostics
redirectsVal, objDiags = types.ObjectValue(redirectsTypes, map[string]attr.Value{
"rules": tfRulesList,
})
if objDiags.HasError() {
return core.DiagsToError(objDiags)
}
}

// Prepare Backend Values
var backendValues map[string]attr.Value
originRequestHeaders := types.MapNull(types.StringType)
Expand Down Expand Up @@ -383,6 +530,7 @@ func mapDataSourceFields(ctx context.Context, distribution *cdn.Distribution, mo
"regions": modelRegions,
"blocked_countries": modelBlockedCountries,
"optimizer": optimizerVal,
"redirects": redirectsVal,
})
if diags.HasError() {
return core.DiagsToError(diags)
Expand Down
Loading