Skip to content

Commit 878555a

Browse files
authored
APIVersion: Add API Version to protobuf (#911)
1 parent a502fa5 commit 878555a

File tree

6 files changed

+93
-11
lines changed

6 files changed

+93
-11
lines changed

backend/common.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ type AppInstanceSettings struct {
3737

3838
// Updated is the last time this plugin instance's configuration was updated.
3939
Updated time.Time
40+
41+
// The API Version when settings were saved
42+
// NOTE: this may be older than the current version
43+
APIVersion string
4044
}
4145

4246
// HTTPClientOptions creates httpclient.Options based on settings.
@@ -57,7 +61,7 @@ func (s *AppInstanceSettings) HTTPClientOptions(_ context.Context) (httpclient.O
5761
// In Grafana a data source instance is a data source plugin of certain
5862
// type that have been configured and created in a Grafana organization.
5963
type DataSourceInstanceSettings struct {
60-
// ID is the Grafana assigned numeric identifier of the the data source instance.
64+
// Deprecated ID is the Grafana assigned numeric identifier of the the data source instance.
6165
ID int64
6266

6367
// UID is the Grafana assigned string identifier of the the data source instance.
@@ -98,6 +102,10 @@ type DataSourceInstanceSettings struct {
98102

99103
// Updated is the last time the configuration for the data source instance was updated.
100104
Updated time.Time
105+
106+
// The API Version when settings were saved
107+
// NOTE: this may be older than the current version
108+
APIVersion string
101109
}
102110

103111
// HTTPClientOptions creates httpclient.Options based on settings.
@@ -178,6 +186,9 @@ type PluginContext struct {
178186
// UserAgent is the user agent of the Grafana server that initiated the gRPC request.
179187
// Will only be set if request is made from Grafana v10.2.0 or later.
180188
UserAgent *useragent.UserAgent
189+
190+
// The requested API version
191+
APIVersion string
181192
}
182193

183194
func setCustomOptionsFromHTTPSettings(opts *httpclient.Options, httpSettings *HTTPSettings) {

backend/convert_from_protobuf.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func (f ConvertFromProtobuf) AppInstanceSettings(proto *pluginv2.AppInstanceSett
4646
JSONData: proto.JsonData,
4747
DecryptedSecureJSONData: proto.DecryptedSecureJsonData,
4848
Updated: time.Unix(0, proto.LastUpdatedMS*int64(time.Millisecond)),
49+
APIVersion: proto.ApiVersion,
4950
}
5051
}
5152

@@ -68,6 +69,7 @@ func (f ConvertFromProtobuf) DataSourceInstanceSettings(proto *pluginv2.DataSour
6869
JSONData: proto.JsonData,
6970
DecryptedSecureJSONData: proto.DecryptedSecureJsonData,
7071
Updated: time.Unix(0, proto.LastUpdatedMS*int64(time.Millisecond)),
72+
APIVersion: proto.ApiVersion,
7173
}
7274
}
7375

@@ -89,6 +91,7 @@ func (f ConvertFromProtobuf) PluginContext(proto *pluginv2.PluginContext) Plugin
8991
OrgID: proto.OrgId,
9092
PluginID: proto.PluginId,
9193
PluginVersion: proto.PluginVersion,
94+
APIVersion: proto.ApiVersion,
9295
User: f.User(proto.User),
9396
AppInstanceSettings: f.AppInstanceSettings(proto.AppInstanceSettings),
9497
DataSourceInstanceSettings: f.DataSourceInstanceSettings(proto.DataSourceInstanceSettings, proto.PluginId),

backend/convert_from_protobuf_test.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ var protoAppInstanceSettings = &pluginv2.AppInstanceSettings{
106106
JsonData: []byte(`{ "foo": "gpp"`),
107107
DecryptedSecureJsonData: map[string]string{"secret": "quiet"},
108108
LastUpdatedMS: lastUpdatedMS,
109+
ApiVersion: "v1beta2",
109110
}
110111

111112
func TestConvertFromProtobufAppInstanceSettings(t *testing.T) {
@@ -135,6 +136,7 @@ func TestConvertFromProtobufAppInstanceSettings(t *testing.T) {
135136
requireCounter.Equal(t, json.RawMessage(protoAIS.JsonData), sdkAIS.JSONData)
136137
requireCounter.Equal(t, map[string]string{"secret": "quiet"}, sdkAIS.DecryptedSecureJSONData)
137138
requireCounter.Equal(t, lastUpdatedTime, sdkAIS.Updated)
139+
requireCounter.Equal(t, protoAIS.ApiVersion, sdkAIS.APIVersion)
138140

139141
require.Equal(t, requireCounter.Count, sdkWalker.FieldCount, "untested fields in conversion")
140142
}
@@ -151,6 +153,7 @@ var protoDataSourceInstanceSettings = &pluginv2.DataSourceInstanceSettings{
151153
JsonData: []byte(`{ "foo": "gpp"`),
152154
DecryptedSecureJsonData: map[string]string{"secret": "quiet"},
153155
LastUpdatedMS: lastUpdatedMS,
156+
ApiVersion: "v0alpha3",
154157
}
155158

156159
func TestConvertFromProtobufDataSourceInstanceSettings(t *testing.T) {
@@ -189,6 +192,7 @@ func TestConvertFromProtobufDataSourceInstanceSettings(t *testing.T) {
189192
requireCounter.Equal(t, json.RawMessage(protoDSIS.JsonData), sdkDSIS.JSONData)
190193
requireCounter.Equal(t, map[string]string{"secret": "quiet"}, sdkDSIS.DecryptedSecureJSONData)
191194
requireCounter.Equal(t, lastUpdatedTime, sdkDSIS.Updated)
195+
requireCounter.Equal(t, protoDSIS.ApiVersion, sdkDSIS.APIVersion)
192196

193197
require.Equal(t, requireCounter.Count, sdkWalker.FieldCount, "untested fields in conversion")
194198
}
@@ -208,7 +212,8 @@ var protoPluginContext = &pluginv2.PluginContext{
208212
GrafanaConfig: map[string]string{
209213
"foo": "bar",
210214
},
211-
UserAgent: "Grafana/10.0.0 (linux; amd64)",
215+
UserAgent: "Grafana/10.0.0 (linux; amd64)",
216+
ApiVersion: "v0alpha1",
212217
}
213218

214219
func TestConvertFromProtobufPluginContext(t *testing.T) {
@@ -248,13 +253,16 @@ func TestConvertFromProtobufPluginContext(t *testing.T) {
248253
requireCounter.Equal(t, json.RawMessage(protoCtx.AppInstanceSettings.JsonData), sdkCtx.AppInstanceSettings.JSONData)
249254
requireCounter.Equal(t, map[string]string{"secret": "quiet"}, sdkCtx.AppInstanceSettings.DecryptedSecureJSONData)
250255
requireCounter.Equal(t, time.Unix(0, 86400*2*1e9), sdkCtx.AppInstanceSettings.Updated)
256+
requireCounter.Equal(t, protoCtx.AppInstanceSettings.ApiVersion, sdkCtx.AppInstanceSettings.APIVersion)
251257

252258
// Datasource Instance Settings
253259
requireCounter.Equal(t, protoCtx.DataSourceInstanceSettings.Name, sdkCtx.DataSourceInstanceSettings.Name)
254260
requireCounter.Equal(t, protoCtx.DataSourceInstanceSettings.Id, sdkCtx.DataSourceInstanceSettings.ID)
255261
requireCounter.Equal(t, protoCtx.DataSourceInstanceSettings.Uid, sdkCtx.DataSourceInstanceSettings.UID)
262+
requireCounter.Equal(t, protoCtx.DataSourceInstanceSettings.ApiVersion, sdkCtx.DataSourceInstanceSettings.APIVersion)
256263
requireCounter.Equal(t, protoCtx.PluginId, sdkCtx.DataSourceInstanceSettings.Type)
257264
requireCounter.Equal(t, protoCtx.PluginVersion, sdkCtx.PluginVersion)
265+
requireCounter.Equal(t, protoCtx.ApiVersion, sdkCtx.APIVersion)
258266
requireCounter.Equal(t, protoCtx.DataSourceInstanceSettings.Url, sdkCtx.DataSourceInstanceSettings.URL)
259267
requireCounter.Equal(t, protoCtx.DataSourceInstanceSettings.User, sdkCtx.DataSourceInstanceSettings.User)
260268
requireCounter.Equal(t, protoCtx.DataSourceInstanceSettings.Database, sdkCtx.DataSourceInstanceSettings.Database)
@@ -396,6 +404,7 @@ func TestConvertFromProtobufQueryDataRequest(t *testing.T) {
396404
// PluginContext
397405
requireCounter.Equal(t, protoQDR.PluginContext.OrgId, sdkQDR.PluginContext.OrgID)
398406
requireCounter.Equal(t, protoQDR.PluginContext.PluginId, sdkQDR.PluginContext.PluginID)
407+
requireCounter.Equal(t, protoQDR.PluginContext.ApiVersion, sdkQDR.PluginContext.APIVersion)
399408
// User
400409
requireCounter.Equal(t, protoQDR.PluginContext.User.Login, sdkQDR.PluginContext.User.Login)
401410
requireCounter.Equal(t, protoQDR.PluginContext.User.Name, sdkQDR.PluginContext.User.Name)
@@ -406,11 +415,13 @@ func TestConvertFromProtobufQueryDataRequest(t *testing.T) {
406415
requireCounter.Equal(t, json.RawMessage(protoQDR.PluginContext.AppInstanceSettings.JsonData), sdkQDR.PluginContext.AppInstanceSettings.JSONData)
407416
requireCounter.Equal(t, map[string]string{"secret": "quiet"}, sdkQDR.PluginContext.AppInstanceSettings.DecryptedSecureJSONData)
408417
requireCounter.Equal(t, time.Unix(0, 86400*2*1e9), sdkQDR.PluginContext.AppInstanceSettings.Updated)
418+
requireCounter.Equal(t, protoQDR.PluginContext.AppInstanceSettings.ApiVersion, sdkQDR.PluginContext.AppInstanceSettings.APIVersion)
409419

410420
// Datasource Instance Settings
411421
requireCounter.Equal(t, protoQDR.PluginContext.DataSourceInstanceSettings.Name, sdkQDR.PluginContext.DataSourceInstanceSettings.Name)
412422
requireCounter.Equal(t, protoQDR.PluginContext.DataSourceInstanceSettings.Id, sdkQDR.PluginContext.DataSourceInstanceSettings.ID)
413423
requireCounter.Equal(t, protoQDR.PluginContext.DataSourceInstanceSettings.Uid, sdkQDR.PluginContext.DataSourceInstanceSettings.UID)
424+
requireCounter.Equal(t, protoQDR.PluginContext.DataSourceInstanceSettings.ApiVersion, sdkQDR.PluginContext.DataSourceInstanceSettings.APIVersion)
414425
requireCounter.Equal(t, protoQDR.PluginContext.PluginId, sdkQDR.PluginContext.DataSourceInstanceSettings.Type)
415426
requireCounter.Equal(t, protoQDR.PluginContext.PluginVersion, sdkQDR.PluginContext.PluginVersion)
416427
requireCounter.Equal(t, protoQDR.PluginContext.DataSourceInstanceSettings.Url, sdkQDR.PluginContext.DataSourceInstanceSettings.URL)

backend/convert_to_protobuf.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ func (t ConvertToProtobuf) AppInstanceSettings(s *AppInstanceSettings) *pluginv2
4545
JsonData: s.JSONData,
4646
DecryptedSecureJsonData: s.DecryptedSecureJSONData,
4747
LastUpdatedMS: s.Updated.UnixNano() / int64(time.Millisecond),
48+
ApiVersion: s.APIVersion,
4849
}
4950
}
5051

@@ -66,6 +67,7 @@ func (t ConvertToProtobuf) DataSourceInstanceSettings(s *DataSourceInstanceSetti
6667
JsonData: s.JSONData,
6768
DecryptedSecureJsonData: s.DecryptedSecureJSONData,
6869
LastUpdatedMS: s.Updated.UnixNano() / int64(time.Millisecond),
70+
ApiVersion: s.APIVersion,
6971
}
7072
}
7173

@@ -84,6 +86,7 @@ func (t ConvertToProtobuf) PluginContext(pluginCtx PluginContext) *pluginv2.Plug
8486
OrgId: pluginCtx.OrgID,
8587
PluginId: pluginCtx.PluginID,
8688
PluginVersion: pluginCtx.PluginVersion,
89+
ApiVersion: pluginCtx.APIVersion,
8790
User: t.User(pluginCtx.User),
8891
AppInstanceSettings: t.AppInstanceSettings(pluginCtx.AppInstanceSettings),
8992
DataSourceInstanceSettings: t.DataSourceInstanceSettings(pluginCtx.DataSourceInstanceSettings),

genproto/pluginv2/backend.pb.go

Lines changed: 47 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

proto/backend.proto

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,14 @@ message AppInstanceSettings {
1111
bytes jsonData = 3;
1212
map<string,string> decryptedSecureJsonData = 4;
1313
int64 lastUpdatedMS = 5;
14+
15+
// The API version when the settings were saved
16+
// NOTE: this may be an older version than the current apiVersion
17+
string apiVersion = 6;
1418
}
1519

1620
message DataSourceInstanceSettings {
21+
// Deprecatd: Internal ID, do not use this for anythign important
1722
int64 id = 1;
1823
string name = 2;
1924
string url = 3;
@@ -23,8 +28,16 @@ message DataSourceInstanceSettings {
2328
string basicAuthUser = 7;
2429
bytes jsonData = 8;
2530
map<string,string> decryptedSecureJsonData = 9;
31+
32+
// timestamp when the settings where last changed
2633
int64 lastUpdatedMS = 10;
34+
35+
// Datasoruce unique ID (within an org/stack namespace)
2736
string uid = 11;
37+
38+
// The API version when the settings were saved.
39+
// NOTE: this may be an older version than the current apiVersion
40+
string apiVersion = 12;
2841
}
2942

3043
message User {
@@ -68,6 +81,9 @@ message PluginContext {
6881

6982
// The user agent of the Grafana server that initiated the gRPC request.
7083
string userAgent = 8;
84+
85+
// The API version that initiated a request
86+
string apiVersion = 9;
7187
}
7288

7389
//---------------------------------------------------------

0 commit comments

Comments
 (0)