Skip to content

Commit

Permalink
CRUSOE-8787: Fix disk size conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
Bryan Lim committed May 10, 2024
1 parent ab18db0 commit 968f2a8
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions internal/disk/disk_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package disk
import (
"context"
"fmt"
"strconv"
"strings"

"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
Expand All @@ -19,6 +21,7 @@ import (

const (
defaultDiskType = "persistent-ssd"
gibInTib = 1024
)

type diskResource struct {
Expand Down Expand Up @@ -163,6 +166,7 @@ func (r *diskResource) Create(ctx context.Context, req resource.CreateRequest, r
plan.Type = types.StringValue(disk.Type_)
plan.Location = types.StringValue(disk.Location)
plan.SerialNumber = types.StringValue(disk.SerialNumber)
plan.Size = types.StringValue(formatSize(disk.Size))
plan.ProjectID = types.StringValue(projectID)

diags = resp.State.Set(ctx, plan)
Expand Down Expand Up @@ -219,7 +223,7 @@ func (r *diskResource) Read(ctx context.Context, req resource.ReadRequest, resp

state.Name = types.StringValue(disk.Name)
state.Type = types.StringValue(disk.Type_)
state.Size = types.StringValue(disk.Size)
state.Size = types.StringValue(formatSize(disk.Size))
state.SerialNumber = types.StringValue(disk.SerialNumber)

diags = resp.State.Set(ctx, &state)
Expand Down Expand Up @@ -250,7 +254,7 @@ func (r *diskResource) Update(ctx context.Context, req resource.UpdateRequest, r
if err != nil {
resp.Diagnostics.AddError("Failed to resize disk",
fmt.Sprintf("There was an error starting a resize operation: %s.\n\n"+
"Make sure the disk still exists, you are englarging the disk,"+
"Make sure the disk still exists, you are enlarging the disk,"+
" and if the disk is attached to a VM, the VM is powered off.", common.UnpackAPIError(err)))

return
Expand All @@ -261,7 +265,7 @@ func (r *diskResource) Update(ctx context.Context, req resource.UpdateRequest, r
if err != nil {
resp.Diagnostics.AddError("Failed to resize disk",
fmt.Sprintf("There was an error resizing a disk: %s.\n\n"+
"Make sure the disk still exists, you are englarging the disk,"+
"Make sure the disk still exists, you are enlarging the disk,"+
" and if the disk is attached to a VM, the VM is powered off.", common.UnpackAPIError(err)))

return
Expand Down Expand Up @@ -297,3 +301,18 @@ func (r *diskResource) Delete(ctx context.Context, req resource.DeleteRequest, r
return
}
}

func formatSize(sizeStr string) string {
sizeStr = strings.ToLower(sizeStr)
if strings.HasSuffix(sizeStr, "gib") {
if size, err := strconv.Atoi(sizeStr[:len(sizeStr)-3]); err != nil &&
size >= gibInTib && size%gibInTib == 0 {

return strconv.Itoa(size/gibInTib) + "TiB"
}

return sizeStr
}

return sizeStr
}

0 comments on commit 968f2a8

Please sign in to comment.