Skip to content

Commit

Permalink
Add ability to change db name for db proxy (#883) (#927)
Browse files Browse the repository at this point in the history
  • Loading branch information
gzdunek authored Jun 27, 2022
1 parent bda7925 commit 42e29e0
Show file tree
Hide file tree
Showing 16 changed files with 585 additions and 198 deletions.
3 changes: 3 additions & 0 deletions web/packages/shared/components/FieldInput/FieldInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default function FieldInput({
onChange,
onKeyPress,
placeholder,
defaultValue,
rule = defaultRule,
type = 'text',
autoFocus = false,
Expand Down Expand Up @@ -99,6 +100,7 @@ export default function FieldInput({
onKeyPress={onKeyPress}
readOnly={readonly}
inputMode={inputMode}
defaultValue={defaultValue}
/>
);

Expand Down Expand Up @@ -143,6 +145,7 @@ type Props = {
onChange: (e: React.ChangeEvent<HTMLInputElement>) => void;
onKeyPress?: (e: React.KeyboardEvent<HTMLInputElement>) => void;
readonly?: boolean;
defaultValue?: string;
// TS: temporary handles ...styles
[key: string]: any;
};
80 changes: 42 additions & 38 deletions web/packages/shared/hooks/useAsync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ limitations under the License.

/* eslint-disable @typescript-eslint/ban-types */

import React from 'react';
import { useCallback, useState } from 'react';

/**
* `useAsync` lets you represent the state of an async operation as data. It accepts an async function
Expand Down Expand Up @@ -60,45 +60,49 @@ import React from 'react';
* }
*/
export function useAsync<R, T extends Function>(cb?: AsyncCb<R, T>) {
const [state, setState] = React.useState<Attempt<R>>(() =>
makeEmptyAttempt()
const [state, setState] = useState<Attempt<R>>(makeEmptyAttempt);

const run = useCallback(
(...p: Parameters<AsyncCb<R, T>>) =>
Promise.resolve()
.then(() => {
setState(prevState => ({
...prevState,
status: 'processing',
}));

return cb.call(null, ...p) as R;
})
.then(
data => {
setState(prevState => ({
...prevState,
status: 'success',
data,
}));

return [data, null] as [R, Error];
},
err => {
setState(prevState => ({
...prevState,
status: 'error',
statusText: err?.message,
data: null,
}));

return [null, err] as [R, Error];
}
),
[setState, cb]
);

const run = (...p: Parameters<AsyncCb<R, T>>) =>
Promise.resolve()
.then(() => {
setState({
...state,
status: 'processing',
});

return cb.call(null, ...p) as R;
})
.then(
data => {
setState({
...state,
status: 'success',
data,
});

return [data, null] as [R, Error];
},
err => {
setState({
...state,
status: 'error',
statusText: err?.message,
data: null,
});

return [null, err] as [R, Error];
}
);

function setAttempt(attempt: Attempt<R>) {
setState(attempt);
}
const setAttempt = useCallback(
(attempt: Attempt<R>) => {
setState(attempt);
},
[setState]
);

return [state, run, setAttempt] as const;
}
Expand Down
18 changes: 18 additions & 0 deletions web/packages/teleterm/src/services/tshd/createClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,24 @@ export default function createClient(addr: string) {
});
});
},

async setGatewayTargetSubresourceName(
gatewayUri = '',
targetSubresourceName = ''
) {
const req = new api.SetGatewayTargetSubresourceNameRequest()
.setGatewayUri(gatewayUri)
.setTargetSubresourceName(targetSubresourceName);
return new Promise<types.Gateway>((resolve, reject) => {
tshd.setGatewayTargetSubresourceName(req, (err, response) => {
if (err) {
reject(err);
} else {
resolve(response.toObject());
}
});
});
},
};

return client;
Expand Down
62 changes: 40 additions & 22 deletions web/packages/teleterm/src/services/tshd/fixtures/mocks.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,44 @@
import * as types from 'teleterm/services/tshd/types';
import {
Application,
AuthSettings,
Cluster,
CreateGatewayParams,
Database,
Gateway,
Kube,
LoginParams,
Server,
TshAbortController,
TshAbortSignal,
TshClient,
} from '../types';

