forked from ekristen/aws-nuke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoscaling-groups.go
88 lines (68 loc) · 2.01 KB
/
autoscaling-groups.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package resources
import (
"context"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/autoscaling"
"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 AutoScalingGroupResource = "AutoScalingGroup"
func init() {
registry.Register(®istry.Registration{
Name: AutoScalingGroupResource,
Scope: nuke.Account,
Lister: &AutoScalingGroupLister{},
})
}
type AutoScalingGroupLister struct{}
func (l *AutoScalingGroupLister) List(_ context.Context, o interface{}) ([]resource.Resource, error) {
opts := o.(*nuke.ListerOpts)
svc := autoscaling.New(opts.Session)
resources := make([]resource.Resource, 0)
params := &autoscaling.DescribeAutoScalingGroupsInput{}
err := svc.DescribeAutoScalingGroupsPages(params,
func(page *autoscaling.DescribeAutoScalingGroupsOutput, lastPage bool) bool {
for _, asg := range page.AutoScalingGroups {
resources = append(resources, &AutoScalingGroup{
group: asg,
svc: svc,
tags: asg.Tags,
})
}
return !lastPage
})
if err != nil {
return nil, err
}
return resources, nil
}
type AutoScalingGroup struct {
svc *autoscaling.AutoScaling
group *autoscaling.Group
tags []*autoscaling.TagDescription
}
func (asg *AutoScalingGroup) Remove(_ context.Context) error {
params := &autoscaling.DeleteAutoScalingGroupInput{
AutoScalingGroupName: asg.group.AutoScalingGroupName,
ForceDelete: aws.Bool(true),
}
_, err := asg.svc.DeleteAutoScalingGroup(params)
if err != nil {
return err
}
return nil
}
func (asg *AutoScalingGroup) String() string {
return *asg.group.AutoScalingGroupName
}
func (asg *AutoScalingGroup) Properties() types.Properties {
properties := types.NewProperties()
for _, tag := range asg.tags {
properties.SetTag(tag.Key, tag.Value)
}
properties.Set("CreatedTime", asg.group.CreatedTime)
properties.Set("Name", asg.group.AutoScalingGroupName)
return properties
}