Skip to content

Commit 7cca33f

Browse files
author
Matheus Politano
committed
feat: add redirect in toCreatePayload
1 parent 7c989aa commit 7cca33f

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

stackit/internal/services/cdn/distribution/resource.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,6 +1144,7 @@ func toCreatePayload(ctx context.Context, model *Model) (*cdn.CreateDistribution
11441144
Backend: backend,
11451145
BlockedCountries: cfg.BlockedCountries,
11461146
Optimizer: optimizer,
1147+
Redirects: cfg.Redirects,
11471148
}
11481149

11491150
return payload, nil

stackit/internal/services/cdn/distribution/resource_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,40 @@ func TestToCreatePayload(t *testing.T) {
4141
optimizer := types.ObjectValueMust(optimizerTypes, map[string]attr.Value{
4242
"enabled": types.BoolValue(true),
4343
})
44+
45+
redirectsAttrTypes := configTypes["redirects"].(basetypes.ObjectType).AttrTypes
46+
4447
config := types.ObjectValueMust(configTypes, map[string]attr.Value{
4548
"backend": backend,
4649
"regions": regionsFixture,
4750
"blocked_countries": blockedCountriesFixture,
4851
"optimizer": types.ObjectNull(optimizerTypes),
52+
"redirects": types.ObjectNull(redirectsAttrTypes),
53+
})
54+
55+
matcherValues := types.ListValueMust(types.StringType, []attr.Value{
56+
types.StringValue("/shop/*"),
57+
})
58+
matcherVal := types.ObjectValueMust(matcherTypes, map[string]attr.Value{
59+
"values": matcherValues,
60+
"value_match_condition": types.StringValue("ANY"),
4961
})
62+
matchersList := types.ListValueMust(types.ObjectType{AttrTypes: matcherTypes}, []attr.Value{matcherVal})
63+
64+
ruleVal := types.ObjectValueMust(redirectRuleTypes, map[string]attr.Value{
65+
"description": types.StringValue("Test redirect"),
66+
"enabled": types.BoolValue(true),
67+
"target_url": types.StringValue("https://example.com/redirect"),
68+
"status_code": types.Int32Value(301),
69+
"rule_match_condition": types.StringValue("ANY"),
70+
"matchers": matchersList,
71+
})
72+
rulesList := types.ListValueMust(types.ObjectType{AttrTypes: redirectRuleTypes}, []attr.Value{ruleVal})
73+
74+
redirectsConfigVal := types.ObjectValueMust(redirectsTypes, map[string]attr.Value{
75+
"rules": rulesList,
76+
})
77+
5078
modelFixture := func(mods ...func(*Model)) *Model {
5179
model := &Model{
5280
DistributionId: types.StringValue("test-distribution-id"),
@@ -86,6 +114,7 @@ func TestToCreatePayload(t *testing.T) {
86114
"regions": regionsFixture,
87115
"optimizer": optimizer,
88116
"blocked_countries": blockedCountriesFixture,
117+
"redirects": types.ObjectNull(redirectsAttrTypes),
89118
})
90119
}),
91120
Expected: &cdn.CreateDistributionPayload{
@@ -103,6 +132,47 @@ func TestToCreatePayload(t *testing.T) {
103132
},
104133
IsValid: true,
105134
},
135+
"happy_path_with_redirects": {
136+
Input: modelFixture(func(m *Model) {
137+
m.Config = types.ObjectValueMust(configTypes, map[string]attr.Value{
138+
"backend": backend,
139+
"regions": regionsFixture,
140+
"optimizer": types.ObjectNull(optimizerTypes),
141+
"blocked_countries": blockedCountriesFixture,
142+
"redirects": redirectsConfigVal,
143+
})
144+
}),
145+
Expected: &cdn.CreateDistributionPayload{
146+
Regions: &[]cdn.Region{"EU", "US"},
147+
BlockedCountries: &[]string{"XX", "YY", "ZZ"},
148+
Backend: &cdn.CreateDistributionPayloadBackend{
149+
HttpBackendCreate: &cdn.HttpBackendCreate{
150+
Geofencing: &map[string][]string{"https://de.mycoolapp.com": {"DE", "FR"}},
151+
OriginRequestHeaders: &map[string]string{"testHeader0": "testHeaderValue0", "testHeader1": "testHeaderValue1"},
152+
OriginUrl: cdn.PtrString("https://www.mycoolapp.com"),
153+
Type: cdn.PtrString("http"),
154+
},
155+
},
156+
Redirects: &cdn.RedirectConfig{
157+
Rules: &[]cdn.RedirectRule{
158+
{
159+
Description: cdn.PtrString("Test redirect"),
160+
Enabled: cdn.PtrBool(true),
161+
TargetUrl: cdn.PtrString("https://example.com/redirect"),
162+
StatusCode: cdn.RedirectRuleStatusCode(301).Ptr(),
163+
RuleMatchCondition: cdn.MatchCondition("ANY").Ptr(),
164+
Matchers: &[]cdn.Matcher{
165+
{
166+
Values: &[]string{"/shop/*"},
167+
ValueMatchCondition: cdn.MatchCondition("ANY").Ptr(),
168+
},
169+
},
170+
},
171+
},
172+
},
173+
},
174+
IsValid: true,
175+
},
106176
"happy_path_bucket": {
107177
Input: modelFixture(func(m *Model) {
108178
creds := types.ObjectValueMust(backendCredentialsTypes, map[string]attr.Value{
@@ -123,6 +193,7 @@ func TestToCreatePayload(t *testing.T) {
123193
"regions": regionsFixture, // reusing the existing one
124194
"blocked_countries": blockedCountriesFixture,
125195
"optimizer": types.ObjectNull(optimizerTypes),
196+
"redirects": types.ObjectNull(redirectsAttrTypes),
126197
})
127198
}),
128199
Expected: &cdn.CreateDistributionPayload{

0 commit comments

Comments
 (0)