Skip to content

Commit

Permalink
Merge pull request #270 from ekristen/rebuy-1253-cloudwatch
Browse files Browse the repository at this point in the history
feat: adds two new resources cloudwatch-anomaly-detector, cloudwatch-insight-rule
  • Loading branch information
ekristen authored Aug 31, 2024
2 parents 960da21 + 6a9f157 commit bd7ff3e
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 0 deletions.
83 changes: 83 additions & 0 deletions resources/cloudwatch-anomaly-detector.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package resources

import (
"context"

"github.com/aws/aws-sdk-go/aws"

"github.com/aws/aws-sdk-go/service/cloudwatch"

"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 CloudWatchAnomalyDetectorResource = "CloudWatchAnomalyDetector"

func init() {
registry.Register(&registry.Registration{
Name: CloudWatchAnomalyDetectorResource,
Scope: nuke.Account,
Lister: &CloudWatchAnomalyDetectorLister{},
})
}

type CloudWatchAnomalyDetectorLister struct{}

func (l *CloudWatchAnomalyDetectorLister) List(_ context.Context, o interface{}) ([]resource.Resource, error) {
opts := o.(*nuke.ListerOpts)

svc := cloudwatch.New(opts.Session)
resources := make([]resource.Resource, 0)

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.SingleMetricAnomalyDetector,
MetricName: detector.SingleMetricAnomalyDetector.MetricName,
})
}

if output.NextToken == nil {
break
}

params.NextToken = output.NextToken
}

return resources, nil
}

type CloudWatchAnomalyDetector struct {
svc *cloudwatch.CloudWatch
detector *cloudwatch.SingleMetricAnomalyDetector
MetricName *string
}

func (r *CloudWatchAnomalyDetector) Remove(_ context.Context) error {
_, err := r.svc.DeleteAnomalyDetector(&cloudwatch.DeleteAnomalyDetectorInput{
SingleMetricAnomalyDetector: r.detector,
})

return err
}

func (r *CloudWatchAnomalyDetector) Properties() types.Properties {
return types.NewPropertiesFromStruct(r)
}

func (r *CloudWatchAnomalyDetector) String() string {
return *r.MetricName
}
83 changes: 83 additions & 0 deletions resources/cloudwatch-insight-rule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package resources

import (
"context"

"github.com/aws/aws-sdk-go/aws"

"github.com/aws/aws-sdk-go/service/cloudwatch"

"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 CloudWatchInsightRuleResource = "CloudWatchInsightRule"

func init() {
registry.Register(&registry.Registration{
Name: CloudWatchInsightRuleResource,
Scope: nuke.Account,
Lister: &CloudWatchInsightRuleLister{},
})
}

type CloudWatchInsightRuleLister struct{}

func (l *CloudWatchInsightRuleLister) List(_ context.Context, o interface{}) ([]resource.Resource, error) {
opts := o.(*nuke.ListerOpts)

svc := cloudwatch.New(opts.Session)
resources := make([]resource.Resource, 0)

params := &cloudwatch.DescribeInsightRulesInput{
MaxResults: aws.Int64(25),
}

for {
output, err := svc.DescribeInsightRules(params)
if err != nil {
return nil, err
}

for _, rules := range output.InsightRules {
resources = append(resources, &CloudWatchInsightRule{
svc: svc,
Name: rules.Name,
State: rules.State,
})
}

if output.NextToken == nil {
break
}

params.NextToken = output.NextToken
}

return resources, nil
}

type CloudWatchInsightRule struct {
svc *cloudwatch.CloudWatch
Name *string
State *string
}

func (r *CloudWatchInsightRule) Remove(_ context.Context) error {
_, err := r.svc.DeleteInsightRules(&cloudwatch.DeleteInsightRulesInput{
RuleNames: []*string{r.Name},
})

return err
}

func (r *CloudWatchInsightRule) Properties() types.Properties {
return types.NewPropertiesFromStruct(r)
}

func (r *CloudWatchInsightRule) String() string {
return *r.Name
}

0 comments on commit bd7ff3e

Please sign in to comment.