Verify datasouce before updating

This commit is contained in:
Adria Navarro 2023-05-23 15:53:01 +02:00
parent 886b726627
commit d6b00efa90
2 changed files with 29 additions and 8 deletions

View File

@ -22,16 +22,11 @@
IntegrationNames[datasource.type] || datasource.name || datasource.type
async function validateConfig() {
function displayError(message) {
const displayError = message =>
notifications.error(message ?? "Error validating datasource")
}
let connected = false
try {
if (!datasource.name) {
datasource.name = name
}
const resp = await validateDatasourceConfig(datasource)
if (!resp.connected) {
displayError(resp.error)
@ -42,7 +37,12 @@
}
return connected
}
async function saveDatasource() {
const valid = await validateConfig()
if (!valid) {
return false
}
try {
if (!datasource.name) {
datasource.name = name
@ -64,8 +64,7 @@
<ModalContent
title={`Connect to ${name}`}
onConfirm={() =>
validateConfig().then(connected => connected && saveDatasource())}
onConfirm={() => saveDatasource()}
onCancel={() => modal.show()}
confirmText={datasource.plus
? "Save and fetch tables"

View File

@ -20,6 +20,7 @@
import { isEqual } from "lodash"
import { cloneDeep } from "lodash/fp"
import ImportRestQueriesModal from "components/backend/DatasourceNavigator/modals/ImportRestQueriesModal.svelte"
import { API } from "api"
const querySchema = {
name: {},
@ -45,7 +46,28 @@
}
}
async function validateConfig() {
const displayError = message =>
notifications.error(message ?? "Error validating datasource")
let connected = false
try {
const resp = await API.validateDatasource(datasource)
if (!resp.connected) {
displayError(resp.error)
}
connected = resp.connected
} catch (err) {
displayError(err?.message)
}
return connected
}
const saveDatasource = async () => {
const valid = await validateConfig()
if (!valid) {
return false
}
try {
// Create datasource
await datasources.save(datasource)