Skip to content

Commit 67be0c0

Browse files
Adding Elasticache User and UserGroup Support
Adding go modules for elasticache users and groups. Adding filtering for subnet groups to ignore the default elasticache subnet group.
1 parent 1ebde32 commit 67be0c0

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

resources/elasticache-subnetgroups.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package resources
22

33
import (
4+
"fmt"
5+
"strings"
6+
47
"github.com/aws/aws-sdk-go/aws"
58
"github.com/aws/aws-sdk-go/aws/session"
69
"github.com/aws/aws-sdk-go/service/elasticache"
@@ -35,6 +38,13 @@ func ListElasticacheSubnetGroups(sess *session.Session) ([]Resource, error) {
3538
return resources, nil
3639
}
3740

41+
func (i *ElasticacheSubnetGroup) Filter() error {
42+
if strings.HasPrefix(*i.name, "default") {
43+
return fmt.Errorf("Cannot delete default subnet group")
44+
}
45+
return nil
46+
}
47+
3848
func (i *ElasticacheSubnetGroup) Remove() error {
3949
params := &elasticache.DeleteCacheSubnetGroupInput{
4050
CacheSubnetGroupName: i.name,
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package resources
2+
3+
import (
4+
"github.com/aws/aws-sdk-go/aws"
5+
"github.com/aws/aws-sdk-go/aws/session"
6+
"github.com/aws/aws-sdk-go/service/elasticache"
7+
)
8+
9+
type ElasticacheUserGroup struct {
10+
svc *elasticache.ElastiCache
11+
groupId *string
12+
}
13+
14+
func init() {
15+
register("ElasticacheUserGroup", ListElasticacheUserGroups)
16+
}
17+
18+
func ListElasticacheUserGroups(sess *session.Session) ([]Resource, error) {
19+
svc := elasticache.New(sess)
20+
21+
params := &elasticache.DescribeUserGroupsInput{MaxRecords: aws.Int64(100)}
22+
resp, err := svc.DescribeUserGroups(params)
23+
if err != nil {
24+
return nil, err
25+
}
26+
var resources []Resource
27+
for _, userGroup := range resp.UserGroups {
28+
resources = append(resources, &ElasticacheUserGroup{
29+
svc: svc,
30+
groupId: userGroup.UserGroupId,
31+
})
32+
33+
}
34+
35+
return resources, nil
36+
}
37+
38+
func (i *ElasticacheUserGroup) Remove() error {
39+
params := &elasticache.DeleteUserGroupInput{
40+
UserGroupId: i.groupId,
41+
}
42+
43+
_, err := i.svc.DeleteUserGroup(params)
44+
if err != nil {
45+
return err
46+
}
47+
48+
return nil
49+
}
50+
51+
func (i *ElasticacheUserGroup) String() string {
52+
return *i.groupId
53+
}

resources/elasticache-users.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package resources
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/aws/aws-sdk-go/aws"
8+
"github.com/aws/aws-sdk-go/aws/session"
9+
"github.com/aws/aws-sdk-go/service/elasticache"
10+
)
11+
12+
type ElasticacheUser struct {
13+
svc *elasticache.ElastiCache
14+
userId *string
15+
userName *string
16+
}
17+
18+
func init() {
19+
register("ElasticacheUser", ListElasticacheUsers)
20+
}
21+
22+
func ListElasticacheUsers(sess *session.Session) ([]Resource, error) {
23+
svc := elasticache.New(sess)
24+
25+
params := &elasticache.DescribeUsersInput{MaxRecords: aws.Int64(100)}
26+
resp, err := svc.DescribeUsers(params)
27+
if err != nil {
28+
return nil, err
29+
}
30+
var resources []Resource
31+
for _, user := range resp.Users {
32+
resources = append(resources, &ElasticacheUser{
33+
svc: svc,
34+
userId: user.UserId,
35+
userName: user.UserName,
36+
})
37+
38+
}
39+
40+
return resources, nil
41+
}
42+
43+
func (i *ElasticacheUser) Filter() error {
44+
if strings.HasPrefix(*i.userName, "default") {
45+
return fmt.Errorf("Cannot delete default user")
46+
}
47+
return nil
48+
}
49+
50+
func (i *ElasticacheUser) Remove() error {
51+
params := &elasticache.DeleteUserInput{
52+
UserId: i.userId,
53+
}
54+
55+
_, err := i.svc.DeleteUser(params)
56+
if err != nil {
57+
return err
58+
}
59+
60+
return nil
61+
}
62+
63+
func (i *ElasticacheUser) String() string {
64+
return *i.userId
65+
}

0 commit comments

Comments
 (0)