Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit fe8da22

Browse files
authored
Add tag filtering for cloudwatch alarms (#900)
* Add tag filtering for cloudwatch alarms * Caught errors from the cloudwatch 'get tags for resource' api call
1 parent 022025a commit fe8da22

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

resources/cloudwatch-alarms.go

+22-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
type CloudWatchAlarm struct {
1111
svc *cloudwatch.CloudWatch
1212
alarmName *string
13+
tags []*cloudwatch.Tag
1314
}
1415

1516
func init() {
@@ -31,9 +32,14 @@ func ListCloudWatchAlarms(sess *session.Session) ([]Resource, error) {
3132
}
3233

3334
for _, metricAlarm := range output.MetricAlarms {
35+
tags, err := GetAlarmTags(svc, metricAlarm.AlarmArn)
36+
if err != nil {
37+
return nil, err
38+
}
3439
resources = append(resources, &CloudWatchAlarm{
3540
svc: svc,
3641
alarmName: metricAlarm.AlarmName,
42+
tags: tags,
3743
})
3844
}
3945

@@ -47,6 +53,15 @@ func ListCloudWatchAlarms(sess *session.Session) ([]Resource, error) {
4753
return resources, nil
4854
}
4955

56+
func GetAlarmTags(svc *cloudwatch.CloudWatch, arn *string) ([]*cloudwatch.Tag, error) {
57+
resp, err := svc.ListTagsForResource(&cloudwatch.ListTagsForResourceInput{ResourceARN: arn})
58+
if err != nil {
59+
return nil, err
60+
}
61+
62+
return resp.Tags, nil
63+
}
64+
5065
func (f *CloudWatchAlarm) Remove() error {
5166

5267
_, err := f.svc.DeleteAlarms(&cloudwatch.DeleteAlarmsInput{
@@ -57,8 +72,13 @@ func (f *CloudWatchAlarm) Remove() error {
5772
}
5873

5974
func (f *CloudWatchAlarm) Properties() types.Properties {
60-
return types.NewProperties().
61-
Set("Name", f.alarmName)
75+
properties := types.NewProperties()
76+
properties.Set("Name", f.alarmName)
77+
78+
for _, tag := range f.tags {
79+
properties.SetTag(tag.Key, tag.Value)
80+
}
81+
return properties
6282
}
6383

6484
func (f *CloudWatchAlarm) String() string {

0 commit comments

Comments
 (0)