grafana/pkg/tsdb/cloudwatch/models/request/utils.go
Erik Sundell 4c654ddb76
Cloudwatch: Refactor metrics resource request (#57424)
* refactor metrics request

* Update pkg/tsdb/cloudwatch/routes/dimension_keys_test.go

Co-authored-by: Shirley <4163034+fridgepoet@users.noreply.github.com>

* return metric struct value intead of pointer

* make it possible to test hard coded metrics service

* test all paths in route

* fix broken test

* fix one more broken test

* add integration test

Co-authored-by: Shirley <4163034+fridgepoet@users.noreply.github.com>
2022-10-25 14:00:54 +02:00

54 lines
1.2 KiB
Go

package request
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
}