|
@@ -80,9 +80,23 @@ export function loadDataSources(): ThunkResult<void> {
|
|
|
};
|
|
};
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-export function addDataSource(name: string, type: string): ThunkResult<void> {
|
|
|
|
|
- return async dispatch => {
|
|
|
|
|
- const result = await getBackendSrv().post('/api/datasources', { name: name, type: type, access: 'proxy' });
|
|
|
|
|
|
|
+export function addDataSource(plugin: Plugin): ThunkResult<void> {
|
|
|
|
|
+ return async (dispatch, getStore) => {
|
|
|
|
|
+ let dataSources = getStore().dataSources.dataSources;
|
|
|
|
|
+
|
|
|
|
|
+ if (dataSources.length === 0) {
|
|
|
|
|
+ dispatch(loadDataSources());
|
|
|
|
|
+
|
|
|
|
|
+ dataSources = getStore().dataSources.dataSources;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ let name = plugin.name;
|
|
|
|
|
+
|
|
|
|
|
+ if (nameExits(dataSources, name)) {
|
|
|
|
|
+ name = findNewName(dataSources, name);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ const result = await getBackendSrv().post('/api/datasources', { name: name, type: plugin.id, access: 'proxy' });
|
|
|
dispatch(updateLocation({ path: `/datasources/edit/${result.id}` }));
|
|
dispatch(updateLocation({ path: `/datasources/edit/${result.id}` }));
|
|
|
};
|
|
};
|
|
|
}
|
|
}
|
|
@@ -93,3 +107,47 @@ export function loadDataSourceTypes(): ThunkResult<void> {
|
|
|
dispatch(dataSourceTypesLoaded(result));
|
|
dispatch(dataSourceTypesLoaded(result));
|
|
|
};
|
|
};
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+export function nameExits(dataSources, name) {
|
|
|
|
|
+ return (
|
|
|
|
|
+ dataSources.filter(dataSource => {
|
|
|
|
|
+ return dataSource.name === name;
|
|
|
|
|
+ }).length > 0
|
|
|
|
|
+ );
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+export function findNewName(dataSources, name) {
|
|
|
|
|
+ // Need to loop through current data sources to make sure
|
|
|
|
|
+ // the name doesn't exist
|
|
|
|
|
+ while (nameExits(dataSources, name)) {
|
|
|
|
|
+ // If there's a duplicate name that doesn't end with '-x'
|
|
|
|
|
+ // we can add -1 to the name and be done.
|
|
|
|
|
+ if (!nameHasSuffix(name)) {
|
|
|
|
|
+ name = `${name}-1`;
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // if there's a duplicate name that ends with '-x'
|
|
|
|
|
+ // we can try to increment the last digit until the name is unique
|
|
|
|
|
+
|
|
|
|
|
+ // remove the 'x' part and replace it with the new number
|
|
|
|
|
+ name = `${getNewName(name)}${incrementLastDigit(getLastDigit(name))}`;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return name;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function nameHasSuffix(name) {
|
|
|
|
|
+ return name.endsWith('-', name.length - 1);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function getLastDigit(name) {
|
|
|
|
|
+ return parseInt(name.slice(-1), 10);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function incrementLastDigit(digit) {
|
|
|
|
|
+ return isNaN(digit) ? 1 : digit + 1;
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+function getNewName(name) {
|
|
|
|
|
+ return name.slice(0, name.length - 1);
|
|
|
|
|
+}
|