Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/regex matching #105

Draft
wants to merge 27 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a19bd8a
feature: add flag to interpet filter as regex
notrepo05 Jun 14, 2021
0de4eeb
test: interpreting the filter as regex via a flag
notrepo05 Jun 15, 2021
2f09529
refactor: regex_match test to use ginkgo
notrepo05 Jun 15, 2021
7b106cb
refactor: rename RegexMatch to ResourceMatches
notrepo05 Jun 15, 2021
19962d8
refactor: remove redundant declaration
notrepo05 Jun 15, 2021
58b1f09
refactor: kill a line of white space
notrepo05 Jun 15, 2021
5924211
fix: enable filter by regex for vsphere
notrepo05 Jun 15, 2021
53d0bd9
feature: wait for rds_instances to be deleted
notrepo05 Jun 15, 2021
9832ef5
fix: order of resource deletion
notrepo05 Jun 15, 2021
ea813ea
Merge pull request #1 from notrepo05/feature/dependent-rds-cleanup
notrepo05 Jun 15, 2021
3742ab1
Update README.md
notrepo05 Jun 16, 2021
cd2d592
Update README.md
notrepo05 Jun 16, 2021
4df8225
Update README.md
notrepo05 Jun 16, 2021
1e7e4ab
refactor: rename describe to resource matches
notrepo05 Jun 16, 2021
69ce9f9
feature: update our vendored dependencies
Jun 17, 2021
723c575
feat: use extended regex for resource filtering
notrepo05 Jun 17, 2021
8efb52b
Merge branch 'feature/regex-matching' of github.com:notrepo05/leftove…
notrepo05 Jun 17, 2021
2efedf4
Set version for beta release tag
notrepo05 Jun 17, 2021
633db7f
bump version
notrepo05 Jun 18, 2021
1fe5290
misc: revert version back to dev
notrepo05 Jun 18, 2021
08d7c0b
Update readme to describe filtering
notrepo05 Jun 18, 2021
b16c7b6
Fix typo in readme
notrepo05 Jun 18, 2021
341f42e
Update README.md
notrepo05 Jun 18, 2021
163a318
Update README.md
notrepo05 Jun 18, 2021
97bf0b7
feat: add github actions unit tests on push
notrepo05 Jun 18, 2021
b93d2ab
fix: github action syntax
notrepo05 Jun 18, 2021
36e523c
fix: github actions syntax
notrepo05 Jun 18, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feature: add flag to interpet filter as regex
Authored-by: Nick Rohn <nrohn@vmware.com>
notrepo05 committed Jun 15, 2021
commit a19bd8aa54499f72e530f35d5ed9ad630e119c4a
12 changes: 6 additions & 6 deletions acceptance/aws_test.go
Original file line number Diff line number Diff line change
@@ -49,12 +49,12 @@ var _ = Describe("AWS", func() {
})

AfterEach(func() {
err := deleter.Delete(filter)
err := deleter.Delete(filter, false)
Expect(err).NotTo(HaveOccurred())
})

