grafana/pkg/tsdb/azuremonitor/metrics/url-builder.go
Kevin Yu 1a4b1184bd
Azure Monitor: allow metrics call to use resource uri (#46858)
* Azure Monitor: allow metrics call to use resource uri

* test case when only resource uri is provided

* remove logs

* Rename json field name from resource to resourceUri

* Group legacy URL builder params test cases

* move comment to the correct position

* Add clarifications in comments

Co-authored-by: Sarah Zinger <sarah.zinger@grafana.com>

Co-authored-by: Sarah Zinger <sarah.zinger@grafana.com>
2022-03-31 08:20:29 -07:00

57 lines
1.5 KiB
Go

package metrics
import (
"fmt"
"strings"
)
// urlBuilder builds the URL for calling the Azure Monitor API
type urlBuilder struct {
ResourceURI string
// Following fields will be deprecated in grafana 9 and will not included in new queries.
// For backwards compat, we recreate the ResourceURI using these fields
DefaultSubscription string
Subscription string
ResourceGroup string
MetricDefinition string
ResourceName string
}
// BuildMetricsURL checks the metric definition property to see which form of the url
// should be returned
func (params *urlBuilder) BuildMetricsURL() string {
resourceURI := params.ResourceURI
// Prior to Grafana 9, we had a legacy query object rather than a resourceURI, so we manually create the resource URI
if resourceURI == "" {
subscription := params.Subscription
if params.Subscription == "" {
subscription = params.DefaultSubscription
}
metricDefinitionArray := strings.Split(params.MetricDefinition, "/")
resourceNameArray := strings.Split(params.ResourceName, "/")
provider := metricDefinitionArray[0]
metricDefinitionArray = metricDefinitionArray[1:]
urlArray := []string{
subscription,
"resourceGroups",
params.ResourceGroup,
"providers",
provider,
}
for i := range metricDefinitionArray {
urlArray = append(urlArray, metricDefinitionArray[i])
urlArray = append(urlArray, resourceNameArray[i])
}
resourceURI = strings.Join(urlArray[:], "/")
}
return fmt.Sprintf("%s/providers/microsoft.insights/metrics", resourceURI)
}