Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[workspace] fix set as default error in data source and index pattern #9187

Merged
Merged
Show file tree
Hide file tree
Changes from all 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: 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
Loading