From 193189ed6421ca64ca5dec0da8468ea343d637fd Mon Sep 17 00:00:00 2001 From: Craig Bryan Date: Tue, 20 Dec 2022 20:44:53 -0600 Subject: [PATCH 1/2] Add tag filtering for cloudwatch alarms --- resources/cloudwatch-alarms.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/resources/cloudwatch-alarms.go b/resources/cloudwatch-alarms.go index 263051b7b..c2e0a07c2 100644 --- a/resources/cloudwatch-alarms.go +++ b/resources/cloudwatch-alarms.go @@ -10,6 +10,7 @@ import ( type CloudWatchAlarm struct { svc *cloudwatch.CloudWatch alarmName *string + tags []*cloudwatch.Tag } func init() { @@ -34,6 +35,7 @@ func ListCloudWatchAlarms(sess *session.Session) ([]Resource, error) { resources = append(resources, &CloudWatchAlarm{ svc: svc, alarmName: metricAlarm.AlarmName, + tags: GetAlarmTags(svc, metricAlarm.AlarmArn), }) } @@ -47,6 +49,11 @@ func ListCloudWatchAlarms(sess *session.Session) ([]Resource, error) { return resources, nil } +func GetAlarmTags(svc *cloudwatch.CloudWatch, arn *string) []*cloudwatch.Tag { + resp, _ := svc.ListTagsForResource(&cloudwatch.ListTagsForResourceInput{ResourceARN: arn}) + return resp.Tags +} + func (f *CloudWatchAlarm) Remove() error { _, err := f.svc.DeleteAlarms(&cloudwatch.DeleteAlarmsInput{ @@ -57,8 +64,13 @@ func (f *CloudWatchAlarm) Remove() error { } func (f *CloudWatchAlarm) Properties() types.Properties { - return types.NewProperties(). - Set("Name", f.alarmName) + properties := types.NewProperties() + properties.Set("Name", f.alarmName) + + for _, tag := range f.tags { + properties.SetTag(tag.Key, tag.Value) + } + return properties } func (f *CloudWatchAlarm) String() string { From ca8d8c4c4e3ea563584c22c6a50382f247d343b1 Mon Sep 17 00:00:00 2001 From: Craig Bryan Date: Wed, 21 Dec 2022 11:25:23 -0600 Subject: [PATCH 2/2] Caught errors from the cloudwatch 'get tags for resource' api call --- resources/cloudwatch-alarms.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/resources/cloudwatch-alarms.go b/resources/cloudwatch-alarms.go index c2e0a07c2..55fa2d839 100644 --- a/resources/cloudwatch-alarms.go +++ b/resources/cloudwatch-alarms.go @@ -32,10 +32,14 @@ func ListCloudWatchAlarms(sess *session.Session) ([]Resource, error) { } for _, metricAlarm := range output.MetricAlarms { + tags, err := GetAlarmTags(svc, metricAlarm.AlarmArn) + if err != nil { + return nil, err + } resources = append(resources, &CloudWatchAlarm{ svc: svc, alarmName: metricAlarm.AlarmName, - tags: GetAlarmTags(svc, metricAlarm.AlarmArn), + tags: tags, }) } @@ -49,9 +53,13 @@ func ListCloudWatchAlarms(sess *session.Session) ([]Resource, error) { return resources, nil } -func GetAlarmTags(svc *cloudwatch.CloudWatch, arn *string) []*cloudwatch.Tag { - resp, _ := svc.ListTagsForResource(&cloudwatch.ListTagsForResourceInput{ResourceARN: arn}) - return resp.Tags +func GetAlarmTags(svc *cloudwatch.CloudWatch, arn *string) ([]*cloudwatch.Tag, error) { + resp, err := svc.ListTagsForResource(&cloudwatch.ListTagsForResourceInput{ResourceARN: arn}) + if err != nil { + return nil, err + } + + return resp.Tags, nil } func (f *CloudWatchAlarm) Remove() error {