2024-01-09 14:26:24 -06:00
|
|
|
package datasource
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2024-03-19 03:02:52 -05:00
|
|
|
"net/url"
|
2024-01-09 14:26:24 -06:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
"k8s.io/apiserver/pkg/registry/rest"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana/pkg/plugins/httpresponsesender"
|
|
|
|
)
|
|
|
|
|
|
|
|
type subResourceREST struct {
|
|
|
|
builder *DataSourceAPIBuilder
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ = rest.Connecter(&subResourceREST{})
|
|
|
|
|
|
|
|
func (r *subResourceREST) New() runtime.Object {
|
|
|
|
return &metav1.Status{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *subResourceREST) Destroy() {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *subResourceREST) ConnectMethods() []string {
|
|
|
|
// All for now??? ideally we have a schema for resource and limit this
|
|
|
|
return []string{
|
|
|
|
http.MethodGet,
|
|
|
|
http.MethodHead,
|
|
|
|
http.MethodPost,
|
|
|
|
http.MethodPut,
|
|
|
|
http.MethodPatch,
|
|
|
|
http.MethodDelete,
|
|
|
|
http.MethodOptions,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *subResourceREST) NewConnectOptions() (runtime.Object, bool, string) {
|
|
|
|
return nil, true, ""
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *subResourceREST) Connect(ctx context.Context, name string, opts runtime.Object, responder rest.Responder) (http.Handler, error) {
|
2024-01-22 13:32:25 -06:00
|
|
|
pluginCtx, err := r.builder.getPluginContext(ctx, name)
|
2024-01-09 14:26:24 -06:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-02-26 06:02:55 -06:00
|
|
|
ctx = backend.WithGrafanaConfig(ctx, pluginCtx.GrafanaConfig)
|
2024-03-13 04:14:16 -05:00
|
|
|
ctx = contextualMiddlewares(ctx)
|
2024-01-09 14:26:24 -06:00
|
|
|
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
|
2024-04-02 13:09:18 -05:00
|
|
|
clonedReq, err := resourceRequest(req)
|
2024-01-09 14:26:24 -06:00
|
|
|
if err != nil {
|
|
|
|
responder.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-02 13:09:18 -05:00
|
|
|
body, err := io.ReadAll(req.Body)
|
|
|
|
if err != nil {
|
|
|
|
responder.Error(err)
|
2024-01-09 14:26:24 -06:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-22 13:32:25 -06:00
|
|
|
err = r.builder.client.CallResource(ctx, &backend.CallResourceRequest{
|
2024-01-19 08:56:52 -06:00
|
|
|
PluginContext: pluginCtx,
|
2024-03-19 03:02:52 -05:00
|
|
|
Path: clonedReq.URL.Path,
|
2024-01-09 14:26:24 -06:00
|
|
|
Method: req.Method,
|
2024-04-02 13:09:18 -05:00
|
|
|
URL: clonedReq.URL.String(),
|
2024-01-09 14:26:24 -06:00
|
|
|
Body: body,
|
2024-03-19 03:02:52 -05:00
|
|
|
Headers: req.Header,
|
2024-01-09 14:26:24 -06:00
|
|
|
}, httpresponsesender.New(w))
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
responder.Error(err)
|
|
|
|
}
|
|
|
|
}), nil
|
|
|
|
}
|
2024-04-02 13:09:18 -05:00
|
|
|
|
|
|
|
func resourceRequest(req *http.Request) (*http.Request, error) {
|
|
|
|
idx := strings.LastIndex(req.URL.Path, "/resource")
|
|
|
|
if idx < 0 {
|
|
|
|
return nil, fmt.Errorf("expected resource path") // 400?
|
|
|
|
}
|
|
|
|
|
|
|
|
clonedReq := req.Clone(req.Context())
|
|
|
|
rawURL := strings.TrimLeft(req.URL.Path[idx+len("/resource"):], "/")
|
|
|
|
|
|
|
|
clonedReq.URL = &url.URL{
|
|
|
|
Path: rawURL,
|
|
|
|
RawQuery: clonedReq.URL.RawQuery,
|
|
|
|
}
|
|
|
|
|
|
|
|
return clonedReq, nil
|
|
|
|
}
|