diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2cb8b6f..4ba612a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -81,6 +81,6 @@ jobs: uses: codecov/codecov-action@v2 with: directory: ./coverage/ - files: ./coverage/lcov.info + files: ./coverage/lcov.info,./coverage/backend.out env_vars: OS,PYTHON fail_ci_if_error: true diff --git a/CHANGELOG.md b/CHANGELOG.md index ec1aafd..cd31aec 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,15 @@ # Changelog +## 1.1.0 (IN PROGRESS) + +### Features / Enhancements + +- Add Tests, Provisioning and update README (#1) + ## 1.0.0 (2022-02-20) +### Features / Enhancements + - Initial release based on the Grafana 8.4.1. - Returns Environment Variables. -- Allows to filter unnecessary or secured variables using Regex. +- Allows filtering unnecessary or secured variables using Regex. diff --git a/README.md b/README.md index d3bbb1e..c2a220e 100644 --- a/README.md +++ b/README.md @@ -23,11 +23,32 @@ Environment data source can be installed from the Grafana Marketplace or use the grafana-cli plugins install volkovlabs-env-datasource ``` +![Grafana Marketplace](https://raw.githubusercontent.com/VolkovLabs/volkovlabs-env-datasource/main/src/img/grafana-marketplace.png) + ## Features - Returns Environment Variables. - Allows to filter unnecessary or secured variables using Regex. +## Provisioning + +Grafana supports managing data sources by adding one or more YAML config files in the `provisioning/datasources` folder. + +Example of provisioning the Environment Data Source with a filter `GF_` to return only Grafana related variables. + +``` +datasources: + - name: Environment + type: volkovlabs-env-datasource + access: proxy + isDefault: true + orgId: 1 + version: 1 + editable: true + jsonData: + filter: GF_ +``` + ## Feedback We love to hear from users, developers, and the whole community interested in this plugin. These are various ways to get in touch with us: diff --git a/go.mod b/go.mod index b1c1f95..e63b2cd 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module github.com/grafana/grafana-starter-datasource-backend go 1.16 -require github.com/grafana/grafana-plugin-sdk-go v0.126.0 +require ( + github.com/grafana/grafana-plugin-sdk-go v0.126.0 + github.com/stretchr/testify v1.7.0 +) diff --git a/go.sum b/go.sum index 8ac9ee6..9e4dee2 100644 --- a/go.sum +++ b/go.sum @@ -194,8 +194,10 @@ github.com/klauspost/compress v1.13.1/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8 github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= +github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/magefile/mage v1.11.0 h1:C/55Ywp9BpgVVclD3lRnSYCwXTYxmSppIgLeDYlNuls= github.com/magefile/mage v1.11.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= @@ -595,6 +597,7 @@ google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/package.json b/package.json index 7da67ce..a238c09 100644 --- a/package.json +++ b/package.json @@ -5,8 +5,7 @@ "@grafana/data": "8.4.1", "@grafana/runtime": "8.4.1", "@grafana/toolkit": "8.4.1", - "@grafana/ui": "8.4.1", - "@types/lodash": "4.14.178" + "@grafana/ui": "8.4.1" }, "engines": { "node": ">=16" @@ -22,9 +21,9 @@ "start": "docker-compose pull && docker-compose up", "stop": "docker-compose down", "test": "grafana-toolkit plugin:test", - "test:backend": "mage cover", + "test:backend": "mage coverage", "upgrade": "yarn upgrade --latest", "watch": "grafana-toolkit plugin:dev --watch" }, - "version": "1.0.0" + "version": "1.1.0" } diff --git a/pkg/plugin/plugin.go b/pkg/plugin/plugin.go index 7a4db82..e272e49 100644 --- a/pkg/plugin/plugin.go +++ b/pkg/plugin/plugin.go @@ -79,8 +79,6 @@ func (d *Datasource) query(_ context.Context, settings PluginSettings, query bac var ids []string var values []string - log.DefaultLogger.Info("Query called", "filter", settings.Filter) - /** * Compile regex */ @@ -119,10 +117,10 @@ func (d *Datasource) query(_ context.Context, settings PluginSettings, query bac * CheckHealth handles health checks sent from Grafana to the plugin */ func (d *Datasource) CheckHealth(ctx context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) { - log.DefaultLogger.Info("CheckHealth called", "request", req) + log.DefaultLogger.Debug("CheckHealth called", "request", req) var status = backend.HealthStatusOk - var message = "Data source is working" + var message = "Data Source is working as expected." /** * Create response struct @@ -141,7 +139,7 @@ func (d *Datasource) CheckHealth(ctx context.Context, req *backend.CheckHealthRe */ if res.Frames[0].Fields[0].Len() < 1 { status = backend.HealthStatusError - message = "Can't find any environment variable" + message = "Can't find any environment variable." } return &backend.CheckHealthResult{ diff --git a/pkg/plugin/plugin_test.go b/pkg/plugin/plugin_test.go index 00bb764..40ecd6d 100644 --- a/pkg/plugin/plugin_test.go +++ b/pkg/plugin/plugin_test.go @@ -6,15 +6,14 @@ import ( "github.com/grafana/grafana-plugin-sdk-go/backend" "github.com/grafana/grafana-starter-datasource-backend/pkg/plugin" + "github.com/stretchr/testify/require" ) /** * Test Query */ func TestQueryData(t *testing.T) { - settings := plugin.PluginSettings{ - Filter: "", - } + settings := plugin.PluginSettings{} ds := plugin.Datasource{Settings: &settings} resp, err := ds.QueryData( @@ -34,3 +33,45 @@ func TestQueryData(t *testing.T) { t.Fatal("QueryData must return a response") } } + +/** + * Test Health Check + */ +func TestCheckHealth(t *testing.T) { + settings := plugin.PluginSettings{} + ds := plugin.Datasource{Settings: &settings} + + resp, err := ds.CheckHealth( + context.Background(), + &backend.CheckHealthRequest{}, + ) + + if err != nil { + t.Error(err) + } + + require.NoError(t, err) + require.Equal(t, resp.Status, backend.HealthStatusOk) +} + +/** + * Test Health Check with Error + */ +func TestCheckHealthWithError(t *testing.T) { + settings := plugin.PluginSettings{ + Filter: "ABC", + } + ds := plugin.Datasource{Settings: &settings} + + resp, err := ds.CheckHealth( + context.Background(), + &backend.CheckHealthRequest{}, + ) + + if err != nil { + t.Error(err) + } + + require.NoError(t, err) + require.Equal(t, resp.Status, backend.HealthStatusError) +} diff --git a/provisioning/datasources/datasources.yaml b/provisioning/datasources/datasources.yaml index 025fd28..433dd8e 100644 --- a/provisioning/datasources/datasources.yaml +++ b/provisioning/datasources/datasources.yaml @@ -8,3 +8,5 @@ datasources: orgId: 1 version: 1 editable: true + jsonData: + filter: GF_ diff --git a/src/img/grafana-marketplace.png b/src/img/grafana-marketplace.png new file mode 100644 index 0000000..c652421 Binary files /dev/null and b/src/img/grafana-marketplace.png differ diff --git a/yarn.lock b/yarn.lock index 56a1c5a..558c2d1 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2364,11 +2364,6 @@ resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.149.tgz#1342d63d948c6062838fbf961012f74d4e638440" integrity sha512-ijGqzZt/b7BfzcK9vTrS6MFljQRPn5BFWOx8oE0GYxribu6uV+aA9zZuXI1zc/etK9E8nrgdoF2+LgUw7+9tJQ== -"@types/lodash@4.14.178": - version "4.14.178" - resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.178.tgz#341f6d2247db528d4a13ddbb374bcdc80406f4f8" - integrity sha512-0d5Wd09ItQWH1qFbEyQ7oTQ3GZrMfth5JkbN3EvTKLXcHLRDSXeLnlvlOn0wvxVIwK5o2M8JzP/OWz7T3NRsbw== - "@types/mime@^1": version "1.3.2" resolved "https://registry.yarnpkg.com/@types/mime/-/mime-1.3.2.tgz#93e25bf9ee75fe0fd80b594bc4feb0e862111b5a" @@ -10810,11 +10805,11 @@ rxjs@^6.6.3: tslib "^1.9.0" rxjs@^7.2.0: - version "7.3.0" - resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-7.3.0.tgz#39fe4f3461dc1e50be1475b2b85a0a88c1e938c6" - integrity sha512-p2yuGIg9S1epc3vrjKf6iVb3RCaAYjYskkO+jHIaV0IjOPlJop4UnodOoFb2xeNwlguqLYvGw1b1McillYb5Gw== + version "7.5.4" + resolved "https://registry.yarnpkg.com/rxjs/-/rxjs-7.5.4.tgz#3d6bd407e6b7ce9a123e76b1e770dc5761aa368d" + integrity sha512-h5M3Hk78r6wAheJF0a5YahB1yRQKCsZ4MsGdZ5O9ETbVtjPcScGfrMmoOq7EBsCRzd4BDkvDJ7ogP8Sz5tTFiQ== dependencies: - tslib "~2.1.0" + tslib "^2.1.0" safe-buffer@^5.0.1, safe-buffer@^5.1.0, safe-buffer@^5.1.1, safe-buffer@^5.1.2, safe-buffer@^5.2.0, safe-buffer@~5.2.0: version "5.2.1" @@ -11990,11 +11985,6 @@ tslib@^1.10.0, tslib@^1.8.1, tslib@^1.9.0, tslib@^1.9.3: resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.14.1.tgz#cf2d38bdc34a134bcaf1091c41f6619e2f672d00" integrity sha512-Xni35NKzjgMrwevysHTCArtLDpPvye8zV/0E4EyYn43P7/7qvQwPh9BGkHewbMulVntbigmcT7rdX3BNo9wRJg== -tslib@~2.1.0: - version "2.1.0" - resolved "https://registry.yarnpkg.com/tslib/-/tslib-2.1.0.tgz#da60860f1c2ecaa5703ab7d39bc05b6bf988b97a" - integrity sha512-hcVC3wYEziELGGmEEXue7D75zbwIIVUMWAVbHItGPx0ziyXxrOMQx4rQEVEV45Ut/1IotuEvwqPopzIOkDMf0A== - tsutils@^3.21.0: version "3.21.0" resolved "https://registry.yarnpkg.com/tsutils/-/tsutils-3.21.0.tgz#b48717d394cea6c1e096983eed58e9d61715b623"