Skip to content

Commit 52211b6

Browse files
authored
Store cloudAddr in unencrypted JSON data. Simplify logic for updating and resetting plugin settings (#103)
Signed-off-by: Dom Del Nano <[email protected]>
1 parent 1b60590 commit 52211b6

File tree

3 files changed

+36
-95
lines changed

3 files changed

+36
-95
lines changed

pkg/pixie_plugin.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,17 @@ func (td *PixieDatasource) QueryData(ctx context.Context, req *backend.QueryData
5959
// Loop over queries and execute them individually. Save the response
6060
// in a hashmap with RefID as identifier.
6161
for _, q := range req.Queries {
62-
res, err := td.query(ctx, q, req.PluginContext.DataSourceInstanceSettings.DecryptedSecureJSONData)
62+
dataSourceSettings := req.PluginContext.DataSourceInstanceSettings
63+
jsonData := dataSourceSettings.JSONData
64+
secureJsonData := dataSourceSettings.DecryptedSecureJSONData
65+
66+
var jsonDataMap map[string]interface{}
67+
err := json.Unmarshal(jsonData, &jsonDataMap)
68+
if err != nil {
69+
return nil, fmt.Errorf("error unmarshalling JSON: %v", err)
70+
}
71+
72+
res, err := td.query(ctx, q, jsonDataMap, secureJsonData)
6373
if err != nil {
6474
return response, err
6575
}
@@ -149,11 +159,12 @@ type queryModel struct {
149159

150160
// Handle an incoming query
151161
func (td *PixieDatasource) query(ctx context.Context, query backend.DataQuery,
152-
config map[string]string) (*backend.DataResponse, error) {
162+
config map[string]interface{}, decryptedConfig map[string]string) (*backend.DataResponse, error) {
163+
164+
cloudAddr := config[cloudAddrField].(string)
153165

154-
apiToken := config[apiKeyField]
155-
cloudAddr := config[cloudAddrField]
156-
clusterID := config[clusterIDField]
166+
apiToken := decryptedConfig[apiKeyField]
167+
clusterID := decryptedConfig[clusterIDField]
157168
var qm queryModel
158169
if err := json.Unmarshal(query.JSON, &qm); err != nil {
159170
return nil, fmt.Errorf("error unmarshalling JSON: %v", err)

src/config_editor.tsx

Lines changed: 16 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@
1616
* SPDX-License-Identifier: Apache-2.0
1717
*/
1818

19-
import React, { ChangeEvent, PureComponent } from 'react';
19+
import React, { PureComponent } from 'react';
2020
import { LegacyForms } from '@grafana/ui';
21-
import { DataSourcePluginOptionsEditorProps } from '@grafana/data';
21+
import {
22+
DataSourcePluginOptionsEditorProps,
23+
onUpdateDatasourceJsonDataOption,
24+
onUpdateDatasourceSecureJsonDataOption,
25+
updateDatasourcePluginResetOption,
26+
} from '@grafana/data';
2227
import { PixieDataSourceOptions, PixieSecureDataSourceOptions } from './types';
2328

2429
const { FormField, SecretFormField } = LegacyForms;
@@ -28,94 +33,19 @@ interface Props extends DataSourcePluginOptionsEditorProps<PixieDataSourceOption
2833
interface State {}
2934

3035
export class ConfigEditor extends PureComponent<Props, State> {
31-
onAPIKeyChange = (event: ChangeEvent<HTMLInputElement>) => {
32-
const { onOptionsChange, options } = this.props;
33-
34-
onOptionsChange({
35-
...options,
36-
secureJsonData: {
37-
...options?.secureJsonData,
38-
apiKey: event.target.value,
39-
},
40-
});
41-
};
42-
43-
onClusterIdChange = (event: ChangeEvent<HTMLInputElement>) => {
44-
const { onOptionsChange, options } = this.props;
45-
46-
onOptionsChange({
47-
...options,
48-
secureJsonData: {
49-
...options?.secureJsonData,
50-
clusterId: event.target.value,
51-
},
52-
});
53-
};
54-
55-
onCloudAddrChange = (event: ChangeEvent<HTMLInputElement>) => {
56-
const { onOptionsChange, options } = this.props;
57-
58-
onOptionsChange({
59-
...options,
60-
secureJsonData: {
61-
...options?.secureJsonData,
62-
cloudAddr: event.target.value,
63-
},
64-
});
65-
};
66-
6736
onResetAPIKey = () => {
68-
const { onOptionsChange, options } = this.props;
69-
70-
onOptionsChange({
71-
...options,
72-
secureJsonFields: {
73-
...options.secureJsonFields,
74-
apiKey: false,
75-
},
76-
secureJsonData: {
77-
...options.secureJsonData,
78-
apiKey: '',
79-
},
80-
});
37+
updateDatasourcePluginResetOption(this.props, 'apiKey');
8138
};
8239

8340
onResetClusterId = () => {
84-
const { onOptionsChange, options } = this.props;
85-
86-
onOptionsChange({
87-
...options,
88-
secureJsonFields: {
89-
...options.secureJsonFields,
90-
clusterId: false,
91-
},
92-
secureJsonData: {
93-
...options.secureJsonData,
94-
clusterId: '',
95-
},
96-
});
97-
};
98-
99-
onResetCloudAddr = () => {
100-
const { onOptionsChange, options } = this.props;
101-
102-
onOptionsChange({
103-
...options,
104-
secureJsonFields: {
105-
...options.secureJsonFields,
106-
cloudAddr: false,
107-
},
108-
secureJsonData: {
109-
...options.secureJsonData,
110-
cloudAddr: '',
111-
},
112-
});
41+
updateDatasourcePluginResetOption(this.props, 'clusterId');
11342
};
11443

11544
render() {
11645
const { options } = this.props;
11746
const { secureJsonFields } = options;
11847
const secureJsonData = (options.secureJsonData || {}) as PixieSecureDataSourceOptions;
48+
const jsonData = (options.jsonData || {}) as PixieDataSourceOptions;
11949

12050
return (
12151
<div className="gf-form-group">
@@ -129,7 +59,7 @@ export class ConfigEditor extends PureComponent<Props, State> {
12959
labelWidth={20}
13060
inputWidth={20}
13161
onReset={this.onResetAPIKey}
132-
onChange={this.onAPIKeyChange}
62+
onChange={onUpdateDatasourceSecureJsonDataOption(this.props, 'apiKey')}
13363
/>
13464
</div>
13565
</div>
@@ -144,21 +74,20 @@ export class ConfigEditor extends PureComponent<Props, State> {
14474
labelWidth={20}
14575
inputWidth={20}
14676
onReset={this.onResetClusterId}
147-
onChange={this.onClusterIdChange}
77+
onChange={onUpdateDatasourceSecureJsonDataOption(this.props, 'clusterId')}
14878
/>
14979
</div>
15080
</div>
15181

15282
<div className="gf-form-inline">
15383
<div className="gf-form">
15484
<FormField
155-
value={secureJsonData.cloudAddr || ''}
156-
label="Pixie Cloud address (if not using withpixie.ai)"
157-
placeholder="withpixie.ai:443"
85+
value={jsonData.cloudAddr || ''}
86+
label="Pixie Cloud address (if not using getcosmic.ai)"
87+
placeholder="getcosmic.ai:443"
15888
labelWidth={20}
15989
inputWidth={20}
160-
onReset={this.onResetCloudAddr}
161-
onChange={this.onCloudAddrChange}
90+
onChange={onUpdateDatasourceJsonDataOption(this.props, 'cloudAddr')}
16291
/>
16392
</div>
16493
</div>

src/types.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,14 @@ export const defaultQuery: Partial<PixieDataQuery> = {
6969
},
7070
};
7171

72-
export interface PixieDataSourceOptions extends DataSourceJsonData {}
72+
export interface PixieDataSourceOptions extends DataSourceJsonData {
73+
// Address of Pixie cloud.
74+
cloudAddr?: string;
75+
}
7376

7477
export interface PixieSecureDataSourceOptions {
7578
// Pixie API key.
7679
apiKey?: string;
77-
// Address of Pixie cloud.
78-
cloudAddr?: string;
7980
// ID of the Pixie cluster to query.
8081
clusterId?: string;
8182
}

0 commit comments

Comments
 (0)