DataSourceSettingsPage.tsx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. // Libraries
  2. import React, { PureComponent } from 'react';
  3. import { hot } from 'react-hot-loader';
  4. import { connect } from 'react-redux';
  5. import isString from 'lodash/isString';
  6. // Components
  7. import Page from 'app/core/components/Page/Page';
  8. import { PluginSettings, GenericDataSourcePlugin } from './PluginSettings';
  9. import BasicSettings from './BasicSettings';
  10. import ButtonRow from './ButtonRow';
  11. // Services & Utils
  12. import appEvents from 'app/core/app_events';
  13. import { getBackendSrv } from 'app/core/services/backend_srv';
  14. import { getDatasourceSrv } from 'app/features/plugins/datasource_srv';
  15. // Actions & selectors
  16. import { getDataSource, getDataSourceMeta } from '../state/selectors';
  17. import {
  18. deleteDataSource,
  19. loadDataSource,
  20. setDataSourceName,
  21. setIsDefault,
  22. updateDataSource,
  23. dataSourceLoaded,
  24. } from '../state/actions';
  25. import { getNavModel } from 'app/core/selectors/navModel';
  26. import { getRouteParamsId } from 'app/core/selectors/location';
  27. // Types
  28. import { StoreState } from 'app/types/';
  29. import { UrlQueryMap } from '@grafana/runtime';
  30. import { DataSourceSettings, DataSourcePluginMeta } from '@grafana/ui';
  31. import { NavModel } from '@grafana/data';
  32. import { getDataSourceLoadingNav } from '../state/navModel';
  33. import PluginStateinfo from 'app/features/plugins/PluginStateInfo';
  34. import { importDataSourcePlugin } from 'app/features/plugins/plugin_loader';
  35. export interface Props {
  36. navModel: NavModel;
  37. dataSource: DataSourceSettings;
  38. dataSourceMeta: DataSourcePluginMeta;
  39. pageId: number;
  40. deleteDataSource: typeof deleteDataSource;
  41. loadDataSource: typeof loadDataSource;
  42. setDataSourceName: typeof setDataSourceName;
  43. updateDataSource: typeof updateDataSource;
  44. setIsDefault: typeof setIsDefault;
  45. dataSourceLoaded: typeof dataSourceLoaded;
  46. plugin?: GenericDataSourcePlugin;
  47. query: UrlQueryMap;
  48. page?: string;
  49. }
  50. interface State {
  51. plugin?: GenericDataSourcePlugin;
  52. isTesting?: boolean;
  53. testingMessage?: string;
  54. testingStatus?: string;
  55. loadError?: any;
  56. }
  57. export class DataSourceSettingsPage extends PureComponent<Props, State> {
  58. constructor(props: Props) {
  59. super(props);
  60. this.state = {
  61. plugin: props.plugin,
  62. };
  63. }
  64. async loadPlugin(pluginId?: string) {
  65. const { dataSourceMeta } = this.props;
  66. let importedPlugin: GenericDataSourcePlugin;
  67. try {
  68. importedPlugin = await importDataSourcePlugin(dataSourceMeta);
  69. } catch (e) {
  70. console.log('Failed to import plugin module', e);
  71. }
  72. this.setState({ plugin: importedPlugin });
  73. }
  74. async componentDidMount() {
  75. const { loadDataSource, pageId } = this.props;
  76. if (isNaN(pageId)) {
  77. this.setState({ loadError: 'Invalid ID' });
  78. return;
  79. }
  80. try {
  81. await loadDataSource(pageId);
  82. if (!this.state.plugin) {
  83. await this.loadPlugin();
  84. }
  85. } catch (err) {
  86. this.setState({ loadError: err });
  87. }
  88. }
  89. onSubmit = async (evt: React.FormEvent<HTMLFormElement>) => {
  90. evt.preventDefault();
  91. await this.props.updateDataSource({ ...this.props.dataSource });
  92. this.testDataSource();
  93. };
  94. onTest = async (evt: React.FormEvent<HTMLFormElement>) => {
  95. evt.preventDefault();
  96. this.testDataSource();
  97. };
  98. onDelete = () => {
  99. appEvents.emit('confirm-modal', {
  100. title: 'Delete',
  101. text: 'Are you sure you want to delete this data source?',
  102. yesText: 'Delete',
  103. icon: 'fa-trash',
  104. onConfirm: () => {
  105. this.confirmDelete();
  106. },
  107. });
  108. };
  109. confirmDelete = () => {
  110. this.props.deleteDataSource();
  111. };
  112. onModelChange = (dataSource: DataSourceSettings) => {
  113. this.props.dataSourceLoaded(dataSource);
  114. };
  115. isReadOnly() {
  116. return this.props.dataSource.readOnly === true;
  117. }
  118. renderIsReadOnlyMessage() {
  119. return (
  120. <div className="grafana-info-box span8">
  121. This datasource was added by config and cannot be modified using the UI. Please contact your server admin to
  122. update this datasource.
  123. </div>
  124. );
  125. }
  126. async testDataSource() {
  127. const dsApi = await getDatasourceSrv().get(this.props.dataSource.name);
  128. if (!dsApi.testDatasource) {
  129. return;
  130. }
  131. this.setState({ isTesting: true, testingMessage: 'Testing...', testingStatus: 'info' });
  132. getBackendSrv().withNoBackendCache(async () => {
  133. try {
  134. const result = await dsApi.testDatasource();
  135. this.setState({
  136. isTesting: false,
  137. testingStatus: result.status,
  138. testingMessage: result.message,
  139. });
  140. } catch (err) {
  141. let message = '';
  142. if (err.statusText) {
  143. message = 'HTTP Error ' + err.statusText;
  144. } else {
  145. message = err.message;
  146. }
  147. this.setState({
  148. isTesting: false,
  149. testingStatus: 'error',
  150. testingMessage: message,
  151. });
  152. }
  153. });
  154. }
  155. get hasDataSource() {
  156. return this.props.dataSource.id > 0;
  157. }
  158. renderLoadError(loadError: any) {
  159. let showDelete = false;
  160. let msg = loadError.toString();
  161. if (loadError.data) {
  162. if (loadError.data.message) {
  163. msg = loadError.data.message;
  164. }
  165. } else if (isString(loadError)) {
  166. showDelete = true;
  167. }
  168. const node = {
  169. text: msg,
  170. subTitle: 'Data Source Error',
  171. icon: 'fa fa-fw fa-warning',
  172. };
  173. const nav = {
  174. node: node,
  175. main: node,
  176. };
  177. return (
  178. <Page navModel={nav}>
  179. <Page.Contents>
  180. <div>
  181. <div className="gf-form-button-row">
  182. {showDelete && (
  183. <button type="submit" className="btn btn-danger" onClick={this.onDelete}>
  184. Delete
  185. </button>
  186. )}
  187. <a className="btn btn-inverse" href="datasources">
  188. Back
  189. </a>
  190. </div>
  191. </div>
  192. </Page.Contents>
  193. </Page>
  194. );
  195. }
  196. renderConfigPageBody(page: string) {
  197. const { plugin } = this.state;
  198. if (!plugin || !plugin.configPages) {
  199. return null; // still loading
  200. }
  201. for (const p of plugin.configPages) {
  202. if (p.id === page) {
  203. return <p.body plugin={plugin} query={this.props.query} />;
  204. }
  205. }
  206. return <div>Page Not Found: {page}</div>;
  207. }
  208. renderSettings() {
  209. const { dataSourceMeta, setDataSourceName, setIsDefault, dataSource } = this.props;
  210. const { testingMessage, testingStatus, plugin } = this.state;
  211. return (
  212. <form onSubmit={this.onSubmit}>
  213. {this.isReadOnly() && this.renderIsReadOnlyMessage()}
  214. {dataSourceMeta.state && (
  215. <div className="gf-form">
  216. <label className="gf-form-label width-10">Plugin state</label>
  217. <label className="gf-form-label gf-form-label--transparent">
  218. <PluginStateinfo state={dataSourceMeta.state} />
  219. </label>
  220. </div>
  221. )}
  222. <BasicSettings
  223. dataSourceName={dataSource.name}
  224. isDefault={dataSource.isDefault}
  225. onDefaultChange={state => setIsDefault(state)}
  226. onNameChange={name => setDataSourceName(name)}
  227. />
  228. {plugin && (
  229. <PluginSettings
  230. plugin={plugin}
  231. dataSource={dataSource}
  232. dataSourceMeta={dataSourceMeta}
  233. onModelChange={this.onModelChange}
  234. />
  235. )}
  236. <div className="gf-form-group">
  237. {testingMessage && (
  238. <div className={`alert-${testingStatus} alert`} aria-label="Datasource settings page Alert">
  239. <div className="alert-icon">
  240. {testingStatus === 'error' ? (
  241. <i className="fa fa-exclamation-triangle" />
  242. ) : (
  243. <i className="fa fa-check" />
  244. )}
  245. </div>
  246. <div className="alert-body">
  247. <div className="alert-title" aria-label="Datasource settings page Alert message">
  248. {testingMessage}
  249. </div>
  250. </div>
  251. </div>
  252. )}
  253. </div>
  254. <ButtonRow
  255. onSubmit={event => this.onSubmit(event)}
  256. isReadOnly={this.isReadOnly()}
  257. onDelete={this.onDelete}
  258. onTest={event => this.onTest(event)}
  259. />
  260. </form>
  261. );
  262. }
  263. render() {
  264. const { navModel, page } = this.props;
  265. const { loadError } = this.state;
  266. if (loadError) {
  267. return this.renderLoadError(loadError);
  268. }
  269. return (
  270. <Page navModel={navModel}>
  271. <Page.Contents isLoading={!this.hasDataSource}>
  272. {this.hasDataSource && <div>{page ? this.renderConfigPageBody(page) : this.renderSettings()}</div>}
  273. </Page.Contents>
  274. </Page>
  275. );
  276. }
  277. }
  278. function mapStateToProps(state: StoreState) {
  279. const pageId = getRouteParamsId(state.location);
  280. const dataSource = getDataSource(state.dataSources, pageId);
  281. const page = state.location.query.page as string;
  282. return {
  283. navModel: getNavModel(
  284. state.navIndex,
  285. page ? `datasource-page-${page}` : `datasource-settings-${pageId}`,
  286. getDataSourceLoadingNav('settings')
  287. ),
  288. dataSource: getDataSource(state.dataSources, pageId),
  289. dataSourceMeta: getDataSourceMeta(state.dataSources, dataSource.type),
  290. pageId: pageId,
  291. query: state.location.query,
  292. page,
  293. };
  294. }
  295. const mapDispatchToProps = {
  296. deleteDataSource,
  297. loadDataSource,
  298. setDataSourceName,
  299. updateDataSource,
  300. setIsDefault,
  301. dataSourceLoaded,
  302. };
  303. export default hot(module)(
  304. connect(
  305. mapStateToProps,
  306. mapDispatchToProps
  307. )(DataSourceSettingsPage)
  308. );