|
| 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