Skip to content
This repository has been archived by the owner on Oct 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #19 from rebuy-de/cloud-1047-simplify-multiple-reg…
Browse files Browse the repository at this point in the history
…ions

Cloud 1047 simplify multiple regions
  • Loading branch information
Florian Zeidler authored Apr 20, 2017
2 parents d58149b + 51060e6 commit 604543a
Show file tree
Hide file tree
Showing 40 changed files with 212 additions and 340 deletions.
3 changes: 2 additions & 1 deletion cmd/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ var (
ColorID = *color.New(color.Bold)
)

func Log(r resources.Resource, c color.Color, msg string) {
func Log(reg string, r resources.Resource, c color.Color, msg string) {
ColorID.Printf("'%s'", reg)
fmt.Print(resources.GetCategory(r))
fmt.Printf(" - ")
ColorID.Printf("'%s'", r.String())
Expand Down
13 changes: 7 additions & 6 deletions cmd/queue.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,24 @@ type Item struct {
Lister resources.ResourceLister

State ItemState
Region string
Reason string
}

func (i *Item) Print() {
switch i.State {
case ItemStateNew:
Log(i.Resource, ReasonWaitPending, "would remove")
Log(i.Region, i.Resource, ReasonWaitPending, "would remove")
case ItemStatePending:
Log(i.Resource, ReasonWaitPending, "triggered remove")
Log(i.Region, i.Resource, ReasonWaitPending, "triggered remove")
case ItemStateWaiting:
Log(i.Resource, ReasonWaitPending, "waiting")
Log(i.Region, i.Resource, ReasonWaitPending, "waiting")
case ItemStateFailed:
Log(i.Resource, ReasonError, i.Reason)
Log(i.Region, i.Resource, ReasonError, i.Reason)
case ItemStateFiltered:
Log(i.Resource, ReasonSkip, i.Reason)
Log(i.Region, i.Resource, ReasonSkip, i.Reason)
case ItemStateFinished:
Log(i.Resource, ReasonSuccess, "removed")
Log(i.Region, i.Resource, ReasonSuccess, "removed")
}
}

Expand Down
1 change: 1 addition & 0 deletions cmd/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func Scan(sess *session.Session) *Scanner {

for _, r := range r {
items <- &Item{
Region: *sess.Config.Region,
Resource: r,
Service: resources.GetCategory(r),
Lister: lister,
Expand Down
14 changes: 5 additions & 9 deletions resources/autoscaling-groups.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package resources

import (
"fmt"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/autoscaling"
)
Expand All @@ -17,18 +15,16 @@ func (n *AutoScalingNuke) ListGroups() ([]Resource, error) {
resources := make([]Resource, 0)
for _, asg := range resp.AutoScalingGroups {
resources = append(resources, &AutoScalingGroup{
svc: n.Service,
name: asg.AutoScalingGroupName,
region: n.Service.Config.Region,
svc: n.Service,
name: asg.AutoScalingGroupName,
})
}
return resources, nil
}

type AutoScalingGroup struct {
svc *autoscaling.AutoScaling
name *string
region *string
svc *autoscaling.AutoScaling
name *string
}

func (asg *AutoScalingGroup) Remove() error {
Expand All @@ -46,5 +42,5 @@ func (asg *AutoScalingGroup) Remove() error {
}

func (asg *AutoScalingGroup) String() string {
return fmt.Sprintf("%s in %s ", *asg.name, *asg.region)
return *asg.name
}
18 changes: 6 additions & 12 deletions resources/cloudformation-stack.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package resources

import (
"fmt"

"github.com/aws/aws-sdk-go/service/cloudformation"
)
import "github.com/aws/aws-sdk-go/service/cloudformation"

func (n *CloudFormationNuke) ListStacks() ([]Resource, error) {
resp, err := n.Service.DescribeStacks(nil)
Expand All @@ -15,18 +11,16 @@ func (n *CloudFormationNuke) ListStacks() ([]Resource, error) {
resources := make([]Resource, 0)
for _, stack := range resp.Stacks {
resources = append(resources, &CloudFormationStack{
svc: n.Service,
name: stack.StackName,
region: n.Service.Config.Region,
svc: n.Service,
name: stack.StackName,
})
}
return resources, nil
}

type CloudFormationStack struct {
svc *cloudformation.CloudFormation
name *string
region *string
svc *cloudformation.CloudFormation
name *string
}

func (cfs *CloudFormationStack) Remove() error {
Expand All @@ -37,5 +31,5 @@ func (cfs *CloudFormationStack) Remove() error {
}

func (csf *CloudFormationStack) String() string {
return fmt.Sprintf("%s in %s", *csf.name, *csf.region)
return *csf.name
}
12 changes: 5 additions & 7 deletions resources/cloudwatchevents-rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,17 @@ func (n *CloudWatchEventsNuke) ListRules() ([]Resource, error) {
resources := make([]Resource, 0)
for _, rule := range resp.Rules {
resources = append(resources, &CloudWatchEventsRule{
svc: n.Service,
name: rule.Name,
region: n.Service.Config.Region,
svc: n.Service,
name: rule.Name,
})

}
return resources, nil
}

type CloudWatchEventsRule struct {
svc *cloudwatchevents.CloudWatchEvents
name *string
region *string
svc *cloudwatchevents.CloudWatchEvents
name *string
}

func (rule *CloudWatchEventsRule) Remove() error {
Expand All @@ -37,5 +35,5 @@ func (rule *CloudWatchEventsRule) Remove() error {
}

func (rule *CloudWatchEventsRule) String() string {
return fmt.Sprintf("%s in %s", *rule.name, *rule.region)
return fmt.Sprintf("Rule: %s", *rule.name)
}
4 changes: 1 addition & 3 deletions resources/cloudwatchevents-targets.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ func (n *CloudWatchEventsNuke) ListTargets() ([]Resource, error) {
svc: n.Service,
ruleName: rule.Name,
targetId: target.Id,
region: n.Service.Config.Region,
})
}
}
Expand All @@ -36,7 +35,6 @@ type CloudWatchEventsTarget struct {
svc *cloudwatchevents.CloudWatchEvents
targetId *string
ruleName *string
region *string
}

func (target *CloudWatchEventsTarget) Remove() error {
Expand All @@ -49,5 +47,5 @@ func (target *CloudWatchEventsTarget) Remove() error {
}

func (target *CloudWatchEventsTarget) String() string {
return fmt.Sprintf("Rule: %s Target ID: %s in %s", *target.ruleName, *target.targetId, *target.region)
return fmt.Sprintf("Rule: %s Target ID: %s", *target.ruleName, *target.targetId)
}
16 changes: 7 additions & 9 deletions resources/ec2-customer-gateways.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import (
)

type EC2CustomerGateway struct {
svc *ec2.EC2
id string
state string
region string
svc *ec2.EC2
id string
state string
}

func (n *EC2Nuke) ListCustomerGateways() ([]Resource, error) {
Expand All @@ -23,10 +22,9 @@ func (n *EC2Nuke) ListCustomerGateways() ([]Resource, error) {
resources := make([]Resource, 0)
for _, out := range resp.CustomerGateways {
resources = append(resources, &EC2CustomerGateway{
svc: n.Service,
id: *out.CustomerGatewayId,
state: *out.State,
region: *n.Service.Config.Region,
svc: n.Service,
id: *out.CustomerGatewayId,
state: *out.State,
})
}

Expand Down Expand Up @@ -54,5 +52,5 @@ func (e *EC2CustomerGateway) Remove() error {
}

func (e *EC2CustomerGateway) String() string {
return fmt.Sprintf("%s in %s", e.id, e.region)
return e.id
}
18 changes: 6 additions & 12 deletions resources/ec2-dhcp-options.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
package resources

import (
"fmt"

"github.com/aws/aws-sdk-go/service/ec2"
)
import "github.com/aws/aws-sdk-go/service/ec2"

type EC2DhcpOption struct {
svc *ec2.EC2
id *string
region *string
svc *ec2.EC2
id *string
}

func (n *EC2Nuke) ListDhcpOptions() ([]Resource, error) {
Expand All @@ -22,9 +17,8 @@ func (n *EC2Nuke) ListDhcpOptions() ([]Resource, error) {
for _, out := range resp.DhcpOptions {

resources = append(resources, &EC2DhcpOption{
svc: n.Service,
id: out.DhcpOptionsId,
region: n.Service.Config.Region,
svc: n.Service,
id: out.DhcpOptionsId,
})
}

Expand All @@ -45,5 +39,5 @@ func (e *EC2DhcpOption) Remove() error {
}

func (e *EC2DhcpOption) String() string {
return fmt.Sprintf("%s in %s", *e.id, *e.region)
return *e.id
}
22 changes: 8 additions & 14 deletions resources/ec2-eip.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
package resources

import (
"fmt"

"github.com/aws/aws-sdk-go/service/ec2"
)
import "github.com/aws/aws-sdk-go/service/ec2"

type EC2Address struct {
svc *ec2.EC2
id string
ip string
region string
svc *ec2.EC2
id string
ip string
}

func (n *EC2Nuke) ListAddresses() ([]Resource, error) {
Expand All @@ -23,10 +18,9 @@ func (n *EC2Nuke) ListAddresses() ([]Resource, error) {
resources := make([]Resource, 0)
for _, out := range resp.Addresses {
resources = append(resources, &EC2Address{
svc: n.Service,
id: *out.AllocationId,
ip: *out.PublicIp,
region: *n.Service.Config.Region,
svc: n.Service,
id: *out.AllocationId,
ip: *out.PublicIp,
})
}

Expand All @@ -45,5 +39,5 @@ func (e *EC2Address) Remove() error {
}

func (e *EC2Address) String() string {
return fmt.Sprintf("%s in %s", e.ip, e.region)
return e.ip
}
16 changes: 7 additions & 9 deletions resources/ec2-instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import (
)

type EC2Instance struct {
svc *ec2.EC2
id *string
state string
region *string
svc *ec2.EC2
id *string
state string
}

func (n *EC2Nuke) ListInstances() ([]Resource, error) {
Expand All @@ -24,10 +23,9 @@ func (n *EC2Nuke) ListInstances() ([]Resource, error) {
for _, reservation := range resp.Reservations {
for _, instance := range reservation.Instances {
resources = append(resources, &EC2Instance{
svc: n.Service,
id: instance.InstanceId,
state: *instance.State.Name,
region: n.Service.Config.Region,
svc: n.Service,
id: instance.InstanceId,
state: *instance.State.Name,
})
}
}
Expand Down Expand Up @@ -56,5 +54,5 @@ func (i *EC2Instance) Remove() error {
}

func (i *EC2Instance) String() string {
return fmt.Sprintf("%s in %s", *i.id, *i.region)
return *i.id
}
16 changes: 7 additions & 9 deletions resources/ec2-internet-gateway-attachements.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import (
)

type EC2InternetGatewayAttachement struct {
svc *ec2.EC2
vpcId *string
igwId *string
region *string
svc *ec2.EC2
vpcId *string
igwId *string
}

func (n *EC2Nuke) ListInternetGatewayAttachements() ([]Resource, error) {
Expand All @@ -38,10 +37,9 @@ func (n *EC2Nuke) ListInternetGatewayAttachements() ([]Resource, error) {

for _, out := range resp.InternetGateways {
resources = append(resources, &EC2InternetGatewayAttachement{
svc: n.Service,
vpcId: vpc.VpcId,
igwId: out.InternetGatewayId,
region: n.Service.Config.Region,
svc: n.Service,
vpcId: vpc.VpcId,
igwId: out.InternetGatewayId,
})
}
}
Expand All @@ -64,5 +62,5 @@ func (e *EC2InternetGatewayAttachement) Remove() error {
}

func (e *EC2InternetGatewayAttachement) String() string {
return fmt.Sprintf("%s -> %s in %s", *e.igwId, *e.vpcId, *e.region)
return fmt.Sprintf("%s -> %s", *e.igwId, *e.vpcId)
}
Loading

0 comments on commit 604543a

Please sign in to comment.