From e48863f9a0154ecc70d234bfe7b28189f4e4b071 Mon Sep 17 00:00:00 2001 From: Quentin MOUTY Date: Thu, 8 Aug 2024 10:20:47 +0200 Subject: [PATCH] Remove backend component --- Magefile.go | 12 ---- pkg/main.go | 24 ------- pkg/models/settings.go | 36 ----------- pkg/plugin/datasource.go | 116 ---------------------------------- pkg/plugin/datasource_test.go | 28 -------- 5 files changed, 216 deletions(-) delete mode 100644 Magefile.go delete mode 100644 pkg/main.go delete mode 100644 pkg/models/settings.go delete mode 100644 pkg/plugin/datasource.go delete mode 100644 pkg/plugin/datasource_test.go diff --git a/Magefile.go b/Magefile.go deleted file mode 100644 index 4710a8a..0000000 --- a/Magefile.go +++ /dev/null @@ -1,12 +0,0 @@ -//go:build mage -// +build mage - -package main - -import ( - // mage:import - build "github.com/grafana/grafana-plugin-sdk-go/build" -) - -// Default configures the default target. -var Default = build.BuildAll diff --git a/pkg/main.go b/pkg/main.go deleted file mode 100644 index e3cbb47..0000000 --- a/pkg/main.go +++ /dev/null @@ -1,24 +0,0 @@ -package main - -import ( - "os" - - "github.com/grafana/grafana-plugin-sdk-go/backend/datasource" - "github.com/grafana/grafana-plugin-sdk-go/backend/log" - "github.com/ov-hcloud/akvorado/pkg/plugin" -) - -func main() { - // Start listening to requests sent from Grafana. This call is blocking so - // it won't finish until Grafana shuts down the process or the plugin choose - // to exit by itself using os.Exit. Manage automatically manages life cycle - // of datasource instances. It accepts datasource instance factory as first - // argument. This factory will be automatically called on incoming request - // from Grafana to create different instances of SampleDatasource (per datasource - // ID). When datasource configuration changed Dispose method will be called and - // new datasource instance created using NewSampleDatasource factory. - if err := datasource.Manage("ovhcloud-akvorado-datasource", plugin.NewDatasource, datasource.ManageOpts{}); err != nil { - log.DefaultLogger.Error(err.Error()) - os.Exit(1) - } -} diff --git a/pkg/models/settings.go b/pkg/models/settings.go deleted file mode 100644 index bc24978..0000000 --- a/pkg/models/settings.go +++ /dev/null @@ -1,36 +0,0 @@ -package models - -import ( - "encoding/json" - "fmt" - - "github.com/grafana/grafana-plugin-sdk-go/backend" -) - -type PluginSettings struct { - Url string `json:"url"` - Path string `json:"path"` - Secrets *SecretPluginSettings `json:"-"` -} - -type SecretPluginSettings struct { - ApiKey string `json:"apiKey"` -} - -func LoadPluginSettings(source backend.DataSourceInstanceSettings) (*PluginSettings, error) { - settings := PluginSettings{} - err := json.Unmarshal(source.JSONData, &settings) - if err != nil { - return nil, fmt.Errorf("could not unmarshal PluginSettings json: %w", err) - } - - settings.Secrets = loadSecretPluginSettings(source.DecryptedSecureJSONData) - - return &settings, nil -} - -func loadSecretPluginSettings(source map[string]string) *SecretPluginSettings { - return &SecretPluginSettings{ - ApiKey: source["apiKey"], - } -} diff --git a/pkg/plugin/datasource.go b/pkg/plugin/datasource.go deleted file mode 100644 index cc491df..0000000 --- a/pkg/plugin/datasource.go +++ /dev/null @@ -1,116 +0,0 @@ -package plugin - -import ( - "context" - "encoding/json" - "fmt" - "time" - - "github.com/grafana/grafana-plugin-sdk-go/backend" - "github.com/grafana/grafana-plugin-sdk-go/backend/instancemgmt" - "github.com/grafana/grafana-plugin-sdk-go/data" - "github.com/ov-hcloud/akvorado/pkg/models" -) - -// Make sure Datasource implements required interfaces. This is important to do -// since otherwise we will only get a not implemented error response from plugin in -// runtime. In this example datasource instance implements backend.QueryDataHandler, -// backend.CheckHealthHandler interfaces. Plugin should not implement all these -// interfaces - only those which are required for a particular task. -var ( - _ backend.QueryDataHandler = (*Datasource)(nil) - _ backend.CheckHealthHandler = (*Datasource)(nil) - _ instancemgmt.InstanceDisposer = (*Datasource)(nil) -) - -// NewDatasource creates a new datasource instance. -func NewDatasource(_ context.Context, _ backend.DataSourceInstanceSettings) (instancemgmt.Instance, error) { - return &Datasource{}, nil -} - -// Datasource is an example datasource which can respond to data queries, reports -// its health and has streaming skills. -type Datasource struct{} - -// Dispose here tells plugin SDK that plugin wants to clean up resources when a new instance -// created. As soon as datasource settings change detected by SDK old datasource instance will -// be disposed and a new one will be created using NewSampleDatasource factory function. -func (d *Datasource) Dispose() { - // Clean up datasource instance resources. -} - -// QueryData handles multiple queries and returns multiple responses. -// req contains the queries []DataQuery (where each query contains RefID as a unique identifier). -// The QueryDataResponse contains a map of RefID to the response for each query, and each response -// contains Frames ([]*Frame). -func (d *Datasource) QueryData(ctx context.Context, req *backend.QueryDataRequest) (*backend.QueryDataResponse, error) { - // create response struct - response := backend.NewQueryDataResponse() - - // loop over queries and execute them individually. - for _, q := range req.Queries { - res := d.query(ctx, req.PluginContext, q) - - // save the response in a hashmap - // based on with RefID as identifier - response.Responses[q.RefID] = res - } - - return response, nil -} - -type queryModel struct{} - -func (d *Datasource) query(_ context.Context, pCtx backend.PluginContext, query backend.DataQuery) backend.DataResponse { - var response backend.DataResponse - - // Unmarshal the JSON into our queryModel. - var qm queryModel - - err := json.Unmarshal(query.JSON, &qm) - if err != nil { - return backend.ErrDataResponse(backend.StatusBadRequest, fmt.Sprintf("json unmarshal: %v", err.Error())) - } - - // create data frame response. - // For an overview on data frames and how grafana handles them: - // https://grafana.com/developers/plugin-tools/introduction/data-frames - frame := data.NewFrame("response") - - // add fields. - frame.Fields = append(frame.Fields, - data.NewField("time", nil, []time.Time{query.TimeRange.From, query.TimeRange.To}), - data.NewField("values", nil, []int64{10, 20}), - ) - - // add the frames to the response. - response.Frames = append(response.Frames, frame) - - return response -} - -// CheckHealth handles health checks sent from Grafana to the plugin. -// The main use case for these health checks is the test button on the -// datasource configuration page which allows users to verify that -// a datasource is working as expected. -func (d *Datasource) CheckHealth(_ context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) { - res := &backend.CheckHealthResult{} - config, err := models.LoadPluginSettings(*req.PluginContext.DataSourceInstanceSettings) - - if err != nil { - res.Status = backend.HealthStatusError - res.Message = "Unable to load settings" - return res, nil - } - - if config.Secrets.ApiKey == "" { - res.Status = backend.HealthStatusError - res.Message = "API key is missing" - return res, nil - } - - return &backend.CheckHealthResult{ - Status: backend.HealthStatusOk, - Message: "Data source is working", - }, nil -} diff --git a/pkg/plugin/datasource_test.go b/pkg/plugin/datasource_test.go deleted file mode 100644 index 1e68821..0000000 --- a/pkg/plugin/datasource_test.go +++ /dev/null @@ -1,28 +0,0 @@ -package plugin - -import ( - "context" - "testing" - - "github.com/grafana/grafana-plugin-sdk-go/backend" -) - -func TestQueryData(t *testing.T) { - ds := Datasource{} - - resp, err := ds.QueryData( - context.Background(), - &backend.QueryDataRequest{ - Queries: []backend.DataQuery{ - {RefID: "A"}, - }, - }, - ) - if err != nil { - t.Error(err) - } - - if len(resp.Responses) != 1 { - t.Fatal("QueryData must return a response") - } -}