Skip to content

Commit 6eff597

Browse files
authored
Move hide logic to datasource.ts filter and reuse client for test connection (#13)
1 parent f4d3a87 commit 6eff597

2 files changed

Lines changed: 5 additions & 28 deletions

File tree

pkg/plugin/plugin.go

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,6 @@ func (d *CloudLoggingDatasource) QueryData(ctx context.Context, req *backend.Que
180180
type queryModel struct {
181181
QueryText string `json:"queryText"`
182182
ProjectID string `json:"projectId"`
183-
Hide bool `json:"hide"`
184183
}
185184

186185
func (d *CloudLoggingDatasource) query(ctx context.Context, pCtx backend.PluginContext, query backend.DataQuery) backend.DataResponse {
@@ -192,11 +191,6 @@ func (d *CloudLoggingDatasource) query(ctx context.Context, pCtx backend.PluginC
192191
return response
193192
}
194193

195-
// Don't query if query should be hidden
196-
if q.Hide {
197-
return response
198-
}
199-
200194
clientRequest := cloudlogging.Query{
201195
ProjectID: q.ProjectID,
202196
Filter: q.QueryText,
@@ -260,28 +254,7 @@ func (d *CloudLoggingDatasource) CheckHealth(ctx context.Context, req *backend.C
260254
return nil, fmt.Errorf("unmarshal: %w", err)
261255
}
262256

263-
privateKey, ok := settings.DecryptedSecureJSONData[privateKeyKey]
264-
if !ok || privateKey == "" {
265-
return nil, errMissingCredentials
266-
}
267-
268-
serviceAccount, err := conf.toServiceAccountJSON(privateKey)
269-
if err != nil {
270-
return nil, fmt.Errorf("create credentials: %w", err)
271-
}
272-
273-
client, err := cloudlogging.NewClient(ctx, serviceAccount, conf.Endpoint)
274-
if err != nil {
275-
return nil, err
276-
}
277-
278-
defer func() {
279-
if err := client.Close(); err != nil {
280-
log.DefaultLogger.Warn("failed closing client", "error", err)
281-
}
282-
}()
283-
284-
if err := client.TestConnection(ctx, conf.DefaultProject); err != nil {
257+
if err := d.client.TestConnection(ctx, conf.DefaultProject); err != nil {
285258
return &backend.CheckHealthResult{
286259
Status: backend.HealthStatusError,
287260
Message: fmt.Sprintf("failed to run test query: %s", err),

src/datasource.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,8 @@ export class DataSource extends DataSourceWithBackend<Query, CloudLoggingOptions
4545
return [];
4646
}
4747
}
48+
49+
filterQuery(query: Query): boolean {
50+
return !query.hide;
51+
}
4852
}

0 commit comments

Comments
 (0)