2020-01-31 04:15:50 -06:00
|
|
|
package backendplugin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strconv"
|
2020-02-19 12:17:05 -06:00
|
|
|
"time"
|
2020-01-31 04:15:50 -06:00
|
|
|
|
2020-03-03 04:45:16 -06:00
|
|
|
"github.com/grafana/grafana/pkg/components/simplejson"
|
2020-03-06 07:37:36 -06:00
|
|
|
"github.com/grafana/grafana/pkg/models"
|
2020-03-03 04:45:16 -06:00
|
|
|
|
2020-01-31 04:15:50 -06:00
|
|
|
"github.com/grafana/grafana-plugin-sdk-go/genproto/pluginv2"
|
|
|
|
)
|
|
|
|
|
|
|
|
// HealthStatus is the status of the plugin.
|
|
|
|
type HealthStatus int
|
|
|
|
|
|
|
|
const (
|
|
|
|
// HealthStatusUnknown means the status of the plugin is unknown.
|
|
|
|
HealthStatusUnknown HealthStatus = iota
|
|
|
|
// HealthStatusOk means the status of the plugin is good.
|
|
|
|
HealthStatusOk
|
|
|
|
// HealthStatusError means the plugin is in an error state.
|
|
|
|
HealthStatusError
|
|
|
|
)
|
|
|
|
|
|
|
|
var healthStatusNames = map[int]string{
|
|
|
|
0: "UNKNOWN",
|
|
|
|
1: "OK",
|
|
|
|
2: "ERROR",
|
|
|
|
}
|
|
|
|
|
|
|
|
func (hs HealthStatus) String() string {
|
|
|
|
s, exists := healthStatusNames[int(hs)]
|
|
|
|
if exists {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
return strconv.Itoa(int(hs))
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckHealthResult check health result.
|
|
|
|
type CheckHealthResult struct {
|
2020-03-06 07:37:36 -06:00
|
|
|
Status HealthStatus
|
|
|
|
Message string
|
|
|
|
JSONDetails string
|
2020-01-31 04:15:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func checkHealthResultFromProto(protoResp *pluginv2.CheckHealth_Response) *CheckHealthResult {
|
|
|
|
status := HealthStatusUnknown
|
|
|
|
switch protoResp.Status {
|
|
|
|
case pluginv2.CheckHealth_Response_ERROR:
|
|
|
|
status = HealthStatusError
|
|
|
|
case pluginv2.CheckHealth_Response_OK:
|
|
|
|
status = HealthStatusOk
|
|
|
|
}
|
|
|
|
|
|
|
|
return &CheckHealthResult{
|
2020-03-06 07:37:36 -06:00
|
|
|
Status: status,
|
|
|
|
Message: protoResp.Message,
|
|
|
|
JSONDetails: protoResp.JsonDetails,
|
2020-01-31 04:15:50 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-19 12:17:05 -06:00
|
|
|
type DataSourceConfig struct {
|
|
|
|
ID int64
|
|
|
|
Name string
|
|
|
|
URL string
|
|
|
|
User string
|
|
|
|
Database string
|
|
|
|
BasicAuthEnabled bool
|
|
|
|
BasicAuthUser string
|
2020-01-31 04:15:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type PluginConfig struct {
|
2020-02-19 12:17:05 -06:00
|
|
|
OrgID int64
|
|
|
|
PluginID string
|
|
|
|
PluginType string
|
2020-03-03 04:45:16 -06:00
|
|
|
JSONData *simplejson.Json
|
2020-02-19 12:17:05 -06:00
|
|
|
DecryptedSecureJSONData map[string]string
|
|
|
|
Updated time.Time
|
|
|
|
DataSourceConfig *DataSourceConfig
|
2020-01-31 04:15:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type CallResourceRequest struct {
|
|
|
|
Config PluginConfig
|
|
|
|
Path string
|
|
|
|
Method string
|
|
|
|
URL string
|
|
|
|
Headers map[string][]string
|
|
|
|
Body []byte
|
2020-03-06 07:37:36 -06:00
|
|
|
User *models.SignedInUser
|
2020-01-31 04:15:50 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// CallResourceResult call resource result.
|
|
|
|
type CallResourceResult struct {
|
|
|
|
Status int
|
|
|
|
Headers map[string][]string
|
|
|
|
Body []byte
|
|
|
|
}
|
2020-03-05 12:44:07 -06:00
|
|
|
|
|
|
|
type callResourceResultStream interface {
|
|
|
|
Recv() (*CallResourceResult, error)
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type callResourceResultStreamImpl struct {
|
|
|
|
stream pluginv2.Core_CallResourceClient
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *callResourceResultStreamImpl) Recv() (*CallResourceResult, error) {
|
|
|
|
protoResp, err := s.stream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
respHeaders := map[string][]string{}
|
|
|
|
for key, values := range protoResp.Headers {
|
|
|
|
respHeaders[key] = values.Values
|
|
|
|
}
|
|
|
|
|
|
|
|
return &CallResourceResult{
|
|
|
|
Headers: respHeaders,
|
|
|
|
Body: protoResp.Body,
|
|
|
|
Status: int(protoResp.Code),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *callResourceResultStreamImpl) Close() error {
|
|
|
|
return s.stream.CloseSend()
|
|
|
|
}
|
|
|
|
|
|
|
|
type singleCallResourceResult struct {
|
|
|
|
result *CallResourceResult
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *singleCallResourceResult) Recv() (*CallResourceResult, error) {
|
|
|
|
return s.result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *singleCallResourceResult) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|