Skip to content

Commit cf6f255

Browse files
author
Matheus Politano
committed
feat: add in the map field mapfield
1 parent e850a14 commit cf6f255

File tree

2 files changed

+158
-0
lines changed

2 files changed

+158
-0
lines changed

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

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,7 @@ func (r *distributionResource) Update(ctx context.Context, req resource.UpdateRe
786786
Backend: configPatchBackend,
787787
Regions: &regions,
788788
BlockedCountries: blockedCountries,
789+
Redirects: redirectsConfig,
789790
}
790791

791792
if !utils.IsUndefined(configModel.Optimizer) {
@@ -948,6 +949,99 @@ func mapFields(ctx context.Context, distribution *cdn.Distribution, model *Model
948949
}
949950
}
950951

952+
// redirects
953+
redirectsVal := types.ObjectNull(redirectsTypes)
954+
if distribution.Config != nil && distribution.Config.Redirects != nil && distribution.Config.Redirects.Rules != nil {
955+
var tfRules []attr.Value
956+
for _, r := range *distribution.Config.Redirects.Rules {
957+
var tfMatchers []attr.Value
958+
if r.Matchers != nil {
959+
for _, m := range *r.Matchers {
960+
var tfValues []attr.Value
961+
if m.Values != nil {
962+
for _, v := range *m.Values {
963+
tfValues = append(tfValues, types.StringValue(v))
964+
}
965+
}
966+
tfValuesList, diags := types.ListValue(types.StringType, tfValues)
967+
if diags.HasError() {
968+
return core.DiagsToError(diags)
969+
}
970+
971+
tfValMatchCond := types.StringNull()
972+
if m.ValueMatchCondition != nil {
973+
tfValMatchCond = types.StringValue(string(*m.ValueMatchCondition))
974+
}
975+
976+
tfMatcherObj, diags := types.ObjectValue(matcherTypes, map[string]attr.Value{
977+
"values": tfValuesList,
978+
"value_match_condition": tfValMatchCond,
979+
})
980+
if diags.HasError() {
981+
return core.DiagsToError(diags)
982+
}
983+
tfMatchers = append(tfMatchers, tfMatcherObj)
984+
}
985+
}
986+
987+
tfMatchersList, diags := types.ListValue(types.ObjectType{AttrTypes: matcherTypes}, tfMatchers)
988+
if diags.HasError() {
989+
return core.DiagsToError(diags)
990+
}
991+
992+
tfDesc := types.StringNull()
993+
if r.Description != nil {
994+
tfDesc = types.StringValue(*r.Description)
995+
}
996+
997+
tfEnabled := types.BoolNull()
998+
if r.Enabled != nil {
999+
tfEnabled = types.BoolValue(*r.Enabled)
1000+
}
1001+
1002+
tfTargetUrl := types.StringNull()
1003+
if r.TargetUrl != nil {
1004+
tfTargetUrl = types.StringValue(*r.TargetUrl)
1005+
}
1006+
1007+
tfStatusCode := types.Int32Null()
1008+
if r.StatusCode != nil {
1009+
tfStatusCode = types.Int32Value(int32(*r.StatusCode))
1010+
}
1011+
1012+
tfRuleMatchCond := types.StringNull()
1013+
if r.RuleMatchCondition != nil {
1014+
tfRuleMatchCond = types.StringValue(string(*r.RuleMatchCondition))
1015+
}
1016+
1017+
tfRuleObj, diags := types.ObjectValue(redirectRuleTypes, map[string]attr.Value{
1018+
"description": tfDesc,
1019+
"enabled": tfEnabled,
1020+
"target_url": tfTargetUrl,
1021+
"status_code": tfStatusCode,
1022+
"rule_match_condition": tfRuleMatchCond,
1023+
"matchers": tfMatchersList,
1024+
})
1025+
if diags.HasError() {
1026+
return core.DiagsToError(diags)
1027+
}
1028+
tfRules = append(tfRules, tfRuleObj)
1029+
}
1030+
1031+
tfRulesList, diags := types.ListValue(types.ObjectType{AttrTypes: redirectRuleTypes}, tfRules)
1032+
if diags.HasError() {
1033+
return core.DiagsToError(diags)
1034+
}
1035+
1036+
var objDiags diag.Diagnostics
1037+
redirectsVal, objDiags = types.ObjectValue(redirectsTypes, map[string]attr.Value{
1038+
"rules": tfRulesList,
1039+
})
1040+
if objDiags.HasError() {
1041+
return core.DiagsToError(objDiags)
1042+
}
1043+
}
1044+
9511045
// blockedCountries
9521046
var blockedCountries []attr.Value
9531047
if distribution.Config != nil && distribution.Config.BlockedCountries != nil {
@@ -1085,6 +1179,7 @@ func mapFields(ctx context.Context, distribution *cdn.Distribution, model *Model
10851179
"regions": modelRegions,
10861180
"blocked_countries": modelBlockedCountries,
10871181
"optimizer": optimizerVal,
1182+
"redirects": redirectsVal,
10881183
})
10891184
if diags.HasError() {
10901185
return core.DiagsToError(diags)

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

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,11 +521,56 @@ func TestMapFields(t *testing.T) {
521521
optimizer := types.ObjectValueMust(optimizerTypes, map[string]attr.Value{
522522
"enabled": types.BoolValue(true),
523523
})
524+
525+
redirectsAttrTypes := configTypes["redirects"].(basetypes.ObjectType).AttrTypes
526+
524527
config := types.ObjectValueMust(configTypes, map[string]attr.Value{
525528
"backend": backend,
526529
"regions": regionsFixture,
527530
"blocked_countries": blockedCountriesFixture,
528531
"optimizer": types.ObjectNull(optimizerTypes),
532+
"redirects": types.ObjectNull(redirectsAttrTypes),
533+
})
534+
535+
redirectsInput := &cdn.RedirectConfig{
536+
Rules: &[]cdn.RedirectRule{
537+
{
538+
Description: cdn.PtrString("Test redirect"),
539+
Enabled: cdn.PtrBool(true),
540+
TargetUrl: cdn.PtrString("https://example.com/redirect"),
541+
StatusCode: cdn.RedirectRuleStatusCode(301).Ptr(),
542+
RuleMatchCondition: cdn.MatchCondition("ANY").Ptr(),
543+
Matchers: &[]cdn.Matcher{
544+
{
545+
Values: &[]string{"/shop/*"},
546+
ValueMatchCondition: cdn.MatchCondition("ANY").Ptr(),
547+
},
548+
},
549+
},
550+
},
551+
}
552+
553+
matcherValuesExpected := types.ListValueMust(types.StringType, []attr.Value{
554+
types.StringValue("/shop/*"),
555+
})
556+
matcherValExpected := types.ObjectValueMust(matcherTypes, map[string]attr.Value{
557+
"values": matcherValuesExpected,
558+
"value_match_condition": types.StringValue("ANY"),
559+
})
560+
matchersListExpected := types.ListValueMust(types.ObjectType{AttrTypes: matcherTypes}, []attr.Value{matcherValExpected})
561+
562+
ruleValExpected := types.ObjectValueMust(redirectRuleTypes, map[string]attr.Value{
563+
"description": types.StringValue("Test redirect"),
564+
"enabled": types.BoolValue(true),
565+
"target_url": types.StringValue("https://example.com/redirect"),
566+
"status_code": types.Int32Value(301),
567+
"rule_match_condition": types.StringValue("ANY"),
568+
"matchers": matchersListExpected,
569+
})
570+
rulesListExpected := types.ListValueMust(types.ObjectType{AttrTypes: redirectRuleTypes}, []attr.Value{ruleValExpected})
571+
572+
redirectsConfigExpected := types.ObjectValueMust(redirectsTypes, map[string]attr.Value{
573+
"rules": rulesListExpected,
529574
})
530575

531576
emtpyErrorsList := types.ListValueMust(types.StringType, []attr.Value{})
@@ -607,6 +652,7 @@ func TestMapFields(t *testing.T) {
607652
"regions": regionsFixture,
608653
"blocked_countries": blockedCountriesFixture,
609654
"optimizer": types.ObjectNull(optimizerTypes),
655+
"redirects": types.ObjectNull(redirectsAttrTypes),
610656
})
611657
tests := map[string]struct {
612658
Input *cdn.Distribution
@@ -626,6 +672,7 @@ func TestMapFields(t *testing.T) {
626672
"regions": regionsFixture,
627673
"optimizer": optimizer,
628674
"blocked_countries": blockedCountriesFixture,
675+
"redirects": types.ObjectNull(redirectsAttrTypes),
629676
})
630677
}),
631678
Input: distributionFixture(func(d *cdn.Distribution) {
@@ -651,13 +698,29 @@ func TestMapFields(t *testing.T) {
651698
"regions": regionsFixture,
652699
"optimizer": types.ObjectNull(optimizerTypes),
653700
"blocked_countries": blockedCountriesFixture,
701+
"redirects": types.ObjectNull(redirectsAttrTypes),
654702
})
655703
}),
656704
Input: distributionFixture(func(d *cdn.Distribution) {
657705
d.Config.Backend.HttpBackend.Geofencing = &geofencingInput
658706
}),
659707
IsValid: true,
660708
},
709+
"happy_path_with_redirects": {
710+
Expected: expectedModel(func(m *Model) {
711+
m.Config = types.ObjectValueMust(configTypes, map[string]attr.Value{
712+
"backend": backend,
713+
"regions": regionsFixture,
714+
"optimizer": types.ObjectNull(optimizerTypes),
715+
"blocked_countries": blockedCountriesFixture,
716+
"redirects": redirectsConfigExpected,
717+
})
718+
}),
719+
Input: distributionFixture(func(d *cdn.Distribution) {
720+
d.Config.Redirects = redirectsInput
721+
}),
722+
IsValid: true,
723+
},
661724
"happy_path_status_error": {
662725
Expected: expectedModel(func(m *Model) {
663726
m.Status = types.StringValue("ERROR")

0 commit comments

Comments
 (0)