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