Skip to content

Commit

Permalink
Use connection type display name if available and type path changes
Browse files Browse the repository at this point in the history
  • Loading branch information
emilys314 committed Sep 11, 2024
1 parent 5c2ff28 commit c059291
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 98 deletions.
33 changes: 33 additions & 0 deletions frontend/src/__mocks__/mockConnection.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Connection } from '~/concepts/connectionTypes/types';

type MockConnection = {
name?: string;
namespace?: string;
connectionType?: string;
displayName?: string;
description?: string;
data?: { [key: string]: string };
};

export const mockConnection = ({
name = 's3-connection',
namespace = 'ds-project-1',
connectionType = 's3',
displayName,
description,
data = {},
}: MockConnection): Connection => ({
kind: 'Secret',
apiVersion: 'v1',
metadata: {
name,
namespace,
labels: { 'opendatahub.io/dashboard': 'true', 'opendatahub.io/managed': 'true' },
annotations: {
'opendatahub.io/connection-type': connectionType,
...(displayName && { 'openshift.io/display-name': displayName }),
...(description && { 'openshift.io/description': description }),
},
},
data,
});
22 changes: 21 additions & 1 deletion frontend/src/concepts/connectionTypes/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { K8sResourceCommon } from '@openshift/dynamic-plugin-sdk-utils';
import { DashboardLabels, DisplayNameAnnotations } from '~/k8sTypes';
import { DashboardLabels, DisplayNameAnnotations, SecretKind } from '~/k8sTypes';

export enum ConnectionTypeFieldType {
Boolean = 'boolean',
Expand Down Expand Up @@ -135,3 +135,23 @@ export type ConnectionTypeConfigMapObj = Omit<ConnectionTypeConfigMap, 'data'> &
fields?: ConnectionTypeField[];
};
};

export type Connection = SecretKind & {
metadata: {
labels: DashboardLabels & {
'opendatahub.io/managed': 'true';
};
annotations: DisplayNameAnnotations & {
'opendatahub.io/connection-type': string;
};
};
data: {
[key: string]: string;
};
};

export const isConnection = (secret: SecretKind): secret is Connection =>
!!secret.metadata.annotations &&
'opendatahub.io/connection-type' in secret.metadata.annotations &&
!!secret.metadata.labels &&
secret.metadata.labels['opendatahub.io/managed'] === 'true';
2 changes: 1 addition & 1 deletion frontend/src/pages/projects/ProjectDetailsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ import useTemplateDisablement from '~/pages/modelServing/customServingRuntimes/u
import { useDashboardNamespace } from '~/redux/selectors';
import { getTokenNames } from '~/pages/modelServing/utils';
import { SupportedArea, useIsAreaAvailable } from '~/concepts/areas';
import { Connection } from '~/concepts/connectionTypes/types';
import { useGroups, useTemplates } from '~/api';
import { NotebookState } from './notebook/types';
import { DataConnection } from './types';
import useDataConnections from './screens/detail/data-connections/useDataConnections';
import useProjectNotebookStates from './notebook/useProjectNotebookStates';
import useProjectPvcs from './screens/detail/storage/useProjectPvcs';
import useProjectSharing from './projectSharing/useProjectSharing';
import { Connection } from './screens/detail/connections/types';
import useConnections from './screens/detail/connections/useConnections';

