Repository navigation
Expand file tree
/
Copy pathinstancetype.go
More file actions
109 lines (91 loc) · 2.83 KB
/
Copy pathinstancetype.go
File metadata and controls
109 lines (91 loc) · 2.83 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package v1
import (
"context"
"fmt"
"strconv"
"strings"
"time"
"github.com/alecthomas/units"
"github.com/bojanz/currency"
openapi "github.com/brevdev/cloud/internal/fluidstack/gen/fluidstack"
"github.com/brevdev/cloud/pkg/v1"
)
func (c *FluidStackClient) GetInstanceTypes(ctx context.Context, _ v1.GetInstanceTypeArgs) ([]v1.InstanceType, error) {
authCtx := c.makeAuthContext(ctx)
projectCtx := c.makeProjectContext(authCtx)
resp, httpResp, err := c.client.InstanceTypesAPI.ListInstanceTypes(projectCtx).Execute()
if httpResp != nil && httpResp.Body != nil {
defer func() { _ = httpResp.Body.Close() }()
}
if err != nil {
return nil, fmt.Errorf("failed to get instance types: %w", err)
}
var instanceTypes []v1.InstanceType
for _, fsInstanceType := range resp {
instanceType := convertFluidStackInstanceTypeToV1InstanceType("", fsInstanceType, true)
instanceTypes = append(instanceTypes, instanceType)
}
return instanceTypes, nil
}
func (c *FluidStackClient) GetInstanceTypePollTime() time.Duration {
return 5 * time.Minute
}
func (c *FluidStackClient) GetLocations(ctx context.Context, _ v1.GetLocationsArgs) ([]v1.Location, error) {
authCtx := c.makeAuthContext(ctx)
projectCtx := c.makeProjectContext(authCtx)
resp, httpResp, err := c.client.CapacityAPI.ListCapacity(projectCtx).Execute()
if httpResp != nil && httpResp.Body != nil {
defer func() { _ = httpResp.Body.Close() }()
}
if err != nil {
return nil, fmt.Errorf("failed to get locations: %w", err)
}
var locations []v1.Location
for _, capacity := range resp {
location := v1.Location{
Name: capacity.Name,
Description: capacity.Name,
Available: capacity.Capacity > 0,
}
locations = append(locations, location)
}
return locations, nil
}
func convertFluidStackInstanceTypeToV1InstanceType(location string, fsInstanceType openapi.InstanceType, isAvailable bool) v1.InstanceType {
var gpus []v1.GPU
if fsInstanceType.GpuCount != nil && *fsInstanceType.GpuCount > 0 {
count := int(*fsInstanceType.GpuCount)
model := "GPU"
if fsInstanceType.GpuModel != nil {
model = *fsInstanceType.GpuModel
}
for i := 0; i < count; i++ {
gpus = append(gpus, v1.GPU{
Name: model,
})
}
}
var ram units.Base2Bytes
if fsInstanceType.Memory != "" {
memoryStr := strings.TrimSuffix(fsInstanceType.Memory, "GB")
memoryStr = strings.TrimSpace(memoryStr)
if memoryGB, err := strconv.ParseFloat(memoryStr, 64); err == nil {
ram = units.Base2Bytes(memoryGB) * units.Gibibyte
}
}
vcpus := fsInstanceType.Cpu
if vcpus < 0 {
vcpus = 0
}
price, _ := currency.NewAmount("0", "USD")
return v1.InstanceType{
Type: fsInstanceType.Name,
VCPU: vcpus,
Memory: ram,
SupportedGPUs: gpus,
BasePrice: &price,
IsAvailable: isAvailable,
Location: location,
Provider: CloudProviderID,
}
}