packer-cn/helper/tests/aws.go

44 lines
1.1 KiB
Go
Raw Normal View History

package testshelper
import (
2020-03-13 12:17:42 -04:00
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
awscommon "github.com/hashicorp/packer/builder/amazon/common"
)
type AWSHelper struct {
2020-03-13 11:36:30 -04:00
Region string
AMIName string
}
func (a *AWSHelper) CleanUpAmi(t *testing.T) {
accessConfig := &awscommon.AccessConfig{}
session, err := accessConfig.Session()
if err != nil {
t.Errorf("AWSAMICleanUp: Unable to create aws session %s", err.Error())
}
regionconn := ec2.New(session.Copy(&aws.Config{
Region: aws.String(a.Region),
}))
resp, err := regionconn.DescribeImages(&ec2.DescribeImagesInput{
Owners: aws.StringSlice([]string{"self"}),
Filters: []*ec2.Filter{{
Name: aws.String("name"),
Values: aws.StringSlice([]string{a.AMIName}),
}}})
if err != nil {
2020-03-13 11:36:30 -04:00
t.Errorf("AWSAMICleanUp: Unable to find Image %s: %s", a.AMIName, err.Error())
}
_, err = regionconn.DeregisterImage(&ec2.DeregisterImageInput{
ImageId: resp.Images[0].ImageId,
})
if err != nil {
t.Errorf("AWSAMICleanUp: Unable to Deregister Image %s", err.Error())
}
2020-03-13 11:36:30 -04:00
}