Skip to content

Commit 16446d7

Browse files
authored
fix: region handling in routingtable resources didn't detect default_region changes (#1006)
1 parent e5c2c7e commit 16446d7

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

stackit/internal/services/iaasalpha/routingtable/route/resource.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ var (
3131
_ resource.Resource = &routeResource{}
3232
_ resource.ResourceWithConfigure = &routeResource{}
3333
_ resource.ResourceWithImportState = &routeResource{}
34+
_ resource.ResourceWithModifyPlan = &routeResource{}
3435
)
3536

3637
// NewRoutingTableRouteResource is a helper function to simplify the provider implementation.
@@ -70,6 +71,36 @@ func (r *routeResource) Configure(ctx context.Context, req resource.ConfigureReq
7071
tflog.Info(ctx, "IaaS alpha client configured")
7172
}
7273

74+
// ModifyPlan implements resource.ResourceWithModifyPlan.
75+
// Use the modifier to set the effective region in the current plan.
76+
func (r *routeResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { // nolint:gocritic // function signature required by Terraform
77+
// skip initial empty configuration to avoid follow-up errors
78+
if req.Config.Raw.IsNull() {
79+
return
80+
}
81+
82+
var configModel shared.RouteModel
83+
resp.Diagnostics.Append(req.Config.Get(ctx, &configModel)...)
84+
if resp.Diagnostics.HasError() {
85+
return
86+
}
87+
var planModel shared.RouteModel
88+
resp.Diagnostics.Append(req.Plan.Get(ctx, &planModel)...)
89+
if resp.Diagnostics.HasError() {
90+
return
91+
}
92+
93+
utils.AdaptRegion(ctx, configModel.Region, &planModel.Region, r.providerData.GetRegion(), resp)
94+
if resp.Diagnostics.HasError() {
95+
return
96+
}
97+
98+
resp.Diagnostics.Append(resp.Plan.Set(ctx, planModel)...)
99+
if resp.Diagnostics.HasError() {
100+
return
101+
}
102+
}
103+
73104
// Schema defines the schema for the resource.
74105
func (r *routeResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
75106
description := "Routing table route resource schema. Must have a `region` specified in the provider configuration."

stackit/internal/services/iaasalpha/routingtable/table/resource.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"time"
99

1010
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
11-
1211
"github.com/hashicorp/terraform-plugin-framework/resource/schema/boolplanmodifier"
1312

1413
iaasUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/utils"
@@ -36,6 +35,7 @@ var (
3635
_ resource.Resource = &routingTableResource{}
3736
_ resource.ResourceWithConfigure = &routingTableResource{}
3837
_ resource.ResourceWithImportState = &routingTableResource{}
38+
_ resource.ResourceWithModifyPlan = &routingTableResource{}
3939
)
4040

4141
type Model struct {
@@ -89,6 +89,36 @@ func (r *routingTableResource) Configure(ctx context.Context, req resource.Confi
8989
tflog.Info(ctx, "IaaS alpha client configured")
9090
}
9191

92+
// ModifyPlan implements resource.ResourceWithModifyPlan.
93+
// Use the modifier to set the effective region in the current plan.
94+
func (r *routingTableResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { // nolint:gocritic // function signature required by Terraform
95+
// skip initial empty configuration to avoid follow-up errors
96+
if req.Config.Raw.IsNull() {
97+
return
98+
}
99+
var configModel Model
100+
resp.Diagnostics.Append(req.Config.Get(ctx, &configModel)...)
101+
if resp.Diagnostics.HasError() {
102+
return
103+
}
104+
105+
var planModel Model
106+
resp.Diagnostics.Append(req.Plan.Get(ctx, &planModel)...)
107+
if resp.Diagnostics.HasError() {
108+
return
109+
}
110+
111+
utils.AdaptRegion(ctx, configModel.Region, &planModel.Region, r.providerData.GetRegion(), resp)
112+
if resp.Diagnostics.HasError() {
113+
return
114+
}
115+
116+
resp.Diagnostics.Append(resp.Plan.Set(ctx, planModel)...)
117+
if resp.Diagnostics.HasError() {
118+
return
119+
}
120+
}
121+
92122
func (r *routingTableResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
93123
description := "Routing table resource schema. Must have a `region` specified in the provider configuration."
94124
resp.Schema = schema.Schema{

0 commit comments

Comments
 (0)