url-builder.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. package azuremonitor
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. // urlBuilder builds the URL for calling the Azure Monitor API
  7. type urlBuilder struct {
  8. DefaultSubscription string
  9. Subscription string
  10. ResourceGroup string
  11. MetricDefinition string
  12. ResourceName string
  13. }
  14. // Build checks the metric definition property to see which form of the url
  15. // should be returned
  16. func (ub *urlBuilder) Build() string {
  17. subscription := ub.Subscription
  18. if ub.Subscription == "" {
  19. subscription = ub.DefaultSubscription
  20. }
  21. if strings.Count(ub.MetricDefinition, "/") > 1 {
  22. rn := strings.Split(ub.ResourceName, "/")
  23. lastIndex := strings.LastIndex(ub.MetricDefinition, "/")
  24. service := ub.MetricDefinition[lastIndex+1:]
  25. md := ub.MetricDefinition[0:lastIndex]
  26. return fmt.Sprintf("%s/resourceGroups/%s/providers/%s/%s/%s/%s/providers/microsoft.insights/metrics", subscription, ub.ResourceGroup, md, rn[0], service, rn[1])
  27. }
  28. return fmt.Sprintf("%s/resourceGroups/%s/providers/%s/%s/providers/microsoft.insights/metrics", subscription, ub.ResourceGroup, ub.MetricDefinition, ub.ResourceName)
  29. }