forked from gruntwork-io/cloud-nuke
/
cloudwatch_dashboard.go
87 lines (73 loc) · 2.85 KB
/
cloudwatch_dashboard.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
package resources
import (
"context"
"github.com/andrewderr/cloud-nuke-a1/telemetry"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatch"
"github.com/gruntwork-io/go-commons/errors"
commonTelemetry "github.com/gruntwork-io/go-commons/telemetry"
"github.com/andrewderr/cloud-nuke-a1/config"
"github.com/andrewderr/cloud-nuke-a1/logging"
"github.com/andrewderr/cloud-nuke-a1/report"
)
func (cwdb *CloudWatchDashboards) getAll(c context.Context, configObj config.Config) ([]*string, error) {
allDashboards := []*string{}
input := &cloudwatch.ListDashboardsInput{}
err := cwdb.Client.ListDashboardsPages(
input,
func(page *cloudwatch.ListDashboardsOutput, lastPage bool) bool {
for _, dashboard := range page.DashboardEntries {
if configObj.CloudWatchDashboard.ShouldInclude(config.ResourceValue{
Name: dashboard.DashboardName,
Time: dashboard.LastModified,
}) {
allDashboards = append(allDashboards, dashboard.DashboardName)
}
}
return !lastPage
},
)
return allDashboards, errors.WithStackTrace(err)
}
func (cwdb *CloudWatchDashboards) nukeAll(identifiers []*string) error {
if len(identifiers) == 0 {
logging.Debugf("No CloudWatch Dashboards to nuke in region %s", cwdb.Region)
return nil
}
// NOTE: we don't need to do pagination here, because the pagination is handled by the caller to this function,
// based on CloudWatchDashboard.MaxBatchSize, however we add a guard here to warn users when the batching fails and has a
// chance of throttling AWS. Since we concurrently make one call for each identifier, we pick 100 for the limit here
// because many APIs in AWS have a limit of 100 requests per second.
if len(identifiers) > 100 {
logging.Errorf("Nuking too many CloudWatch Dashboards at once (100): halting to avoid hitting AWS API rate limiting")
return TooManyCloudWatchDashboardsErr{}
}
logging.Debugf("Deleting CloudWatch Dashboards in region %s", cwdb.Region)
input := cloudwatch.DeleteDashboardsInput{DashboardNames: identifiers}
_, err := cwdb.Client.DeleteDashboards(&input)
// Record status of this resource
e := report.BatchEntry{
Identifiers: aws.StringValueSlice(identifiers),
ResourceType: "CloudWatch Dashboard",
Error: err,
}
report.RecordBatch(e)
if err != nil {
logging.Debugf("[Failed] %s", err)
telemetry.TrackEvent(commonTelemetry.EventContext{
EventName: "Error Nuking Cloudwatch Dashboard",
}, map[string]interface{}{
"region": cwdb.Region,
})
return errors.WithStackTrace(err)
}
for _, dashboardName := range identifiers {
logging.Debugf("[OK] CloudWatch Dashboard %s was deleted in %s", aws.StringValue(dashboardName), cwdb.Region)
}
return nil
}
// Custom errors
type TooManyCloudWatchDashboardsErr struct{}
func (err TooManyCloudWatchDashboardsErr) Error() string {
return "Too many CloudWatch Dashboards requested at once."
}