type ProjectDetailsContextType = {
Expand Down
13 changes: 6 additions & 7 deletions frontend/src/pages/projects/screens/detail/ProjectDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,12 @@ const ProjectDetails: React.FC = () => {
component: <ConnectionsList />,
},
]
: [
{
id: ProjectSectionID.DATA_CONNECTIONS,
title: 'Data connections',
component: <DataConnectionsList />,
},
]),
: []),
{
id: ProjectSectionID.DATA_CONNECTIONS,
title: 'Data connections',
component: <DataConnectionsList />,
},
...(projectSharingEnabled && allowCreate
? [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import DetailsSection from '~/pages/projects/screens/detail/DetailsSection';
import EmptyDetailsView from '~/components/EmptyDetailsView';
import DashboardPopupIconButton from '~/concepts/dashboard/DashboardPopupIconButton';
import { ProjectObjectType, typedEmptyImage } from '~/concepts/design/utils';
import { useWatchConnectionTypes } from '~/utilities/useWatchConnectionTypes';
import ConnectionsTable from './ConnectionsTable';

const ConnectionsDescription =
Expand All @@ -17,6 +18,7 @@ const ConnectionsList: React.FC = () => {
const {
connections: { data: connections, loaded, error },
} = React.useContext(ProjectDetailsContext);
const [connectionTypes, connectionTypesLoaded, connectionTypesError] = useWatchConnectionTypes();

return (
<DetailsSection
Expand All @@ -37,9 +39,9 @@ const ConnectionsList: React.FC = () => {
Add connection
</Button>,
]}
isLoading={!loaded}
isLoading={!loaded || !connectionTypesLoaded}
isEmpty={connections.length === 0}
loadError={error}
loadError={error || connectionTypesError}
emptyState={
<EmptyDetailsView
title="No connections"
Expand All @@ -58,7 +60,7 @@ const ConnectionsList: React.FC = () => {
/>
}
>
<ConnectionsTable connections={connections} />
<ConnectionsTable connections={connections} connectionTypes={connectionTypes} />
</DetailsSection>
);
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import * as React from 'react';
import { Connection, ConnectionTypeConfigMapObj } from '~/concepts/connectionTypes/types';
import { Table } from '~/components/table';
import ConnectionsTableRow from './ConnectionsTableRow';
import { columns } from './connectionsTableColumns';
import { Connection } from './types';

type ConnectionsTableProps = {
connections: Connection[];
connectionTypes?: ConnectionTypeConfigMapObj[];
};

const ConnectionsTable: React.FC<ConnectionsTableProps> = ({ connections }) => (
const ConnectionsTable: React.FC<ConnectionsTableProps> = ({ connections, connectionTypes }) => (
<Table
data={connections}
data-testid="connection-table"
Expand All @@ -17,6 +18,7 @@ const ConnectionsTable: React.FC<ConnectionsTableProps> = ({ connections }) => (
<ConnectionsTableRow
key={connection.metadata.name}
obj={connection}
connectionTypes={connectionTypes}
onEditConnection={() => undefined}
onDeleteConnection={() => undefined}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,50 +1,63 @@
import * as React from 'react';
import { ActionsColumn, Td, Tr } from '@patternfly/react-table';
import { Connection, ConnectionTypeConfigMapObj } from '~/concepts/connectionTypes/types';
import { TableRowTitleDescription } from '~/components/table';
import { Connection } from './types';

type ConnectionsTableRowProps = {
obj: Connection;
connectionTypes?: ConnectionTypeConfigMapObj[];
onEditConnection: (pvc: Connection) => void;
onDeleteConnection: (dataConnection: Connection) => void;
};

const ConnectionsTableRow: React.FC<ConnectionsTableRowProps> = ({
obj,
connectionTypes,
onEditConnection,
onDeleteConnection,
}) => (
<Tr>
<Td dataLabel="Name">
<TableRowTitleDescription
title={obj.metadata.annotations['openshift.io/display-name']}
boldTitle={false}
resource={obj}
description={obj.metadata.annotations['openshift.io/description']}
/>
</Td>
<Td dataLabel="Type">{obj.metadata.annotations['opendatahub.io/connection-type']}</Td>
<Td dataLabel="Compatibility">-</Td>
<Td dataLabel="Connected resources">-</Td>
<Td isActionCell>
<ActionsColumn
items={[
{
title: 'Edit',
onClick: () => {
onEditConnection(obj);
}) => {
const connectionTypeDisplayName = React.useMemo(() => {
const matchingType = connectionTypes?.find(
(type) => type.metadata.name === obj.metadata.annotations['opendatahub.io/connection-type'],
);
return (
matchingType?.metadata.annotations?.['openshift.io/display-name'] ||
obj.metadata.annotations['opendatahub.io/connection-type']
);
}, [obj, connectionTypes]);

return (
<Tr>
<Td dataLabel="Name">
<TableRowTitleDescription
title={obj.metadata.annotations['openshift.io/display-name'] || obj.metadata.name}
boldTitle={false}
resource={obj}
description={obj.metadata.annotations['openshift.io/description']}
/>
</Td>
<Td dataLabel="Type">{connectionTypeDisplayName}</Td>
<Td dataLabel="Connected resources">-</Td>
<Td isActionCell>
<ActionsColumn
items={[
{
title: 'Edit',
onClick: () => {
onEditConnection(obj);
},
},
},
{
title: 'Delete',
onClick: () => {
onDeleteConnection(obj);
{
title: 'Delete',
onClick: () => {
onDeleteConnection(obj);
},
},
},
]}
/>
</Td>
</Tr>
);
]}
/>
</Td>
</Tr>
);
};

export default ConnectionsTableRow;
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import ConnectionsTable from '~/pages/projects/screens/detail/connections/ConnectionsTable';
import { Connection } from '~/pages/projects/screens/detail/connections/types';

const connections: Connection[] = [
{
kind: 'Secret',
apiVersion: 'v1',
metadata: {
name: 'connection1',
namespace: 'ds-project',
labels: { 'opendatahub.io/dashboard': 'true', 'opendatahub.io/managed': 'true' },
annotations: {
'opendatahub.io/connection-type': 's3',
'openshift.io/display-name': 'connection1',
'openshift.io/description': 'desc1',
},
},
data: {},
},
];
import { mockConnectionTypeConfigMapObj } from '~/__mocks__/mockConnectionType';
import { mockConnection } from '~/__mocks__/mockConnection';

describe('ConnectionsTable', () => {
it('should render table', () => {
render(<ConnectionsTable connections={connections} />);
render(
<ConnectionsTable
connections={[mockConnection({ displayName: 'connection1', description: 'desc1' })]}
/>,
);

expect(screen.getByTestId('connection-table')).toBeTruthy();
expect(screen.getByText('connection1')).toBeTruthy();
expect(screen.getByText('desc1')).toBeTruthy();
expect(screen.getByText('s3')).toBeTruthy();
});

it('should show display name of connection type if available', () => {
render(
<ConnectionsTable
connections={[mockConnection({ displayName: 'connection1', description: 'desc1' })]}
connectionTypes={[
mockConnectionTypeConfigMapObj({ name: 's3', displayName: 'S3 Buckets' }),
]}
/>,
);

expect(screen.getByTestId('connection-table')).toBeTruthy();
expect(screen.getByText('connection1')).toBeTruthy();
expect(screen.getByText('desc1')).toBeTruthy();
expect(screen.queryByText('s3')).toBeFalsy();
expect(screen.getByText('S3 Buckets')).toBeTruthy();
});
});
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Connection } from '~/concepts/connectionTypes/types';
import { SortableData } from '~/components/table';
import { Connection } from './types';

export const columns: SortableData<Connection>[] = [
{
field: 'name',
label: 'Name',
width: 25,
width: 35,
sortable: (a, b) =>
(a.metadata.annotations['openshift.io/display-name'] ?? '').localeCompare(
b.metadata.annotations['openshift.io/display-name'] ?? '',
Expand All @@ -14,22 +14,16 @@ export const columns: SortableData<Connection>[] = [
{
field: 'type',
label: 'Type',
width: 20,
width: 25,
sortable: (a, b) =>
a.metadata.annotations['opendatahub.io/connection-type'].localeCompare(
b.metadata.annotations['opendatahub.io/connection-type'],
),
},
{
field: 'compatibility',
label: 'Compatibility',
width: 20,
sortable: true,
},
{
field: 'connections',
label: 'Connected resources',
width: 30,
width: 35,
sortable: false,
},
{
Expand Down
21 changes: 0 additions & 21 deletions frontend/src/pages/projects/screens/detail/connections/types.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import useFetchState, {
FetchStateCallbackPromise,
NotReadyError,
} from '~/utilities/useFetchState';
import { Connection, isConnection } from '~/concepts/connectionTypes/types';
import { LABEL_SELECTOR_DASHBOARD_RESOURCE, LABEL_SELECTOR_DATA_CONNECTION_AWS } from '~/const';
import { Connection, isConnection } from './types';

const useConnections = (namespace?: string): FetchState<Connection[]> => {
const callback = React.useCallback<FetchStateCallbackPromise<Connection[]>>(
Expand Down

0 comments on commit c059291

Please sign in to comment.