Skip to content

Commit a3a9494

Browse files
Merge pull request #24 from oreillymedia/CL-508-add-cloudwatch-app-monitoring
CL-508 Adding support for CloudWatch Insight Rules
2 parents 89f3f5f + 8dd7d66 commit a3a9494

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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/cloudwatch"
7+
"github.com/rebuy-de/aws-nuke/v2/pkg/types"
8+
)
9+
10+
type CloudWatchAnomalyDetector struct {
11+
svc *cloudwatch.CloudWatch
12+
detector *cloudwatch.AnomalyDetector
13+
}
14+
15+
func init() {
16+
register("CloudWatchAnomalyDetector", ListCloudWatchAnomalyDetectors)
17+
}
18+
19+
func ListCloudWatchAnomalyDetectors(sess *session.Session) ([]Resource, error) {
20+
svc := cloudwatch.New(sess)
21+
resources := []Resource{}
22+
23+
params := &cloudwatch.DescribeAnomalyDetectorsInput{
24+
MaxResults: aws.Int64(25),
25+
}
26+
27+
for {
28+
output, err := svc.DescribeAnomalyDetectors(params)
29+
if err != nil {
30+
return nil, err
31+
}
32+
33+
for _, detector := range output.AnomalyDetectors {
34+
resources = append(resources, &CloudWatchAnomalyDetector{
35+
svc: svc,
36+
detector: detector,
37+
})
38+
}
39+
40+
if output.NextToken == nil {
41+
break
42+
}
43+
44+
params.NextToken = output.NextToken
45+
}
46+
47+
return resources, nil
48+
}
49+
50+
func (f *CloudWatchAnomalyDetector) Remove() error {
51+
_, err := f.svc.DeleteAnomalyDetector(&cloudwatch.DeleteAnomalyDetectorInput{
52+
SingleMetricAnomalyDetector: f.detector.SingleMetricAnomalyDetector,
53+
})
54+
55+
return err
56+
}
57+
58+
func (f *CloudWatchAnomalyDetector) Properties() types.Properties {
59+
properties := types.NewProperties()
60+
properties.Set("MetricName", f.detector.SingleMetricAnomalyDetector.MetricName)
61+
return properties
62+
}
63+
64+
func (f *CloudWatchAnomalyDetector) String() string {
65+
return *f.detector.SingleMetricAnomalyDetector.MetricName
66+
}

resources/cloudwatch-insight-rules.go

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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/cloudwatch"
7+
"github.com/rebuy-de/aws-nuke/v2/pkg/types"
8+
)
9+
10+
type CloudWatchInsightRule struct {
11+
svc *cloudwatch.CloudWatch
12+
name *string
13+
}
14+
15+
func init() {
16+
register("CloudWatchInsightRule", ListCloudWatchInsightRules)
17+
}
18+
19+
func ListCloudWatchInsightRules(sess *session.Session) ([]Resource, error) {
20+
svc := cloudwatch.New(sess)
21+
resources := []Resource{}
22+
23+
params := &cloudwatch.DescribeInsightRulesInput{
24+
MaxResults: aws.Int64(25),
25+
}
26+
27+
for {
28+
output, err := svc.DescribeInsightRules(params)
29+
if err != nil {
30+
return nil, err
31+
}
32+
33+
for _, rules := range output.InsightRules {
34+
resources = append(resources, &CloudWatchInsightRule{
35+
svc: svc,
36+
name: rules.Name,
37+
})
38+
}
39+
40+
if output.NextToken == nil {
41+
break
42+
}
43+
44+
params.NextToken = output.NextToken
45+
}
46+
47+
return resources, nil
48+
}
49+
50+
func (f *CloudWatchInsightRule) Remove() error {
51+
_, err := f.svc.DeleteInsightRules(&cloudwatch.DeleteInsightRulesInput{
52+
RuleNames: []*string{f.name},
53+
})
54+
55+
return err
56+
}
57+
58+
func (f *CloudWatchInsightRule) Properties() types.Properties {
59+
properties := types.NewProperties()
60+
properties.Set("Name", f.name)
61+
return properties
62+
}
63+
64+
func (f *CloudWatchInsightRule) String() string {
65+
return *f.name
66+
}

0 commit comments

Comments
 (0)