mirror of
https://github.com/grafana/grafana.git
synced 2024-11-26 02:40:26 -06:00
718620c197
* generalize error handling in forking request handlers * remove MatchesBackend and change test to test Can * add 404 to route specs * change backendTypeByUID to getDatasourceByUID of expected type * use common errors in api testing * handle 401 in errorToResponse * replace backend type error with "unexpected datasource type" * update swagger spec
56 lines
1.7 KiB
Go
56 lines
1.7 KiB
Go
package api
|
|
|
|
import (
|
|
"github.com/grafana/grafana/pkg/api/response"
|
|
"github.com/grafana/grafana/pkg/models"
|
|
"github.com/grafana/grafana/pkg/services/datasources"
|
|
apimodels "github.com/grafana/grafana/pkg/services/ngalert/api/tooling/definitions"
|
|
)
|
|
|
|
type PrometheusApiHandler struct {
|
|
ProxySvc *LotexProm
|
|
GrafanaSvc *PrometheusSrv
|
|
DatasourceCache datasources.CacheService
|
|
}
|
|
|
|
// NewForkingProm implements a set of routes that proxy to various Prometheus-compatible backends.
|
|
func NewForkingProm(datasourceCache datasources.CacheService, proxy *LotexProm, grafana *PrometheusSrv) *PrometheusApiHandler {
|
|
return &PrometheusApiHandler{
|
|
ProxySvc: proxy,
|
|
GrafanaSvc: grafana,
|
|
DatasourceCache: datasourceCache,
|
|
}
|
|
}
|
|
|
|
func (f *PrometheusApiHandler) handleRouteGetAlertStatuses(ctx *models.ReqContext, dsUID string) response.Response {
|
|
t, err := f.getService(ctx)
|
|
if err != nil {
|
|
return errorToResponse(err)
|
|
}
|
|
return t.RouteGetAlertStatuses(ctx)
|
|
}
|
|
|
|
func (f *PrometheusApiHandler) handleRouteGetRuleStatuses(ctx *models.ReqContext, dsUID string) response.Response {
|
|
t, err := f.getService(ctx)
|
|
if err != nil {
|
|
return errorToResponse(err)
|
|
}
|
|
return t.RouteGetRuleStatuses(ctx)
|
|
}
|
|
|
|
func (f *PrometheusApiHandler) handleRouteGetGrafanaAlertStatuses(ctx *models.ReqContext) response.Response {
|
|
return f.GrafanaSvc.RouteGetAlertStatuses(ctx)
|
|
}
|
|
|
|
func (f *PrometheusApiHandler) handleRouteGetGrafanaRuleStatuses(ctx *models.ReqContext) response.Response {
|
|
return f.GrafanaSvc.RouteGetRuleStatuses(ctx)
|
|
}
|
|
|
|
func (f *PrometheusApiHandler) getService(ctx *models.ReqContext) (*LotexProm, error) {
|
|
_, err := getDatasourceByUID(ctx, f.DatasourceCache, apimodels.LoTexRulerBackend)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return f.ProxySvc, nil
|
|
}
|