Skip to content

Commit 584b92a

Browse files
committed
oreilly modules need refactor. Temporarily move them to need_refactor dir to prevent aws-nuke from breaking
1 parent aecdbee commit 584b92a

File tree

5 files changed

+67
-46
lines changed

5 files changed

+67
-46
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/ekristen/aws-nuke/v3
22

3-
go 1.21
3+
go 1.21.6
44

55
require (
66
github.com/aws/aws-sdk-go v1.54.20
File renamed without changes.
File renamed without changes.
File renamed without changes.

resources/elasticache-subnetgroups.go

Lines changed: 66 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ package resources
22

33
import (
44
"context"
5+
"fmt"
6+
"strings"
7+
8+
"github.com/sirupsen/logrus"
59

610
"github.com/aws/aws-sdk-go/aws"
711
"github.com/aws/aws-sdk-go/service/elasticache"
12+
"github.com/aws/aws-sdk-go/service/elasticache/elasticacheiface"
813

914
"github.com/ekristen/libnuke/pkg/registry"
1015
"github.com/ekristen/libnuke/pkg/resource"
@@ -13,78 +18,94 @@ import (
1318
"github.com/ekristen/aws-nuke/v3/pkg/nuke"
1419
)
1520

16-
type ElasticacheUserGroup struct {
17-
svc *elasticache.ElastiCache
18-
groupID *string
19-
}
20-
21-
const ElasticacheUserGroupResource = "ElasticacheUserGroup"
21+
const ElasticacheSubnetGroupResource = "ElasticacheSubnetGroup"
2222

2323
func init() {
2424
registry.Register(&registry.Registration{
25-
Name: ElasticacheUserGroupResource,
25+
Name: ElasticacheSubnetGroupResource,
2626
Scope: nuke.Account,
27-
Lister: &ElasticacheUserGroupLister{},
27+
Lister: &ElasticacheSubnetGroupLister{},
2828
})
2929
}
3030

31-
type ElasticacheUserGroupLister struct{}
31+
type ElasticacheSubnetGroupLister struct {
32+
mockSvc elasticacheiface.ElastiCacheAPI
33+
}
3234

33-
func (l *ElasticacheUserGroupLister) List(_ context.Context, o interface{}) ([]resource.Resource, error) {
35+
func (l *ElasticacheSubnetGroupLister) List(_ context.Context, o interface{}) ([]resource.Resource, error) {
3436
opts := o.(*nuke.ListerOpts)
3537

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+
}
3944

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+
})
4656
if err != nil {
47-
return nil, err
57+
logrus.WithError(err).Error("unable to retrieve tags")
58+
continue
4859
}
4960

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+
}
5667

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+
}
6170

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")
6480
}
81+
return nil
82+
}
6583

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
6794
}
6895

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,
7299
}
73100

74-
_, err := i.svc.DeleteUserGroup(params)
101+
_, err := i.svc.DeleteCacheSubnetGroup(params)
75102
if err != nil {
76103
return err
77104
}
78105

79106
return nil
80107
}
81108

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
90111
}

0 commit comments

Comments
 (0)