Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit 3e60ecd

Browse files
author
Tomasz Fratczak
committed
adds nuking of dynamodb-items and dynamodb-tables
adds dynamodb-items nuking adds dynamodb-table nuking
1 parent 5236b44 commit 3e60ecd

File tree

5 files changed

+135
-0
lines changed

5 files changed

+135
-0
lines changed

resources/dynamodb-items.go

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package resources
2+
3+
import (
4+
"strings"
5+
6+
"github.com/aws/aws-sdk-go/aws"
7+
"github.com/aws/aws-sdk-go/service/dynamodb"
8+
)
9+
10+
type DynamoDBTableItem struct {
11+
svc *dynamodb.DynamoDB
12+
id map[string]*dynamodb.AttributeValue
13+
table Resource
14+
}
15+
16+
func (n *DynamoDBNuke) ListItems() ([]Resource, error) {
17+
tables, tablesErr := n.ListTables()
18+
if tablesErr != nil {
19+
return nil, tablesErr
20+
}
21+
22+
resources := make([]Resource, 0)
23+
for _, dynamoTable := range tables {
24+
describeParams := &dynamodb.DescribeTableInput{
25+
TableName: aws.String(dynamoTable.String()),
26+
}
27+
28+
descResp, descErr := n.Service.DescribeTable(describeParams)
29+
if descErr != nil {
30+
return nil, descErr
31+
}
32+
33+
key := *descResp.Table.KeySchema[0].AttributeName
34+
params := &dynamodb.ScanInput{
35+
TableName: aws.String(dynamoTable.String()),
36+
ProjectionExpression: aws.String(key),
37+
}
38+
39+
scanResp, scanErr := n.Service.Scan(params)
40+
if scanErr != nil {
41+
return nil, scanErr
42+
}
43+
44+
for _, itemMap := range scanResp.Items {
45+
resources = append(resources, &DynamoDBTableItem{
46+
svc: n.Service,
47+
id: itemMap,
48+
table: dynamoTable,
49+
})
50+
}
51+
}
52+
53+
return resources, nil
54+
}
55+
56+
func (i *DynamoDBTableItem) Remove() error {
57+
params := &dynamodb.DeleteItemInput{
58+
Key: i.id,
59+
TableName: aws.String(i.table.String()),
60+
}
61+
62+
_, err := i.svc.DeleteItem(params)
63+
if err != nil {
64+
return err
65+
}
66+
67+
return nil
68+
}
69+
70+
func (i *DynamoDBTableItem) String() string {
71+
table := i.table.String()
72+
var keyField string
73+
74+
for _, value := range i.id {
75+
value := strings.TrimSpace(value.String())
76+
keyField = string([]rune(value)[8:(len([]rune(value)) - 3)])
77+
}
78+
79+
return table + " -> " + keyField
80+
}

resources/dynamodb-tables.go

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package resources
2+
3+
import (
4+
"github.com/aws/aws-sdk-go/aws"
5+
"github.com/aws/aws-sdk-go/service/dynamodb"
6+
)
7+
8+
type DynamoDBTable struct {
9+
svc *dynamodb.DynamoDB
10+
id string
11+
}
12+
13+
func (n *DynamoDBNuke) ListTables() ([]Resource, error) {
14+
resp, err := n.Service.ListTables(&dynamodb.ListTablesInput{})
15+
if err != nil {
16+
return nil, err
17+
}
18+
19+
resources := make([]Resource, 0)
20+
for _, tableName := range resp.TableNames {
21+
resources = append(resources, &DynamoDBTable{
22+
svc: n.Service,
23+
id: *tableName,
24+
})
25+
}
26+
27+
return resources, nil
28+
}
29+
30+
func (i *DynamoDBTable) Remove() error {
31+
params := &dynamodb.DeleteTableInput{
32+
TableName: aws.String(i.id),
33+
}
34+
35+
_, err := i.svc.DeleteTable(params)
36+
if err != nil {
37+
return err
38+
}
39+
40+
return nil
41+
}
42+
43+
func (i *DynamoDBTable) String() string {
44+
return i.id
45+
}

resources/listers.go

+4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"github.com/aws/aws-sdk-go/service/cloudformation"
77
"github.com/aws/aws-sdk-go/service/cloudtrail"
88
"github.com/aws/aws-sdk-go/service/cloudwatchevents"
9+
"github.com/aws/aws-sdk-go/service/dynamodb"
910
"github.com/aws/aws-sdk-go/service/ec2"
1011
"github.com/aws/aws-sdk-go/service/ecr"
1112
"github.com/aws/aws-sdk-go/service/efs"
@@ -37,6 +38,7 @@ func GetListers(sess *session.Session) []ResourceLister {
3738
kms = KMSNuke{kms.New(sess)}
3839
lambda = LambdaNuke{lambda.New(sess)}
3940
rds = RDSNuke{rds.New(sess)}
41+
dynamodb = DynamoDBNuke{dynamodb.New(sess)}
4042
route53 = Route53Nuke{route53.New(sess)}
4143
s3 = S3Nuke{s3.New(sess)}
4244
sns = SNSNuke{sns.New(sess)}
@@ -98,6 +100,8 @@ func GetListers(sess *session.Session) []ResourceLister {
98100
rds.ListParameterGroups,
99101
rds.ListSnapshots,
100102
rds.ListSubnetGroups,
103+
dynamodb.ListItems,
104+
dynamodb.ListTables,
101105
route53.ListHostedZones,
102106
route53.ListResourceRecords,
103107
s3.ListBuckets,

resources/rds-dbparametergroups.go

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package resources
33
import (
44
"fmt"
55
"strings"
6+
67
"github.com/aws/aws-sdk-go/aws"
78
"github.com/aws/aws-sdk-go/service/rds"
89
)

resources/types.go

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/aws/aws-sdk-go/service/cloudformation"
66
"github.com/aws/aws-sdk-go/service/cloudtrail"
77
"github.com/aws/aws-sdk-go/service/cloudwatchevents"
8+
"github.com/aws/aws-sdk-go/service/dynamodb"
89
"github.com/aws/aws-sdk-go/service/ec2"
910
"github.com/aws/aws-sdk-go/service/ecr"
1011
"github.com/aws/aws-sdk-go/service/efs"
@@ -72,6 +73,10 @@ type RDSNuke struct {
7273
Service *rds.RDS
7374
}
7475

76+
type DynamoDBNuke struct {
77+
Service *dynamodb.DynamoDB
78+
}
79+
7580
type Route53Nuke struct {
7681
Service *route53.Route53
7782
}

0 commit comments

Comments
 (0)