Skip to content

Commit 1a58bd6

Browse files
authored
Validate volume resize on the schema (#660)
Signed-off-by: Alexander Dahmen <alexander.dahmen@inovex.de>
1 parent 75d6760 commit 1a58bd6

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

stackit/internal/services/iaas/volume/resource.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,9 @@ func (r *volumeResource) Schema(_ context.Context, _ resource.SchemaRequest, res
247247
"size": schema.Int64Attribute{
248248
Description: "The size of the volume in GB. It can only be updated to a larger value than the current size. Either `size` or `source` must be provided",
249249
Optional: true,
250+
PlanModifiers: []planmodifier.Int64{
251+
volumeResizeModifier{},
252+
},
250253
},
251254
"source": schema.SingleNestedAttribute{
252255
Description: "The source of the volume. It can be either a volume, an image, a snapshot or a backup. Either `size` or `source` must be provided",
@@ -275,6 +278,39 @@ func (r *volumeResource) Schema(_ context.Context, _ resource.SchemaRequest, res
275278
}
276279
}
277280

281+
var _ planmodifier.Int64 = volumeResizeModifier{}
282+
283+
type volumeResizeModifier struct {
284+
}
285+
286+
// Description implements planmodifier.String.
287+
func (v volumeResizeModifier) Description(context.Context) string {
288+
return "validates volume resize"
289+
}
290+
291+
// MarkdownDescription implements planmodifier.String.
292+
func (v volumeResizeModifier) MarkdownDescription(ctx context.Context) string {
293+
return v.Description(ctx)
294+
}
295+
296+
// PlanModifyInt64 implements planmodifier.Int64.
297+
func (v volumeResizeModifier) PlanModifyInt64(ctx context.Context, req planmodifier.Int64Request, resp *planmodifier.Int64Response) { // nolint:gocritic // function signature required by Terraform
298+
var planSize types.Int64
299+
var currentSize types.Int64
300+
301+
resp.Diagnostics.Append(req.Plan.GetAttribute(ctx, path.Root("size"), &planSize)...)
302+
if resp.Diagnostics.HasError() {
303+
return
304+
}
305+
resp.Diagnostics.Append(req.State.GetAttribute(ctx, path.Root("size"), &currentSize)...)
306+
if resp.Diagnostics.HasError() {
307+
return
308+
}
309+
if planSize.ValueInt64() < currentSize.ValueInt64() {
310+
core.LogAndAddError(ctx, &resp.Diagnostics, "Error changing volume size", "A volume cannot be made smaller in order to prevent data loss.")
311+
}
312+
}
313+
278314
// Create creates the resource and sets the initial Terraform state.
279315
func (r *volumeResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { // nolint:gocritic // function signature required by Terraform
280316
// Retrieve values from plan

0 commit comments

Comments
 (0)