From 8dd7d66307e6cfa472e0786ae6a02da2316866b1 Mon Sep 17 00:00:00 2001 From: Sherd White Date: Fri, 26 Apr 2024 14:03:00 -0500 Subject: [PATCH] Adding anomaly detection support. --- resources/cloudwatch-anomaly-detectors.go | 66 +++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 resources/cloudwatch-anomaly-detectors.go diff --git a/resources/cloudwatch-anomaly-detectors.go b/resources/cloudwatch-anomaly-detectors.go new file mode 100644 index 000000000..d39543df8 --- /dev/null +++ b/resources/cloudwatch-anomaly-detectors.go @@ -0,0 +1,66 @@ +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/cloudwatch" + "github.com/rebuy-de/aws-nuke/v2/pkg/types" +) + +type CloudWatchAnomalyDetector struct { + svc *cloudwatch.CloudWatch + detector *cloudwatch.AnomalyDetector +} + +func init() { + register("CloudWatchAnomalyDetector", ListCloudWatchAnomalyDetectors) +} + +func ListCloudWatchAnomalyDetectors(sess *session.Session) ([]Resource, error) { + svc := cloudwatch.New(sess) + resources := []Resource{} + + params := &cloudwatch.DescribeAnomalyDetectorsInput{ + MaxResults: aws.Int64(25), + } + + for { + output, err := svc.DescribeAnomalyDetectors(params) + if err != nil { + return nil, err + } + + for _, detector := range output.AnomalyDetectors { + resources = append(resources, &CloudWatchAnomalyDetector{ + svc: svc, + detector: detector, + }) + } + + if output.NextToken == nil { + break + } + + params.NextToken = output.NextToken + } + + return resources, nil +} + +func (f *CloudWatchAnomalyDetector) Remove() error { + _, err := f.svc.DeleteAnomalyDetector(&cloudwatch.DeleteAnomalyDetectorInput{ + SingleMetricAnomalyDetector: f.detector.SingleMetricAnomalyDetector, + }) + + return err +} + +func (f *CloudWatchAnomalyDetector) Properties() types.Properties { + properties := types.NewProperties() + properties.Set("MetricName", f.detector.SingleMetricAnomalyDetector.MetricName) + return properties +} + +func (f *CloudWatchAnomalyDetector) String() string { + return *f.detector.SingleMetricAnomalyDetector.MetricName +}