Skip to content
This repository was archived by the owner on Aug 5, 2025. It is now read-only.
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions pkg/grafana/dashboard-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package grafana
import (
"fmt"
"path/filepath"
"regexp"

"github.com/grafana/grizzly/pkg/grizzly"
"github.com/grafana/grizzly/pkg/grizzly/notifier"
Expand All @@ -21,13 +22,17 @@ func NewDashboardHandler(provider Provider) *DashboardHandler {
}
}

func formatUID(name string) string {
var pattern = regexp.MustCompile(`[^a-zA-Z0-9_-]`)
return pattern.ReplaceAllString(name, "")
}

// Validate returns the uid of resource
func (h *DashboardHandler) Validate(resource grizzly.Resource) error {
uid, exist := resource.GetSpecString("uid")
if exist {
if uid != resource.Name() {
return fmt.Errorf("uid '%s' and name '%s', don't match", uid, resource.Name())
}
uid, _ := resource.GetSpecString("uid")

if uid != formatUID(resource.Name()) {
return fmt.Errorf("uid '%s' and name '%s', don't match", uid, resource.Name())
}
return nil
}
Expand Down Expand Up @@ -70,7 +75,7 @@ func (h *DashboardHandler) ResourceFilePath(resource grizzly.Resource, filetype
// Parse parses a manifest object into a struct for this resource type
func (h *DashboardHandler) Parse(m manifest.Manifest) (grizzly.Resources, error) {
resource := grizzly.Resource(m)
resource.SetSpecString("uid", resource.GetMetadata("name"))
resource.SetSpecString("uid", formatUID(resource.Name()))
if !resource.HasMetadata("folder") {
resource.SetMetadata("folder", dashboardFolderDefault)
}
Expand Down Expand Up @@ -103,9 +108,8 @@ func (h *DashboardHandler) GetByUID(UID string) (*grizzly.Resource, error) {

// GetRemote retrieves a dashboard as a resource
func (h *DashboardHandler) GetRemote(resource grizzly.Resource) (*grizzly.Resource, error) {
uid, _ := resource.GetSpecString("uid")
if uid != resource.Name() {
return nil, fmt.Errorf("uid '%s' and name '%s', don't match", uid, resource.Name())
if err := h.Validate(resource); err != nil {
return nil, err
}
return getRemoteDashboard(resource.Name())
}
Expand Down