Skip to content

Commit 4b00bf1

Browse files
authored
Fix update of PostgreSQL Flex instance flavor (#494)
* Fix update of PostgreSQL Flex instance flavor * Fix linter
1 parent c322717 commit 4b00bf1

File tree

2 files changed

+88
-3
lines changed

2 files changed

+88
-3
lines changed

stackit/internal/services/postgresflex/instance/resource.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func (r *instanceResource) Configure(ctx context.Context, req resource.Configure
131131
}
132132

133133
// Schema defines the schema for the resource.
134-
func (r *instanceResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
134+
func (r *instanceResource) Schema(_ context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
135135
descriptions := map[string]string{
136136
"main": "Postgres Flex instance resource schema. Must have a `region` specified in the provider configuration.",
137137
"id": "Terraform's internal resource ID. It is structured as \"`project_id`,`instance_id`\".",
@@ -199,13 +199,13 @@ func (r *instanceResource) Schema(_ context.Context, _ resource.SchemaRequest, r
199199
"id": schema.StringAttribute{
200200
Computed: true,
201201
PlanModifiers: []planmodifier.String{
202-
stringplanmodifier.UseStateForUnknown(),
202+
UseStateForUnknownIfFlavorUnchanged(req),
203203
},
204204
},
205205
"description": schema.StringAttribute{
206206
Computed: true,
207207
PlanModifiers: []planmodifier.String{
208-
stringplanmodifier.UseStateForUnknown(),
208+
UseStateForUnknownIfFlavorUnchanged(req),
209209
},
210210
},
211211
"cpu": schema.Int64Attribute{
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package postgresflex
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-framework/resource"
7+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
8+
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
9+
)
10+
11+
type useStateForUnknownIfFlavorUnchangedModifier struct {
12+
Req resource.SchemaRequest
13+
}
14+
15+
// UseStateForUnknownIfFlavorUnchanged returns a plan modifier similar to UseStateForUnknown
16+
// if the RAM and CPU values are not changed in the plan. Otherwise, the plan modifier does nothing.
17+
func UseStateForUnknownIfFlavorUnchanged(req resource.SchemaRequest) planmodifier.String {
18+
return useStateForUnknownIfFlavorUnchangedModifier{
19+
Req: req,
20+
}
21+
}
22+
23+
func (m useStateForUnknownIfFlavorUnchangedModifier) Description(context.Context) string {
24+
return "UseStateForUnknownIfFlavorUnchanged returns a plan modifier similar to UseStateForUnknown if the RAM and CPU values are not changed in the plan. Otherwise, the plan modifier does nothing."
25+
}
26+
27+
func (m useStateForUnknownIfFlavorUnchangedModifier) MarkdownDescription(ctx context.Context) string {
28+
return m.Description(ctx)
29+
}
30+
31+
func (m useStateForUnknownIfFlavorUnchangedModifier) PlanModifyString(ctx context.Context, req planmodifier.StringRequest, resp *planmodifier.StringResponse) { // nolint:gocritic // function signature required by Terraform
32+
// Do nothing if there is no state value.
33+
if req.StateValue.IsNull() {
34+
return
35+
}
36+
37+
// Do nothing if there is a known planned value.
38+
if !req.PlanValue.IsUnknown() {
39+
return
40+
}
41+
42+
// Do nothing if there is an unknown configuration value, otherwise interpolation gets messed up.
43+
if req.ConfigValue.IsUnknown() {
44+
return
45+
}
46+
47+
// The above checks are taken from the UseStateForUnknown plan modifier implementation
48+
// (https://github.com/hashicorp/terraform-plugin-framework/blob/main/resource/schema/stringplanmodifier/use_state_for_unknown.go#L38)
49+
50+
var stateModel Model
51+
diags := req.State.Get(ctx, &stateModel)
52+
resp.Diagnostics.Append(diags...)
53+
if resp.Diagnostics.HasError() {
54+
return
55+
}
56+
57+
var stateFlavor = &flavorModel{}
58+
if !(stateModel.Flavor.IsNull() || stateModel.Flavor.IsUnknown()) {
59+
diags = stateModel.Flavor.As(ctx, stateFlavor, basetypes.ObjectAsOptions{})
60+
resp.Diagnostics.Append(diags...)
61+
if resp.Diagnostics.HasError() {
62+
return
63+
}
64+
}
65+
66+
var planModel Model
67+
diags = req.Plan.Get(ctx, &planModel)
68+
resp.Diagnostics.Append(diags...)
69+
if resp.Diagnostics.HasError() {
70+
return
71+
}
72+
73+
var planFlavor = &flavorModel{}
74+
if !(planModel.Flavor.IsNull() || planModel.Flavor.IsUnknown()) {
75+
diags = planModel.Flavor.As(ctx, planFlavor, basetypes.ObjectAsOptions{})
76+
resp.Diagnostics.Append(diags...)
77+
if resp.Diagnostics.HasError() {
78+
return
79+
}
80+
}
81+
82+
if planFlavor.CPU == stateFlavor.CPU && planFlavor.RAM == stateFlavor.RAM {
83+
resp.PlanValue = req.StateValue
84+
}
85+
}

0 commit comments

Comments
 (0)