Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proxy library elements in grr serve #494

Merged
merged 2 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
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
23 changes: 10 additions & 13 deletions pkg/grafana/dashboard-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -357,20 +357,17 @@ func (h *DashboardHandler) DashboardJSONGetHandler(s grizzly.Server) http.Handle
if resource.GetSpecValue("version") == nil {
resource.SetSpecValue("version", 1)
}
meta := map[string]interface{}{
"type": "db",
"isStarred": false,
"folderID": 0,
"folderUID": "",
"url": fmt.Sprintf("/d/%s/slug", uid),
}
wrapper := map[string]interface{}{
"dashboard": resource.Spec(),
"meta": meta,
}

out, _ := json.Marshal(wrapper)
writeOrLog(w, out)
writeJSONOrLog(w, map[string]any{
"dashboard": resource.Spec(),
"meta": map[string]any{
"type": "db",
"isStarred": false,
"folderID": 0,
"folderUID": "",
"url": h.ProxyURL(uid),
},
})
}
}

Expand Down
10 changes: 10 additions & 0 deletions pkg/grafana/errors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package grafana

import (
"encoding/json"
"fmt"
"net/http"
"strings"
Expand All @@ -26,3 +27,12 @@ func writeOrLog(w http.ResponseWriter, content []byte) {
log.Errorf("error writing response: %v", err)
}
}

func writeJSONOrLog(w http.ResponseWriter, content any) {
responseJSON, err := json.Marshal(content)
if err != nil {
log.Errorf("error marshalling response to JSON: %v", err)
}

writeOrLog(w, responseJSON)
}
46 changes: 45 additions & 1 deletion pkg/grafana/library-element-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ import (
"encoding/json"
"errors"
"fmt"
"net/http"

"github.com/go-chi/chi"
library "github.com/grafana/grafana-openapi-client-go/client/library_elements"
"github.com/grafana/grafana-openapi-client-go/models"
"github.com/grafana/grizzly/pkg/grizzly"
log "github.com/sirupsen/logrus"
)

const LibraryElementKind = "LibraryElement"

// LibraryElementHandler is a Grizzly Handler for Grafana dashboard folders
type LibraryElementHandler struct {
grizzly.BaseHandler
Expand All @@ -20,7 +25,7 @@ var _ grizzly.Handler = &LibraryElementHandler{}
// NewLibraryElementHandler returns configuration defining a new Grafana Library Element Handler
func NewLibraryElementHandler(provider grizzly.Provider) *LibraryElementHandler {
return &LibraryElementHandler{
BaseHandler: grizzly.NewBaseHandler(provider, "LibraryElement", false),
BaseHandler: grizzly.NewBaseHandler(provider, LibraryElementKind, false),
}
}

Expand Down Expand Up @@ -115,6 +120,45 @@ func (h *LibraryElementHandler) Update(existing, resource grizzly.Resource) erro
return h.updateElement(existing, resource)
}

func (h *LibraryElementHandler) GetProxyEndpoints(s grizzly.Server) []grizzly.HTTPEndpoint {
return []grizzly.HTTPEndpoint{
{
Method: "GET",
URL: "/api/library-elements/{uid}",
Handler: h.LibraryElementJSONGetHandler(s),
},
}
}

func (h *LibraryElementHandler) ProxyURL(uid string) string {
return fmt.Sprintf("/api/library-elements/%s", uid)
}

func (h *LibraryElementHandler) LibraryElementJSONGetHandler(s grizzly.Server) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
uid := chi.URLParam(r, "uid")
if uid == "" {
grizzly.SendError(w, "No UID specified", fmt.Errorf("no UID specified within the URL"), 400)
return
}

resource, found := s.Resources.Find(grizzly.NewResourceRef(LibraryElementKind, uid))
if !found {
log.Debug("Library element not found in memory, proxying request...", "uid", uid)
s.ProxyRequestHandler(w, r)
return
}

if resource.GetSpecValue("version") == nil {
resource.SetSpecValue("version", 1)
}

writeJSONOrLog(w, map[string]any{
"result": resource.Spec(),
})
}
}

func (h *LibraryElementHandler) listElements() ([]string, error) {
params := library.NewGetLibraryElementsParams()
client, err := h.Provider.(ClientProvider).Client()
Expand Down
3 changes: 1 addition & 2 deletions pkg/grizzly/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package grizzly

import (
"fmt"
"net/http"

"log"
"net/http"
)

// Pluraliser returns a string describing the count of items, with a plural 's'
Expand Down
Loading