CloudWatch: fix custom namespace for listing dimension keys, refactor to non-pointer types, add test assertions, rename packages (#59106)

Co-authored-by: Erik Sundell <erik.sundell87@gmail.com>
This commit is contained in:
Shirley
2022-11-22 15:59:11 +01:00
committed by GitHub
co-authored by Erik Sundell
parent f62e55f461
commit c43e1a721f
32 changed files with 194 additions and 160 deletions
@@ -0,0 +1,53 @@
package resources
import (
"encoding/json"
"fmt"
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/constants"
)
func parseDimensionFilter(dimensionFilter string) ([]*Dimension, error) {
dimensionFilters := map[string]interface{}{}
dimensionFilterJson := []byte(dimensionFilter)
if len(dimensionFilterJson) > 0 {
err := json.Unmarshal(dimensionFilterJson, &dimensionFilters)
if err != nil {
return nil, fmt.Errorf("error unmarshaling dimensionFilters: %v", err)
}
}
dimensions := []*Dimension{}
addDimension := func(key string, value string) {
d := &Dimension{
Name: key,
}
// if value is not specified or a wildcard is used, simply don't use the value field
if value != "" && value != "*" {
d.Value = value
}
dimensions = append(dimensions, d)
}
for k, v := range dimensionFilters {
// due to legacy, value can be a string, a string slice or nil
if vv, ok := v.(string); ok {
addDimension(k, vv)
} else if vv, ok := v.([]interface{}); ok {
for _, v := range vv {
addDimension(k, v.(string))
}
} else if v == nil {
addDimension(k, "")
}
}
return dimensions, nil
}
func isCustomNamespace(namespace string) bool {
if _, ok := constants.NamespaceMetricsMap[namespace]; ok {
return false
}
return true
}