-
-
Notifications
You must be signed in to change notification settings - Fork 359
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
support deleting vpcs that are non-default (#243)
- Loading branch information
Showing
4 changed files
with
115 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package aws | ||
|
||
import ( | ||
awsgo "github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
"github.com/gruntwork-io/cloud-nuke/logging" | ||
"github.com/gruntwork-io/go-commons/errors" | ||
"github.com/hashicorp/go-multierror" | ||
) | ||
|
||
func getAllVpcs(session *session.Session, region string) ([]*string, []Vpc, error) { | ||
svc := ec2.New(session) | ||
|
||
result, err := svc.DescribeVpcs(&ec2.DescribeVpcsInput{ | ||
Filters: []*ec2.Filter{ | ||
// Note: this filter omits the default since there is special | ||
// handling for default resources already | ||
{ | ||
Name: awsgo.String("is-default"), | ||
Values: awsgo.StringSlice([]string{"false"}), | ||
}, | ||
}, | ||
}) | ||
if err != nil { | ||
return nil, nil, errors.WithStackTrace(err) | ||
} | ||
|
||
var ids []*string | ||
var vpcs []Vpc | ||
for _, vpc := range result.Vpcs { | ||
ids = append(ids, vpc.VpcId) | ||
|
||
vpcs = append(vpcs, Vpc{ | ||
VpcId: *vpc.VpcId, | ||
Region: region, | ||
svc: svc, | ||
}) | ||
} | ||
|
||
return ids, vpcs, nil | ||
} | ||
|
||
func nukeAllVPCs(session *session.Session, vpcIds []string, vpcs []Vpc) error { | ||
if len(vpcIds) == 0 { | ||
logging.Logger.Info("No VPCs to nuke") | ||
return nil | ||
} | ||
|
||
logging.Logger.Info("Deleting all VPCs") | ||
|
||
deletedVPCs := 0 | ||
multiErr := new(multierror.Error) | ||
|
||
for _, vpc := range vpcs { | ||
if err := vpc.nuke(); err != nil { | ||
logging.Logger.Errorf("[Failed] %s", err) | ||
multierror.Append(multiErr, err) | ||
} else { | ||
deletedVPCs++ | ||
logging.Logger.Infof("Deleted VPC: %s", vpc.VpcId) | ||
} | ||
} | ||
|
||
logging.Logger.Infof("[OK] %d VPC terminated", deletedVPCs) | ||
|
||
return nil | ||
} |