forked from ekristen/aws-nuke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathathena-work-group.go
181 lines (154 loc) · 4.57 KB
/
athena-work-group.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package resources
import (
"context"
"errors"
"fmt"
"github.com/gotidy/ptr"
"github.com/sirupsen/logrus"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/athena"
"github.com/ekristen/libnuke/pkg/registry"
"github.com/ekristen/libnuke/pkg/resource"
"github.com/ekristen/libnuke/pkg/types"
"github.com/ekristen/aws-nuke/v3/pkg/nuke"
)
const AthenaWorkGroupResource = "AthenaWorkGroup"
func init() {
registry.Register(®istry.Registration{
Name: AthenaWorkGroupResource,
Scope: nuke.Account,
Lister: &AthenaWorkGroupLister{},
})
}
type AthenaWorkGroupLister struct{}
func (l *AthenaWorkGroupLister) List(_ context.Context, o interface{}) ([]resource.Resource, error) {
opts := o.(*nuke.ListerOpts)
svc := athena.New(opts.Session)
resources := make([]resource.Resource, 0)
// List WorkGroup
var workgroupNames []*string
err := svc.ListWorkGroupsPages(
&athena.ListWorkGroupsInput{},
func(page *athena.ListWorkGroupsOutput, lastPage bool) bool {
for _, workgroup := range page.WorkGroups {
workgroupNames = append(workgroupNames, workgroup.Name)
}
return true
},
)
if err != nil {
return nil, err
}
// Create AthenaWorkGroup resource objects
for _, name := range workgroupNames {
resources = append(resources, &AthenaWorkGroup{
svc: svc,
name: name,
// The GetWorkGroup API doesn't return an ARN,
// so we need to construct one ourselves
arn: aws.String(fmt.Sprintf(
"arn:aws:athena:%s:%s:workgroup/%s",
opts.Region.Name, *opts.AccountID, *name,
)),
})
}
return resources, err
}
type AthenaWorkGroup struct {
svc *athena.Athena
name *string
arn *string
}
func (r *AthenaWorkGroup) Remove(_ context.Context) error {
// Primary WorkGroup cannot be deleted,
// but we can reset it to r clean state
if *r.name == "primary" {
// TODO: pass logger via ListerOpts instead of using global
logrus.Info("Primary Athena WorkGroup may not be deleted. Resetting configuration only.")
// Reset the configuration to its default state
_, err := r.svc.UpdateWorkGroup(&athena.UpdateWorkGroupInput{
// See https://docs.aws.amazon.com/athena/latest/APIReference/API_WorkGroupConfigurationUpdates.html
// for documented defaults
ConfigurationUpdates: &athena.WorkGroupConfigurationUpdates{
EnforceWorkGroupConfiguration: aws.Bool(false),
PublishCloudWatchMetricsEnabled: aws.Bool(false),
RemoveBytesScannedCutoffPerQuery: aws.Bool(true),
RequesterPaysEnabled: aws.Bool(false),
ResultConfigurationUpdates: &athena.ResultConfigurationUpdates{
RemoveEncryptionConfiguration: aws.Bool(true),
RemoveOutputLocation: aws.Bool(true),
},
},
Description: aws.String(""),
WorkGroup: r.name,
})
if err != nil {
return err
}
// Remove any tags
wgTagsRes, err := r.svc.ListTagsForResource(&athena.ListTagsForResourceInput{
ResourceARN: r.arn,
})
if err != nil {
return err
}
var tagKeys []*string
for _, tag := range wgTagsRes.Tags {
tagKeys = append(tagKeys, tag.Key)
}
_, err = r.svc.UntagResource(&athena.UntagResourceInput{
ResourceARN: r.arn,
TagKeys: tagKeys,
})
if err != nil {
return err
}
return nil
}
_, err := r.svc.DeleteWorkGroup(&athena.DeleteWorkGroupInput{
RecursiveDeleteOption: aws.Bool(true),
WorkGroup: r.name,
})
return err
}
func (r *AthenaWorkGroup) Filter() error {
// If this is the primary work group,
// check if it's already had its configuration reset
if *r.name == "primary" {
// Get workgroup configuration
wgConfigRes, err := r.svc.GetWorkGroup(&athena.GetWorkGroupInput{
WorkGroup: r.name,
})
if err != nil {
return err
}
// Get workgroup tags
wgTagsRes, err := r.svc.ListTagsForResource(&athena.ListTagsForResourceInput{
ResourceARN: r.arn,
})
if err != nil {
return err
}
// If the workgroup is already in r "clean" state, then
// don't add it to our plan
wgConfig := wgConfigRes.WorkGroup.Configuration
isCleanConfig := wgConfig.BytesScannedCutoffPerQuery == nil &&
!ptr.ToBool(wgConfig.EnforceWorkGroupConfiguration) &&
!ptr.ToBool(wgConfig.PublishCloudWatchMetricsEnabled) &&
!ptr.ToBool(wgConfig.RequesterPaysEnabled) &&
*wgConfig.ResultConfiguration == athena.ResultConfiguration{} &&
len(wgTagsRes.Tags) == 0
if isCleanConfig {
return errors.New("cannot delete primary athena work group")
}
}
return nil
}
func (r *AthenaWorkGroup) Properties() types.Properties {
return types.NewProperties().
Set("Name", *r.name).
Set("ARN", *r.arn)
}
func (r *AthenaWorkGroup) String() string {
return *r.name
}