Skip to content

Commit

Permalink
add test connection prep + rm permissions check from hook
Browse files Browse the repository at this point in the history
  • Loading branch information
riahk committed Sep 10, 2020
1 parent e7cf588 commit 358cfdd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 34 deletions.
36 changes: 34 additions & 2 deletions superset-frontend/src/views/CRUD/data/database/DatabaseModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
import React, { FunctionComponent, useState, useEffect } from 'react';
import { styled, t } from '@superset-ui/core';
import { SupersetClient } from '@superset-ui/connection';
import { InfoTooltipWithTrigger } from '@superset-ui/chart-controls';
import { useSingleViewResource } from 'src/views/CRUD/hooks';
import withToasts from 'src/messageToasts/enhancers/withToasts';
Expand Down Expand Up @@ -125,6 +126,7 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
const [isHidden, setIsHidden] = useState<boolean>(true);

const isEditMode = database !== null;
const testEndpointEnabled = false; // TODO: temporary flag until new test connection endpoint is up

// Database fetch logic
const {
Expand All @@ -138,6 +140,35 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
addDangerToast,
);

// Test Connection logic
const testConnection = () => {
if (!db || !testEndpointEnabled) {
return;
}

if (!db.sqlalchemy_uri || !db.sqlalchemy_uri.length) {
addDangerToast(t('Please enter a SQLAlchemy URI to test'));
return;
}

const connection = {
sqlalchemy_uri: db ? db.sqlalchemy_uri : '',
database_name:
db && db.database_name.length ? db.database_name : undefined,
impersonate_user: db ? db.impersonate_user : undefined,
extra: db ? db.extra : undefined,
encrypted_extra: db ? db.encrypted_extra : undefined,
server_cert: db ? db.server_cert : undefined,
};

// TODO: add response logic once endpoint is up
SupersetClient.post({
endpoint: '/api/v1/database/test_connection/',
body: JSON.stringify(connection),
headers: { 'Content-Type': 'application/json' },
}).then();
};

// Functions
const hide = () => {
setIsHidden(true);
Expand Down Expand Up @@ -206,7 +237,8 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
if (target.type === 'checkbox') {
data[target.name] = target.checked;
} else {
data[target.name] = target.value;
data[target.name] =
typeof target.value === 'string' ? target.value.trim() : target.value;
}

setDB(data);
Expand Down Expand Up @@ -320,7 +352,7 @@ const DatabaseModal: FunctionComponent<DatabaseModalProps> = ({
placeholder={t('SQLAlchemy URI')}
onChange={onInputChange}
/>
<Button buttonStyle="primary" cta>
<Button buttonStyle="primary" onClick={testConnection} cta>
{t('Test Connection')}
</Button>
</div>
Expand Down
32 changes: 0 additions & 32 deletions superset-frontend/src/views/CRUD/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ export function useListViewResource<D extends object = any>(
interface SingleViewResourceState<D extends object = any> {
loading: boolean;
resource: D | null;
permissions: string[];
}

export function useSingleViewResource<D extends object = any>(
Expand All @@ -182,42 +181,12 @@ export function useSingleViewResource<D extends object = any>(
const [state, setState] = useState<SingleViewResourceState<D>>({
loading: false,
resource: null,
permissions: [],
});

function updateState(update: Partial<SingleViewResourceState<D>>) {
setState(currentState => ({ ...currentState, ...update }));
}

useEffect(() => {
SupersetClient.get({
endpoint: `/api/v1/${resourceName}/_info`,
}).then(
({ json: infoJson = {} }) => {
updateState({
permissions: infoJson.permissions,
});
},
createErrorHandler(errMsg =>
handleErrorMsg(
t(
'An error occurred while fetching %ss info: %s',
resourceLabel,
errMsg,
),
),
),
);
}, []);

function hasPerm(perm: string) {
if (!state.permissions.length) {
return false;
}

return Boolean(state.permissions.find(p => p === perm));
}

const fetchResource = useCallback((resourceID: number) => {
// Set loading state
updateState({
Expand Down Expand Up @@ -321,7 +290,6 @@ export function useSingleViewResource<D extends object = any>(
updateState({
resource: update,
}),
hasPerm,
fetchResource,
createResource,
updateResource,
Expand Down

0 comments on commit 358cfdd

Please sign in to comment.