Skip to content
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
5 changes: 5 additions & 0 deletions internal/namespaces/instance/v1/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ func GetCommands() *core.Commands {

cmds.MustFind("instance", "server-type", "list").Override(serverTypeListBuilder)

//
// Get-Compatible-Types
//
cmds.MustFind("instance", "server", "get-compatible-types").Override(getCompatibleTypesBuilder)

//
// IP
//
Expand Down
130 changes: 119 additions & 11 deletions internal/namespaces/instance/v1/custom_server_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package instance

import (
"context"
"errors"
"fmt"
"sort"
"strings"

Expand Down Expand Up @@ -32,6 +34,17 @@ var serverTypesAvailabilityMarshalSpecs = human.EnumMarshalSpecs{
// Builders
//

type customServerType struct {
Name string `json:"name"`
HourlyPrice *scw.Money `json:"hourly_price"`
LocalVolumeMaxSize scw.Size `json:"local_volume_max_size"`
CPU uint32 `json:"cpu"`
GPU *uint64 `json:"gpu"`
RAM scw.Size `json:"ram"`
Arch instance.Arch `json:"arch"`
Availability instance.ServerTypesAvailability `json:"availability"`
}

// serverTypeListBuilder transforms the server map into a list to display a
// table of server types instead of a flat key/value list.
// We need it for:
Expand Down Expand Up @@ -63,17 +76,6 @@ func serverTypeListBuilder(c *core.Command) *core.Command {
}

c.Run = func(ctx context.Context, argsI interface{}) (interface{}, error) {
type customServerType struct {
Name string `json:"name"`
HourlyPrice *scw.Money `json:"hourly_price"`
LocalVolumeMaxSize scw.Size `json:"local_volume_max_size"`
CPU uint32 `json:"cpu"`
GPU *uint64 `json:"gpu"`
RAM scw.Size `json:"ram"`
Arch instance.Arch `json:"arch"`
Availability instance.ServerTypesAvailability `json:"availability"`
}

api := instance.NewAPI(core.ExtractClient(ctx))

// Get server types.
Expand Down Expand Up @@ -142,3 +144,109 @@ func serverTypeListBuilder(c *core.Command) *core.Command {
func serverTypeCategory(serverTypeName string) (category string) {
return strings.Split(serverTypeName, "-")[0]
}

func getCompatibleTypesBuilder(c *core.Command) *core.Command {
c.Interceptor = func(ctx context.Context, argsI interface{}, runner core.CommandRunner) (interface{}, error) {
rawResp, err := runner(ctx, argsI)
if err != nil {
return rawResp, err
}
getCompatibleTypesResp := rawResp.(*instance.ServerCompatibleTypes)

request := argsI.(*instance.GetServerCompatibleTypesRequest)
client := core.ExtractClient(ctx)
api := instance.NewAPI(client)
warnings := []error(nil)

// Get all server types to fill in the details in the response.
listServersTypesResponse, err := api.ListServersTypes(&instance.ListServersTypesRequest{
Zone: request.Zone,
}, scw.WithAllPages())
if err != nil {
return nil, err
}

// Build compatible types list with details
compatibleServerTypesCustom := []*customServerType(nil)
for _, compatibleType := range getCompatibleTypesResp.CompatibleTypes {
serverType, ok := listServersTypesResponse.Servers[compatibleType]
if !ok {
warnings = append(
warnings,
fmt.Errorf("could not find details on compatible type %q", compatibleType),
)
}

compatibleServerTypesCustom = append(compatibleServerTypesCustom, &customServerType{
Name: compatibleType,
HourlyPrice: scw.NewMoneyFromFloat(
float64(serverType.HourlyPrice),
"EUR",
3,
),
LocalVolumeMaxSize: serverType.VolumesConstraint.MaxSize,
CPU: serverType.Ncpus,
GPU: serverType.Gpu,
RAM: scw.Size(serverType.RAM),
Arch: serverType.Arch,
})
}

// Get server's current type to fill in the details in the response
server, err := api.GetServer(&instance.GetServerRequest{
Zone: request.Zone,
ServerID: request.ServerID,
})
if err != nil {
return nil, err
}
currentServerType, ok := listServersTypesResponse.Servers[server.Server.CommercialType]
if !ok {
warnings = append(
warnings,
fmt.Errorf(
"could not find details on current type %q",
server.Server.CommercialType,
),
)
}
currentServerTypeCustom := []*customServerType{
{
Name: server.Server.CommercialType,
HourlyPrice: scw.NewMoneyFromFloat(
float64(currentServerType.HourlyPrice),
"EUR",
3,
),
LocalVolumeMaxSize: currentServerType.VolumesConstraint.MaxSize,
CPU: currentServerType.Ncpus,
GPU: currentServerType.Gpu,
RAM: scw.Size(currentServerType.RAM),
Arch: currentServerType.Arch,
},
}

return &struct {
CurrentServerType []*customServerType
CompatibleServerTypes []*customServerType
}{
currentServerTypeCustom,
compatibleServerTypesCustom,
}, errors.Join(warnings...)
}

c.View = &core.View{
Sections: []*core.ViewSection{
{
Title: "Current Server Type",
FieldName: "CurrentServerType",
},
{
Title: "Compatible Server Types",
FieldName: "CompatibleServerTypes",
},
},
}

return c
}
Loading