Skip to content

Commit

Permalink
feat(instance): add volume_type param in the root_volume of a server (s…
Browse files Browse the repository at this point in the history
…caleway#1125)

Co-authored-by: Clement <cgilbert@scaleway.com>
  • Loading branch information
2 people authored and remyleone committed Mar 28, 2022
1 parent fa4b86d commit 689c150
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 25 deletions.
9 changes: 5 additions & 4 deletions scaleway/helpers_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,12 @@ func sanitizeVolumeMap(serverName string, volumes map[string]*instance.VolumeSer
ID: v.ID,
Name: v.Name,
}
// For the root volume (index 0) if the specified size is not 0 it is considered as a new volume
// It does not have yet a volume ID, it is passed to the API with only the size to be dynamically created by the API
case index == "0" && v.Size != 0:
// For the root volume (index 0) if the size is 0, it is considered as a volume created from an image.
// The size is not passed to the API, so it's computed by the API
case index == "0" && v.Size == 0:
v = &instance.VolumeServerTemplate{
Size: v.Size,
VolumeType: v.VolumeType,
Boot: v.Boot,
}
// If none of the above conditions are met, the volume is passed as it to the API
default:
Expand Down
57 changes: 36 additions & 21 deletions scaleway/resource_instance_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,17 @@ func resourceScalewayInstanceServer() *schema.Resource {
ForceNew: true,
Description: "Size of the root volume in gigabytes",
},
"volume_type": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
Description: "Volume type of the root volume",
ValidateFunc: validation.StringInSlice([]string{
instance.VolumeVolumeTypeBSSD.String(),
instance.VolumeVolumeTypeLSSD.String(),
}, false),
},
"delete_on_termination": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -312,31 +323,34 @@ func resourceScalewayInstanceServerCreate(ctx context.Context, d *schema.Resourc
req.Volumes = make(map[string]*instance.VolumeServerTemplate)
isBootOnBlock := serverType.VolumesConstraint.MaxSize == 0
isBoot := expandBoolPtr(d.Get("root_volume.0.boot"))
if isBootOnBlock {
if size, ok := d.GetOk("root_volume.0.size_in_gb"); ok {
req.Volumes["0"] = &instance.VolumeServerTemplate{
Size: scw.Size(uint64(size.(int)) * gb),
VolumeType: instance.VolumeVolumeTypeBSSD,
Boot: *isBoot,
}
}
} else {
if size, ok := d.GetOk("root_volume.0.size_in_gb"); ok {
req.Volumes["0"] = &instance.VolumeServerTemplate{
Size: scw.Size(uint64(size.(int)) * gb),
VolumeType: instance.VolumeVolumeTypeLSSD,
Boot: *isBoot,
}
volumeType := d.Get("root_volume.0.volume_type").(string)
sizeInput := d.Get("root_volume.0.size_in_gb").(int)

// If the volumeType is not defined, define it depending of the offer
if volumeType == "" {
if isBootOnBlock {
volumeType = instance.VolumeVolumeTypeBSSD.String()
} else {
// We add a local root volume if it is not already present
req.Volumes["0"] = &instance.VolumeServerTemplate{
Name: newRandomName("vol"),
VolumeType: instance.VolumeVolumeTypeLSSD,
Size: serverType.VolumesConstraint.MaxSize,
}
volumeType = instance.VolumeVolumeTypeLSSD.String()
}
}

var size scw.Size
if sizeInput == 0 && volumeType == instance.VolumeVolumeTypeLSSD.String() {
// Compute the size so it will be valid against the local volume constraints
// It wouldn't be valid if another local volume is added, but in this case
// the user would be informed that it does not fulfill the local volume constraints
size = serverType.VolumesConstraint.MaxSize
} else {
size = scw.Size(uint64(sizeInput) * gb)
}

req.Volumes["0"] = &instance.VolumeServerTemplate{
VolumeType: instance.VolumeVolumeType(volumeType),
Size: size,
Boot: *isBoot,
}

if raw, ok := d.GetOk("additional_volume_ids"); ok {
for i, volumeID := range raw.([]interface{}) {
// We have to get the volume to know whether it is a local or a block volume
Expand Down Expand Up @@ -560,6 +574,7 @@ func resourceScalewayInstanceServerRead(ctx context.Context, d *schema.ResourceD
rootVolume["size_in_gb"] = int(uint64(volume.Size) / gb)
_, rootVolumeAttributeSet := d.GetOk("root_volume") // Related to https://github.com/hashicorp/terraform-plugin-sdk/issues/142
rootVolume["delete_on_termination"] = d.Get("root_volume.0.delete_on_termination").(bool) || !rootVolumeAttributeSet
rootVolume["volume_type"] = volume.VolumeType

_ = d.Set("root_volume", []map[string]interface{}{rootVolume})
} else {
Expand Down

0 comments on commit 689c150

Please sign in to comment.