@@ -151,3 +151,175 @@ func Test_toMetricDataResult(t *testing.T) {
151151 })
152152 }
153153}
154+
155+ func Test_toModelMetric (t * testing.T ) {
156+ type testCase struct {
157+ name string
158+ listMetricsOutput * cloudwatch.ListMetricsOutput
159+ includeLinkedAccounts []string
160+ expectedMetrics []* model.Metric
161+ }
162+
163+ testCases := []testCase {
164+ {
165+ name : "no linked accounts filter - original behavior" ,
166+ listMetricsOutput : & cloudwatch.ListMetricsOutput {
167+ Metrics : []* cloudwatch.Metric {
168+ {
169+ MetricName : aws .String ("CPUUtilization" ),
170+ Namespace : aws .String ("AWS/EC2" ),
171+ Dimensions : []* cloudwatch.Dimension {
172+ {Name : aws .String ("InstanceId" ), Value : aws .String ("i-12345" )},
173+ },
174+ },
175+ {
176+ MetricName : aws .String ("NetworkIn" ),
177+ Namespace : aws .String ("AWS/EC2" ),
178+ Dimensions : []* cloudwatch.Dimension {
179+ {Name : aws .String ("InstanceId" ), Value : aws .String ("i-67890" )},
180+ },
181+ },
182+ },
183+ },
184+ includeLinkedAccounts : nil ,
185+ expectedMetrics : []* model.Metric {
186+ {
187+ MetricName : "CPUUtilization" ,
188+ Namespace : "AWS/EC2" ,
189+ Dimensions : []model.Dimension {
190+ {Name : "InstanceId" , Value : "i-12345" },
191+ },
192+ },
193+ {
194+ MetricName : "NetworkIn" ,
195+ Namespace : "AWS/EC2" ,
196+ Dimensions : []model.Dimension {
197+ {Name : "InstanceId" , Value : "i-67890" },
198+ },
199+ },
200+ },
201+ },
202+ {
203+ name : "with wildcard linked accounts - include all" ,
204+ listMetricsOutput : & cloudwatch.ListMetricsOutput {
205+ Metrics : []* cloudwatch.Metric {
206+ {
207+ MetricName : aws .String ("CPUUtilization" ),
208+ Namespace : aws .String ("AWS/EC2" ),
209+ Dimensions : []* cloudwatch.Dimension {
210+ {Name : aws .String ("InstanceId" ), Value : aws .String ("i-12345" )},
211+ },
212+ },
213+ {
214+ MetricName : aws .String ("NetworkIn" ),
215+ Namespace : aws .String ("AWS/EC2" ),
216+ Dimensions : []* cloudwatch.Dimension {
217+ {Name : aws .String ("InstanceId" ), Value : aws .String ("i-67890" )},
218+ },
219+ },
220+ },
221+ OwningAccounts : []* string {
222+ aws .String ("111111111111" ),
223+ aws .String ("222222222222" ),
224+ },
225+ },
226+ includeLinkedAccounts : []string {"*" },
227+ expectedMetrics : []* model.Metric {
228+ {
229+ MetricName : "CPUUtilization" ,
230+ Namespace : "AWS/EC2" ,
231+ Dimensions : []model.Dimension {
232+ {Name : "InstanceId" , Value : "i-12345" },
233+ },
234+ LinkedAccountID : "111111111111" ,
235+ },
236+ {
237+ MetricName : "NetworkIn" ,
238+ Namespace : "AWS/EC2" ,
239+ Dimensions : []model.Dimension {
240+ {Name : "InstanceId" , Value : "i-67890" },
241+ },
242+ LinkedAccountID : "222222222222" ,
243+ },
244+ },
245+ },
246+ {
247+ name : "with specific linked accounts - filter by account ID" ,
248+ listMetricsOutput : & cloudwatch.ListMetricsOutput {
249+ Metrics : []* cloudwatch.Metric {
250+ {
251+ MetricName : aws .String ("CPUUtilization" ),
252+ Namespace : aws .String ("AWS/EC2" ),
253+ Dimensions : []* cloudwatch.Dimension {
254+ {Name : aws .String ("InstanceId" ), Value : aws .String ("i-12345" )},
255+ },
256+ },
257+ {
258+ MetricName : aws .String ("NetworkIn" ),
259+ Namespace : aws .String ("AWS/EC2" ),
260+ Dimensions : []* cloudwatch.Dimension {
261+ {Name : aws .String ("InstanceId" ), Value : aws .String ("i-67890" )},
262+ },
263+ },
264+ {
265+ MetricName : aws .String ("DiskReadOps" ),
266+ Namespace : aws .String ("AWS/EC2" ),
267+ Dimensions : []* cloudwatch.Dimension {
268+ {Name : aws .String ("InstanceId" ), Value : aws .String ("i-11111" )},
269+ },
270+ },
271+ },
272+ OwningAccounts : []* string {
273+ aws .String ("111111111111" ),
274+ aws .String ("222222222222" ),
275+ aws .String ("333333333333" ),
276+ },
277+ },
278+ includeLinkedAccounts : []string {"111111111111" , "333333333333" },
279+ expectedMetrics : []* model.Metric {
280+ {
281+ MetricName : "CPUUtilization" ,
282+ Namespace : "AWS/EC2" ,
283+ Dimensions : []model.Dimension {
284+ {Name : "InstanceId" , Value : "i-12345" },
285+ },
286+ LinkedAccountID : "111111111111" ,
287+ },
288+ {
289+ MetricName : "DiskReadOps" ,
290+ Namespace : "AWS/EC2" ,
291+ Dimensions : []model.Dimension {
292+ {Name : "InstanceId" , Value : "i-11111" },
293+ },
294+ LinkedAccountID : "333333333333" ,
295+ },
296+ },
297+ },
298+ {
299+ name : "with linked accounts filter - no matches" ,
300+ listMetricsOutput : & cloudwatch.ListMetricsOutput {
301+ Metrics : []* cloudwatch.Metric {
302+ {
303+ MetricName : aws .String ("CPUUtilization" ),
304+ Namespace : aws .String ("AWS/EC2" ),
305+ Dimensions : []* cloudwatch.Dimension {
306+ {Name : aws .String ("InstanceId" ), Value : aws .String ("i-12345" )},
307+ },
308+ },
309+ },
310+ OwningAccounts : []* string {
311+ aws .String ("111111111111" ),
312+ },
313+ },
314+ includeLinkedAccounts : []string {"999999999999" },
315+ expectedMetrics : []* model.Metric {},
316+ },
317+ }
318+
319+ for _ , tc := range testCases {
320+ t .Run (tc .name , func (t * testing.T ) {
321+ result := toModelMetric (tc .listMetricsOutput , tc .includeLinkedAccounts )
322+ require .Equal (t , tc .expectedMetrics , result )
323+ })
324+ }
325+ }
0 commit comments