-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #270 from ekristen/rebuy-1253-cloudwatch
feat: adds two new resources cloudwatch-anomaly-detector, cloudwatch-insight-rule
- Loading branch information
Showing
2 changed files
with
166 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(®istry.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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(®istry.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 | ||
} |