diff --git a/Makefile b/Makefile index cb3c0181..4fba8efa 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ PKG_LIST := $(shell go list ./...) .PHONY: setup setup: ## Install build, test, and lint dependencies - curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s v1.21.0 + curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | sh -s v1.29.0 go install github.com/golang/mock/mockgen curl -sSfL https://raw.githubusercontent.com/jckuester/go-acc/master/install.sh | sh -s v0.2.1 diff --git a/main.go b/main.go index 93b6ad4c..4320ae22 100644 --- a/main.go +++ b/main.go @@ -42,8 +42,8 @@ func mainExitCode() int { flags.StringVar(&outputType, "output", "string", "The type of output result (String, JSON or YAML)") flags.BoolVar(&dryRun, "dry-run", false, "Don't delete anything, just show what would be deleted") flags.BoolVar(&logDebug, "debug", false, "Enable debug logging") - flags.StringVar(&profile, "profile", "", "The AWS profile for the account to delete resources in") - flags.StringVar(®ion, "region", "", "The region to delete resources in") + flags.StringVarP(&profile, "profile", "p", "", "The AWS profile for the account to delete resources in") + flags.StringVarP(®ion, "region", "r", "", "The region to delete resources in") flags.IntVar(¶llel, "parallel", 10, "Limit the number of concurrent delete operations") flags.BoolVar(&version, "version", false, "Show application version") flags.BoolVar(&force, "force", false, "Delete without asking for confirmation") @@ -104,17 +104,12 @@ func mainExitCode() int { return 1 } - if profile != "" { - err := os.Setenv("AWS_PROFILE", profile) - if err != nil { - log.WithError(err).Error("failed to set AWS profile") - } - } - var profiles []string var regions []string if profile != "" { + profiles = []string{profile} + } else { env, ok := os.LookupEnv("AWS_PROFILE") if ok { profiles = []string{env} diff --git a/pkg/resource/list.go b/pkg/resource/list.go index 9c04a600..fc61dde3 100644 --- a/pkg/resource/list.go +++ b/pkg/resource/list.go @@ -39,7 +39,7 @@ func List(filter *Filter, clients map[util.AWSClientKey]awsls.Client, log.WithError(err).Fatal("failed to get raw resources") } - deletableResources, err := DeletableResources(rType, rawResources) + deletableResources, err := DeletableResources(rType, rawResources, client) if err != nil { log.WithError(err).Fatal("failed to convert raw resources into deletable resources") } @@ -229,7 +229,7 @@ func getEfsMountTargets(efsFileSystems []awsls.Resource, client awsls.Client, var result []awsls.Resource for _, fs := range efsFileSystems { - // TODO result is paginated, but not there paginator API function + // TODO result is paginated, but there is no paginator API function req := client.Efsconn.DescribeMountTargetsRequest(&efs.DescribeMountTargetsInput{ FileSystemId: &fs.ID, }) diff --git a/pkg/resource/resource.go b/pkg/resource/resource.go index 2774c095..2afcaa1c 100644 --- a/pkg/resource/resource.go +++ b/pkg/resource/resource.go @@ -4,14 +4,13 @@ import ( "reflect" "time" - "github.com/apex/log" awsls "github.com/jckuester/awsls/aws" "github.com/pkg/errors" ) // Resources converts given raw resources for a given resource type // into a format that can be deleted by the Terraform API. -func DeletableResources(resType string, resources interface{}) ([]awsls.Resource, error) { +func DeletableResources(resType string, resources interface{}, client awsls.Client) ([]awsls.Resource, error) { var deletableResources []awsls.Resource reflectResources := reflect.ValueOf(resources) @@ -26,11 +25,6 @@ func DeletableResources(resType string, resources interface{}) ([]awsls.Resource return nil, errors.Wrapf(err, "Field with delete ID required for deleting resource") } - tags, err := findTags(reflectResources.Index(i)) - if err != nil { - log.WithError(err).Debug("failed to find tags") - } - var creationTime *time.Time creationTimeField, err := findField(creationTimeFieldNames, reflect.Indirect(reflectResources.Index(i))) if err == nil { @@ -51,8 +45,10 @@ func DeletableResources(resType string, resources interface{}) ([]awsls.Resource deletableResources = append(deletableResources, awsls.Resource{ Type: resType, ID: deleteIDField.Elem().String(), - Tags: tags, CreatedAt: creationTime, + Region: client.Region, + Profile: client.Profile, + AccountID: client.AccountID, }) } @@ -77,21 +73,3 @@ func findField(names []string, v reflect.Value) (reflect.Value, error) { } return reflect.Value{}, errors.Errorf("Fields not found: %s", names) } - -// findTags finds findTags via reflection in the describe output. -func findTags(res reflect.Value) (map[string]string, error) { - tags := map[string]string{} - - ts, err := findField(tagFieldNames, reflect.Indirect(res)) - if err != nil { - return nil, errors.Wrap(err, "No tags found") - } - - for i := 0; i < ts.Len(); i++ { - key := reflect.Indirect(ts.Index(i)).FieldByName("Key").Elem() - value := reflect.Indirect(ts.Index(i)).FieldByName("Value").Elem() - tags[key.String()] = value.String() - } - - return tags, nil -} diff --git a/pkg/resource/resource_test.go b/pkg/resource/resource_test.go index 4053a362..42e32ac1 100644 --- a/pkg/resource/resource_test.go +++ b/pkg/resource/resource_test.go @@ -4,6 +4,8 @@ import ( "testing" "time" + awsls "github.com/jckuester/awsls/aws" + "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/autoscaling" "github.com/aws/aws-sdk-go-v2/service/ec2" @@ -15,32 +17,26 @@ import ( var ( testImageId = "test-ami" testAutoscalingGroupName = "test-auto-scaling-group" - testTags = map[string]string{ - "test-tag-key": "test-tag-value", - } ) func TestDeletableResources_CreationDateIsTypeTime(t *testing.T) { // given - testLaunchTime := aws.Time(time.Date(2018, 11, 17, 5, 0, 0, 0, time.UTC)) - + testCreationDate := aws.Time(time.Date(2018, 11, 17, 5, 0, 0, 0, time.UTC)) rawResources := []*autoscaling.AutoScalingGroup{ { AutoScalingGroupName: &testAutoscalingGroupName, - Tags: convertTags(testTags), - CreatedTime: testLaunchTime, + CreatedTime: testCreationDate, }, } // when - res, err := resource.DeletableResources(resource.AutoscalingGroup, rawResources) + res, err := resource.DeletableResources(resource.AutoscalingGroup, rawResources, awsls.Client{}) require.NoError(t, err) require.Len(t, res, 1) // then assert.Equal(t, testAutoscalingGroupName, res[0].ID) - assert.Equal(t, testTags, res[0].Tags) - assert.Equal(t, testLaunchTime, res[0].CreatedAt) + assert.Equal(t, testCreationDate, res[0].CreatedAt) } func TestDeletableResources_CreationDateIsTypeString(t *testing.T) { @@ -54,22 +50,10 @@ func TestDeletableResources_CreationDateIsTypeString(t *testing.T) { } // when - res, err := resource.DeletableResources(resource.Ami, rawResources) + res, err := resource.DeletableResources(resource.Ami, rawResources, awsls.Client{}) require.NoError(t, err) // then require.Len(t, res, 1) require.Equal(t, testCreationDate, res[0].CreatedAt.Format("2006-01-02T15:04:05.000Z0700")) } - -func convertTags(tags map[string]string) []autoscaling.TagDescription { - var tagDescriptions = make([]autoscaling.TagDescription, 0, len(tags)) - - for key, value := range tags { - tagDescriptions = append(tagDescriptions, autoscaling.TagDescription{ - Key: aws.String(key), - Value: aws.String(value), - }) - } - return tagDescriptions -} diff --git a/pkg/resource/supported.go b/pkg/resource/supported.go index 10ac0909..f139eeaa 100644 --- a/pkg/resource/supported.go +++ b/pkg/resource/supported.go @@ -71,11 +71,6 @@ var ( CloudTrail: 8800, } - tagFieldNames = []string{ - "Tags", - "TagSet", - } - // creationTimeFieldNames are a list field names that are used to find the creation date of a resource. creationTimeFieldNames = []string{ "LaunchTime", diff --git a/test/helper_test.go b/test/helper_test.go index ab15319c..1112c209 100644 --- a/test/helper_test.go +++ b/test/helper_test.go @@ -44,7 +44,9 @@ func InitEnv(t *testing.T) EnvVars { t.Fatal("env variable AWS_DEFAULT_REGION needs to be set for tests") } - client, err := awsls.NewClient(external.WithRegion(region)) + client, err := awsls.NewClient( + external.WithSharedConfigProfile(profile), + external.WithRegion(region)) require.NoError(t, err) return EnvVars{ diff --git a/test/test-fixtures/cloudformation-stack/filter.yml b/test/test-fixtures/cloudformation-stack/filter.yml deleted file mode 100644 index 9f65d7ba..00000000 --- a/test/test-fixtures/cloudformation-stack/filter.yml +++ /dev/null @@ -1 +0,0 @@ -aws_cloudformation_stack: diff --git a/test/test-fixtures/cloudformation-stack/main.tf b/test/test-fixtures/cloudformation-stack/main.tf deleted file mode 100644 index 2517d780..00000000 --- a/test/test-fixtures/cloudformation-stack/main.tf +++ /dev/null @@ -1,41 +0,0 @@ -provider "aws" { - version = "~> 2.0" - - profile = var.profile - region = var.region -} - -resource "aws_cloudformation_stack" "test" { - name = var.name - - parameters = { - VPCCidr = "10.0.0.0/16" - } - - template_body = <