export class MockTshClient implements types.TshClient {
listGateways: () => Promise<types.Gateway[]>;
listRootClusters: () => Promise<types.Cluster[]>;
listLeafClusters: (clusterUri: string) => Promise<types.Cluster[]>;
listDatabases: (clusterUri: string) => Promise<types.Database[]>;
export class MockTshClient implements TshClient {
listRootClusters: () => Promise<Cluster[]>;
listLeafClusters: (clusterUri: string) => Promise<Cluster[]>;
listApps: (clusterUri: string) => Promise<Application[]>;
listKubes: (clusterUri: string) => Promise<Kube[]>;
listDatabases: (clusterUri: string) => Promise<Database[]>;
listDatabaseUsers: (dbUri: string) => Promise<string[]>;
listKubes: (clusterUri: string) => Promise<types.Kube[]>;
listApps: (clusterUri: string) => Promise<types.Application[]>;
listServers: (clusterUri: string) => Promise<types.Server[]>;
addRootCluster: (clusterUri: string) => Promise<types.Cluster>;
createGateway: (params: types.CreateGatewayParams) => Promise<types.Gateway>;
createAbortController: () => types.TshAbortController;
getCluster: (clusterUri: string) => Promise<types.Cluster>;
getAuthSettings: (clusterUri: string) => Promise<types.AuthSettings>;
ssoLogin: (clusterUri: string, pType: string, pName: string) => Promise<void>;
removeGateway: (gatewayUri: string) => Promise<void>;
restartGateway: (gatewayUri: string) => Promise<void>;
listServers: (clusterUri: string) => Promise<Server[]>;
createAbortController: () => TshAbortController;
addRootCluster: (addr: string) => Promise<Cluster>;

listGateways: () => Promise<Gateway[]>;
createGateway: (params: CreateGatewayParams) => Promise<Gateway>;
removeGateway: (gatewayUri: string) => Promise<undefined>;
restartGateway: (gatewayUri: string) => Promise<undefined>;
setGatewayTargetSubresourceName: (
gatewayUri: string,
targetSubresourceName: string
) => Promise<Gateway>;

getCluster: (clusterUri: string) => Promise<Cluster>;
getAuthSettings: (clusterUri: string) => Promise<AuthSettings>;
removeCluster: (clusterUri: string) => Promise<undefined>;
login: (
params: types.LoginParams,
abortSignal?: types.TshAbortSignal
) => Promise<void>;
logout: (clusterUri: string) => Promise<void>;
removeCluster: (clusterUri: string) => Promise<void>;
params: LoginParams,
abortSignal?: TshAbortSignal
) => Promise<undefined>;
logout: (clusterUri: string) => Promise<undefined>;
}
12 changes: 9 additions & 3 deletions web/packages/teleterm/src/services/tshd/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ export type AuthProvider = apiAuthSettings.AuthProvider.AsObject;
export type AuthSettings = apiAuthSettings.AuthSettings.AsObject;

export type TshClient = {
listGateways: () => Promise<Gateway[]>;
listRootClusters: () => Promise<Cluster[]>;
listLeafClusters: (clusterUri: string) => Promise<Cluster[]>;
listApps: (clusterUri: string) => Promise<Application[]>;
Expand All @@ -38,11 +37,18 @@ export type TshClient = {
listServers: (clusterUri: string) => Promise<Server[]>;
createAbortController: () => TshAbortController;
addRootCluster: (addr: string) => Promise<Cluster>;

listGateways: () => Promise<Gateway[]>;
createGateway: (params: CreateGatewayParams) => Promise<Gateway>;
getCluster: (clusterUri: string) => Promise<Cluster>;
getAuthSettings: (clusterUri: string) => Promise<AuthSettings>;
removeGateway: (gatewayUri: string) => Promise<void>;
restartGateway: (gatewayUri: string) => Promise<void>;
setGatewayTargetSubresourceName: (
gatewayUri: string,
targetSubresourceName: string
) => Promise<Gateway>;

getCluster: (clusterUri: string) => Promise<Cluster>;
getAuthSettings: (clusterUri: string) => Promise<AuthSettings>;
removeCluster: (clusterUri: string) => Promise<void>;
login: (params: LoginParams, abortSignal?: TshAbortSignal) => Promise<void>;
logout: (clusterUri: string) => Promise<void>;
Expand Down
Loading

0 comments on commit 42e29e0

Please sign in to comment.