/
metric.go
194 lines (175 loc) · 4.92 KB
/
metric.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Package metric provides functions and filters to test AWS CloudWatch Metrics
package metric
import (
"log"
"os"
"strings"
"testing"
"github.com/GSA/grace-tftest/aws/cloudwatch/metric/alarm"
"github.com/GSA/grace-tftest/aws/shared"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/client"
"github.com/aws/aws-sdk-go/service/cloudwatch"
)
// Metric contains the necessary properties for testing *cloudwatch.Metric objects
type Metric struct {
client client.ConfigProvider
metric *cloudwatch.Metric
filters []shared.Filter
}
// New returns a new *Metric
func New(client client.ConfigProvider) *Metric {
return &Metric{client: client}
}
// Selected returns the currently selected *cloudwatch.Metric
func (r *Metric) Selected() *cloudwatch.Metric {
return r.metric
}
// Alarm returns a new *alarm.Alarm
func (r *Metric) Alarm() *alarm.Alarm {
return alarm.New(r.client, r.metric)
}
// Assert applies all filters that have been called, resets the list of filters,
// fails the test if there is not exactly one match, and stores the matched metric
// if metrics is not provided, *cloudwatch.Metric objects will be retreived from AWS
func (r *Metric) Assert(t *testing.T, metrics ...*cloudwatch.Metric) *Metric {
var err error
metrics, err = r.filter(metrics)
if err != nil {
t.Fatal(err)
}
switch l := len(metrics); {
case l == 0:
t.Fatal("no matching metric was found")
case l > 1:
t.Fatal("more than one matching metric was found")
default:
r.metric = metrics[0]
}
r.filters = []shared.Filter{}
return r
}
// First applies all filters that have been called, resets the list of filters,
// fails the test if there are no matches, and stores the first match
// if metrics is not provided, *cloudwatch.Metric objects will be retreived from AWS
func (r *Metric) First(t *testing.T, metrics ...*cloudwatch.Metric) *Metric {
var err error
metrics, err = r.filter(metrics)
if err != nil {
t.Fatal(err)
}
if len(metrics) == 0 {
t.Fatal("no matching metric was found")
} else {
r.metric = metrics[0]
}
r.filters = []shared.Filter{}
return r
}
// Filter adds the 'filter' provided to the filter list
func (r *Metric) Filter(filter shared.Filter) *Metric {
r.filters = append(r.filters, filter)
return r
}
// Namespace adds the Namespace filter to the filter list
// the Namespace filter: filters metrics by Namespace where 'name' provided
// is the expected Namespace value
func (r *Metric) Namespace(name string) *Metric {
r.filters = append(r.filters, func(v interface{}) bool {
metric := convert(v)
if metric == nil {
return false
}
shared.Debugf(
"%s like %s -> %t\n",
name,
aws.StringValue(metric.Namespace),
strings.EqualFold(name, aws.StringValue(metric.Namespace)),
)
return strings.EqualFold(name, aws.StringValue(metric.Namespace))
})
return r
}
// MetricName adds the MetricName filter to the filter list
// the MetricName filter: filters metrics by MetricName where 'name' provided
// is the expected MetricName value
func (r *Metric) MetricName(name string) *Metric {
r.filters = append(r.filters, func(v interface{}) bool {
metric := convert(v)
if metric == nil {
return false
}
shared.Debugf(
"%s like %s -> %t\n",
name,
aws.StringValue(metric.MetricName),
strings.EqualFold(name, aws.StringValue(metric.MetricName)),
)
return strings.EqualFold(name, aws.StringValue(metric.MetricName))
})
return r
}
// Name adds the Name filter as an alias to the MetricName filter
func (r *Metric) Name(name string) *Metric {
return r.MetricName(name)
}
func (r *Metric) filter(metrics []*cloudwatch.Metric) ([]*cloudwatch.Metric, error) {
if len(metrics) == 0 {
var err error
metrics, err = r.metrics()
if err != nil {
return nil, err
}
}
results := fromIface(shared.GenericFilter(r.filters, toIface(metrics)))
if len(results) == 0 {
log.Println("aws.cloudwatch.metric.filter had zero results: ")
shared.Spew(os.Stdout, metrics)
}
return results, nil
}
func (r *Metric) metrics() ([]*cloudwatch.Metric, error) {
svc := cloudwatch.New(r.client)
input := &cloudwatch.ListMetricsInput{}
result, err := svc.ListMetrics(input)
if err != nil {
return nil, err
}
metrics := result.Metrics
token := aws.StringValue(result.NextToken)
for token != "" {
input.NextToken = &token
result, err := svc.ListMetrics(input)
if err != nil {
return nil, err
}
metrics = append(metrics, result.Metrics...)
token = aws.StringValue(result.NextToken)
}
return metrics, nil
}
func convert(in interface{}) *cloudwatch.Metric {
out, ok := in.(*cloudwatch.Metric)
if !ok {
shared.Debugf("object not convertible to *cloudwatch.Metric: ")
shared.Dump(in)
return nil
}
return out
}
func toIface(in []*cloudwatch.Metric) (out []interface{}) {
for _, i := range in {
out = append(out, i)
}
return
}
func fromIface(in []interface{}) (out []*cloudwatch.Metric) {
for _, i := range in {
v := convert(i)
if v == nil {
continue
}
out = append(out, v)
}
return
}