Skip to content

Commit

Permalink
DE19399:Volume capacity attribute size/unit problems (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
ron-saito authored Feb 1, 2024
1 parent ac1d5d9 commit 4e30f09
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions internal/resources/resource_volume.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// (C) Copyright 2020-2023 Hewlett Packard Enterprise Development LP
// (C) Copyright 2020-2024 Hewlett Packard Enterprise Development LP

package resources

import (
"fmt"
"math"
"strings"
"time"

Expand Down Expand Up @@ -36,6 +37,9 @@ const (
vID = "id"
vDiscoveryIP = "discovery_ip"
vTargetIQN = "target_iqn"

GBToGiBConversion float64 = 0.931323
KiBToGBConversion float64 = 976562.5 // 0.931323 *1024 * 1024
)

func volumeSchema() map[string]*schema.Schema {
Expand Down Expand Up @@ -219,7 +223,7 @@ func resourceMetalVolumeCreate(d *schema.ResourceData, meta interface{}) (err er

capacity, ok := d.Get(vSize).(float64)
if !ok || capacity <= 0 {
return fmt.Errorf("invalid capacity %v", capacity)
return fmt.Errorf("invalid capacity %v GB", capacity)
}

if vcID, ok = d.Get(vCollectionID).(string); !ok || vcID == "" {
Expand All @@ -235,7 +239,7 @@ func resourceMetalVolumeCreate(d *schema.ResourceData, meta interface{}) (err er

volume := rest.NewVolume{
Name: d.Get(vName).(string),
Capacity: int64(capacity),
Capacity: int64(math.Round(capacity * GBToGiBConversion)), // convert from GB to GiB
Description: d.Get(vDescription).(string),
FlavorID: vfID,
Shareable: d.Get(vShareable).(bool),
Expand Down Expand Up @@ -316,7 +320,12 @@ func resourceMetalVolumeRead(d *schema.ResourceData, meta interface{}) (err erro
}

d.SetId(volume.ID)
d.Set(vSize, float64(volume.Capacity/1024/1024))

// convert from KiB to GB
if err = d.Set(vSize, math.Round(float64(volume.Capacity)/KiBToGBConversion)); err != nil {
return fmt.Errorf("set Size: %v", err)
}

d.Set(vName, volume.Name)
d.Set(vDescription, volume.Description)
flavorName, _ := p.GetVolumeFlavorName(volume.FlavorID)
Expand Down Expand Up @@ -390,7 +399,7 @@ func resourceMetalVolumeUpdate(d *schema.ResourceData, meta interface{}) (err er
ETag: vol.ETag,
}

updateVol.Capacity = int64(newSize)
updateVol.Capacity = int64(math.Round(newSize * GBToGiBConversion)) // convert from GB to GiB

// add tags
if m, ok := d.Get(vLabels).(map[string]interface{}); ok {
Expand Down

0 comments on commit 4e30f09

Please sign in to comment.