Skip to content

Commit

Permalink
Merge pull request #215 from ekristen/fix-autoscaling-resources
Browse files Browse the repository at this point in the history
feat(autoscaling): standardize and test lifecycle hook and launch configuration resources
  • Loading branch information
ekristen authored Jul 9, 2024
2 parents c6f4c17 + 2971b6b commit f3ae5f7
Show file tree
Hide file tree
Showing 8 changed files with 4,191 additions and 154 deletions.
3,815 changes: 3,815 additions & 0 deletions mocks/mock_autoscalingiface/mock.go

Large diffs are not rendered by default.

91 changes: 91 additions & 0 deletions resources/autoscaling-launch-configuration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package resources

import (
"context"
"time"

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

"github.com/ekristen/libnuke/pkg/registry"
"github.com/ekristen/libnuke/pkg/resource"
"github.com/ekristen/libnuke/pkg/types"

"github.com/ekristen/aws-nuke/v3/pkg/nuke"
)

const AutoScalingLaunchConfigurationResource = "AutoScalingLaunchConfiguration"

func init() {
registry.Register(&registry.Registration{
Name: AutoScalingLaunchConfigurationResource,
Scope: nuke.Account,
Lister: &AutoScalingLaunchConfigurationLister{},
DeprecatedAliases: []string{
"LaunchConfiguration",
},
})
}

type AutoScalingLaunchConfigurationLister struct {
mockSvc autoscalingiface.AutoScalingAPI
}

func (l *AutoScalingLaunchConfigurationLister) List(_ context.Context, o interface{}) ([]resource.Resource, error) {
opts := o.(*nuke.ListerOpts)

resources := make([]resource.Resource, 0)

var svc autoscalingiface.AutoScalingAPI
if l.mockSvc != nil {
svc = l.mockSvc
} else {
svc = autoscaling.New(opts.Session)
}

params := &autoscaling.DescribeLaunchConfigurationsInput{}
err := svc.DescribeLaunchConfigurationsPages(params,
func(page *autoscaling.DescribeLaunchConfigurationsOutput, lastPage bool) bool {
for _, launchConfig := range page.LaunchConfigurations {
resources = append(resources, &AutoScalingLaunchConfiguration{
svc: svc,
Name: launchConfig.LaunchConfigurationName,
CreatedTime: launchConfig.CreatedTime,
})
}
return !lastPage
})

if err != nil {
return nil, err
}

return resources, nil
}

type AutoScalingLaunchConfiguration struct {
svc autoscalingiface.AutoScalingAPI
Name *string
CreatedTime *time.Time
}

func (r *AutoScalingLaunchConfiguration) Properties() types.Properties {
return types.NewPropertiesFromStruct(r)
}

func (r *AutoScalingLaunchConfiguration) Remove(_ context.Context) error {
params := &autoscaling.DeleteLaunchConfigurationInput{
LaunchConfigurationName: r.Name,
}

_, err := r.svc.DeleteLaunchConfiguration(params)
if err != nil {
return err
}

return nil
}

func (r *AutoScalingLaunchConfiguration) String() string {
return *r.Name
}
73 changes: 0 additions & 73 deletions resources/autoscaling-launch-configurations.go

This file was deleted.

91 changes: 91 additions & 0 deletions resources/autoscaling-launch-configurations_mock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package resources

import (
"context"
"testing"
"time"

"github.com/golang/mock/gomock"
"github.com/gotidy/ptr"
"github.com/stretchr/testify/assert"

"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/autoscaling"

"github.com/ekristen/aws-nuke/v3/mocks/mock_autoscalingiface"
"github.com/ekristen/aws-nuke/v3/pkg/nuke"
)

func Test_Mock_AutoScalingLaunchConfiguration_List(t *testing.T) {
a := assert.New(t)
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockSvc := mock_autoscalingiface.NewMockAutoScalingAPI(ctrl)

mockSvc.EXPECT().
DescribeLaunchConfigurationsPages(gomock.Eq(&autoscaling.DescribeLaunchConfigurationsInput{}), gomock.Any()).
Do(func(input *autoscaling.DescribeLaunchConfigurationsInput,
fn func(res *autoscaling.DescribeLaunchConfigurationsOutput, lastPage bool) bool) {
fn(&autoscaling.DescribeLaunchConfigurationsOutput{
LaunchConfigurations: []*autoscaling.LaunchConfiguration{
{
LaunchConfigurationName: ptr.String("foo"),
CreatedTime: ptr.Time(time.Now()),
},
},
}, true)
}).
Return(nil)

lister := AutoScalingLaunchConfigurationLister{
mockSvc: mockSvc,
}

resources, err := lister.List(context.TODO(), &nuke.ListerOpts{
Region: &nuke.Region{
Name: "us-east-2",
},
Session: session.Must(session.NewSession()),
})

a.Nil(err)
a.Len(resources, 1)
}

