-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #215 from ekristen/fix-autoscaling-resources
feat(autoscaling): standardize and test lifecycle hook and launch configuration resources
- Loading branch information
Showing
8 changed files
with
4,191 additions
and
154 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(®istry.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 | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(®istry.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 | ||
} |
Oops, something went wrong.