Skip to content

Commit

Permalink
Merge branch '2.x' into backport/backport-6385-to-2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
ZilongX authored Apr 17, 2024
2 parents f499b4f + b275b4e commit 087dd16
Show file tree
Hide file tree
Showing 41 changed files with 987 additions and 176 deletions.
6 changes: 2 additions & 4 deletions src/core/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export type { Logos } from '../common';
export { PackageInfo, EnvironmentMode } from '../server/types';
/** @interal */
export { CoreContext, CoreSystem } from './core_system';
export { DEFAULT_APP_CATEGORIES } from '../utils';
export { DEFAULT_APP_CATEGORIES, WORKSPACE_TYPE } from '../utils';
export {
AppCategory,
UiSettingsParams,
Expand Down Expand Up @@ -357,6 +357,4 @@ export {

export { __osdBootstrap__ } from './osd_bootstrap';

export { WorkspacesStart, WorkspacesSetup, WorkspacesService } from './workspace';

export { WORKSPACE_TYPE } from '../utils';
export { WorkspacesStart, WorkspacesSetup, WorkspacesService, WorkspaceObject } from './workspace';
1 change: 1 addition & 0 deletions src/core/public/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ function createCoreSetupMock({
} = {}) {
const mock = {
application: applicationServiceMock.createSetupContract(),
chrome: chromeServiceMock.createSetupContract(),
context: contextServiceMock.createSetupContract(),
docLinks: docLinksServiceMock.createSetupContract(),
fatalErrors: fatalErrorsServiceMock.createSetupContract(),
Expand Down
1 change: 1 addition & 0 deletions src/core/public/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ export {
WORKSPACE_TYPE,
formatUrlWithWorkspaceId,
getWorkspaceIdFromUrl,
cleanWorkspaceId,
} from '../../utils';
7 changes: 6 additions & 1 deletion src/core/public/workspace/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,9 @@
* SPDX-License-Identifier: Apache-2.0
*/

export { WorkspacesStart, WorkspacesService, WorkspacesSetup } from './workspaces_service';
export {
WorkspacesStart,
WorkspacesService,
WorkspacesSetup,
WorkspaceObject,
} from './workspaces_service';
44 changes: 25 additions & 19 deletions src/core/public/workspace/workspaces_service.mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,33 @@
import { BehaviorSubject } from 'rxjs';
import type { PublicMethodsOf } from '@osd/utility-types';

import { WorkspacesService } from './workspaces_service';
import { WorkspaceAttribute } from '..';
import { WorkspacesService, WorkspaceObject } from './workspaces_service';

const currentWorkspaceId$ = new BehaviorSubject<string>('');
const workspaceList$ = new BehaviorSubject<WorkspaceAttribute[]>([]);
const currentWorkspace$ = new BehaviorSubject<WorkspaceAttribute | null>(null);
const initialized$ = new BehaviorSubject<boolean>(false);

const createWorkspacesSetupContractMock = () => ({
currentWorkspaceId$,
workspaceList$,
currentWorkspace$,
initialized$,
});
const createWorkspacesSetupContractMock = () => {
const currentWorkspaceId$ = new BehaviorSubject<string>('');
const workspaceList$ = new BehaviorSubject<WorkspaceObject[]>([]);
const currentWorkspace$ = new BehaviorSubject<WorkspaceObject | null>(null);
const initialized$ = new BehaviorSubject<boolean>(false);
return {
currentWorkspaceId$,
workspaceList$,
currentWorkspace$,
initialized$,
};
};

const createWorkspacesStartContractMock = () => ({
currentWorkspaceId$,
workspaceList$,
currentWorkspace$,
initialized$,
});
const createWorkspacesStartContractMock = () => {
const currentWorkspaceId$ = new BehaviorSubject<string>('');
const workspaceList$ = new BehaviorSubject<WorkspaceObject[]>([]);
const currentWorkspace$ = new BehaviorSubject<WorkspaceObject | null>(null);
const initialized$ = new BehaviorSubject<boolean>(false);
return {
currentWorkspaceId$,
workspaceList$,
currentWorkspace$,
initialized$,
};
};

export type WorkspacesServiceContract = PublicMethodsOf<WorkspacesService>;
const createMock = (): jest.Mocked<WorkspacesServiceContract> => ({
Expand Down
2 changes: 1 addition & 1 deletion src/core/public/workspace/workspaces_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { isEqual } from 'lodash';

import { CoreService, WorkspaceAttribute } from '../../types';

type WorkspaceObject = WorkspaceAttribute & { readonly?: boolean };
export type WorkspaceObject = WorkspaceAttribute & { readonly?: boolean };

interface WorkspaceObservables {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import { shallow } from 'enzyme';
import { SavedObjectsClientContract } from '../../../../../core/public';
import React from 'react';
import IndexPatternSelect from './index_pattern_select';

describe('IndexPatternSelect', () => {
let client: SavedObjectsClientContract;
const bulkGetMock = jest.fn();

const nextTick = () => new Promise((res) => process.nextTick(res));

beforeEach(() => {
client = {
find: jest.fn().mockResolvedValue({
savedObjects: [
{
references: [{ id: 'testDataSourceId', type: 'data-source' }],
attributes: { title: 'testTitle1' },
},
{
references: [{ id: 'testDataSourceId', type: 'data-source' }],
attributes: { title: 'testTitle2' },
},
],
}),
bulkGet: bulkGetMock,
get: jest.fn().mockResolvedValue({
references: [{ id: 'someId', type: 'data-source' }],
attributes: { title: 'testTitle' },
}),
} as any;
});

it('should render index pattern select', async () => {
const onChangeMock = jest.fn();
const compInstance = shallow(
<IndexPatternSelect
placeholder={'test index pattern'}
indexPatternId={'testId'}
onChange={onChangeMock}
data-test-subj={'testId'}
savedObjectsClient={client}
/>
).instance();

bulkGetMock.mockResolvedValue({ savedObjects: [{ attributes: { title: 'test1' } }] });
compInstance.debouncedFetch('');
await new Promise((resolve) => setTimeout(resolve, 300));
await nextTick();
expect(bulkGetMock).toBeCalledWith([{ id: 'testDataSourceId', type: 'data-source' }]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,15 @@ export default class IndexPatternSelect extends Component<IndexPatternSelectProp
// order than they were sent out. Only load results for the most recent search.
if (searchValue === this.state.searchValue) {
const dataSourcesToFetch: Array<{ type: string; id: string }> = [];
const dataSourceIdSet = new Set();
savedObjects.map((indexPatternSavedObject: SimpleSavedObject<any>) => {
const dataSourceReference = getDataSourceReference(indexPatternSavedObject.references);
if (dataSourceReference && !this.state.dataSourceIdToTitle.has(dataSourceReference.id)) {
if (
dataSourceReference &&
!this.state.dataSourceIdToTitle.has(dataSourceReference.id) &&
!dataSourceIdSet.has(dataSourceReference.id)
) {
dataSourceIdSet.add(dataSourceReference.id);
dataSourcesToFetch.push({ type: 'data-source', id: dataSourceReference.id });
}
});
Expand Down
23 changes: 23 additions & 0 deletions src/plugins/data_source/server/lib/error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,27 @@ describe('CreateDataSourceError', () => {
new DataSourceError(new Error('dummy'), 'Connection Failure', 400)
);
});

it('create data source error should be casted to a 400 DataSourceError', () => {
const error = new ResponseError({
statusCode: 401,
body: {
error: {
type: 'index_not_found_exception',
},
},
warnings: [],
headers: {
'WWW-Authenticate': 'content',
},
meta: {} as any,
});

const actual = new DataSourceError(error);

expect(actual).toMatchObject({
statusCode: 401,
body: { error: { type: 'index_not_found_exception' } },
});
});
});
6 changes: 6 additions & 0 deletions src/plugins/data_source/server/lib/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { OsdError } from '../../../opensearch_dashboards_utils/common';
export class DataSourceError extends OsdError {
// must have statusCode to avoid route handler in search.ts to return 500
statusCode: number;
body: any;

constructor(error: any, context?: string, statusCode?: number) {
let message: string;
if (context) {
Expand All @@ -23,6 +25,10 @@ export class DataSourceError extends OsdError {

super('Data Source Error: ' + message);

if (error.body) {
this.body = error.body;
}

if (statusCode) {
this.statusCode = statusCode;
} else if (error.statusCode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
"ui": true,
"requiredPlugins": ["management", "dataSource", "indexPatternManagement"],
"optionalPlugins": [],
"requiredBundles": ["opensearchDashboardsReact", "dataSource"],
"requiredBundles": ["opensearchDashboardsReact", "dataSource", "opensearchDashboardsUtils"],
"extraPublicDirs": ["public/components/utils"]
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import {
} from '@elastic/eui';
import { i18n } from '@osd/i18n';
import { SavedObjectsClientContract, ToastsStart } from 'opensearch-dashboards/public';
import { getDataSourcesWithFields } from '../utils';
import { getDataSourcesWithFields, handleDataSourceFetchError } from '../utils';
import { SavedObject } from '../../../../../core/public';
import { DataSourceAttributes } from '../../types';
import { DataSourceErrorMenu } from '../data_source_error_menu';
import { DataSourceBaseState } from '../data_source_menu/types';

interface DataSourceAggregatedViewProps {
savedObjectsClient: SavedObjectsClientContract;
Expand All @@ -27,7 +29,7 @@ interface DataSourceAggregatedViewProps {
displayAllCompatibleDataSources: boolean;
}

interface DataSourceAggregatedViewState {
interface DataSourceAggregatedViewState extends DataSourceBaseState {
isPopoverOpen: boolean;
allDataSourcesIdToTitleMap: Map<string, any>;
}
Expand All @@ -44,6 +46,7 @@ export class DataSourceAggregatedView extends React.Component<
this.state = {
isPopoverOpen: false,
allDataSourcesIdToTitleMap: new Map(),
showError: false,
};
}

Expand Down Expand Up @@ -89,15 +92,18 @@ export class DataSourceAggregatedView extends React.Component<
}
})
.catch(() => {
this.props.notifications.addWarning(
i18n.translate('dataSource.fetchDataSourceError', {
defaultMessage: 'Unable to fetch existing data sources',
})
);
handleDataSourceFetchError(this.onError.bind(this), this.props.notifications);
});
}

onError() {
this.setState({ showError: true });
}

render() {
if (this.state.showError) {
return <DataSourceErrorMenu />;
}
const button = (
<EuiButtonIcon
data-test-subj="dataSourceAggregatedViewInfoButton"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { EuiIcon, EuiText } from '@elastic/eui';
import React from 'react';

export const DataSourceErrorMenu = () => {
return (
<>
<EuiIcon type={'crossInCircleFilled'} color={'danger'} />
<EuiText color={'danger'}>Error</EuiText>
</>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
export { DataSourceErrorMenu } from './data_source_error_menu';

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 087dd16

Please sign in to comment.