diff --git a/stackit/internal/services/iaasalpha/routingtable/route/resource.go b/stackit/internal/services/iaasalpha/routingtable/route/resource.go index d10e569ff..c5ba2a7ed 100644 --- a/stackit/internal/services/iaasalpha/routingtable/route/resource.go +++ b/stackit/internal/services/iaasalpha/routingtable/route/resource.go @@ -31,6 +31,7 @@ var ( _ resource.Resource = &routeResource{} _ resource.ResourceWithConfigure = &routeResource{} _ resource.ResourceWithImportState = &routeResource{} + _ resource.ResourceWithModifyPlan = &routeResource{} ) // NewRoutingTableRouteResource is a helper function to simplify the provider implementation. @@ -70,6 +71,36 @@ func (r *routeResource) Configure(ctx context.Context, req resource.ConfigureReq tflog.Info(ctx, "IaaS alpha client configured") } +// ModifyPlan implements resource.ResourceWithModifyPlan. +// Use the modifier to set the effective region in the current plan. +func (r *routeResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { // nolint:gocritic // function signature required by Terraform + // skip initial empty configuration to avoid follow-up errors + if req.Config.Raw.IsNull() { + return + } + + var configModel shared.RouteModel + resp.Diagnostics.Append(req.Config.Get(ctx, &configModel)...) + if resp.Diagnostics.HasError() { + return + } + var planModel shared.RouteModel + resp.Diagnostics.Append(req.Plan.Get(ctx, &planModel)...) + if resp.Diagnostics.HasError() { + return + } + + utils.AdaptRegion(ctx, configModel.Region, &planModel.Region, r.providerData.GetRegion(), resp) + if resp.Diagnostics.HasError() { + return + } + + resp.Diagnostics.Append(resp.Plan.Set(ctx, planModel)...) + if resp.Diagnostics.HasError() { + return + } +} + // Schema defines the schema for the resource. func (r *routeResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { description := "Routing table route resource schema. Must have a `region` specified in the provider configuration." diff --git a/stackit/internal/services/iaasalpha/routingtable/table/resource.go b/stackit/internal/services/iaasalpha/routingtable/table/resource.go index b1a5761dc..43d6d9070 100644 --- a/stackit/internal/services/iaasalpha/routingtable/table/resource.go +++ b/stackit/internal/services/iaasalpha/routingtable/table/resource.go @@ -8,7 +8,6 @@ import ( "time" "github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault" - "github.com/hashicorp/terraform-plugin-framework/resource/schema/boolplanmodifier" iaasUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/iaas/utils" @@ -36,6 +35,7 @@ var ( _ resource.Resource = &routingTableResource{} _ resource.ResourceWithConfigure = &routingTableResource{} _ resource.ResourceWithImportState = &routingTableResource{} + _ resource.ResourceWithModifyPlan = &routingTableResource{} ) type Model struct { @@ -89,6 +89,36 @@ func (r *routingTableResource) Configure(ctx context.Context, req resource.Confi tflog.Info(ctx, "IaaS alpha client configured") } +// ModifyPlan implements resource.ResourceWithModifyPlan. +// Use the modifier to set the effective region in the current plan. +func (r *routingTableResource) ModifyPlan(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) { // nolint:gocritic // function signature required by Terraform + // skip initial empty configuration to avoid follow-up errors + if req.Config.Raw.IsNull() { + return + } + var configModel Model + resp.Diagnostics.Append(req.Config.Get(ctx, &configModel)...) + if resp.Diagnostics.HasError() { + return + } + + var planModel Model + resp.Diagnostics.Append(req.Plan.Get(ctx, &planModel)...) + if resp.Diagnostics.HasError() { + return + } + + utils.AdaptRegion(ctx, configModel.Region, &planModel.Region, r.providerData.GetRegion(), resp) + if resp.Diagnostics.HasError() { + return + } + + resp.Diagnostics.Append(resp.Plan.Set(ctx, planModel)...) + if resp.Diagnostics.HasError() { + return + } +} + func (r *routingTableResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { description := "Routing table resource schema. Must have a `region` specified in the provider configuration." resp.Schema = schema.Schema{