Skip to content

Commit

Permalink
add SpotInstance resource
Browse files Browse the repository at this point in the history
  • Loading branch information
pww217 committed Aug 4, 2022
1 parent 9e19854 commit 41efb93
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions resources/ec2-spot-instance-request.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package resources

import (
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
)

type EC2SpotInstanceRequest struct {
svc *ec2.EC2
id string
state string
}

func init() {
register("EC2SpotInstanceRequest", ListEC2SpotInstanceRequests)
}

func ListEC2SpotInstanceRequests(sess *session.Session) ([]Resource, error) {
svc := ec2.New(sess)

resp, err := svc.DescribeSpotInstanceRequests(nil)
if err != nil {
return nil, err
}

resources := make([]Resource, 0)
for _, config := range resp.SpotInstanceRequestConfigs {

This comment has been minimized.

Copy link
@ximena9201

ximena9201 Aug 4, 2022

Collaborator

instead of SpotInstanceRequestConfigs, put SpotInstanceRequests. Looks like this parameter doesn't exist.

https://docs.aws.amazon.com/cli/latest/reference/ec2/cancel-spot-instance-requests.html

resources = append(resources, &EC2SpotInstanceRequest{
svc: svc,
id: *config.SpotInstanceRequestId,
state: *config.SpotInstanceRequestState,

This comment has been minimized.

Copy link
@ximena9201

ximena9201 Aug 4, 2022

Collaborator

Remove this line 34. I was looking and looks like the parameter SpotInstanceRequestState only applies for the spot fleet request.

https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_CancelSpotInstanceRequests.html
https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_CancelSpotFleetRequests.html

})
}

return resources, nil
}

func (i *EC2SpotInstanceRequest) Filter() error {
if i.state == "cancelled" {
return fmt.Errorf("already cancelled")
}
return nil
}

func (i *EC2SpotInstanceRequest) Remove() error {
params := &ec2.CancelSpotInstanceRequestsInput{
TerminateInstances: aws.Bool(true),

This comment has been minimized.

Copy link
@ximena9201

ximena9201 Aug 4, 2022

Collaborator

This comment has been minimized.

Copy link
@pww217

pww217 Aug 4, 2022

Author Owner

Thanks Xime - I made these changes but it's still failing. I'm awful at Go and I don't know if it's worth spending too much time on this. Although it does seem like the easiest way to fix our problem...that I can think of.

SpotInstanceRequestIds: []*string{
&i.id,
},
}

_, err := i.svc.CancelSpotInstanceRequests(params)
if err != nil {
return err
}

return nil
}

func (i *EC2SpotInstanceRequest) String() string {
return i.id
}

0 comments on commit 41efb93

Please sign in to comment.