Skip to content

Commit

Permalink
[workspace] fix set as default error in data source and index pattern (
Browse files Browse the repository at this point in the history
…opensearch-project#9187)

* fix set as default error

Signed-off-by: Qxisylolo <[email protected]>

* Changeset file for PR opensearch-project#9187 created/updated

* add tests

Signed-off-by: Qxisylolo <[email protected]>

* fix tests

Signed-off-by: Qxisylolo <[email protected]>

* resolve comments

Signed-off-by: Qxisylolo <[email protected]>

---------

Signed-off-by: Qxisylolo <[email protected]>
Co-authored-by: opensearch-changeset-bot[bot] <154024398+opensearch-changeset-bot[bot]@users.noreply.github.com>
  • Loading branch information
Qxisylolo and opensearch-changeset-bot[bot] authored Jan 16, 2025
1 parent 53fe4c1 commit 5a290a4
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 13 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/9187.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
- Fix set as default error in data source and index pattern ([#9187](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/9187))
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export interface EditDataSourceProps {
handleSubmit: (formValues: DataSourceAttributes) => Promise<void>;
handleTestConnection: (formValues: DataSourceAttributes) => Promise<void>;
onDeleteDataSource?: () => Promise<void>;
onSetDefaultDataSource: () => Promise<void>;
onSetDefaultDataSource: () => Promise<boolean>;
displayToastMessage: (info: DataSourceManagementToastMessageItem) => void;
canManageDataSource: boolean;
}
Expand Down Expand Up @@ -412,9 +412,7 @@ export class EditDataSourceForm extends React.Component<EditDataSourceProps, Edi
};

setDefaultDataSource = async () => {
if (this.props.onSetDefaultDataSource) {
await this.props.onSetDefaultDataSource();
}
return await this.props.onSetDefaultDataSource();
};

onClickTestConnection = async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,27 @@ describe('Datasource Management: Edit Datasource Header', () => {
test('should render normally', () => {
expect(component.find(setDefaultButtonIdentifier).exists()).toBe(true);
});
test('should not change to default if onClickSetDefault returns false', async () => {
onClickSetDefault.mockReturnValue(false);
expect(component.find(setDefaultButtonIdentifier).first().text()).toBe('Set as default');
component.find(setDefaultButtonIdentifier).first().simulate('click');
expect(onClickSetDefault).toHaveBeenCalled();
component.update();
expect(component.find(setDefaultButtonIdentifier).first().text()).toBe('Set as default');
});

test('should update isDefaultDataSourceState to true if onClickSetDefault returns true', async () => {
onClickSetDefault.mockReturnValue(true);
expect(component.find(setDefaultButtonIdentifier).first().text()).toBe('Set as default');

await act(async () => {
component.find(setDefaultButtonIdentifier).first().simulate('click');
});

expect(onClickSetDefault).toHaveBeenCalled();
component.update();
expect(component.find(setDefaultButtonIdentifier).first().text()).toBe('Default');
});
test('default button should show as "Set as default" and should be clickable', () => {
expect(component.find(setDefaultButtonIdentifier).first().text()).toBe('Set as default');
expect(component.find(setDefaultButtonIdentifier).first().prop('disabled')).toBe(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const Header = ({
isFormValid: boolean;
onClickDeleteIcon: () => void;
onClickTestConnection: () => void;
onClickSetDefault: () => void;
onClickSetDefault: () => Promise<boolean>;
dataSourceName: string;
isDefault: boolean;
canManageDataSource: boolean;
Expand All @@ -70,9 +70,10 @@ export const Header = ({
const renderDefaultIcon = () => {
return (
<EuiSmallButtonEmpty
onClick={() => {
onClickSetDefault();
setIsDefaultDataSourceState(!isDefaultDataSourceState);
onClick={async () => {
if (await onClickSetDefault()) {
setIsDefaultDataSourceState(!isDefaultDataSourceState);
}
}}
disabled={isDefaultDataSourceState}
iconType={isDefaultDataSourceState ? 'starFilled' : 'starEmpty'}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
sigV4AuthMethod,
usernamePasswordAuthMethod,
} from '../../types';

const formIdentifier = 'EditDataSourceForm';
const notFoundIdentifier = '[data-test-subj="dataSourceNotFound"]';

Expand Down Expand Up @@ -139,6 +138,19 @@ describe('Datasource Management: Edit Datasource Wizard', () => {
});
expect(uiSettings.set).toHaveBeenCalled();
});

test('should not set default data source if no permission', async () => {
spyOn(uiSettings, 'set').and.returnValue(Promise.resolve(false));
await act(async () => {
// @ts-ignore
const result = await component.find(formIdentifier).first().prop('onSetDefaultDataSource')(
mockDataSourceAttributesWithAuth
);
expect(result).toBe(false);
});
expect(uiSettings.set).toHaveBeenCalled();
});

test('should delete datasource successfully', async () => {
spyOn(utils, 'deleteDataSourceById').and.returnValue({});
spyOn(utils, 'setFirstDataSourceAsDefault').and.returnValue({});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export const EditDataSource: React.FunctionComponent<RouteComponentProps<{ id: s
};

const handleSetDefault = async () => {
await uiSettings.set(DEFAULT_DATA_SOURCE_UI_SETTINGS_ID, dataSourceID);
return await uiSettings.set(DEFAULT_DATA_SOURCE_UI_SETTINGS_ID, dataSourceID);
};

const isDefaultDataSource = getDefaultDataSourceId(uiSettings) === dataSourceID;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,11 @@ export const EditIndexPattern = withRouter(
setTags(indexPatternTags);
}, [defaultIndex, indexPattern, indexPatternManagementStart.list]);

const setDefaultPattern = useCallback(() => {
uiSettings.set('defaultIndex', indexPattern.id);
setDefaultIndex(indexPattern.id || '');
const setDefaultPattern = useCallback(async () => {
const isSuccess = await uiSettings.set('defaultIndex', indexPattern.id);
if (isSuccess) {
setDefaultIndex(indexPattern.id || '');
}
}, [uiSettings, indexPattern.id]);

const refreshFields = () => {
Expand Down

0 comments on commit 5a290a4

Please sign in to comment.