From 5494771800d6e22eb6e19f8f6b37b808964107c5 Mon Sep 17 00:00:00 2001 From: Joel Baranick Date: Tue, 3 Sep 2019 09:07:32 -0700 Subject: [PATCH] Add support for filtering on region --- command/wipe.go | 4 +++- command/wrapped_main.go | 1 + resource/filter.go | 22 +++++++++++++++++++--- resource/resource.go | 3 ++- resource/resource_test.go | 7 ++++--- resource/supported.go | 1 + resource/supported_test.go | 1 + 7 files changed, 31 insertions(+), 8 deletions(-) diff --git a/command/wipe.go b/command/wipe.go index 20b50dd8a..e002759a2 100644 --- a/command/wipe.go +++ b/command/wipe.go @@ -24,6 +24,7 @@ type Wipe struct { UI cli.Ui dryRun bool forceDelete bool + region string client *resource.AWS provider *terraform.ResourceProvider filter *resource.Filter @@ -67,7 +68,7 @@ func (c *Wipe) Run(args []string) int { log.Fatal(err) } - deletableResources, err := resource.DeletableResources(resType, rawResources) + deletableResources, err := resource.DeletableResources(c.region, resType, rawResources) if err != nil { log.Fatal(err) } @@ -106,6 +107,7 @@ func printString(res resource.Resources) { for _, r := range res { printStat := fmt.Sprintf("\tId:\t\t%s", r.ID) + printStat += fmt.Sprintf("\n\tRegion:\t\t%s", r.Region) if r.Tags != nil { if len(r.Tags) > 0 { var keys []string diff --git a/command/wrapped_main.go b/command/wrapped_main.go index 158924efa..b83840873 100644 --- a/command/wrapped_main.go +++ b/command/wrapped_main.go @@ -85,6 +85,7 @@ func WrappedMain() int { }, client: client, provider: p, + region: *region, dryRun: *dryRunFlag, forceDelete: *forceDeleteFlag, outputType: *outputType, diff --git a/resource/filter.go b/resource/filter.go index d9c2f1f8f..666d241e6 100644 --- a/resource/filter.go +++ b/resource/filter.go @@ -26,8 +26,9 @@ type Config map[TerraformResourceType][]TypeFilter // TypeFilter represents an entry in Config and selects the resources of a particular resource type. type TypeFilter struct { - ID *StringFilter `yaml:",omitempty"` - Tags map[string]*StringFilter `yaml:",omitempty"` + ID *StringFilter `yaml:",omitempty"` + Region *StringFilter `yaml:",omitempty"` + Tags map[string]*StringFilter `yaml:",omitempty"` // select resources by creation time Created *Created `yaml:",omitempty"` } @@ -165,6 +166,21 @@ func (rtf TypeFilter) matchCreated(creationTime *time.Time) bool { return createdAfter && createdBefore } +func (rtf TypeFilter) matchRegion(region string) bool { + if rtf.Region == nil { + return true + } + + if ok, err := rtf.Region.matches(region); ok { + if err != nil { + log.Fatal(err) + } + return true + } + + return false +} + // matches checks whether a resource matches the filter criteria. func (f Filter) matches(r *Resource) bool { resTypeFilters, found := f.Cfg[r.Type] @@ -177,7 +193,7 @@ func (f Filter) matches(r *Resource) bool { } for _, rtf := range resTypeFilters { - if rtf.matchTags(r.Tags) && rtf.matchID(r.ID) && rtf.matchCreated(r.Created) { + if rtf.matchTags(r.Tags) && rtf.matchID(r.ID) && rtf.matchCreated(r.Created) && rtf.matchRegion(r.Region) { return true } } diff --git a/resource/resource.go b/resource/resource.go index ab97f1595..b46c26736 100644 --- a/resource/resource.go +++ b/resource/resource.go @@ -11,7 +11,7 @@ import ( // Resources converts given raw resources for a given resource type // into a format that can be deleted by the Terraform API. -func DeletableResources(resType TerraformResourceType, resources interface{}) (Resources, error) { +func DeletableResources(region string, resType TerraformResourceType, resources interface{}) (Resources, error) { deletableResources := Resources{} reflectResources := reflect.ValueOf(resources) @@ -51,6 +51,7 @@ func DeletableResources(resType TerraformResourceType, resources interface{}) (R deletableResources = append(deletableResources, &Resource{ Type: resType, ID: deleteIDField.Elem().String(), + Region: region, Tags: tags, Created: creationTime, }) diff --git a/resource/resource_test.go b/resource/resource_test.go index 1545e6f65..8f0984038 100644 --- a/resource/resource_test.go +++ b/resource/resource_test.go @@ -22,12 +22,13 @@ func TestDeletableResources(t *testing.T) { } // when - res, err := resource.DeletableResources(resource.AutoscalingGroup, rawResources) + res, err := resource.DeletableResources(testRegion, resource.AutoscalingGroup, rawResources) require.NoError(t, err) // then require.Len(t, res, 1) require.Equal(t, testAutoscalingGroupName, res[0].ID) + require.Equal(t, testRegion, res[0].Region) require.Equal(t, testTags, res[0].Tags) } @@ -42,7 +43,7 @@ func TestDeletableResources_Created(t *testing.T) { } // when - res, err := resource.DeletableResources(resource.Instance, rawResources) + res, err := resource.DeletableResources(testRegion, resource.Instance, rawResources) require.NoError(t, err) // then @@ -63,7 +64,7 @@ func TestDeletableResources_CreatedFieldIsTypeString(t *testing.T) { } // when - res, err := resource.DeletableResources(resource.Ami, rawResources) + res, err := resource.DeletableResources(testRegion, resource.Ami, rawResources) require.NoError(t, err) // then diff --git a/resource/supported.go b/resource/supported.go index a5d6476f4..10823b329 100644 --- a/resource/supported.go +++ b/resource/supported.go @@ -199,6 +199,7 @@ type Resource struct { // ID by which the resource can be deleted (in some cases the ID is the resource's name, but not always; // that's why we need the deleteIDs map) ID string + Region string Tags map[string]string Created *time.Time Attrs map[string]string diff --git a/resource/supported_test.go b/resource/supported_test.go index 06034770c..1e2523e45 100644 --- a/resource/supported_test.go +++ b/resource/supported_test.go @@ -37,6 +37,7 @@ var ( }, } + testRegion = "us-east-1" testAutoscalingGroupName = "test-auto-scaling-group" testTags = map[string]string{ "test-tag-key": "test-tag-value",