Skip to content

Commit

Permalink
Add set default datasource
Browse files Browse the repository at this point in the history
Signed-off-by: Yuanqi(Ella) Zhu <[email protected]>
  • Loading branch information
zhyuanqi committed Mar 18, 2024
1 parent 05abf5e commit 8c72245
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- [Discover] Enhanced the data source selector with added sorting functionality ([#5609](https://github.com/opensearch-project/OpenSearch-Dashboards/issues/5609))
- [Multiple Datasource] Add datasource picker component and use it in devtools and tutorial page when multiple datasource is enabled ([#5756](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5756))
- [Multiple Datasource] Add datasource picker to import saved object flyout when multiple data source is enabled ([#5781](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/5781))
- [Multiple Datasource] Add default functionality for customer to choose default datasource ([#6058](https://github.com/opensearch-project/OpenSearch-Dashboards/issues/6058))

### 🐛 Bug Fixes

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,11 @@ import { extractRegisteredAuthTypeCredentials, getDefaultAuthMethod } from '../.
export interface EditDataSourceProps {
existingDataSource: DataSourceAttributes;
existingDatasourceNamesList: string[];
isDefault: boolean;
handleSubmit: (formValues: DataSourceAttributes) => Promise<void>;
handleTestConnection: (formValues: DataSourceAttributes) => Promise<void>;
onDeleteDataSource?: () => Promise<void>;
onSetDefaultDataSource: () => Promise<void>;
displayToastMessage: (info: ToastMessageItem) => void;
}
export interface EditDataSourceState {
Expand Down Expand Up @@ -400,6 +402,16 @@ export class EditDataSourceForm extends React.Component<EditDataSourceProps, Edi
}
};

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

Check warning on line 407 in src/plugins/data_source_management/public/components/edit_data_source/components/edit_form/edit_data_source_form.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data_source_management/public/components/edit_data_source/components/edit_form/edit_data_source_form.tsx#L407

Added line #L407 was not covered by tests
}
}

Check failure on line 409 in src/plugins/data_source_management/public/components/edit_data_source/components/edit_form/edit_data_source_form.tsx

View workflow job for this annotation

GitHub Actions / Build and Verify on Linux (ciGroup1)

Insert `;`

isDefault = () => {

Check failure on line 411 in src/plugins/data_source_management/public/components/edit_data_source/components/edit_form/edit_data_source_form.tsx

View workflow job for this annotation

GitHub Actions / Build and Verify on Linux (ciGroup1)

Delete `·`
return this.props.isDefault;
}

Check failure on line 413 in src/plugins/data_source_management/public/components/edit_data_source/components/edit_form/edit_data_source_form.tsx

View workflow job for this annotation

GitHub Actions / Build and Verify on Linux (ciGroup1)

Insert `;`

onClickTestConnection = async () => {
this.setState({ isLoading: true });
const isNewCredential = !!(this.state.auth.type !== this.props.existingDataSource.auth.type);
Expand Down Expand Up @@ -634,6 +646,8 @@ export class EditDataSourceForm extends React.Component<EditDataSourceProps, Edi
onClickDeleteIcon={this.onClickDeleteDataSource}
dataSourceName={this.props.existingDataSource.title}
onClickTestConnection={this.onClickTestConnection}
onClickSetDefault={this.setDefaultDataSource}
isDefault={this.isDefault()}
/>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
EuiButtonIcon,
EuiConfirmModal,
EuiButton,
EuiButtonEmpty,
} from '@elastic/eui';
import { i18n } from '@osd/i18n';
import { FormattedMessage } from '@osd/i18n/react';
Expand All @@ -25,22 +26,51 @@ export const Header = ({
isFormValid,
onClickDeleteIcon,
onClickTestConnection,
onClickSetDefault,
dataSourceName,
isDefault,
}: {
showDeleteIcon: boolean;
isFormValid: boolean;
onClickDeleteIcon: () => void;
onClickTestConnection: () => void;
onClickSetDefault: () => void;
dataSourceName: string;
isDefault: boolean;
}) => {
/* State Variables */
const [isDeleteModalVisible, setIsDeleteModalVisible] = useState(false);
const [isDefaultDataSourceState, setIsDefaultDataSourceState] = useState(isDefault);

const changeTitle = useOpenSearchDashboards<DataSourceManagementContext>().services.chrome
.docTitle.change;

changeTitle(dataSourceName);

const setDefaultAriaLabel = i18n.translate(
'dataSourcesManagement.editDataSource.setDefaultDataSource',
{
defaultMessage: 'Set as default Data Source.',
}
);

const renderDefaultIcon = () => {
return (
<EuiButtonEmpty
onClick={() => {
onClickSetDefault();
setIsDefaultDataSourceState(!isDefaultDataSourceState);

Check warning on line 62 in src/plugins/data_source_management/public/components/edit_data_source/components/header/header.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data_source_management/public/components/edit_data_source/components/header/header.tsx#L61-L62

Added lines #L61 - L62 were not covered by tests
}}
disabled={isDefaultDataSourceState}
iconType={isDefaultDataSourceState ? 'starFilled' : 'starEmpty'}
aria-label={setDefaultAriaLabel}
data-test-subj="datasource-edit-setDefaultDataSource"
>
{isDefaultDataSourceState ? 'Default' : 'Set as default'}
</EuiButtonEmpty>
);
};

const renderDeleteButton = () => {
return (
<>
Expand Down Expand Up @@ -144,6 +174,8 @@ export const Header = ({
{/* Right side buttons */}
<EuiFlexItem grow={false}>
<EuiFlexGroup alignItems="baseline" gutterSize="m" responsive={false}>
{/* Test defaultn button */}
<EuiFlexItem grow={false}>{renderDefaultIcon()}</EuiFlexItem>
{/* Test connection button */}
<EuiFlexItem grow={false}>{renderTestConnectionButton()}</EuiFlexItem>
{/* Delete icon button */}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const EditDataSource: React.FunctionComponent<RouteComponentProps<{ id: s
) => {
/* Initialization */
const {
uiSettings,
savedObjects,
setBreadcrumbs,
http,
Expand Down Expand Up @@ -83,6 +84,12 @@ export const EditDataSource: React.FunctionComponent<RouteComponentProps<{ id: s
}
};

const handleSetDefault = async () => {
await uiSettings.set('defaultDataSource', dataSourceID);

Check warning on line 88 in src/plugins/data_source_management/public/components/edit_data_source/edit_data_source.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data_source_management/public/components/edit_data_source/edit_data_source.tsx#L88

Added line #L88 was not covered by tests
}

Check failure on line 89 in src/plugins/data_source_management/public/components/edit_data_source/edit_data_source.tsx

View workflow job for this annotation

GitHub Actions / Build and Verify on Linux (ciGroup1)

Insert `;`

const isDefaultDataSource = uiSettings.get('defaultDataSource', null) === dataSourceID;

/* Handle submit - create data source*/
const handleSubmit = async (attributes: DataSourceAttributes) => {
await updateDataSourceById(savedObjects.client, dataSourceID, attributes);
Expand Down Expand Up @@ -128,7 +135,9 @@ export const EditDataSource: React.FunctionComponent<RouteComponentProps<{ id: s
<EditDataSourceForm
existingDataSource={dataSource}
existingDatasourceNamesList={existingDatasourceNamesList}
isDefault={isDefaultDataSource}
onDeleteDataSource={handleDelete}
onSetDefaultDataSource={handleSetDefault}
handleSubmit={handleSubmit}
displayToastMessage={handleDisplayToastMessage}
handleTestConnection={handleTestConnection}
Expand Down

0 comments on commit 8c72245

Please sign in to comment.