2022-02-16 13:28:26 -06:00
|
|
|
package cloudwatch
|
|
|
|
|
|
|
|
import (
|
2023-05-24 03:19:34 -05:00
|
|
|
"context"
|
2022-02-16 13:28:26 -06:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend"
|
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/backend/resource/httpadapter"
|
2022-11-02 09:14:02 -05:00
|
|
|
|
2022-10-20 05:53:28 -05:00
|
|
|
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/routes"
|
2022-02-16 13:28:26 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func (e *cloudWatchExecutor) newResourceMux() *http.ServeMux {
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.HandleFunc("/regions", handleResourceReq(e.handleGetRegions))
|
|
|
|
mux.HandleFunc("/ebs-volume-ids", handleResourceReq(e.handleGetEbsVolumeIds))
|
|
|
|
mux.HandleFunc("/ec2-instance-attribute", handleResourceReq(e.handleGetEc2InstanceAttribute))
|
|
|
|
mux.HandleFunc("/resource-arns", handleResourceReq(e.handleGetResourceArns))
|
2023-01-13 13:03:08 -06:00
|
|
|
mux.HandleFunc("/log-groups", routes.ResourceRequestMiddleware(routes.LogGroupsHandler, logger, e.getRequestContext))
|
2022-11-02 09:14:02 -05:00
|
|
|
mux.HandleFunc("/metrics", routes.ResourceRequestMiddleware(routes.MetricsHandler, logger, e.getRequestContext))
|
|
|
|
mux.HandleFunc("/dimension-values", routes.ResourceRequestMiddleware(routes.DimensionValuesHandler, logger, e.getRequestContext))
|
|
|
|
mux.HandleFunc("/dimension-keys", routes.ResourceRequestMiddleware(routes.DimensionKeysHandler, logger, e.getRequestContext))
|
2022-11-28 05:39:12 -06:00
|
|
|
mux.HandleFunc("/accounts", routes.ResourceRequestMiddleware(routes.AccountsHandler, logger, e.getRequestContext))
|
2022-11-02 09:14:02 -05:00
|
|
|
mux.HandleFunc("/namespaces", routes.ResourceRequestMiddleware(routes.NamespacesHandler, logger, e.getRequestContext))
|
2023-01-11 01:12:58 -06:00
|
|
|
mux.HandleFunc("/log-group-fields", routes.ResourceRequestMiddleware(routes.LogGroupFieldsHandler, logger, e.getRequestContext))
|
2022-02-16 13:28:26 -06:00
|
|
|
return mux
|
|
|
|
}
|
|
|
|
|
2023-05-24 03:19:34 -05:00
|
|
|
type handleFn func(ctx context.Context, pluginCtx backend.PluginContext, parameters url.Values) ([]suggestData, error)
|
2022-02-16 13:28:26 -06:00
|
|
|
|
|
|
|
func handleResourceReq(handleFunc handleFn) func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
return func(rw http.ResponseWriter, req *http.Request) {
|
|
|
|
ctx := req.Context()
|
|
|
|
pluginContext := httpadapter.PluginConfigFromContext(ctx)
|
|
|
|
err := req.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
writeResponse(rw, http.StatusBadRequest, fmt.Sprintf("unexpected error %v", err))
|
2023-04-11 12:55:57 -05:00
|
|
|
return
|
2022-02-16 13:28:26 -06:00
|
|
|
}
|
2023-05-24 03:19:34 -05:00
|
|
|
data, err := handleFunc(ctx, pluginContext, req.URL.Query())
|
2022-02-16 13:28:26 -06:00
|
|
|
if err != nil {
|
|
|
|
writeResponse(rw, http.StatusBadRequest, fmt.Sprintf("unexpected error %v", err))
|
2023-04-11 12:55:57 -05:00
|
|
|
return
|
2022-02-16 13:28:26 -06:00
|
|
|
}
|
|
|
|
body, err := json.Marshal(data)
|
|
|
|
if err != nil {
|
|
|
|
writeResponse(rw, http.StatusBadRequest, fmt.Sprintf("unexpected error %v", err))
|
2023-04-11 12:55:57 -05:00
|
|
|
return
|
2022-02-16 13:28:26 -06:00
|
|
|
}
|
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
|
|
_, err = rw.Write(body)
|
|
|
|
if err != nil {
|
2022-11-02 09:14:02 -05:00
|
|
|
logger.Error("Unable to write HTTP response", "error", err)
|
2023-04-11 12:55:57 -05:00
|
|
|
return
|
2022-02-16 13:28:26 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeResponse(rw http.ResponseWriter, code int, msg string) {
|
|
|
|
rw.WriteHeader(code)
|
|
|
|
_, err := rw.Write([]byte(msg))
|
|
|
|
if err != nil {
|
2022-11-02 09:14:02 -05:00
|
|
|
logger.Error("Unable to write HTTP response", "error", err)
|
2022-02-16 13:28:26 -06:00
|
|
|
}
|
|
|
|
}
|