-
-
Notifications
You must be signed in to change notification settings - Fork 357
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor the code structure of aws directory (#629)
- Loading branch information
1 parent
876c0ad
commit 5c9b6fc
Showing
7 changed files
with
233 additions
and
223 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package aws | ||
|
||
import "fmt" | ||
|
||
type CouldNotSelectRegionError struct { | ||
Underlying error | ||
} | ||
|
||
func (err CouldNotSelectRegionError) Error() string { | ||
return fmt.Sprintf("Unable to determine target region set. Please double check your combination of target and excluded regions. Original error: %v", err.Underlying) | ||
} | ||
|
||
type CouldNotDetermineEnabledRegionsError struct { | ||
Underlying error | ||
} | ||
|
||
func (err CouldNotDetermineEnabledRegionsError) Error() string { | ||
return fmt.Sprintf("Unable to determine enabled regions in target account. Original error: %v", err.Underlying) | ||
} | ||
|
||
type InvalidResourceTypesSuppliedError struct { | ||
InvalidTypes []string | ||
} | ||
|
||
func (err InvalidResourceTypesSuppliedError) Error() string { | ||
return fmt.Sprintf("Invalid resourceTypes %s specified: %s", err.InvalidTypes, "Try --list-resource-types to get a list of valid resource types.") | ||
} | ||
|
||
type ResourceTypeAndExcludeFlagsBothPassedError struct{} | ||
|
||
func (err ResourceTypeAndExcludeFlagsBothPassedError) Error() string { | ||
return "You can not specify both --resource-type and --exclude-resource-type" | ||
} | ||
|
||
type InvalidTimeStringPassedError struct { | ||
Entry string | ||
Underlying error | ||
} | ||
|
||
func (err InvalidTimeStringPassedError) Error() string { | ||
return fmt.Sprintf("Could not parse %s as a valid time duration. Underlying error: %s", err.Entry, err.Underlying) | ||
} | ||
|
||
type QueryCreationError struct { | ||
Underlying error | ||
} | ||
|
||
func (err QueryCreationError) Error() string { | ||
return fmt.Sprintf("Error forming a cloud-nuke Query with supplied parameters. Original error: %v", err.Underlying) | ||
} | ||
|
||
type ResourceInspectionError struct { | ||
Underlying error | ||
} | ||
|
||
func (err ResourceInspectionError) Error() string { | ||
return fmt.Sprintf("Error encountered when querying for account resources. Original error: %v", err.Underlying) | ||
} |
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,65 @@ | ||
package aws | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
// Query is a struct that represents the desired parameters for scanning resources within a given account | ||
type Query struct { | ||
Regions []string | ||
ExcludeRegions []string | ||
ResourceTypes []string | ||
ExcludeResourceTypes []string | ||
ExcludeAfter *time.Time | ||
IncludeAfter *time.Time | ||
ListUnaliasedKMSKeys bool | ||
} | ||
|
||
// NewQuery configures and returns a Query struct that can be passed into the InspectResources method | ||
func NewQuery(regions, excludeRegions, resourceTypes, excludeResourceTypes []string, excludeAfter, includeAfter *time.Time, listUnaliasedKMSKeys bool) (*Query, error) { | ||
q := &Query{ | ||
Regions: regions, | ||
ExcludeRegions: excludeRegions, | ||
ResourceTypes: resourceTypes, | ||
ExcludeResourceTypes: excludeResourceTypes, | ||
ExcludeAfter: excludeAfter, | ||
IncludeAfter: includeAfter, | ||
ListUnaliasedKMSKeys: listUnaliasedKMSKeys, | ||
} | ||
|
||
validationErr := q.Validate() | ||
|
||
if validationErr != nil { | ||
return q, validationErr | ||
} | ||
|
||
return q, nil | ||
} | ||
|
||
// Validate ensures the configured values for a Query are valid, returning an error if there are | ||
// any invalid params, or nil if the Query is valid | ||
func (q *Query) Validate() error { | ||
resourceTypes, err := HandleResourceTypeSelections(q.ResourceTypes, q.ExcludeResourceTypes) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
q.ResourceTypes = resourceTypes | ||
|
||
regions, err := GetEnabledRegions() | ||
if err != nil { | ||
return CouldNotDetermineEnabledRegionsError{Underlying: err} | ||
} | ||
|
||
// global is a fake region, used to represent global resources | ||
regions = append(regions, GlobalRegion) | ||
|
||
targetRegions, err := GetTargetRegions(regions, q.Regions, q.ExcludeRegions) | ||
if err != nil { | ||
return CouldNotSelectRegionError{Underlying: err} | ||
} | ||
|
||
q.Regions = targetRegions | ||
|
||
return nil | ||
} |
File renamed without changes.
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,91 @@ | ||
package aws | ||
|
||
import ( | ||
"context" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/gruntwork-io/cloud-nuke/config" | ||
"strings" | ||
) | ||
|
||
// AwsResource is an interface that represents a single AWS resource | ||
type AwsResource interface { | ||
Init(session *session.Session) | ||
ResourceName() string | ||
ResourceIdentifiers() []string | ||
MaxBatchSize() int | ||
Nuke(identifiers []string) error | ||
GetAndSetIdentifiers(c context.Context, configObj config.Config) ([]string, error) | ||
} | ||
|
||
// AwsResources is a struct to hold multiple instances of AwsResource. | ||
type AwsResources struct { | ||
Resources []*AwsResource | ||
} | ||
|
||
// AwsAccountResources is a struct that represents the resources found in a single AWS account | ||
type AwsAccountResources struct { | ||
Resources map[string]AwsResources | ||
} | ||
|
||
func (a *AwsAccountResources) GetRegion(region string) AwsResources { | ||
if val, ok := a.Resources[region]; ok { | ||
return val | ||
} | ||
return AwsResources{} | ||
} | ||
|
||
// TotalResourceCount returns the number of resources found, that are eligible for nuking, across all AWS regions targeted | ||
// In other words, if you have 3 nukeable resources in us-east-1 and 4 nukeable resources in ap-southeast-1, this function | ||
// would return 7 | ||
func (a *AwsAccountResources) TotalResourceCount() int { | ||
total := 0 | ||
for _, regionResource := range a.Resources { | ||
for _, resource := range regionResource.Resources { | ||
total += len((*resource).ResourceIdentifiers()) | ||
} | ||
} | ||
return total | ||
} | ||
|
||
// MapResourceTypeToIdentifiers converts a slice of Resources to a map of resource types to their found identifiers | ||
// For example: ["ec2"] = ["i-0b22a22eec53b9321", "i-0e22a22yec53b9456"] | ||
func (arr *AwsResources) MapResourceTypeToIdentifiers() map[string][]string { | ||
// Initialize map of resource name to identifier, e.g., ["ec2"] = "i-0b22a22eec53b9321" | ||
m := make(map[string][]string) | ||
for _, resource := range arr.Resources { | ||
if len((*resource).ResourceIdentifiers()) > 0 { | ||
for _, id := range (*resource).ResourceIdentifiers() { | ||
m[(*resource).ResourceName()] = append(m[(*resource).ResourceName()], id) | ||
} | ||
} | ||
} | ||
return m | ||
} | ||
|
||
// CountOfResourceType is a convenience method that returns the number of the supplied resource type found | ||
// in the AwsResources | ||
func (arr *AwsResources) CountOfResourceType(resourceType string) int { | ||
idMap := arr.MapResourceTypeToIdentifiers() | ||
resourceType = strings.ToLower(resourceType) | ||
if val, ok := idMap[resourceType]; ok { | ||
return len(val) | ||
} | ||
return 0 | ||
} | ||
|
||
// ResourceTypePresent is a convenience method that returns true, if the given resource is found in the AwsResources, | ||
// or false if it is not | ||
func (arr *AwsResources) ResourceTypePresent(resourceType string) bool { | ||
return arr.CountOfResourceType(resourceType) > 0 | ||
} | ||
|
||
// IdentifiersForResourceType is a convenience method that returns the list of resource identifiers for a given | ||
// resource type, if available | ||
func (arr *AwsResources) IdentifiersForResourceType(resourceType string) []string { | ||
idMap := arr.MapResourceTypeToIdentifiers() | ||
resourceType = strings.ToLower(resourceType) | ||
if val, ok := idMap[resourceType]; ok { | ||
return val | ||
} | ||
return []string{} | ||
} |
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
Oops, something went wrong.