This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 725
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add elasticache user and group support (#1044)
* 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. * Create opensearchservice-packages.go Adding working code for packages cleanup. * Delete opensearchservice-packages.go Moving opensearch changes to separate branch. * Updating elasticache user/group list calls with pagination. * Reverting versions to match oreilly-main Reverting versions to match oreilly-main * Updating go version to match upstream. * Updating to more closely match style of other resource types. * Adding properties to EC user/usergroups.
- Loading branch information
1 parent
a552489
commit 9874f48
Showing
3 changed files
with
171 additions
and
0 deletions.
There are no files selected for viewing
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
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,74 @@ | ||
package resources | ||
|
||
import ( | ||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/elasticache" | ||
"github.com/rebuy-de/aws-nuke/v2/pkg/types" | ||
) | ||
|
||
type ElasticacheUserGroup struct { | ||
svc *elasticache.ElastiCache | ||
groupId *string | ||
} | ||
|
||
func init() { | ||
register("ElasticacheUserGroup", ListElasticacheUserGroups) | ||
} | ||
|
||
func ListElasticacheUserGroups(sess *session.Session) ([]Resource, error) { | ||
svc := elasticache.New(sess) | ||
resources := []Resource{} | ||
var nextToken *string | ||
|
||
for { | ||
params := &elasticache.DescribeUserGroupsInput{ | ||
MaxRecords: aws.Int64(100), | ||
Marker: nextToken, | ||
} | ||
resp, err := svc.DescribeUserGroups(params) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, userGroup := range resp.UserGroups { | ||
resources = append(resources, &ElasticacheUserGroup{ | ||
svc: svc, | ||
groupId: userGroup.UserGroupId, | ||
}) | ||
} | ||
|
||
// Check if there are more results | ||
if resp.Marker == nil { | ||
break // No more results, exit the loop | ||
} | ||
|
||
// Set the nextToken for the next iteration | ||
nextToken = resp.Marker | ||
} | ||
|
||
return resources, nil | ||
} | ||
|
||
func (i *ElasticacheUserGroup) Remove() error { | ||
params := &elasticache.DeleteUserGroupInput{ | ||
UserGroupId: i.groupId, | ||
} | ||
|
||
_, err := i.svc.DeleteUserGroup(params) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (i *ElasticacheUserGroup) Properties() types.Properties { | ||
properties := types.NewProperties() | ||
properties.Set("ID", i.groupId) | ||
return properties | ||
} | ||
|
||
func (i *ElasticacheUserGroup) String() string { | ||
return *i.groupId | ||
} |
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,87 @@ | ||
package resources | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/elasticache" | ||
"github.com/rebuy-de/aws-nuke/v2/pkg/types" | ||
) | ||
|
||
type ElasticacheUser struct { | ||
svc *elasticache.ElastiCache | ||
userId *string | ||
userName *string | ||
} | ||
|
||
func init() { | ||
register("ElasticacheUser", ListElasticacheUsers) | ||
} | ||
|
||
func ListElasticacheUsers(sess *session.Session) ([]Resource, error) { | ||
svc := elasticache.New(sess) | ||
resources := []Resource{} | ||
var nextToken *string | ||
|
||
for { | ||
params := &elasticache.DescribeUsersInput{ | ||
MaxRecords: aws.Int64(100), | ||
Marker: nextToken, | ||
} | ||
resp, err := svc.DescribeUsers(params) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, user := range resp.Users { | ||
resources = append(resources, &ElasticacheUser{ | ||
svc: svc, | ||
userId: user.UserId, | ||
userName: user.UserName, | ||
}) | ||
} | ||
|
||
// Check if there are more results | ||
if resp.Marker == nil { | ||
break // No more results, exit the loop | ||
} | ||
|
||
// Set the nextToken for the next iteration | ||
nextToken = resp.Marker | ||
} | ||
|
||
return resources, nil | ||
} | ||
|
||
func (i *ElasticacheUser) Filter() error { | ||
if strings.HasPrefix(*i.userName, "default") { | ||
return fmt.Errorf("cannot delete default user") | ||
} | ||
return nil | ||
} | ||
|
||
func (i *ElasticacheUser) Remove() error { | ||
params := &elasticache.DeleteUserInput{ | ||
UserId: i.userId, | ||
} | ||
|
||
_, err := i.svc.DeleteUser(params) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (i *ElasticacheUser) Properties() types.Properties { | ||
properties := types.NewProperties() | ||
properties.Set("ID", i.userId) | ||
properties.Set("UserName", i.userName) | ||
return properties | ||
} | ||
|
||
func (i *ElasticacheUser) String() string { | ||
return *i.userId | ||
} |