diff --git a/pkg/pixie_plugin.go b/pkg/pixie_plugin.go index 7201822..07beae2 100644 --- a/pkg/pixie_plugin.go +++ b/pkg/pixie_plugin.go @@ -59,7 +59,17 @@ func (td *PixieDatasource) QueryData(ctx context.Context, req *backend.QueryData // Loop over queries and execute them individually. Save the response // in a hashmap with RefID as identifier. for _, q := range req.Queries { - res, err := td.query(ctx, q, req.PluginContext.DataSourceInstanceSettings.DecryptedSecureJSONData) + dataSourceSettings := req.PluginContext.DataSourceInstanceSettings + jsonData := dataSourceSettings.JSONData + secureJsonData := dataSourceSettings.DecryptedSecureJSONData + + var jsonDataMap map[string]interface{} + err := json.Unmarshal(jsonData, &jsonDataMap) + if err != nil { + return nil, fmt.Errorf("error unmarshalling JSON: %v", err) + } + + res, err := td.query(ctx, q, jsonDataMap, secureJsonData) if err != nil { return response, err } @@ -149,11 +159,12 @@ type queryModel struct { // Handle an incoming query func (td *PixieDatasource) query(ctx context.Context, query backend.DataQuery, - config map[string]string) (*backend.DataResponse, error) { + config map[string]interface{}, decryptedConfig map[string]string) (*backend.DataResponse, error) { + + cloudAddr := config[cloudAddrField].(string) - apiToken := config[apiKeyField] - cloudAddr := config[cloudAddrField] - clusterID := config[clusterIDField] + apiToken := decryptedConfig[apiKeyField] + clusterID := decryptedConfig[clusterIDField] var qm queryModel if err := json.Unmarshal(query.JSON, &qm); err != nil { return nil, fmt.Errorf("error unmarshalling JSON: %v", err) diff --git a/src/config_editor.tsx b/src/config_editor.tsx index 2bbf29e..8a9719a 100644 --- a/src/config_editor.tsx +++ b/src/config_editor.tsx @@ -16,9 +16,14 @@ * SPDX-License-Identifier: Apache-2.0 */ -import React, { ChangeEvent, PureComponent } from 'react'; +import React, { PureComponent } from 'react'; import { LegacyForms } from '@grafana/ui'; -import { DataSourcePluginOptionsEditorProps } from '@grafana/data'; +import { + DataSourcePluginOptionsEditorProps, + onUpdateDatasourceJsonDataOption, + onUpdateDatasourceSecureJsonDataOption, + updateDatasourcePluginResetOption, +} from '@grafana/data'; import { PixieDataSourceOptions, PixieSecureDataSourceOptions } from './types'; const { FormField, SecretFormField } = LegacyForms; @@ -28,94 +33,19 @@ interface Props extends DataSourcePluginOptionsEditorProps { - onAPIKeyChange = (event: ChangeEvent) => { - const { onOptionsChange, options } = this.props; - - onOptionsChange({ - ...options, - secureJsonData: { - ...options?.secureJsonData, - apiKey: event.target.value, - }, - }); - }; - - onClusterIdChange = (event: ChangeEvent) => { - const { onOptionsChange, options } = this.props; - - onOptionsChange({ - ...options, - secureJsonData: { - ...options?.secureJsonData, - clusterId: event.target.value, - }, - }); - }; - - onCloudAddrChange = (event: ChangeEvent) => { - const { onOptionsChange, options } = this.props; - - onOptionsChange({ - ...options, - secureJsonData: { - ...options?.secureJsonData, - cloudAddr: event.target.value, - }, - }); - }; - onResetAPIKey = () => { - const { onOptionsChange, options } = this.props; - - onOptionsChange({ - ...options, - secureJsonFields: { - ...options.secureJsonFields, - apiKey: false, - }, - secureJsonData: { - ...options.secureJsonData, - apiKey: '', - }, - }); + updateDatasourcePluginResetOption(this.props, 'apiKey'); }; onResetClusterId = () => { - const { onOptionsChange, options } = this.props; - - onOptionsChange({ - ...options, - secureJsonFields: { - ...options.secureJsonFields, - clusterId: false, - }, - secureJsonData: { - ...options.secureJsonData, - clusterId: '', - }, - }); - }; - - onResetCloudAddr = () => { - const { onOptionsChange, options } = this.props; - - onOptionsChange({ - ...options, - secureJsonFields: { - ...options.secureJsonFields, - cloudAddr: false, - }, - secureJsonData: { - ...options.secureJsonData, - cloudAddr: '', - }, - }); + updateDatasourcePluginResetOption(this.props, 'clusterId'); }; render() { const { options } = this.props; const { secureJsonFields } = options; const secureJsonData = (options.secureJsonData || {}) as PixieSecureDataSourceOptions; + const jsonData = (options.jsonData || {}) as PixieDataSourceOptions; return (
@@ -129,7 +59,7 @@ export class ConfigEditor extends PureComponent { labelWidth={20} inputWidth={20} onReset={this.onResetAPIKey} - onChange={this.onAPIKeyChange} + onChange={onUpdateDatasourceSecureJsonDataOption(this.props, 'apiKey')} />
@@ -144,7 +74,7 @@ export class ConfigEditor extends PureComponent { labelWidth={20} inputWidth={20} onReset={this.onResetClusterId} - onChange={this.onClusterIdChange} + onChange={onUpdateDatasourceSecureJsonDataOption(this.props, 'clusterId')} /> @@ -152,13 +82,12 @@ export class ConfigEditor extends PureComponent {
diff --git a/src/types.ts b/src/types.ts index c49ed29..f7f0cf6 100644 --- a/src/types.ts +++ b/src/types.ts @@ -69,13 +69,14 @@ export const defaultQuery: Partial = { }, }; -export interface PixieDataSourceOptions extends DataSourceJsonData {} +export interface PixieDataSourceOptions extends DataSourceJsonData { + // Address of Pixie cloud. + cloudAddr?: string; +} export interface PixieSecureDataSourceOptions { // Pixie API key. apiKey?: string; - // Address of Pixie cloud. - cloudAddr?: string; // ID of the Pixie cluster to query. clusterId?: string; }