func Test_Mock_AutoScalingLaunchConfiguration_Remove(t *testing.T) {
a := assert.New(t)
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockSvc := mock_autoscalingiface.NewMockAutoScalingAPI(ctrl)

mockSvc.EXPECT().DeleteLaunchConfiguration(gomock.Eq(&autoscaling.DeleteLaunchConfigurationInput{
LaunchConfigurationName: ptr.String("foo"),
})).Return(&autoscaling.DeleteLaunchConfigurationOutput{}, nil)

resource := AutoScalingLaunchConfiguration{
svc: mockSvc,
Name: ptr.String("foo"),
}

err := resource.Remove(context.TODO())
a.Nil(err)
}

func Test_Mock_AutoScalingLaunchConfiguration_Properties(t *testing.T) {
a := assert.New(t)
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockSvc := mock_autoscalingiface.NewMockAutoScalingAPI(ctrl)

resource := AutoScalingLaunchConfiguration{
svc: mockSvc,
Name: ptr.String("foo"),
}

properties := resource.Properties()

a.Equal("foo", properties.Get("Name"))
}
95 changes: 95 additions & 0 deletions resources/autoscaling-lifecycle-hook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package resources

import (
"context"

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

"github.com/ekristen/libnuke/pkg/registry"
"github.com/ekristen/libnuke/pkg/resource"
"github.com/ekristen/libnuke/pkg/types"

"github.com/ekristen/aws-nuke/v3/pkg/nuke"
)

const AutoScalingLifecycleHookResource = "AutoScalingLifecycleHook"

func init() {
registry.Register(&registry.Registration{
Name: AutoScalingLifecycleHookResource,
Scope: nuke.Account,
Lister: &AutoScalingLifecycleHookLister{},
DeprecatedAliases: []string{
"LifecycleHook",
},
})
}

type AutoScalingLifecycleHookLister struct {
mockSvc autoscalingiface.AutoScalingAPI
}

func (l *AutoScalingLifecycleHookLister) List(_ context.Context, o interface{}) ([]resource.Resource, error) {
opts := o.(*nuke.ListerOpts)

var svc autoscalingiface.AutoScalingAPI
if l.mockSvc != nil {
svc = l.mockSvc
} else {
svc = autoscaling.New(opts.Session)
}

asgResp, err := svc.DescribeAutoScalingGroups(&autoscaling.DescribeAutoScalingGroupsInput{})
if err != nil {
return nil, err
}

resources := make([]resource.Resource, 0)
for _, asg := range asgResp.AutoScalingGroups {
lchResp, err := svc.DescribeLifecycleHooks(&autoscaling.DescribeLifecycleHooksInput{
AutoScalingGroupName: asg.AutoScalingGroupName,
})
if err != nil {
return nil, err
}

for _, lch := range lchResp.LifecycleHooks {
resources = append(resources, &AutoScalingLifecycleHook{
svc: svc,
Name: lch.LifecycleHookName,
GroupName: lch.AutoScalingGroupName,
})
}
}

return resources, nil
}

type AutoScalingLifecycleHook struct {
svc autoscalingiface.AutoScalingAPI
Name *string
GroupName *string
}

func (r *AutoScalingLifecycleHook) Properties() types.Properties {
return types.NewPropertiesFromStruct(r)
}

func (r *AutoScalingLifecycleHook) Remove(_ context.Context) error {
params := &autoscaling.DeleteLifecycleHookInput{
AutoScalingGroupName: r.GroupName,
LifecycleHookName: r.Name,
}

_, err := r.svc.DeleteLifecycleHook(params)
if err != nil {
return err
}

return nil
}

func (r *AutoScalingLifecycleHook) String() string {
return *r.Name
}
Loading

0 comments on commit f3ae5f7

Please sign in to comment.