Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change port launch config, launch template, msk cluster, oidc provider to awsSDKv2 #770 #792

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions aws/resources/launch_config.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/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 @@ -13,7 +13,7 @@ import (

// Returns a formatted string of Launch config Names
func (lc *LaunchConfigs) getAll(c context.Context, configObj config.Config) ([]*string, error) {
result, err := lc.Client.DescribeLaunchConfigurationsWithContext(lc.Context, &autoscaling.DescribeLaunchConfigurationsInput{})
result, err := lc.Client.DescribeLaunchConfigurations(lc.Context, &autoscaling.DescribeLaunchConfigurationsInput{})
if err != nil {
return nil, errors.WithStackTrace(err)
}
Expand Down Expand Up @@ -47,11 +47,11 @@ func (lc *LaunchConfigs) nukeAll(configNames []*string) error {
LaunchConfigurationName: configName,
}

_, err := lc.Client.DeleteLaunchConfigurationWithContext(lc.Context, params)
_, err := lc.Client.DeleteLaunchConfiguration(lc.Context, params)

// Record status of this resource
e := report.Entry{
Identifier: aws.StringValue(configName),
Identifier: aws.ToString(configName),
ResourceType: "Launch configuration",
Error: err,
}
Expand Down
31 changes: 14 additions & 17 deletions aws/resources/launch_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,24 @@ 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/autoscaling/autoscalingiface"
"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/require"

awsgo "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/autoscaling"
)

type mockedLaunchConfiguration struct {
autoscalingiface.AutoScalingAPI
LaunchConfigsAPI
DescribeLaunchConfigurationsOutput autoscaling.DescribeLaunchConfigurationsOutput
DeleteLaunchConfigurationOutput autoscaling.DeleteLaunchConfigurationOutput
}

func (m mockedLaunchConfiguration) DescribeLaunchConfigurationsWithContext(_ aws.Context, input *autoscaling.DescribeLaunchConfigurationsInput, _ ...request.Option) (*autoscaling.DescribeLaunchConfigurationsOutput, error) {
func (m mockedLaunchConfiguration) DescribeLaunchConfigurations(ctx context.Context, params *autoscaling.DescribeLaunchConfigurationsInput, optFns ...func(*autoscaling.Options)) (*autoscaling.DescribeLaunchConfigurationsOutput, error) {
return &m.DescribeLaunchConfigurationsOutput, nil
}

func (m mockedLaunchConfiguration) DeleteLaunchConfigurationWithContext(_ aws.Context, input *autoscaling.DeleteLaunchConfigurationInput, _ ...request.Option) (*autoscaling.DeleteLaunchConfigurationOutput, error) {
func (m mockedLaunchConfiguration) DeleteLaunchConfiguration(ctx context.Context, params *autoscaling.DeleteLaunchConfigurationInput, optFns ...func(*autoscaling.Options)) (*autoscaling.DeleteLaunchConfigurationOutput, error) {
return &m.DeleteLaunchConfigurationOutput, nil
}

Expand All @@ -40,14 +37,14 @@ func TestLaunchConfigurations_GetAll(t *testing.T) {
lc := LaunchConfigs{
Client: mockedLaunchConfiguration{
DescribeLaunchConfigurationsOutput: autoscaling.DescribeLaunchConfigurationsOutput{
LaunchConfigurations: []*autoscaling.LaunchConfiguration{
LaunchConfigurations: []types.LaunchConfiguration{
{
LaunchConfigurationName: awsgo.String(testName1),
CreatedTime: awsgo.Time(now),
LaunchConfigurationName: aws.String(testName1),
CreatedTime: aws.Time(now),
},
{
LaunchConfigurationName: awsgo.String(testName2),
CreatedTime: awsgo.Time(now.Add(1)),
LaunchConfigurationName: aws.String(testName2),
CreatedTime: aws.Time(now.Add(1)),
},
},
},
Expand All @@ -74,7 +71,7 @@ func TestLaunchConfigurations_GetAll(t *testing.T) {
"timeAfterExclusionFilter": {
configObj: config.ResourceType{
ExcludeRule: config.FilterRule{
TimeAfter: awsgo.Time(now.Add(-1 * time.Hour)),
TimeAfter: aws.Time(now.Add(-1 * time.Hour)),
}},
expected: []string{},
},
Expand All @@ -85,7 +82,7 @@ func TestLaunchConfigurations_GetAll(t *testing.T) {
LaunchConfiguration: tc.configObj,
})
require.NoError(t, err)
require.Equal(t, tc.expected, awsgo.StringValueSlice(names))
require.Equal(t, tc.expected, aws.ToStringSlice(names))
})
}
}
Expand All @@ -100,6 +97,6 @@ func TestLaunchConfigurations_NukeAll(t *testing.T) {
},
}

err := lc.nukeAll([]*string{awsgo.String("test")})
err := lc.nukeAll([]*string{aws.String("test")})
require.NoError(t, err)
}
23 changes: 14 additions & 9 deletions aws/resources/launch_config_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,31 @@ package resources
import (
"context"

awsgo "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/aws/aws-sdk-go/service/autoscaling/autoscalingiface"
"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/go-commons/errors"
)

type LaunchConfigsAPI interface {
DescribeLaunchConfigurations(ctx context.Context, params *autoscaling.DescribeLaunchConfigurationsInput, optFns ...func(*autoscaling.Options)) (*autoscaling.DescribeLaunchConfigurationsOutput, error)
DeleteLaunchConfiguration(ctx context.Context, params *autoscaling.DeleteLaunchConfigurationInput, optFns ...func(*autoscaling.Options)) (*autoscaling.DeleteLaunchConfigurationOutput, error)
}

// LaunchConfigs - represents all launch configurations
type LaunchConfigs struct {
BaseAwsResource
Client autoscalingiface.AutoScalingAPI
Client LaunchConfigsAPI
Region string
LaunchConfigurationNames []string
}

func (lc *LaunchConfigs) Init(session *session.Session) {
lc.Client = autoscaling.New(session)
func (lc *LaunchConfigs) InitV2(cfg aws.Config) {
lc.Client = autoscaling.NewFromConfig(cfg)
}

func (lc *LaunchConfigs) IsUsingV2() bool { return true }

// ResourceName - the simple name of the aws resource
func (lc *LaunchConfigs) ResourceName() string {
return "lc"
Expand All @@ -48,13 +53,13 @@ func (lc *LaunchConfigs) GetAndSetIdentifiers(c context.Context, configObj confi
return nil, err
}

lc.LaunchConfigurationNames = awsgo.StringValueSlice(identifiers)
lc.LaunchConfigurationNames = aws.ToStringSlice(identifiers)
return lc.LaunchConfigurationNames, nil
}

// Nuke - nuke 'em all!!!
func (lc *LaunchConfigs) Nuke(identifiers []string) error {
if err := lc.nukeAll(awsgo.StringSlice(identifiers)); err != nil {
if err := lc.nukeAll(aws.StringSlice(identifiers)); err != nil {
return errors.WithStackTrace(err)
}

Expand Down
19 changes: 9 additions & 10 deletions aws/resources/launch_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package resources
import (
"context"

"github.com/aws/aws-sdk-go/aws"
awsgo "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"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 +13,7 @@ import (

// Returns a formatted string of Launch Template Names
func (lt *LaunchTemplates) getAll(c context.Context, configObj config.Config) ([]*string, error) {
result, err := lt.Client.DescribeLaunchTemplatesWithContext(lt.Context, &ec2.DescribeLaunchTemplatesInput{})
result, err := lt.Client.DescribeLaunchTemplates(lt.Context, &ec2.DescribeLaunchTemplatesInput{})
if err != nil {
return nil, errors.WithStackTrace(err)
}
Expand All @@ -31,9 +30,9 @@ func (lt *LaunchTemplates) getAll(c context.Context, configObj config.Config) ([

// checking the nukable permissions
lt.VerifyNukablePermissions(templateNames, func(id *string) error {
_, err := lt.Client.DeleteLaunchTemplateWithContext(lt.Context, &ec2.DeleteLaunchTemplateInput{
_, err := lt.Client.DeleteLaunchTemplate(lt.Context, &ec2.DeleteLaunchTemplateInput{
LaunchTemplateName: id,
DryRun: awsgo.Bool(true),
DryRun: aws.Bool(true),
})
return err
})
Expand All @@ -53,18 +52,18 @@ func (lt *LaunchTemplates) nukeAll(templateNames []*string) error {

for _, templateName := range templateNames {

if nukable, reason := lt.IsNukable(awsgo.StringValue(templateName)); !nukable {
logging.Debugf("[Skipping] %s nuke because %v", awsgo.StringValue(templateName), reason)
if nukable, reason := lt.IsNukable(aws.ToString(templateName)); !nukable {
logging.Debugf("[Skipping] %s nuke because %v", aws.ToString(templateName), reason)
continue
}

_, err := lt.Client.DeleteLaunchTemplateWithContext(lt.Context, &ec2.DeleteLaunchTemplateInput{
_, err := lt.Client.DeleteLaunchTemplate(lt.Context, &ec2.DeleteLaunchTemplateInput{
LaunchTemplateName: templateName,
})

// Record status of this resource
e := report.Entry{
Identifier: aws.StringValue(templateName),
Identifier: aws.ToString(templateName),
ResourceType: "Launch template",
Error: err,
}
Expand Down
30 changes: 14 additions & 16 deletions aws/resources/launch_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,24 @@ import (
"testing"
"time"

"github.com/aws/aws-sdk-go/aws"
awsgo "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/stretchr/testify/require"
)

type mockedLaunchTemplate struct {
ec2iface.EC2API
LaunchTemplatesAPI
DescribeLaunchTemplatesOutput ec2.DescribeLaunchTemplatesOutput
DeleteLaunchTemplateOutput ec2.DeleteLaunchTemplateOutput
}

func (m mockedLaunchTemplate) DescribeLaunchTemplatesWithContext(_ aws.Context, input *ec2.DescribeLaunchTemplatesInput, _ ...request.Option) (*ec2.DescribeLaunchTemplatesOutput, error) {
func (m mockedLaunchTemplate) DescribeLaunchTemplates(ctx context.Context, params *ec2.DescribeLaunchTemplatesInput, optFns ...func(*ec2.Options)) (*ec2.DescribeLaunchTemplatesOutput, error) {
return &m.DescribeLaunchTemplatesOutput, nil
}

func (m mockedLaunchTemplate) DeleteLaunchTemplateWithContext(_ aws.Context, input *ec2.DeleteLaunchTemplateInput, _ ...request.Option) (*ec2.DeleteLaunchTemplateOutput, error) {
func (m mockedLaunchTemplate) DeleteLaunchTemplate(ctx context.Context, params *ec2.DeleteLaunchTemplateInput, optFns ...func(*ec2.Options)) (*ec2.DeleteLaunchTemplateOutput, error) {
return &m.DeleteLaunchTemplateOutput, nil
}

Expand All @@ -39,14 +37,14 @@ func TestLaunchTemplate_GetAll(t *testing.T) {
lt := LaunchTemplates{
Client: mockedLaunchTemplate{
DescribeLaunchTemplatesOutput: ec2.DescribeLaunchTemplatesOutput{
LaunchTemplates: []*ec2.LaunchTemplate{
LaunchTemplates: []types.LaunchTemplate{
{
LaunchTemplateName: awsgo.String(testName1),
CreateTime: awsgo.Time(now),
LaunchTemplateName: aws.String(testName1),
CreateTime: aws.Time(now),
},
{
LaunchTemplateName: awsgo.String(testName2),
CreateTime: awsgo.Time(now.Add(1)),
LaunchTemplateName: aws.String(testName2),
CreateTime: aws.Time(now.Add(1)),
},
},
},
Expand All @@ -73,7 +71,7 @@ func TestLaunchTemplate_GetAll(t *testing.T) {
"timeAfterExclusionFilter": {
configObj: config.ResourceType{
ExcludeRule: config.FilterRule{
TimeAfter: awsgo.Time(now),
TimeAfter: aws.Time(now),
}},
expected: []string{testName1},
},
Expand All @@ -84,7 +82,7 @@ func TestLaunchTemplate_GetAll(t *testing.T) {
LaunchTemplate: tc.configObj,
})
require.NoError(t, err)
require.Equal(t, tc.expected, awsgo.StringValueSlice(names))
require.Equal(t, tc.expected, aws.ToStringSlice(names))
})
}
}
Expand All @@ -99,6 +97,6 @@ func TestLaunchTemplate_NukeAll(t *testing.T) {
},
}

err := lt.nukeAll([]*string{awsgo.String("test")})
err := lt.nukeAll([]*string{aws.String("test")})
require.NoError(t, err)
}
23 changes: 14 additions & 9 deletions aws/resources/launch_template_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,31 @@ package resources
import (
"context"

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/aws/aws-sdk-go/service/ec2/ec2iface"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/gruntwork-io/cloud-nuke/config"
"github.com/gruntwork-io/go-commons/errors"
)

type LaunchTemplatesAPI interface {
DescribeLaunchTemplates(ctx context.Context, params *ec2.DescribeLaunchTemplatesInput, optFns ...func(*ec2.Options)) (*ec2.DescribeLaunchTemplatesOutput, error)
DeleteLaunchTemplate(ctx context.Context, params *ec2.DeleteLaunchTemplateInput, optFns ...func(*ec2.Options)) (*ec2.DeleteLaunchTemplateOutput, error)
}

// LaunchTemplates - represents all launch templates
type LaunchTemplates struct {
BaseAwsResource
Client ec2iface.EC2API
Client LaunchTemplatesAPI
Region string
LaunchTemplateNames []string
}

func (lt *LaunchTemplates) Init(session *session.Session) {
lt.Client = ec2.New(session)
func (lt *LaunchTemplates) InitV2(cfg aws.Config) {
lt.Client = ec2.NewFromConfig(cfg)
}

func (lt *LaunchTemplates) IsUsingV2() bool { return true }

// ResourceName - the simple name of the aws resource
func (lt *LaunchTemplates) ResourceName() string {
return "lt"
Expand All @@ -48,13 +53,13 @@ func (lt *LaunchTemplates) GetAndSetIdentifiers(c context.Context, configObj con
return nil, err
}

lt.LaunchTemplateNames = awsgo.StringValueSlice(identifiers)
lt.LaunchTemplateNames = aws.ToStringSlice(identifiers)
return lt.LaunchTemplateNames, nil
}

// Nuke - nuke 'em all!!!
func (lt *LaunchTemplates) Nuke(identifiers []string) error {
if err := lt.nukeAll(awsgo.StringSlice(identifiers)); err != nil {
if err := lt.nukeAll(aws.StringSlice(identifiers)); err != nil {
return errors.WithStackTrace(err)
}

Expand Down
Loading