Skip to content
This repository was archived by the owner on Feb 8, 2024. It is now read-only.

Create database route, endpoint and service #288

Merged
merged 14 commits into from
Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/design/src/DataTable/Table.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ const TextCell = props => {
const renderLabelCell = labels => {
const $labels = labels.map(label => (
<Label mb="1" mr="1" key={label} kind="secondary">
{`${label}`}
{label}
</Label>
));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function DatabaseList(props: Props) {
function sortAndFilter(search) {
const filtered = databases.filter(obj =>
isMatch(obj, search, {
searchableProps: ['name', 'desc', 'protocol', 'type', 'uri', 'tags'],
searchableProps: ['name', 'desc', 'type', 'tags'],
cb: searchAndFilterCb,
})
);
Expand Down Expand Up @@ -101,17 +101,6 @@ function DatabaseList(props: Props) {
}
cell={<TextCell />}
/>
<Column
columnKey="protocol"
header={
<SortHeaderCell
sortDir={sortDir.protocol}
onSortChange={onSortChange}
title="Protocol"
/>
}
cell={<TextCell />}
/>
<Column
columnKey="type"
header={
Expand All @@ -123,17 +112,6 @@ function DatabaseList(props: Props) {
}
cell={<TextCell />}
/>
<Column
columnKey="uri"
header={
<SortHeaderCell
sortDir={sortDir.uri}
onSortChange={onSortChange}
title="URI"
/>
}
cell={<TextCell />}
/>
<Column header={<Cell>Labels</Cell>} cell={<LabelCell />} />
</StyledTable>
</>
Expand Down
2 changes: 1 addition & 1 deletion packages/teleport/src/Databases/Databases.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
FeatureHeader,
FeatureHeaderTitle,
} from 'teleport/components/Layout';
import DatabaseList from 'teleport/components/DatabaseList';
import DatabaseList from './DatabaseList';
import { Database } from 'teleport/services/database';
import { Attempt } from 'shared/hooks/useAttemptNext';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ exports[`failed 1`] = `
kind="danger"
>
Server Error

</div>
</div>
`;
Expand Down Expand Up @@ -428,15 +427,6 @@ exports[`loaded 1`] = `
/>
</a>
</th>
<th>
<a>
Protocol
<span
class="c8 c12 icon icon-chevrons-expand-vertical "
color="light"
/>
</a>
</th>
<th>
<a>
Type
Expand All @@ -446,15 +436,6 @@ exports[`loaded 1`] = `
/>
</a>
</th>
<th>
<a>
URI
<span
class="c8 c12 icon icon-chevrons-expand-vertical "
color="light"
/>
</a>
</th>
<th>
Labels
</th>
Expand All @@ -469,13 +450,7 @@ exports[`loaded 1`] = `
PostgreSQL 11.6: AWS Aurora
</td>
<td>
postgres
</td>
<td>
rds
</td>
<td>
postgres-aurora-instance-1.c1xpjrob56xs.us-west-1.rds.amazonaws.com:5432
RDS PostgreSQL
</td>
<td>
<div
Expand All @@ -500,13 +475,7 @@ exports[`loaded 1`] = `
MySQL 5.6: AWS Aurora Longname For SQL
</td>
<td>
mysql
</td>
<td>
self-hosted
</td>
<td>
mysql-aurora-56.cluster-c1xpjrob56xsadckjUkjk.us-west-1.rds.amazonaws.com:3306
Self-hosted MySQL
</td>
<td>
<div
Expand All @@ -531,13 +500,7 @@ exports[`loaded 1`] = `
MPostgreSQL 9.6: Google Cloud SQL
</td>
<td>
postgres
</td>
<td>
gcp
</td>
<td>
35.193.70.43:5432
Cloud SQL PostgreSQL
</td>
<td>
<div
Expand Down
14 changes: 3 additions & 11 deletions packages/teleport/src/Databases/fixtures/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,19 @@ export const databases: Database[] = [
{
name: 'aurora',
desc: 'PostgreSQL 11.6: AWS Aurora ',
protocol: 'postgres',
type: 'rds',
uri:
'postgres-aurora-instance-1.c1xpjrob56xs.us-west-1.rds.amazonaws.com:5432',
type: 'RDS PostgreSQL',
tags: ['cluster: root', 'env: aws'],
},
{
name: 'postgres-gcp',
desc: 'MPostgreSQL 9.6: Google Cloud SQL',
protocol: 'postgres',
type: 'gcp',
uri: '35.193.70.43:5432 ',
type: 'Cloud SQL PostgreSQL',
tags: ['cluster: env', 'value: gcp'],
},
{
name: 'mysql-aurora-56',
desc: 'MySQL 5.6: AWS Aurora Longname For SQL',
protocol: 'mysql',
type: 'self-hosted',
uri:
'mysql-aurora-56.cluster-c1xpjrob56xsadckjUkjk.us-west-1.rds.amazonaws.com:3306',
type: 'Self-hosted MySQL',
tags: ['cluster: root', 'env: aws'],
},
];
10 changes: 10 additions & 0 deletions packages/teleport/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const cfg = {
nodes: '/web/cluster/:clusterId/nodes',
sessions: '/web/cluster/:clusterId/sessions',
recordings: '/web/cluster/:clusterId/recordings',
databases: '/web/cluster/:clusterId/databases',
users: '/web/users',
console: '/web/cluster/:clusterId/console',
consoleNodes: '/web/cluster/:clusterId/console/nodes',
Expand Down Expand Up @@ -93,6 +94,7 @@ const cfg = {
u2fChangePassChallengePath: '/v1/webapi/u2f/password/changerequest',
u2fSessionPath: '/v1/webapi/u2f/sessions',
nodesPath: '/v1/webapi/sites/:clusterId/nodes',
databasesPath: `/v1/webapi/sites/:clusterId/databases`,
siteSessionPath: '/v1/webapi/sites/:siteId/sessions',
sessionEventsPath: '/v1/webapi/sites/:siteId/sessions/:sid/events',
siteEventSessionFilterPath: `/v1/webapi/sites/:siteId/sessions`,
Expand Down Expand Up @@ -148,6 +150,10 @@ const cfg = {
return generatePath(cfg.routes.nodes, { clusterId });
},

getDatabasesRoute(clusterId: string) {
return generatePath(cfg.routes.databases, { clusterId });
},

getNodeJoinTokenUrl() {
return cfg.api.nodeTokenPath;
},
Expand Down Expand Up @@ -252,6 +258,10 @@ const cfg = {
return generatePath(cfg.api.nodesPath, { clusterId });
},

getDatabasesUrl(clusterId: string) {
return generatePath(cfg.api.databasesPath, { clusterId });
},

getApplicationsUrl(clusterId: string) {
return generatePath(cfg.api.applicationsPath, { clusterId });
},
Expand Down
74 changes: 74 additions & 0 deletions packages/teleport/src/services/database/databases.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
Copyright 2021 Gravitational, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import DatabaseService from './databases';
import api from 'teleport/services/api';

test('correct formatting of database fetch response', async () => {
jest.spyOn(api, 'get').mockResolvedValue(mockResponse);

const database = new DatabaseService();
const response = await database.fetchDatabases('im-a-cluster');

expect(response).toEqual([
{
name: 'aurora',
desc: 'PostgreSQL 11.6: AWS Aurora',
type: 'RDS PostgreSQL',
tags: ['cluster: root', 'env: aws'],
},
]);
});

describe('correct formatting of all type and protocol combos', () => {
test.each`
type | protocol | combined
${'self-hosted'} | ${'mysql'} | ${'Self-hosted MySQL'}
${'rds'} | ${'mysql'} | ${'RDS MySQL'}
${'gcp'} | ${'mysql'} | ${'Cloud SQL MySQL'}
${'redshift'} | ${'mysql'} | ${'Redshift MySQL'}
${'self-hosted'} | ${'postgres'} | ${'Self-hosted PostgreSQL'}
${'rds'} | ${'postgres'} | ${'RDS PostgreSQL'}
${'gcp'} | ${'postgres'} | ${'Cloud SQL PostgreSQL'}
${'redshift'} | ${'postgres'} | ${'Redshift PostgreSQL'}
${'some other type'} | ${'some other protocol'} | ${'some other type some other protocol'}
`(
'should combine type: $type and protocol: $protocol correctly',
async ({ type, protocol, combined }) => {
jest.spyOn(api, 'get').mockResolvedValue([{ type, protocol }]);

const database = new DatabaseService();
const response = await database.fetchDatabases('im-a-cluster');

expect(response[0].type).toBe(combined);
}
);
});

const mockResponse = [
{
name: 'aurora',
desc: 'PostgreSQL 11.6: AWS Aurora',
protocol: 'postgres',
type: 'rds',
uri:
'postgres-aurora-instance-1.c1xpjrob56xs.us-west-1.rds.amazonaws.com:5432',
labels: [
{ name: 'cluster', value: 'root' },
{ name: 'env', value: 'aws' },
],
},
];
30 changes: 30 additions & 0 deletions packages/teleport/src/services/database/databases.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
Copyright 2021 Gravitational, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { map } from 'lodash';
import api from 'teleport/services/api';
import cfg from 'teleport/config';
import makeDatabase from './makeDatabase';

class DatabaseService {
fetchDatabases(clusterId?: string) {
return api
.get(cfg.getDatabasesRoute(clusterId))
.then(json => map(json, makeDatabase));
}
}

export default DatabaseService;
3 changes: 3 additions & 0 deletions packages/teleport/src/services/database/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

import service from './databases';

export * from './types';
export default service;
54 changes: 54 additions & 0 deletions packages/teleport/src/services/database/makeDatabase.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Copyright 2021 Gravitational, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import { Database } from './types';

export default function makeDatabase(json): Database {
const { name, desc, protocol, type, labels = [] } = json;

return {
name,
desc,
type: `${formatType(type)} ${formatProtocol(protocol)}`,
tags: labels.map(label => `${label.name}: ${label.value}`),
};
}

const formatType = (input: string) => {
switch (input) {
case 'self-hosted':
return 'Self-hosted';
case 'rds':
return 'RDS';
case 'gcp':
return 'Cloud SQL';
case 'redshift':
return 'Redshift';
default:
return input;
}
};

const formatProtocol = (input: string) => {
switch (input) {
case 'postgres':
return 'PostgreSQL';
case 'mysql':
return 'MySQL';
default:
return input;
}
};
2 changes: 0 additions & 2 deletions packages/teleport/src/services/database/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ limitations under the License.
export interface Database {
name: string;
desc: string;
protocol: string;
type: string;
uri: string;
tags: string[];
}
Loading