-
Notifications
You must be signed in to change notification settings - Fork 32
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
Create plugin: Update data source templates #836
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import { DataSourceInstanceSettings, CoreApp } from '@grafana/data'; | ||
import { DataSourceWithBackend } from '@grafana/runtime'; | ||
import { DataSourceInstanceSettings, CoreApp, ScopedVars } from '@grafana/data'; | ||
import { DataSourceWithBackend, getTemplateSrv } from '@grafana/runtime'; | ||
|
||
import { MyQuery, MyDataSourceOptions, DEFAULT_QUERY } from './types'; | ||
|
||
|
@@ -9,6 +9,18 @@ export class DataSource extends DataSourceWithBackend<MyQuery, MyDataSourceOptio | |
} | ||
|
||
getDefaultQuery(_: CoreApp): Partial<MyQuery> { | ||
return DEFAULT_QUERY | ||
return DEFAULT_QUERY; | ||
} | ||
|
||
applyTemplateVariables(query: MyQuery, scopedVars: ScopedVars): Record<string, any> { | ||
return { | ||
...query, | ||
queryText: getTemplateSrv().replace(query.queryText, scopedVars), | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Regarding scoped variables...currently the ds template for |
||
} | ||
|
||
filterQuery(query: MyQuery): boolean { | ||
// if no query has been provided, prevent the query from being executed | ||
return !!query.queryText; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,16 +3,20 @@ import { InlineField, Input, SecretInput } from '@grafana/ui'; | |
import { DataSourcePluginOptionsEditorProps } from '@grafana/data'; | ||
import { MyDataSourceOptions, MySecureJsonData } from '../types'; | ||
|
||
interface Props extends DataSourcePluginOptionsEditorProps<MyDataSourceOptions> {} | ||
interface Props extends DataSourcePluginOptionsEditorProps<MyDataSourceOptions, MySecureJsonData> {} | ||
|
||
export function ConfigEditor(props: Props) { | ||
const { onOptionsChange, options } = props; | ||
const { jsonData, secureJsonFields, secureJsonData } = options; | ||
|
||
const onPathChange = (event: ChangeEvent<HTMLInputElement>) => { | ||
const jsonData = { | ||
...options.jsonData, | ||
path: event.target.value, | ||
}; | ||
onOptionsChange({ ...options, jsonData }); | ||
onOptionsChange({ | ||
...options, | ||
jsonData: { | ||
...jsonData, | ||
path: event.target.value, | ||
}, | ||
}); | ||
}; | ||
|
||
// Secure field (only sent to the backend) | ||
|
@@ -39,29 +43,29 @@ export function ConfigEditor(props: Props) { | |
}); | ||
}; | ||
|
||
const { jsonData, secureJsonFields } = options; | ||
const secureJsonData = (options.secureJsonData || {}) as MySecureJsonData; | ||
|
||
return ( | ||
<div className="gf-form-group"> | ||
<InlineField label="Path" labelWidth={12}> | ||
<> | ||
<InlineField label="Path" labelWidth={14} interactive tooltip={'Json field returned to frontend'}> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Re label width: Opened a PR in Grafana to increase the width of the name field to 14. That way we at least give the plugin authors a chance to align field width across the entire page. |
||
<Input | ||
id="config-editor-path" | ||
onChange={onPathChange} | ||
value={jsonData.path || ''} | ||
placeholder="json field returned to frontend" | ||
value={jsonData.path} | ||
placeholder="Enter the path, e.g. /api/v1" | ||
width={40} | ||
/> | ||
</InlineField> | ||
<InlineField label="API Key" labelWidth={12}> | ||
<InlineField label="API Key" labelWidth={14} interactive tooltip={'Secure json field (backend only)'}> | ||
<SecretInput | ||
isConfigured={(secureJsonFields && secureJsonFields.apiKey) as boolean} | ||
value={secureJsonData.apiKey || ''} | ||
placeholder="secure json field (backend only)" | ||
required | ||
id="config-editor-api-key" | ||
isConfigured={secureJsonFields.apiKey} | ||
value={secureJsonData?.apiKey} | ||
placeholder="Enter your API key" | ||
width={40} | ||
onReset={onResetAPIKey} | ||
onChange={onAPIKeyChange} | ||
/> | ||
</InlineField> | ||
</div> | ||
</> | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could potentially use the new
Box
component here, but it does not change appearance so don't think we need it.