-
Notifications
You must be signed in to change notification settings - Fork 38
/
get.go
70 lines (55 loc) · 1.64 KB
/
get.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
package instance
import (
"fmt"
"os"
awssdk "github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/awslabs/eksdemo/pkg/aws"
"github.com/awslabs/eksdemo/pkg/printer"
"github.com/awslabs/eksdemo/pkg/resource"
)
type Getter struct {
ec2Client *aws.EC2Client
}
func NewGetter(ec2Client *aws.EC2Client) *Getter {
return &Getter{ec2Client}
}
func (g *Getter) Init() {
if g.ec2Client == nil {
g.ec2Client = aws.NewEC2Client()
}
}
func (g *Getter) Get(id string, output printer.Output, options resource.Options) error {
ec2Options, ok := options.(*Options)
if !ok {
return fmt.Errorf("internal error, unable to cast options to EC2InstanceOptions")
}
filters := []types.Filter{}
if ec2Options.HideTerminated {
filters = append(filters,
aws.NewEC2InstanceStateFilter([]string{"pending", "running", "shutting-down", "stopping", "stopped"}),
)
}
if id != "" {
filters = append(filters, aws.NewEC2InstanceIdFilter(id))
}
cluster := options.Common().Cluster
if cluster != nil {
filters = append(filters, aws.NewEC2VpcFilter(awssdk.ToString(cluster.ResourcesVpcConfig.VpcId)))
}
reservations, err := g.ec2Client.DescribeInstances(filters)
if err != nil {
return err
}
return output.Print(os.Stdout, NewPrinter(reservations))
}
func (g *Getter) GetInstanceById(id string) (types.Instance, error) {
reservations, err := g.ec2Client.DescribeInstances([]types.Filter{aws.NewEC2InstanceIdFilter(id)})
if err != nil {
return types.Instance{}, err
}
if len(reservations) == 0 {
return types.Instance{}, fmt.Errorf("ec2-instance %q not found", id)
}
return reservations[0].Instances[0], nil
}