Skip to content

Commit

Permalink
change port appRunner, ASG, config recorder, config service, dataSync…
Browse files Browse the repository at this point in the history
… location, dataSync task to awsSDKv2 gruntwork-io#770
  • Loading branch information
wakeful committed Oct 20, 2024
1 parent de75802 commit cdd1430
Show file tree
Hide file tree
Showing 22 changed files with 325 additions and 308 deletions.
29 changes: 14 additions & 15 deletions aws/resources/apprunner_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package resources
import (
"context"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/apprunner"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/apprunner"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/gruntwork-io/cloud-nuke/logging"
"github.com/gruntwork-io/cloud-nuke/report"
Expand All @@ -23,7 +23,7 @@ func (a *AppRunnerService) nukeAll(identifiers []*string) error {
for _, identifier := range identifiers {
logging.Debugf("[App Runner Service] Deleting App Runner Service %s in region %s", *identifier, a.Region)

_, err := a.Client.DeleteServiceWithContext(a.Context, &apprunner.DeleteServiceInput{
_, err := a.Client.DeleteService(a.Context, &apprunner.DeleteServiceInput{
ServiceArn: identifier,
})
if err != nil {
Expand All @@ -34,7 +34,7 @@ func (a *AppRunnerService) nukeAll(identifiers []*string) error {
}

e := report.Entry{
Identifier: aws.StringValue(identifier),
Identifier: aws.ToString(identifier),
ResourceType: a.ResourceName(),
Error: err,
}
Expand All @@ -47,7 +47,16 @@ func (a *AppRunnerService) nukeAll(identifiers []*string) error {

func (a *AppRunnerService) getAll(c context.Context, configObj config.Config) ([]*string, error) {
var identifiers []*string
paginator := func(output *apprunner.ListServicesOutput, lastPage bool) bool {
paginator := apprunner.NewListServicesPaginator(a.Client, &apprunner.ListServicesInput{
MaxResults: aws.Int32(19),
})
for paginator.HasMorePages() {
output, err := paginator.NextPage(c)
if err != nil {
logging.Debugf("[App Runner Service] Failed to list app runner services: %s", err)
return nil, errors.WithStackTrace(err)
}

for _, service := range output.ServiceSummaryList {
if configObj.AppRunnerService.ShouldInclude(config.ResourceValue{
Name: service.ServiceName,
Expand All @@ -56,16 +65,6 @@ func (a *AppRunnerService) getAll(c context.Context, configObj config.Config) ([
identifiers = append(identifiers, service.ServiceArn)
}
}
return !lastPage
}

param := &apprunner.ListServicesInput{
MaxResults: aws.Int64(19),
}

if err := a.Client.ListServicesPagesWithContext(c, param, paginator); err != nil {
logging.Debugf("[App Runner Service] Failed to list app runner services: %s", err)
return nil, errors.WithStackTrace(err)
}

return identifiers, nil
Expand Down
26 changes: 11 additions & 15 deletions aws/resources/apprunner_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,29 @@ import (
"testing"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/apprunner"
"github.com/aws/aws-sdk-go/service/apprunner/apprunneriface"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/apprunner"
"github.com/aws/aws-sdk-go-v2/service/apprunner/types"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type mockedAppRunnerService struct {
apprunneriface.AppRunnerAPI
ListServicesOutput apprunner.ListServicesOutput
AppRunnerServiceAPI
DeleteServiceOutput apprunner.DeleteServiceOutput
ListServicesOutput apprunner.ListServicesOutput
}

func (m mockedAppRunnerService) ListServicesPagesWithContext(_ aws.Context, _ *apprunner.ListServicesInput, callback func(*apprunner.ListServicesOutput, bool) bool, _ ...request.Option) error {
callback(&m.ListServicesOutput, true)
return nil
func (m mockedAppRunnerService) DeleteService(ctx context.Context, params *apprunner.DeleteServiceInput, optFns ...func(*apprunner.Options)) (*apprunner.DeleteServiceOutput, error) {
return &m.DeleteServiceOutput, nil
}

func (m mockedAppRunnerService) DeleteServiceWithContext(aws.Context, *apprunner.DeleteServiceInput, ...request.Option) (*apprunner.DeleteServiceOutput, error) {
return &m.DeleteServiceOutput, nil
func (m mockedAppRunnerService) ListServices(ctx context.Context, params *apprunner.ListServicesInput, optFns ...func(*apprunner.Options)) (*apprunner.ListServicesOutput, error) {
return &m.ListServicesOutput, nil
}

func Test_AppRunnerService_GetAll(t *testing.T) {

t.Parallel()

testName1 := "test-service-1"
Expand All @@ -41,7 +38,7 @@ func Test_AppRunnerService_GetAll(t *testing.T) {
service := AppRunnerService{
Client: mockedAppRunnerService{
ListServicesOutput: apprunner.ListServicesOutput{
ServiceSummaryList: []*apprunner.ServiceSummary{
ServiceSummaryList: []types.ServiceSummary{
{
ServiceName: &testName1,
ServiceArn: aws.String(fmt.Sprintf("arn::%s", testName1)),
Expand Down Expand Up @@ -88,13 +85,12 @@ func Test_AppRunnerService_GetAll(t *testing.T) {
AppRunnerService: tc.configObj,
})
require.NoError(t, err)
require.Equal(t, tc.expected, aws.StringValueSlice(names))
require.Equal(t, tc.expected, aws.ToStringSlice(names))
})
}
}

func TestAppRunnerService_NukeAll(t *testing.T) {

t.Parallel()

testName := "test-app-runner-service"
Expand Down
21 changes: 13 additions & 8 deletions aws/resources/apprunner_service_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ package resources
import (
"context"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/apprunner"
"github.com/aws/aws-sdk-go/service/apprunner/apprunneriface"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/apprunner"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/gruntwork-io/go-commons/errors"
)

type AppRunnerServiceAPI interface {
DeleteService(ctx context.Context, params *apprunner.DeleteServiceInput, optFns ...func(*apprunner.Options)) (*apprunner.DeleteServiceOutput, error)
ListServices(ctx context.Context, params *apprunner.ListServicesInput, optFns ...func(*apprunner.Options)) (*apprunner.ListServicesOutput, error)
}

type AppRunnerService struct {
BaseAwsResource
Client apprunneriface.AppRunnerAPI
Client AppRunnerServiceAPI
Region string
AppRunners []string
}
Expand All @@ -22,10 +25,12 @@ func (a *AppRunnerService) GetAndSetResourceConfig(configObj config.Config) conf
return configObj.AppRunnerService
}

func (a *AppRunnerService) Init(session *session.Session) {
a.Client = apprunner.New(session)
func (a *AppRunnerService) InitV2(cfg aws.Config) {
a.Client = apprunner.NewFromConfig(cfg)
}

func (a *AppRunnerService) IsUsingV2() bool { return true }

func (a *AppRunnerService) ResourceName() string { return "app-runner-service" }

func (a *AppRunnerService) ResourceIdentifiers() []string { return a.AppRunners }
Expand All @@ -46,6 +51,6 @@ func (a *AppRunnerService) GetAndSetIdentifiers(c context.Context, configObj con
return nil, err
}

a.AppRunners = aws.StringValueSlice(identifiers)
a.AppRunners = aws.ToStringSlice(identifiers)
return a.AppRunners, nil
}
21 changes: 12 additions & 9 deletions aws/resources/asg.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package resources

import (
"context"
"time"

awsgo "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/autoscaling"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/gruntwork-io/cloud-nuke/logging"
"github.com/gruntwork-io/cloud-nuke/report"
Expand All @@ -14,7 +15,7 @@ import (

// Returns a formatted string of ASG Names
func (ag *ASGroups) getAll(c context.Context, configObj config.Config) ([]*string, error) {
result, err := ag.Client.DescribeAutoScalingGroupsWithContext(ag.Context, &autoscaling.DescribeAutoScalingGroupsInput{})
result, err := ag.Client.DescribeAutoScalingGroups(ag.Context, &autoscaling.DescribeAutoScalingGroupsInput{})
if err != nil {
return nil, errors.WithStackTrace(err)
}
Expand All @@ -41,15 +42,15 @@ func (ag *ASGroups) nukeAll(groupNames []*string) error {
}

logging.Debugf("Deleting all Auto Scaling Groups in region %s", ag.Region)
var deletedGroupNames []*string
var deletedGroupNames []string

for _, groupName := range groupNames {
params := &autoscaling.DeleteAutoScalingGroupInput{
AutoScalingGroupName: groupName,
ForceDelete: awsgo.Bool(true),
ForceDelete: aws.Bool(true),
}

_, err := ag.Client.DeleteAutoScalingGroupWithContext(ag.Context, params)
_, err := ag.Client.DeleteAutoScalingGroup(ag.Context, params)

// Record status of this resource
e := report.Entry{
Expand All @@ -62,15 +63,17 @@ func (ag *ASGroups) nukeAll(groupNames []*string) error {
if err != nil {
logging.Debugf("[Failed] %s", err)
} else {
deletedGroupNames = append(deletedGroupNames, groupName)
deletedGroupNames = append(deletedGroupNames, *groupName)
logging.Debugf("Deleted Auto Scaling Group: %s", *groupName)
}
}

if len(deletedGroupNames) > 0 {
err := ag.Client.WaitUntilGroupNotExistsWithContext(ag.Context, &autoscaling.DescribeAutoScalingGroupsInput{
waiter := autoscaling.NewGroupNotExistsWaiter(ag.Client)
err := waiter.Wait(ag.Context, &autoscaling.DescribeAutoScalingGroupsInput{
AutoScalingGroupNames: deletedGroupNames,
})
}, 5*time.Minute)

if err != nil {
logging.Errorf("[Failed] %s", err)
return errors.WithStackTrace(err)
Expand Down
54 changes: 25 additions & 29 deletions aws/resources/asg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,51 +6,44 @@ import (
"testing"
"time"

"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/autoscaling/autoscalingiface"

awsgo "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/autoscaling"
"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/autoscaling/types"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/stretchr/testify/assert"
)

type mockedASGroups struct {
autoscalingiface.AutoScalingAPI
DescribeAutoScalingGroupsResp autoscaling.DescribeAutoScalingGroupsOutput
DeleteAutoScalingGroupResp autoscaling.DeleteAutoScalingGroupOutput
}

func (m mockedASGroups) DescribeAutoScalingGroupsWithContext(_ awsgo.Context, _ *autoscaling.DescribeAutoScalingGroupsInput, _ ...request.Option) (*autoscaling.DescribeAutoScalingGroupsOutput, error) {
return &m.DescribeAutoScalingGroupsResp, nil
ASGroupsAPI
DescribeAutoScalingGroupsOutput autoscaling.DescribeAutoScalingGroupsOutput
DeleteAutoScalingGroupOutput autoscaling.DeleteAutoScalingGroupOutput
}

func (m mockedASGroups) DeleteAutoScalingGroupWithContext(_ awsgo.Context, _ *autoscaling.DeleteAutoScalingGroupInput, _ ...request.Option) (*autoscaling.DeleteAutoScalingGroupOutput, error) {
return &m.DeleteAutoScalingGroupResp, nil
func (m mockedASGroups) DescribeAutoScalingGroups(ctx context.Context, params *autoscaling.DescribeAutoScalingGroupsInput, optFns ...func(*autoscaling.Options)) (*autoscaling.DescribeAutoScalingGroupsOutput, error) {
return &m.DescribeAutoScalingGroupsOutput, nil
}

func (m mockedASGroups) WaitUntilGroupNotExistsWithContext(_ awsgo.Context, _ *autoscaling.DescribeAutoScalingGroupsInput, _ ...request.WaiterOption) error {
return nil
func (m mockedASGroups) DeleteAutoScalingGroup(ctx context.Context, params *autoscaling.DeleteAutoScalingGroupInput, optFns ...func(*autoscaling.Options)) (*autoscaling.DeleteAutoScalingGroupOutput, error) {
return &m.DeleteAutoScalingGroupOutput, nil
}

func TestAutoScalingGroupGetAll(t *testing.T) {

t.Parallel()

testName := "cloud-nuke-test"
now := time.Now()
ag := ASGroups{
Client: mockedASGroups{
DescribeAutoScalingGroupsResp: autoscaling.DescribeAutoScalingGroupsOutput{
AutoScalingGroups: []*autoscaling.Group{{
AutoScalingGroupName: awsgo.String(testName),
CreatedTime: awsgo.Time(now),
DescribeAutoScalingGroupsOutput: autoscaling.DescribeAutoScalingGroupsOutput{
AutoScalingGroups: []types.AutoScalingGroup{{
AutoScalingGroupName: aws.String(testName),
CreatedTime: aws.Time(now),
}}}}}

// empty filter
groups, err := ag.getAll(context.Background(), config.Config{})
assert.NoError(t, err)
assert.Contains(t, awsgo.StringValueSlice(groups), testName)
assert.Contains(t, aws.ToStringSlice(groups), testName)

// name filter
groups, err = ag.getAll(context.Background(), config.Config{
Expand All @@ -60,27 +53,30 @@ func TestAutoScalingGroupGetAll(t *testing.T) {
RE: *regexp.MustCompile("^cloud-nuke-*"),
}}}}})
assert.NoError(t, err)
assert.NotContains(t, awsgo.StringValueSlice(groups), testName)
assert.NotContains(t, aws.ToStringSlice(groups), testName)

// time filter
groups, err = ag.getAll(context.Background(), config.Config{
AutoScalingGroup: config.ResourceType{
ExcludeRule: config.FilterRule{
TimeAfter: awsgo.Time(now.Add(-1)),
TimeAfter: aws.Time(now.Add(-1)),
}}})
assert.NoError(t, err)
assert.NotContains(t, awsgo.StringValueSlice(groups), testName)
assert.NotContains(t, aws.ToStringSlice(groups), testName)
}

func TestAutoScalingGroupNukeAll(t *testing.T) {

t.Parallel()

ag := ASGroups{
BaseAwsResource: BaseAwsResource{
Context: context.Background(),
},
Client: mockedASGroups{
DeleteAutoScalingGroupResp: autoscaling.DeleteAutoScalingGroupOutput{},
}}
DeleteAutoScalingGroupOutput: autoscaling.DeleteAutoScalingGroupOutput{},
},
}

err := ag.nukeAll([]*string{awsgo.String("cloud-nuke-test")})
err := ag.nukeAll([]*string{aws.String("cloud-nuke-test")})
assert.NoError(t, err)
}
Loading

0 comments on commit cdd1430

Please sign in to comment.