Skip to content

Commit

Permalink
Merge pull request #55 from jayhding/optional-tag-key
Browse files Browse the repository at this point in the history
Add support for optional tag key
  • Loading branch information
cblkwell authored Aug 26, 2019
2 parents 3c636ce + 3d9a145 commit 936e6aa
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
7 changes: 4 additions & 3 deletions pkg/amiclean/ami_cleaner.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ func (a *AMIClean) GetImages() (*ec2.DescribeImagesOutput, error) {
// within an image.
func matchTags(image *ec2.Image, tag *ec2.Tag) (bool, *ec2.Tag) {
for _, imageTag := range image.Tags {
if *tag.Key == *imageTag.Key {
if *tag.Value == *imageTag.Value {
if *tag.Key == "" || *tag.Key == *imageTag.Key {
if *tag.Value == "" || *tag.Value == *imageTag.Value {
// If the tag exists, and has the value we're
// looking for, return true and the image tag.
// looking for or we don't need match the tag,
// return true and the image tag.
return true, imageTag
}
// If the tag exists, and doesn't have the
Expand Down
36 changes: 28 additions & 8 deletions pkg/amiclean/ami_cleaner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,24 @@ var noEbsImage = &ec2.Image{
RootDeviceType: aws.String("instance-store"),
}

var testImages = []*ec2.Image{newMasterImage, newishDevImage, oldDevImage, noEbsImage}
var noTagImage = &ec2.Image{
Name: aws.String("notagimage-beta"),
Description: aws.String("No Tag Image"),
ImageId: aws.String("ami-555555555555555555"),
CreationDate: aws.String("2019-03-01T21:04:57.000Z"),
Tags: nil,
BlockDeviceMappings: []*ec2.BlockDeviceMapping{
{
DeviceName: aws.String("/dev/xvda"),
Ebs: &ec2.EbsBlockDevice{
SnapshotId: aws.String("snap-55555555555555555"),
},
},
},
RootDeviceType: aws.String("ebs"),
}

var testImages = []*ec2.Image{newMasterImage, newishDevImage, oldDevImage, noEbsImage, noTagImage}

var now = time.Date(2019, 4, 1, 0, 0, 0, 0, time.UTC)

Expand All @@ -120,13 +137,16 @@ func TestCheckImage(t *testing.T) {
RetentionDays int
resultSet []bool
}{
{testImages, "", &ec2.Tag{Key: aws.String("Branch"), Value: aws.String("master")}, false, 1, []bool{false, false, false, false}},
{testImages, "", &ec2.Tag{Key: aws.String("Branch"), Value: aws.String("development")}, false, 30, []bool{false, false, true, false}},
{testImages, "", &ec2.Tag{Key: aws.String("Branch"), Value: aws.String("development")}, false, 1, []bool{false, true, true, false}},
{testImages, "", &ec2.Tag{Key: aws.String("Branch"), Value: aws.String("master")}, true, 1, []bool{false, true, true, true}},
{testImages, "devimage", &ec2.Tag{Key: aws.String("Branch"), Value: aws.String("master")}, true, 1, []bool{false, true, true, false}},
{testImages, "", &ec2.Tag{Key: aws.String("Foozle"), Value: aws.String("Whatsit")}, false, 1, []bool{false, false, false, true}},
{testImages, "", &ec2.Tag{Key: aws.String("Foozle"), Value: aws.String("Whatsit")}, true, 0, []bool{true, true, true, false}},
{testImages, "", &ec2.Tag{Key: aws.String("Branch"), Value: aws.String("master")}, false, 1, []bool{false, false, false, false, false}},
{testImages, "", &ec2.Tag{Key: aws.String("Branch"), Value: aws.String("development")}, false, 30, []bool{false, false, true, false, false}},
{testImages, "", &ec2.Tag{Key: aws.String("Branch"), Value: aws.String("development")}, false, 1, []bool{false, true, true, false, false}},
{testImages, "", &ec2.Tag{Key: aws.String("Branch"), Value: aws.String("master")}, true, 1, []bool{false, true, true, true, true}},
{testImages, "devimage", &ec2.Tag{Key: aws.String("Branch"), Value: aws.String("master")}, true, 1, []bool{false, true, true, false, false}},
{testImages, "", &ec2.Tag{Key: aws.String("Foozle"), Value: aws.String("Whatsit")}, false, 1, []bool{false, false, false, true, false}},
{testImages, "", &ec2.Tag{Key: aws.String("Foozle"), Value: aws.String("Whatsit")}, true, 0, []bool{true, true, true, false, true}},
{testImages, "notagimage", &ec2.Tag{Key: aws.String(""), Value: aws.String("")}, true, 0, []bool{false, false, false, false, true}},
{testImages, "", &ec2.Tag{Key: aws.String(""), Value: aws.String("")}, false, 1, []bool{false, true, true, true, false}},
{testImages, "testimage", &ec2.Tag{Key: aws.String(""), Value: aws.String("")}, false, 10, []bool{false, false, false, false, false}},
}

for _, table := range tables {
Expand Down

0 comments on commit 936e6aa

Please sign in to comment.