@@ -2,9 +2,14 @@ package resources
2
2
3
3
import (
4
4
"context"
5
+ "fmt"
6
+ "strings"
7
+
8
+ "github.com/sirupsen/logrus"
5
9
6
10
"github.com/aws/aws-sdk-go/aws"
7
11
"github.com/aws/aws-sdk-go/service/elasticache"
12
+ "github.com/aws/aws-sdk-go/service/elasticache/elasticacheiface"
8
13
9
14
"github.com/ekristen/libnuke/pkg/registry"
10
15
"github.com/ekristen/libnuke/pkg/resource"
@@ -13,78 +18,94 @@ import (
13
18
"github.com/ekristen/aws-nuke/v3/pkg/nuke"
14
19
)
15
20
16
- type ElasticacheUserGroup struct {
17
- svc * elasticache.ElastiCache
18
- groupID * string
19
- }
20
-
21
- const ElasticacheUserGroupResource = "ElasticacheUserGroup"
21
+ const ElasticacheSubnetGroupResource = "ElasticacheSubnetGroup"
22
22
23
23
func init () {
24
24
registry .Register (& registry.Registration {
25
- Name : ElasticacheUserGroupResource ,
25
+ Name : ElasticacheSubnetGroupResource ,
26
26
Scope : nuke .Account ,
27
- Lister : & ElasticacheUserGroupLister {},
27
+ Lister : & ElasticacheSubnetGroupLister {},
28
28
})
29
29
}
30
30
31
- type ElasticacheUserGroupLister struct {}
31
+ type ElasticacheSubnetGroupLister struct {
32
+ mockSvc elasticacheiface.ElastiCacheAPI
33
+ }
32
34
33
- func (l * ElasticacheUserGroupLister ) List (_ context.Context , o interface {}) ([]resource.Resource , error ) {
35
+ func (l * ElasticacheSubnetGroupLister ) List (_ context.Context , o interface {}) ([]resource.Resource , error ) {
34
36
opts := o .(* nuke.ListerOpts )
35
37
36
- svc := elasticache .New (opts .Session )
37
- resources := make ([]resource.Resource , 0 )
38
- var nextToken * string
38
+ var svc elasticacheiface.ElastiCacheAPI
39
+ if l .mockSvc != nil {
40
+ svc = l .mockSvc
41
+ } else {
42
+ svc = elasticache .New (opts .Session )
43
+ }
39
44
40
- for {
41
- params := & elasticache.DescribeUserGroupsInput {
42
- MaxRecords : aws .Int64 (100 ),
43
- Marker : nextToken ,
44
- }
45
- resp , err := svc .DescribeUserGroups (params )
45
+ params := & elasticache.DescribeCacheSubnetGroupsInput {MaxRecords : aws .Int64 (100 )}
46
+ resp , err := svc .DescribeCacheSubnetGroups (params )
47
+ if err != nil {
48
+ return nil , err
49
+ }
50
+
51
+ var resources []resource.Resource
52
+ for _ , subnetGroup := range resp .CacheSubnetGroups {
53
+ tags , err := svc .ListTagsForResource (& elasticache.ListTagsForResourceInput {
54
+ ResourceName : subnetGroup .ARN ,
55
+ })
46
56
if err != nil {
47
- return nil , err
57
+ logrus .WithError (err ).Error ("unable to retrieve tags" )
58
+ continue
48
59
}
49
60
50
- for _ , userGroup := range resp . UserGroups {
51
- resources = append ( resources , & ElasticacheUserGroup {
52
- svc : svc ,
53
- groupID : userGroup . UserGroupId ,
54
- })
55
- }
61
+ resources = append ( resources , & ElasticacheSubnetGroup {
62
+ svc : svc ,
63
+ name : subnetGroup . CacheSubnetGroupName ,
64
+ Tags : tags . TagList ,
65
+ })
66
+ }
56
67
57
- // Check if there are more results
58
- if resp .Marker == nil {
59
- break // No more results, exit the loop
60
- }
68
+ return resources , nil
69
+ }
61
70
62
- // Set the nextToken for the next iteration
63
- nextToken = resp .Marker
71
+ type ElasticacheSubnetGroup struct {
72
+ svc elasticacheiface.ElastiCacheAPI
73
+ name * string
74
+ Tags []* elasticache.Tag
75
+ }
76
+
77
+ func (i * ElasticacheSubnetGroup ) Filter () error {
78
+ if strings .HasPrefix (* i .name , "default" ) {
79
+ return fmt .Errorf ("cannot delete default subnet group" )
64
80
}
81
+ return nil
82
+ }
65
83
66
- return resources , nil
84
+ func (i * ElasticacheSubnetGroup ) Properties () types.Properties {
85
+ properties := types .NewProperties ()
86
+
87
+ properties .Set ("Name" , i .name )
88
+
89
+ for _ , tag := range i .Tags {
90
+ properties .SetTag (tag .Key , tag .Value )
91
+ }
92
+
93
+ return properties
67
94
}
68
95
69
- func (i * ElasticacheUserGroup ) Remove (_ context.Context ) error {
70
- params := & elasticache.DeleteUserGroupInput {
71
- UserGroupId : i .groupID ,
96
+ func (i * ElasticacheSubnetGroup ) Remove (_ context.Context ) error {
97
+ params := & elasticache.DeleteCacheSubnetGroupInput {
98
+ CacheSubnetGroupName : i .name ,
72
99
}
73
100
74
- _ , err := i .svc .DeleteUserGroup (params )
101
+ _ , err := i .svc .DeleteCacheSubnetGroup (params )
75
102
if err != nil {
76
103
return err
77
104
}
78
105
79
106
return nil
80
107
}
81
108
82
- func (i * ElasticacheUserGroup ) Properties () types.Properties {
83
- properties := types .NewProperties ()
84
- properties .Set ("ID" , i .groupID )
85
- return properties
86
- }
87
-
88
- func (i * ElasticacheUserGroup ) String () string {
89
- return * i .groupID
109
+ func (i * ElasticacheSubnetGroup ) String () string {
110
+ return * i .name
90
111
}
0 commit comments