-
Notifications
You must be signed in to change notification settings - Fork 14
/
instance_type.go
89 lines (74 loc) · 2.29 KB
/
instance_type.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package v2
import (
"context"
"strings"
apiv2 "github.com/exoscale/egoscale/v2/api"
"github.com/exoscale/egoscale/v2/oapi"
)
// InstanceType represents a Compute instance type.
type InstanceType struct {
Authorized *bool
CPUs *int64
Family *string
GPUs *int64
ID *string
Memory *int64
Size *string
}
func instanceTypeFromAPI(t *oapi.InstanceType) *InstanceType {
return &InstanceType{
Authorized: t.Authorized,
CPUs: t.Cpus,
Family: (*string)(t.Family),
GPUs: t.Gpus,
ID: t.Id,
Memory: t.Memory,
Size: (*string)(t.Size),
}
}
// ListInstanceTypes returns the list of existing Instance types.
func (c *Client) ListInstanceTypes(ctx context.Context, zone string) ([]*InstanceType, error) {
list := make([]*InstanceType, 0)
resp, err := c.ListInstanceTypesWithResponse(apiv2.WithZone(ctx, zone))
if err != nil {
return nil, err
}
if resp.JSON200.InstanceTypes != nil {
for i := range *resp.JSON200.InstanceTypes {
list = append(list, instanceTypeFromAPI(&(*resp.JSON200.InstanceTypes)[i]))
}
}
return list, nil
}
// GetInstanceType returns the Instance type corresponding to the specified ID.
func (c *Client) GetInstanceType(ctx context.Context, zone, id string) (*InstanceType, error) {
resp, err := c.GetInstanceTypeWithResponse(apiv2.WithZone(ctx, zone), id)
if err != nil {
return nil, err
}
return instanceTypeFromAPI(resp.JSON200), nil
}
// FindInstanceType attempts to find an Instance type by family+size or ID.
// To search by family+size, the expected format for v is "[FAMILY.]SIZE" (e.g. "large", "gpu.medium"),
// with family defaulting to "standard" if not specified.
func (c *Client) FindInstanceType(ctx context.Context, zone, x string) (*InstanceType, error) {
var typeFamily, typeSize string
parts := strings.SplitN(x, ".", 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])
}
}
res, err := c.ListInstanceTypes(ctx, zone)
if err != nil {
return nil, err
}
for _, r := range res {
if *r.ID == x || (*r.Family == typeFamily && *r.Size == typeSize) {
return c.GetInstanceType(ctx, zone, *r.ID)
}
}
return nil, apiv2.ErrNotFound
}