Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(instance_server): use sbs api per default for block volumes #2804

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion internal/services/instance/helpers_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ func prepareRootVolume(rootVolumeI map[string]any, serverType *instance.ServerTy
// If the rootVolumeType is not defined, define it depending on the offer
if rootVolumeType == "" {
if serverTypeCanBootOnBlock {
rootVolumeType = instance.VolumeVolumeTypeBSSD.String()
rootVolumeType = instance.VolumeVolumeTypeSbsVolume.String()
} else {
rootVolumeType = instance.VolumeVolumeTypeLSSD.String()
}
Expand Down
12 changes: 9 additions & 3 deletions internal/services/instance/helpers_instance_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,15 @@ func instanceAndBlockAPIWithZoneAndID(m interface{}, zonedID string) (*BlockAndI
}, zone, ID, nil
}

func volumeTypeToMarketplaceFilter(volumeType any) marketplace.LocalImageType {
if volumeType != nil && instance.VolumeVolumeType(volumeType.(string)) == instance.VolumeVolumeTypeSbsVolume {
func volumeTypeToMarketplaceFilter(volumeType instance.VolumeVolumeType) marketplace.LocalImageType {
switch volumeType {
case instance.VolumeVolumeTypeSbsVolume:
return marketplace.LocalImageTypeInstanceSbs
case instance.VolumeVolumeTypeBSSD:
return marketplace.LocalImageTypeInstanceLocal
case instance.VolumeVolumeTypeLSSD:
return marketplace.LocalImageTypeInstanceLocal
}
return marketplace.LocalImageTypeInstanceLocal

return marketplace.LocalImageTypeInstanceSbs
}
43 changes: 22 additions & 21 deletions internal/services/instance/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,22 +389,6 @@ func ResourceInstanceServerCreate(ctx context.Context, d *schema.ResourceData, m
commercialType := d.Get("type").(string)

imageUUID := locality.ExpandID(d.Get("image"))
if imageUUID != "" && !scwvalidation.IsUUID(imageUUID) {
// Replace dashes with underscores ubuntu-focal -> ubuntu_focal
imageLabel := formatImageLabel(imageUUID)

marketPlaceAPI := marketplace.NewAPI(meta.ExtractScwClient(m))
image, err := marketPlaceAPI.GetLocalImageByLabel(&marketplace.GetLocalImageByLabelRequest{
CommercialType: commercialType,
Zone: zone,
ImageLabel: imageLabel,
Type: volumeTypeToMarketplaceFilter(d.Get("root_volume.0.volume_type")),
})
if err != nil {
return diag.FromErr(fmt.Errorf("could not get image '%s': %s", zonal.NewID(zone, imageLabel), err))
}
imageUUID = image.ID
}

req := &instanceSDK.CreateServerRequest{
Zone: zone,
Expand All @@ -417,10 +401,6 @@ func ResourceInstanceServerCreate(ctx context.Context, d *schema.ResourceData, m
RoutedIPEnabled: types.ExpandBoolPtr(types.GetBool(d, "routed_ip_enabled")),
}

if imageUUID != "" {
req.Image = scw.StringPtr(imageUUID)
}

enableIPv6, ok := d.GetOk("enable_ipv6")
if ok {
req.EnableIPv6 = scw.BoolPtr(enableIPv6.(bool)) //nolint:staticcheck
Expand Down Expand Up @@ -473,6 +453,27 @@ func ResourceInstanceServerCreate(ctx context.Context, d *schema.ResourceData, m
return diag.FromErr(err)
}

if imageUUID != "" && !scwvalidation.IsUUID(imageUUID) {
// Replace dashes with underscores ubuntu-focal -> ubuntu_focal
imageLabel := formatImageLabel(imageUUID)

marketPlaceAPI := marketplace.NewAPI(meta.ExtractScwClient(m))
image, err := marketPlaceAPI.GetLocalImageByLabel(&marketplace.GetLocalImageByLabelRequest{
CommercialType: commercialType,
Zone: zone,
ImageLabel: imageLabel,
Type: volumeTypeToMarketplaceFilter(req.Volumes["0"].VolumeType),
})
if err != nil {
return diag.FromErr(fmt.Errorf("could not get image '%s': %s", zonal.NewID(zone, imageLabel), err))
}
imageUUID = image.ID
}

if imageUUID != "" {
req.Image = scw.StringPtr(imageUUID)
}

res, err := api.CreateServer(req, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
Expand Down Expand Up @@ -1431,7 +1432,7 @@ func instanceServerVolumesUpdate(ctx context.Context, d *schema.ResourceData, ap

// local volumes can only be added when the server is stopped
if volumeHasChange && !serverIsStopped && volume.IsLocal() && volume.IsAttached() {
return nil, errors.New("instanceSDK must be stopped to change local volumes")
return nil, errors.New("instance must be stopped to change local volumes")
}
volumes[strconv.Itoa(i+1)] = volume.VolumeTemplate()
}
Expand Down
Loading
Loading