Skip to content

Commit

Permalink
Fix instance type
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre-Emmanuel Jacquier <[email protected]>
  • Loading branch information
pierre-emmanuelJ committed Jul 15, 2024
1 parent 36e7d44 commit 451f91d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
16 changes: 11 additions & 5 deletions cmd/instance_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/exoscale/cli/pkg/output"
exossh "github.com/exoscale/cli/pkg/ssh"
"github.com/exoscale/cli/pkg/userdata"
"github.com/exoscale/cli/utils"
v3 "github.com/exoscale/egoscale/v3"
)

Expand Down Expand Up @@ -131,11 +132,16 @@ func (c *instanceCreateCmd) cmdRun(_ *cobra.Command, _ []string) error { //nolin
return fmt.Errorf("error listing instance type: %w", err)
}

instanceType, err := instanceTypes.FindInstanceType(c.InstanceType)
if err != nil {
return fmt.Errorf("error retrieving instance type: %w", err)
// c.InstanceType is never empty
instanceType := utils.ParseInstanceType(c.InstanceType)
for i, it := range instanceTypes.InstanceTypes {
if it.Family == instanceType.Family && it.Size == instanceType.Size {
instanceReq.InstanceType = &instanceTypes.InstanceTypes[i]
}
}
if instanceReq.InstanceType == nil {
return fmt.Errorf("error retrieving instance type %s: not found", c.InstanceType)
}
instanceReq.InstanceType = &instanceType

privateNetworks := make([]v3.PrivateNetwork, len(c.PrivateNetworks))
if l := len(c.PrivateNetworks); l > 0 {
Expand Down Expand Up @@ -280,7 +286,7 @@ func (c *instanceCreateCmd) cmdRun(_ *cobra.Command, _ []string) error { //nolin
return fmt.Errorf("error writing SSH private key file: %w", err)
}

op, err := client.DeleteSSHKey(ctx, sshKeys[0].Name)
op, err := client.DeleteSSHKey(ctx, instanceReq.SSHKeys[0].Name)
if err != nil {
return fmt.Errorf("error deleting SSH key: %w", err)
}
Expand Down
20 changes: 20 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/hashicorp/go-multierror"

exoapi "github.com/exoscale/egoscale/v2/api"
v3 "github.com/exoscale/egoscale/v3"

"github.com/exoscale/cli/pkg/account"
v2 "github.com/exoscale/egoscale/v2"
Expand Down Expand Up @@ -236,3 +237,22 @@ func ForEachZone(zones []string, f func(zone string) error) error {

return meg.Wait().ErrorOrNil()
}

// ParseInstanceType returns an v3.InstanceType with family and name.
func ParseInstanceType(instanceType string) v3.InstanceType {
var typeFamily, typeSize string

parts := strings.SplitN(instanceType, ".", 2)
if l := len(parts); l > 0 {
if l == 1 {
typeFamily, typeSize = "standard", strings.ToLower(parts[0])
} else {
typeFamily, typeSize = strings.ToLower(parts[0]), strings.ToLower(parts[1])
}
}

return v3.InstanceType{
Family: v3.InstanceTypeFamily(typeFamily),
Size: v3.InstanceTypeSize(typeSize),
}
}

0 comments on commit 451f91d

Please sign in to comment.