It("lists resources without deleting", func() {
deleter.List(filter)
deleter.List(filter, false)

Expect(stdout.String()).To(ContainSubstring("[EC2 Key Pair: %s]", filter))
Expect(stdout.String()).NotTo(ContainSubstring("[EC2 Key Pair: %s] Deleting...", filter))
@@ -69,12 +69,12 @@ var _ = Describe("AWS", func() {
})

AfterEach(func() {
err := deleter.Delete(filter)
err := deleter.Delete(filter, false)
Expect(err).NotTo(HaveOccurred())
})

It("lists resources of the specified type without deleting", func() {
deleter.List(filter)
deleter.List(filter, false)

Expect(stdout.String()).To(ContainSubstring("[EC2 Key Pair: %s]", filter))
Expect(stdout.String()).NotTo(ContainSubstring("[EC2 Key Pair: %s] Deleting...", filter))
@@ -97,7 +97,7 @@ var _ = Describe("AWS", func() {
})

It("deletes resources with the filter", func() {
err := deleter.Delete(filter)
err := deleter.Delete(filter, false)
Expect(err).NotTo(HaveOccurred())

Expect(stdout.String()).To(ContainSubstring("[EC2 Key Pair: %s] Deleting...", filter))
@@ -112,7 +112,7 @@ var _ = Describe("AWS", func() {
})

It("deletes the key pair resources with the filter", func() {
err := deleter.DeleteByType(filter, "ec2-key-pair")
err := deleter.DeleteByType(filter, "ec2-key-pair", false)
Expect(err).NotTo(HaveOccurred())

Expect(stdout.String()).To(ContainSubstring("[EC2 Key Pair: %s] Deleting...", filter))
8 changes: 4 additions & 4 deletions acceptance/azure_test.go
Original file line number Diff line number Diff line change
@@ -48,12 +48,12 @@ var _ = Describe("Azure", func() {
})

AfterEach(func() {
err := deleter.Delete(filter)
err := deleter.Delete(filter, false)
Expect(err).NotTo(HaveOccurred())
})

It("lists resources without deleting", func() {
deleter.List(filter)
deleter.List(filter, false)

Expect(stdout.String()).To(ContainSubstring("[Resource Group: %s]", filter))
Expect(stdout.String()).To(ContainSubstring("Listing Resource Groups..."))
@@ -76,7 +76,7 @@ var _ = Describe("Azure", func() {
})

It("deletes resources with the filter", func() {
err := deleter.Delete(filter)
err := deleter.Delete(filter, false)
Expect(err).NotTo(HaveOccurred())

Expect(stdout.String()).To(ContainSubstring("[Resource Group: %s] Deleting...", filter))
@@ -91,7 +91,7 @@ var _ = Describe("Azure", func() {
})

PIt("prompts them for subresources after they say no to the resource group", func() {
err := deleter.Delete(filter)
err := deleter.Delete(filter, false)
Expect(err).NotTo(HaveOccurred())

Expect(stdout.String()).NotTo(ContainSubstring("[Resource Group: %s] Deleting...", filter))
12 changes: 6 additions & 6 deletions acceptance/gcp_test.go
Original file line number Diff line number Diff line change
@@ -48,12 +48,12 @@ var _ = Describe("GCP", func() {
})

AfterEach(func() {
err := deleter.Delete(filter)
err := deleter.Delete(filter, false)
Expect(err).NotTo(HaveOccurred())
})

It("lists only the deletable resources that contain the specified filter", func() {
deleter.List(filter)
deleter.List(filter, false)

Expect(stdout.String()).To(ContainSubstring("[Disk: %s]", filter))
Expect(stdout.String()).To(ContainSubstring("Listing Disks for Zone"))
@@ -69,12 +69,12 @@ var _ = Describe("GCP", func() {
})

AfterEach(func() {
err := deleter.Delete(filter)
err := deleter.Delete(filter, false)
Expect(err).NotTo(HaveOccurred())
})

It("lists only the deletable resources of the specified type", func() {
deleter.ListByType(filter, "disk")
deleter.ListByType(filter, "disk", false)

Expect(stdout.String()).To(ContainSubstring("[Disk: %s]", filter))
Expect(stdout.String()).NotTo(ContainSubstring("[Disk: %s] Deleting...", filter))
@@ -99,7 +99,7 @@ var _ = Describe("GCP", func() {
})

It("deletes resources with the filter", func() {
err := deleter.Delete(filter)
err := deleter.Delete(filter, false)
Expect(err).NotTo(HaveOccurred())

Expect(stdout.String()).To(ContainSubstring("[Disk: %s] Deleting...", filter))
@@ -117,7 +117,7 @@ var _ = Describe("GCP", func() {
})

It("deletes resources with the filter", func() {
err := deleter.DeleteByType(filter, "disk")
err := deleter.DeleteByType(filter, "disk", false)
Expect(err).NotTo(HaveOccurred())

Expect(stdout.String()).To(ContainSubstring("[Disk: %s] Deleting...", filter))
4 changes: 2 additions & 2 deletions acceptance/nsxt_test.go
Original file line number Diff line number Diff line change
@@ -47,7 +47,7 @@ var _ = Describe("NSX-T", func() {

It("can list and delete resources with the filter", func() {
By("listing resources first", func() {
deleter.List("leftover")
deleter.List("leftover", false)
Expect(stdout.String()).NotTo(ContainSubstring("403"))

Expect(stdout.String()).To(ContainSubstring("Listing Tier 1 Routers..."))
@@ -56,7 +56,7 @@ var _ = Describe("NSX-T", func() {
})

By("successfully deleting resources", func() {
err := deleter.Delete("leftover")
err := deleter.Delete("leftover", false)
Expect(err).NotTo(HaveOccurred())

Expect(stdout.String()).To(ContainSubstring("[Tier 1 Router: leftover-tier1-router] Deleting..."))
6 changes: 3 additions & 3 deletions acceptance/openstack_test.go
Original file line number Diff line number Diff line change
@@ -77,7 +77,7 @@ var _ = Describe("Openstack", func() {
})

It("lists resources", func() {
deleter.List("")
deleter.List("", false)
Expect(stdout.String()).To(ContainSubstring(fmt.Sprintf("[Volume: %s %s]", volumeName, volumeID)))
})
})
@@ -113,7 +113,7 @@ var _ = Describe("Openstack", func() {
})

It("deletes all resources", func() {
err := deleter.Delete("delete-all")
err := deleter.Delete("delete-all", false)
Expect(err).NotTo(HaveOccurred())

Expect(stdout.String()).To(ContainSubstring(fmt.Sprintf("[Compute Instance: %s %s] Deleted!", instanceName, instanceID)))
@@ -154,7 +154,7 @@ var _ = Describe("Openstack", func() {
})

It("deletes resources of a certain type", func() {
err := deleter.DeleteByType("delete-type", "Image")
err := deleter.DeleteByType("delete-type", "Image", false)
Expect(err).NotTo(HaveOccurred())

Expect(stdout.String()).To(ContainSubstring(fmt.Sprintf("[Image: %s %s] Deleting...", imageName, imageID)))
4 changes: 2 additions & 2 deletions acceptance/vsphere_test.go
Original file line number Diff line number Diff line change
@@ -60,7 +60,7 @@ var _ = Describe("vSphere", func() {

It("can list and delete resources with the filter", func() {
By("listing resources first", func() {
deleter.List(filter)
deleter.List(filter, false)

Expect(stdout.String()).To(ContainSubstring("[Virtual Machine: leftover-vm]"))
Expect(stdout.String()).To(ContainSubstring("[Virtual Machine: leftover-nested-vm]"))
@@ -72,7 +72,7 @@ var _ = Describe("vSphere", func() {
})

By("successfully deleting VMs", func() {
err := deleter.Delete(filter)
err := deleter.Delete(filter, false)
Expect(err).NotTo(HaveOccurred())

Expect(stdout.String()).To(ContainSubstring("[Virtual Machine: leftover-vm] Deleting..."))
13 changes: 7 additions & 6 deletions app/env_vars.go
Original file line number Diff line number Diff line change
@@ -5,12 +5,13 @@ import "os"
type Options struct {
Version bool `short:"v" long:"version" description:"Print version."`

IAAS string `short:"i" long:"iaas" env:"BBL_IAAS" description:"The IaaS for clean up." `
NoConfirm bool `short:"n" long:"no-confirm" description:"Destroy resources without prompting. This is dangerous, make good choices!"`
DryRun bool `short:"d" long:"dry-run" description:"List all resources without deleting any."`
Filter string `short:"f" long:"filter" description:"Filtering resources by an environment name."`
Type string `short:"t" long:"type" description:"Type of resource to delete."`
Debug bool ` long:"debug" description:"Print debug information."`
IAAS string `short:"i" long:"iaas" env:"BBL_IAAS" description:"The IaaS for clean up." `
NoConfirm bool `short:"n" long:"no-confirm" description:"Destroy resources without prompting. This is dangerous, make good choices!"`
DryRun bool `short:"d" long:"dry-run" description:"List all resources without deleting any."`
Filter string `short:"f" long:"filter" description:"Filtering resources by an environment name."`
RegexFiltered bool ` long:"filter-by-regex" description:"Filtering resources by interpreting the filter as a regex matcher."`
Type string `short:"t" long:"type" description:"Type of resource to delete."`
Debug bool ` long:"debug" description:"Print debug information."`

AWSAccessKeyID string `long:"aws-access-key-id" env:"BBL_AWS_ACCESS_KEY_ID" description:"AWS access key id."`
AWSSecretAccessKey string `long:"aws-secret-access-key" env:"BBL_AWS_SECRET_ACCESS_KEY" description:"AWS secret access key."`
7 changes: 3 additions & 4 deletions aws/ec2/addresses.go
Original file line number Diff line number Diff line change
@@ -2,9 +2,8 @@ package ec2

import (
"fmt"
"strings"

awsec2 "github.com/aws/aws-sdk-go/service/ec2"

"github.com/genevieve/leftovers/common"
)

@@ -26,7 +25,7 @@ func NewAddresses(client addressesClient, logger logger) Addresses {
}
}

func (d Addresses) List(filter string) ([]common.Deletable, error) {
func (d Addresses) List(filter string, regex bool) ([]common.Deletable, error) {
addresses, err := d.client.DescribeAddresses(&awsec2.DescribeAddressesInput{})
if err != nil {
return nil, fmt.Errorf("Describing EC2 Addresses: %s", err)
@@ -36,7 +35,7 @@ func (d Addresses) List(filter string) ([]common.Deletable, error) {
for _, a := range addresses.Addresses {
r := NewAddress(d.client, a.PublicIp, a.AllocationId, a.Tags)

if !strings.Contains(r.Name(), filter) {
if !common.MatchRegex(r.Name(), filter, regex) {
continue
}

8 changes: 4 additions & 4 deletions aws/ec2/addresses_test.go
Original file line number Diff line number Diff line change
@@ -42,7 +42,7 @@ var _ = Describe("Addresses", func() {
})

It("releases ec2 addresses", func() {
items, err := addresses.List(filter)
items, err := addresses.List(filter, false)
Expect(err).NotTo(HaveOccurred())

Expect(client.DescribeAddressesCall.CallCount).To(Equal(1))
@@ -54,7 +54,7 @@ var _ = Describe("Addresses", func() {

Context("when the address tags do not contain the filter", func() {
It("does not try releasing them", func() {
_, err := addresses.List("kiwi")
_, err := addresses.List("kiwi", false)
Expect(err).NotTo(HaveOccurred())

Expect(logger.PromptWithDetailsCall.CallCount).To(Equal(0))
@@ -67,7 +67,7 @@ var _ = Describe("Addresses", func() {
})

It("does not try releasing them", func() {
_, err := addresses.List(filter)
_, err := addresses.List(filter, false)
Expect(err).To(MatchError("Describing EC2 Addresses: some error"))
})
})
@@ -78,7 +78,7 @@ var _ = Describe("Addresses", func() {
})

It("does not release the address", func() {
items, err := addresses.List(filter)
items, err := addresses.List(filter, false)
Expect(err).NotTo(HaveOccurred())

Expect(logger.PromptWithDetailsCall.CallCount).To(Equal(1))
7 changes: 3 additions & 4 deletions aws/ec2/images.go
Original file line number Diff line number Diff line change
@@ -2,10 +2,9 @@ package ec2

import (
"fmt"
"strings"

awsec2 "github.com/aws/aws-sdk-go/service/ec2"
awssts "github.com/aws/aws-sdk-go/service/sts"

"github.com/genevieve/leftovers/common"
)

@@ -36,7 +35,7 @@ func NewImages(client imagesClient, stsClient stsClient, logger logger, resource
}
}

func (i Images) List(filter string) ([]common.Deletable, error) {
func (i Images) List(filter string, regex bool) ([]common.Deletable, error) {
caller, err := i.stsClient.GetCallerIdentity(&awssts.GetCallerIdentityInput{})
if err != nil {
return nil, fmt.Errorf("Get caller identity: %s", err)
@@ -53,7 +52,7 @@ func (i Images) List(filter string) ([]common.Deletable, error) {
for _, image := range images.Images {
r := NewImage(i.client, image.ImageId, i.resourceTags)

if !strings.Contains(r.Name(), filter) {
if !common.MatchRegex(r.Name(), filter, regex) {
continue
}

8 changes: 4 additions & 4 deletions aws/ec2/images_test.go
Original file line number Diff line number Diff line change
@@ -45,7 +45,7 @@ var _ = Describe("Images", func() {
})

It("returns a list of ec2 images to delete", func() {
items, err := images.List("")
items, err := images.List("", false)
Expect(err).NotTo(HaveOccurred())

Expect(client.DescribeImagesCall.CallCount).To(Equal(1))
@@ -64,7 +64,7 @@ var _ = Describe("Images", func() {
})

It("returns the error", func() {
_, err := images.List("")
_, err := images.List("", false)
Expect(err).To(MatchError("Describing EC2 Images: some error"))
})
})
@@ -75,7 +75,7 @@ var _ = Describe("Images", func() {
})

It("returns the error", func() {
_, err := images.List("")
_, err := images.List("", false)
Expect(err).To(MatchError("Get caller identity: some error"))
})
})
@@ -86,7 +86,7 @@ var _ = Describe("Images", func() {
})

It("does not return it to the list", func() {
items, err := images.List("")
items, err := images.List("", false)
Expect(err).NotTo(HaveOccurred())

Expect(logger.PromptWithDetailsCall.CallCount).To(Equal(1))
7 changes: 3 additions & 4 deletions aws/ec2/instances.go
Original file line number Diff line number Diff line change
@@ -2,10 +2,9 @@ package ec2

import (
"fmt"
"strings"

"github.com/aws/aws-sdk-go/aws"
awsec2 "github.com/aws/aws-sdk-go/service/ec2"

"github.com/genevieve/leftovers/common"
)

@@ -31,7 +30,7 @@ func NewInstances(client instancesClient, logger logger, resourceTags resourceTa
}
}

func (i Instances) List(filter string) ([]common.Deletable, error) {
func (i Instances) List(filter string, regex bool) ([]common.Deletable, error) {
instances, err := i.client.DescribeInstances(&awsec2.DescribeInstancesInput{
Filters: []*awsec2.Filter{{
Name: aws.String("instance-state-name"),
@@ -47,7 +46,7 @@ func (i Instances) List(filter string) ([]common.Deletable, error) {
for _, instance := range r.Instances {
r := NewInstance(i.client, i.logger, i.resourceTags, instance.InstanceId, instance.KeyName, instance.Tags)

if !strings.Contains(r.Name(), filter) {
if !common.MatchRegex(r.Name(), filter, regex) {
continue
}

Loading