Skip to content

Commit

Permalink
Delete a single type of resource for gcp.
Browse files Browse the repository at this point in the history
  • Loading branch information
Genevieve LEsperance committed Jun 20, 2018
1 parent 381d174 commit 7e2a9a7
Showing 1 changed file with 89 additions and 69 deletions.
158 changes: 89 additions & 69 deletions gcp/leftovers.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,75 +36,6 @@ type Leftovers struct {
resources []resource
}

func (l Leftovers) List(filter string) {
l.logger.NoConfirm()

var deletables []common.Deletable

for _, r := range l.resources {
list, err := r.List(filter)
if err != nil {
l.logger.Println(color.YellowString(err.Error()))
}

deletables = append(deletables, list...)
}

for _, d := range deletables {
l.logger.Println(fmt.Sprintf("[%s: %s]", d.Type(), d.Name()))
}
}

// Types will print all of the resource types that can be deleted.
func (l Leftovers) Types() {
l.logger.NoConfirm()

for _, r := range l.resources {
l.logger.Println(r.Type())
}
}

func (l Leftovers) Delete(filter string) error {
deletables := [][]common.Deletable{}

for _, r := range l.resources {
list, err := r.List(filter)
if err != nil {
l.logger.Println(color.YellowString(err.Error()))
}

deletables = append(deletables, list)
}

var wg sync.WaitGroup

for _, list := range deletables {
for _, d := range list {
wg.Add(1)

go func(d common.Deletable) {
defer wg.Done()

l.logger.Println(fmt.Sprintf("[%s: %s] Deleting...", d.Type(), d.Name()))

if err := d.Delete(); err != nil {
l.logger.Println(fmt.Sprintf("[%s: %s] %s", d.Type(), d.Name(), color.YellowString(err.Error())))
} else {
l.logger.Println(fmt.Sprintf("[%s: %s] %s", d.Type(), d.Name(), color.GreenString("Deleted!")))
}
}(d)
}

wg.Wait()
}

return nil
}

func (l Leftovers) DeleteType(filter, rType string) error {
return l.Delete(filter)
}

func NewLeftovers(logger logger, keyPath string) (Leftovers, error) {
if keyPath == "" {
return Leftovers{}, errors.New("Missing service account key path.")
Expand Down Expand Up @@ -210,3 +141,92 @@ func NewLeftovers(logger logger, keyPath string) (Leftovers, error) {
},
}, nil
}

// List will print all of the resources that match the provided filter.
func (l Leftovers) List(filter string) {
l.logger.NoConfirm()

var deletables []common.Deletable

for _, r := range l.resources {
list, err := r.List(filter)
if err != nil {
l.logger.Println(color.YellowString(err.Error()))
}

deletables = append(deletables, list...)
}

for _, d := range deletables {
l.logger.Println(fmt.Sprintf("[%s: %s]", d.Type(), d.Name()))
}
}

// Types will print all of the resource types that can be deleted.
func (l Leftovers) Types() {
l.logger.NoConfirm()

for _, r := range l.resources {
l.logger.Println(r.Type())
}
}

func (l Leftovers) Delete(filter string) error {
deletables := [][]common.Deletable{}

for _, r := range l.resources {
list, err := r.List(filter)
if err != nil {
l.logger.Println(color.YellowString(err.Error()))
}

deletables = append(deletables, list)
}

l.asyncDelete(deletables)

return nil
}

func (l Leftovers) DeleteType(filter, rType string) error {
deletables := [][]common.Deletable{}

for _, r := range l.resources {
if r.Type() == rType {
list, err := r.List(filter)
if err != nil {
l.logger.Println(color.YellowString(err.Error()))
}

deletables = append(deletables, list)
}
}

l.asyncDelete(deletables)

return nil
}

func (l Leftovers) asyncDelete(deletables [][]common.Deletable) {
var wg sync.WaitGroup

for _, list := range deletables {
for _, d := range list {
wg.Add(1)

go func(d common.Deletable) {
defer wg.Done()

l.logger.Println(fmt.Sprintf("[%s: %s] Deleting...", d.Type(), d.Name()))

if err := d.Delete(); err != nil {
l.logger.Println(fmt.Sprintf("[%s: %s] %s", d.Type(), d.Name(), color.YellowString(err.Error())))
} else {
l.logger.Println(fmt.Sprintf("[%s: %s] %s", d.Type(), d.Name(), color.GreenString("Deleted!")))
}
}(d)
}

wg.Wait()
}
}

0 comments on commit 7e2a9a7

Please sign in to comment.