/
check_instance_type_validity.go
62 lines (48 loc) · 1.19 KB
/
check_instance_type_validity.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
package service
import (
"errors"
"strings"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/yolo-sh/aws-cloud-provider/infrastructure"
"github.com/yolo-sh/yolo/stepper"
)
type ErrInvalidInstanceType struct {
InstanceType string
Region string
}
func (ErrInvalidInstanceType) Error() string {
return "ErrInvalidInstanceType"
}
type ErrInvalidInstanceTypeArch struct {
InstanceType string
SupportedArchs string
}
func (ErrInvalidInstanceTypeArch) Error() string {
return "ErrInvalidInstanceTypeArch"
}
func (a *AWS) CheckInstanceTypeValidity(
stepper stepper.Stepper,
instanceType string,
) error {
ec2Client := ec2.NewFromConfig(a.sdkConfig)
_, err := infrastructure.LookupInstanceTypeInfos(
ec2Client,
instanceType,
)
if err != nil {
if errors.Is(err, infrastructure.ErrInvalidInstanceType) {
return ErrInvalidInstanceType{
InstanceType: instanceType,
Region: a.sdkConfig.Region,
}
}
if errors.Is(err, infrastructure.ErrInvalidInstanceTypeArch) {
return ErrInvalidInstanceTypeArch{
InstanceType: instanceType,
SupportedArchs: strings.Join(infrastructure.SupportedInstanceTypeArchs, ", "),
}
}
return err
}
return nil
}