Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add system field to data source instances #1161

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions backend/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ type DataSourceInstanceSettings struct {
// This should be the same value as PluginContext.PluginId.
Type string

// This indicates that the instance is a system instance and should not appear
// in Grafana except through code that will explicitly request system data sources
System bool

// Name is the configured name of the data source instance.
Name string

Expand Down
2 changes: 2 additions & 0 deletions backend/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func TestDataSourceInstanceSettings(t *testing.T) {
UID: "uid1",
User: "user",
Type: "example-datasource",
System: false,
JSONData: []byte("{}"),
BasicAuthEnabled: true,
BasicAuthUser: "buser",
Expand Down Expand Up @@ -120,6 +121,7 @@ func TestDataSourceInstanceSettings(t *testing.T) {
Name: "ds2",
UID: "uid2",
Type: "example-datasource-2",
System: false,
User: "user",
JSONData: []byte("{}"),
BasicAuthEnabled: false,
Expand Down
1 change: 1 addition & 0 deletions backend/convert_from_protobuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ func (f ConvertFromProtobuf) DataSourceInstanceSettings(proto *pluginv2.DataSour
ID: proto.Id,
UID: proto.Uid,
Type: pluginID,
System: proto.System,
Name: proto.Name,
URL: proto.Url,
User: proto.User,
Expand Down
6 changes: 6 additions & 0 deletions backend/convert_from_protobuf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ var protoDataSourceInstanceSettings = &pluginv2.DataSourceInstanceSettings{
DecryptedSecureJsonData: map[string]string{"secret": "quiet"},
LastUpdatedMS: lastUpdatedMS,
ApiVersion: "v0alpha3",
System: true,
}

func TestConvertFromProtobufDataSourceInstanceSettings(t *testing.T) {
Expand Down Expand Up @@ -195,6 +196,7 @@ func TestConvertFromProtobufDataSourceInstanceSettings(t *testing.T) {
requireCounter.Equal(t, map[string]string{"secret": "quiet"}, sdkDSIS.DecryptedSecureJSONData)
requireCounter.Equal(t, lastUpdatedTime, sdkDSIS.Updated)
requireCounter.Equal(t, protoDSIS.ApiVersion, sdkDSIS.APIVersion)
requireCounter.Equal(t, protoDSIS.System, sdkDSIS.System)

require.Equal(t, requireCounter.Count, sdkWalker.FieldCount, "untested fields in conversion")
}
Expand Down Expand Up @@ -270,6 +272,7 @@ func TestConvertFromProtobufPluginContext(t *testing.T) {
requireCounter.Equal(t, protoCtx.DataSourceInstanceSettings.Database, sdkCtx.DataSourceInstanceSettings.Database)
requireCounter.Equal(t, protoCtx.DataSourceInstanceSettings.BasicAuthEnabled, sdkCtx.DataSourceInstanceSettings.BasicAuthEnabled)
requireCounter.Equal(t, protoCtx.DataSourceInstanceSettings.BasicAuthUser, sdkCtx.DataSourceInstanceSettings.BasicAuthUser)
requireCounter.Equal(t, protoCtx.DataSourceInstanceSettings.System, sdkCtx.DataSourceInstanceSettings.System)
requireCounter.Equal(t, json.RawMessage(protoCtx.DataSourceInstanceSettings.JsonData), sdkCtx.DataSourceInstanceSettings.JSONData)
requireCounter.Equal(t, map[string]string{"secret": "quiet"}, sdkCtx.DataSourceInstanceSettings.DecryptedSecureJSONData)
requireCounter.Equal(t, time.Unix(0, 86400*2*1e9), sdkCtx.DataSourceInstanceSettings.Updated)
Expand Down Expand Up @@ -431,6 +434,7 @@ func TestConvertFromProtobufQueryDataRequest(t *testing.T) {
requireCounter.Equal(t, protoQDR.PluginContext.DataSourceInstanceSettings.Database, sdkQDR.PluginContext.DataSourceInstanceSettings.Database)
requireCounter.Equal(t, protoQDR.PluginContext.DataSourceInstanceSettings.BasicAuthEnabled, sdkQDR.PluginContext.DataSourceInstanceSettings.BasicAuthEnabled)
requireCounter.Equal(t, protoQDR.PluginContext.DataSourceInstanceSettings.BasicAuthUser, sdkQDR.PluginContext.DataSourceInstanceSettings.BasicAuthUser)
requireCounter.Equal(t, protoQDR.PluginContext.DataSourceInstanceSettings.System, sdkQDR.PluginContext.DataSourceInstanceSettings.System)
requireCounter.Equal(t, json.RawMessage(protoQDR.PluginContext.DataSourceInstanceSettings.JsonData), sdkQDR.PluginContext.DataSourceInstanceSettings.JSONData)
requireCounter.Equal(t, map[string]string{"secret": "quiet"}, sdkQDR.PluginContext.DataSourceInstanceSettings.DecryptedSecureJSONData)
requireCounter.Equal(t, time.Unix(0, 86400*2*1e9), sdkQDR.PluginContext.DataSourceInstanceSettings.Updated)
Expand Down Expand Up @@ -604,6 +608,7 @@ func TestConvertFromProtobufAdmissionRequest(t *testing.T) {
requireCounter.Equal(t, map[string]string{"secret": "quiet"}, sdkAR.PluginContext.DataSourceInstanceSettings.DecryptedSecureJSONData)
requireCounter.Equal(t, time.Unix(0, 86400*2*1e9), sdkAR.PluginContext.DataSourceInstanceSettings.Updated)
requireCounter.Equal(t, protoAR.PluginContext.UserAgent, sdkAR.PluginContext.UserAgent.String())
requireCounter.Equal(t, protoAR.PluginContext.DataSourceInstanceSettings.System, sdkAR.PluginContext.DataSourceInstanceSettings.System, "System must be equal")

// The actual request values
requireCounter.Equal(t, protoAR.Kind.Group, sdkAR.Kind.Group)
Expand Down Expand Up @@ -729,6 +734,7 @@ func TestConvertFromProtobufConversionRequest(t *testing.T) {
requireCounter.Equal(t, protoCR.PluginContext.DataSourceInstanceSettings.Database, sdkCR.PluginContext.DataSourceInstanceSettings.Database)
requireCounter.Equal(t, protoCR.PluginContext.DataSourceInstanceSettings.BasicAuthEnabled, sdkCR.PluginContext.DataSourceInstanceSettings.BasicAuthEnabled)
requireCounter.Equal(t, protoCR.PluginContext.DataSourceInstanceSettings.BasicAuthUser, sdkCR.PluginContext.DataSourceInstanceSettings.BasicAuthUser)
requireCounter.Equal(t, protoCR.PluginContext.DataSourceInstanceSettings.System, sdkCR.PluginContext.DataSourceInstanceSettings.System)
requireCounter.Equal(t, json.RawMessage(protoCR.PluginContext.DataSourceInstanceSettings.JsonData), sdkCR.PluginContext.DataSourceInstanceSettings.JSONData)
requireCounter.Equal(t, map[string]string{"secret": "quiet"}, sdkCR.PluginContext.DataSourceInstanceSettings.DecryptedSecureJSONData)
requireCounter.Equal(t, time.Unix(0, 86400*2*1e9), sdkCR.PluginContext.DataSourceInstanceSettings.Updated)
Expand Down
1 change: 1 addition & 0 deletions backend/convert_to_protobuf.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func (t ConvertToProtobuf) DataSourceInstanceSettings(s *DataSourceInstanceSetti
DecryptedSecureJsonData: s.DecryptedSecureJSONData,
LastUpdatedMS: s.Updated.UnixNano() / int64(time.Millisecond),
ApiVersion: s.APIVersion,
System: s.System,
}
}

Expand Down
1 change: 1 addition & 0 deletions backend/convert_to_protobuf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ var testPluginContext = PluginContext{
ID: 1,
UID: "uid",
Type: "pluginID",
System: true,
Name: "name",
URL: "http://example.com",
User: "user",
Expand Down
10 changes: 10 additions & 0 deletions genproto/pluginv2/backend.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions proto/backend.proto
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ message DataSourceInstanceSettings {
// The API version when the settings were saved.
// NOTE: this may be an older version than the current apiVersion
string apiVersion = 12;
bool system = 13;
}

message User {
Expand Down
Loading