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
Show file tree
Hide file tree
Changes from all commits
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
9 changes: 9 additions & 0 deletions .github/tasks/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env bash

set -eux

function main() {
./scripts/test
}

main "$@"
23 changes: 23 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: CI

on:
push:
branches:
- feature/regex-matching

jobs:
unit-tests:
container:
image: pivotalcfreleng/golang

timeout-minutes: 90
runs-on: ubuntu-latest
defaults:
run:
shell: bash

steps:
- uses: actions/checkout@v2
- id: run_unit_tests
name: Run unit tests
run: .github/tasks/test.sh
30 changes: 17 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# This README is for the github.com/notrepo05/leftovers fork from github.com/genevieve/leftovers

### This fork aims to stabilize deletion (so far for AWS) and add extended regex filtering support.

# Leftovers :turkey:

[![GoDoc](https://godoc.org/github.com/genevieve/leftovers?status.svg)](https://godoc.org/github.com/genevieve/leftovers)
Expand All @@ -18,6 +22,7 @@ Go cli & library for cleaning up **orphaned IaaS resources**.
- Your acceptance tests in CI failed, the container disappeared, and
infrastructure resources were tragically orphaned. :-(
- `terraform destroy` isn't working because the refresh step is failing due to a missing resource.
- You're too lazy to `terraform destroy`



Expand Down Expand Up @@ -49,7 +54,7 @@ $ leftovers --filter banana --dry-run
```


Finally, you might want to delete a single resource type::
You might want to delete a single resource type::
```console
$ leftovers types
service-account
Expand All @@ -59,34 +64,31 @@ $ leftovers --filter banana --type service-account --no-confirm
[Service Account: banana@pivotal.io] Deleted!
```


Or you might want to filter using regex (this is a beta feature)
```console
$ BBL_IAAS=gcp leftovers --gcp-service-account-key="gcp-service-account.json" --filter '^(?=.*pull-1234)(?!.*iless-).*' --filter-as-regex --dry-run
[Compute Instance: vm-137e7129-aaa-bbb-cccc-e3ddb3aaec23 (pull-1234-pcf-network, cf-1234567890abc1234567, cf-1234567890abc1234567-control, control, p-bosh, p-bosh-cf-1234567890abc1234567, p-bosh-cf-1234567890abc1234567-control, pcf-lb, pull-1234-cf-ssh, pull-1234-vms)]
```

## <a name='how'></a>Installation

### Option 1
[Install go.](https://golang.org/doc/install) Then:

```console
$ go get -u github.com/genevieve/leftovers/cmd/leftovers
$ go get -u github.com/notrepo05/leftovers/cmd/leftovers
```

### Option 2

```console
$ brew tap genevieve/tap
$ brew install leftovers
```

### Option 3

Linux binaries can be found on the [releases page](https://github.com/genevieve/leftovers/releases).
Linux and OSX binaries for this fork can be found on the [releases page](https://github.com/notrepo05/leftovers/releases).



## <a name='how'></a>Usage

```console
$ leftovers -h
$ ./leftovers --help

Usage:
leftovers [OPTIONS]
Expand All @@ -97,7 +99,9 @@ Application Options:
-n, --no-confirm Destroy resources without prompting. This is dangerous, make good choices!
-d, --dry-run List all resources without deleting any.
-f, --filter= Filtering resources by an environment name.
--filter-as-regex Interpret the filter as regex. (**This is a beta feature** Tested on AWS, Azure, and GCP)
-t, --type= Type of resource to delete.
--debug Print debug information.
--aws-access-key-id= AWS access key id. [$BBL_AWS_ACCESS_KEY_ID]
--aws-secret-access-key= AWS secret access key. [$BBL_AWS_SECRET_ACCESS_KEY]
--aws-session-token= AWS session token. [$BBL_AWS_SESSION_TOKEN]
Expand Down Expand Up @@ -125,7 +129,7 @@ Help Options:
-h, --help Show this help message
```

## <a name='maintainers'></a>Maintainers
## <a name='maintainers'></a>Maintainers of the actual project

- [Genevieve L'Esperance](https://twitter.com/genevieve_vl)
- [Rowan Jacobs](https://github.com/rowanjacobs)
42 changes: 36 additions & 6 deletions acceptance/aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var _ = Describe("AWS", func() {

stdout *bytes.Buffer
filter string
regexFilter string
deleter aws.Leftovers
)

Expand All @@ -45,16 +46,17 @@ var _ = Describe("AWS", func() {
Describe("List", func() {
BeforeEach(func() {
filter = "leftovers-acc-list-all"
regexFilter = "leftovers-acc-lis[t]{1}-a[l]{2}$"
acc.CreateKeyPair(filter)
})

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))
Expand All @@ -65,16 +67,25 @@ var _ = Describe("AWS", func() {
Describe("ListByType", func() {
BeforeEach(func() {
filter = "leftovers-acc-list-by-type"
regexFilter = "leftovers-acc-lis[t]{1}-b[y]{1}-type$"
acc.CreateKeyPair(filter)
})

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))
Expect(stdout.String()).NotTo(ContainSubstring("[EC2 Key Pair: %s] Deleted!", filter))
})

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

Expect(stdout.String()).To(ContainSubstring("[EC2 Key Pair: %s]", filter))
Expect(stdout.String()).NotTo(ContainSubstring("[EC2 Key Pair: %s] Deleting...", filter))
Expand All @@ -93,11 +104,21 @@ var _ = Describe("AWS", func() {
Describe("Delete", func() {
BeforeEach(func() {
filter = "leftovers-acc-delete-all"
regexFilter = "leftovers-acc-dele[t]{1}e-a[l]{2}$"
acc.CreateKeyPair(filter)
})

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))
Expect(stdout.String()).To(ContainSubstring("[EC2 Key Pair: %s] Deleted!", filter))
})


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

Expect(stdout.String()).To(ContainSubstring("[EC2 Key Pair: %s] Deleting...", filter))
Expand All @@ -108,11 +129,20 @@ var _ = Describe("AWS", func() {
Describe("DeleteByType", func() {
BeforeEach(func() {
filter = "leftovers-acc-delete-type"
regexFilter = "leftovers-acc-dele[t]{1}e-type"
acc.CreateKeyPair(filter)
})

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))
Expect(stdout.String()).To(ContainSubstring("[EC2 Key Pair: %s] Deleted!", filter))
})

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

Expect(stdout.String()).To(ContainSubstring("[EC2 Key Pair: %s] Deleting...", filter))
Expand Down
27 changes: 23 additions & 4 deletions acceptance/azure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ var _ = Describe("Azure", func() {
stdout *bytes.Buffer
filter string
deleter azure.Leftovers
regexFilter string
)

BeforeEach(func() {
Expand All @@ -44,16 +45,25 @@ var _ = Describe("Azure", func() {
Describe("List", func() {
BeforeEach(func() {
filter = "leftovers-acc-list"
regexFilter = "leftovers-acc-lis[t]{1}"
acc.CreateResourceGroup(filter)
})

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..."))
Expect(stdout.String()).NotTo(ContainSubstring("[Resource Group: %s] Deleting...", filter))
})

It("lists resources without deleting filtered by regex", func() {
deleter.List(regexFilter, true)

Expect(stdout.String()).To(ContainSubstring("[Resource Group: %s]", filter))
Expect(stdout.String()).To(ContainSubstring("Listing Resource Groups..."))
Expand All @@ -72,11 +82,20 @@ var _ = Describe("Azure", func() {
Describe("Delete", func() {
BeforeEach(func() {
filter = "leftovers-acc-delete"
regexFilter = "leftovers-acc-dele[t]{1}e"
acc.CreateResourceGroup(filter)
})

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))
Expect(stdout.String()).To(ContainSubstring("[Resource Group: %s] Deleted!", filter))
})

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

Expect(stdout.String()).To(ContainSubstring("[Resource Group: %s] Deleting...", filter))
Expand All @@ -91,7 +110,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))
Expand Down
Loading