Skip to content

Commit 41efb93

Browse files
committed
add SpotInstance resource
1 parent 9e19854 commit 41efb93

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package resources
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/aws/aws-sdk-go/aws"
7+
"github.com/aws/aws-sdk-go/aws/session"
8+
"github.com/aws/aws-sdk-go/service/ec2"
9+
)
10+
11+
type EC2SpotInstanceRequest struct {
12+
svc *ec2.EC2
13+
id string
14+
state string
15+
}
16+
17+
func init() {
18+
register("EC2SpotInstanceRequest", ListEC2SpotInstanceRequests)
19+
}
20+
21+
func ListEC2SpotInstanceRequests(sess *session.Session) ([]Resource, error) {
22+
svc := ec2.New(sess)
23+
24+
resp, err := svc.DescribeSpotInstanceRequests(nil)
25+
if err != nil {
26+
return nil, err
27+
}
28+
29+
resources := make([]Resource, 0)
30+
for _, config := range resp.SpotInstanceRequestConfigs {
31+
resources = append(resources, &EC2SpotInstanceRequest{
32+
svc: svc,
33+
id: *config.SpotInstanceRequestId,
34+
state: *config.SpotInstanceRequestState,
35+
})
36+
}
37+
38+
return resources, nil
39+
}
40+
41+
func (i *EC2SpotInstanceRequest) Filter() error {
42+
if i.state == "cancelled" {
43+
return fmt.Errorf("already cancelled")
44+
}
45+
return nil
46+
}
47+
48+
func (i *EC2SpotInstanceRequest) Remove() error {
49+
params := &ec2.CancelSpotInstanceRequestsInput{
50+
TerminateInstances: aws.Bool(true),
51+
SpotInstanceRequestIds: []*string{
52+
&i.id,
53+
},
54+
}
55+
56+
_, err := i.svc.CancelSpotInstanceRequests(params)
57+
if err != nil {
58+
return err
59+
}
60+
61+
return nil
62+
}
63+
64+
func (i *EC2SpotInstanceRequest) String() string {
65+
return i.id
66+
}

0 commit comments

Comments
 (0)