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

[MD] Update delete credentials method to be in parallel #2076

Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {
EuiButton,
EuiFlexGroup,
EuiFlexItem,
EuiGlobalToastList,
EuiGlobalToastListToast,
EuiInMemoryTable,
EuiSpacer,
EuiText,
Expand Down Expand Up @@ -66,6 +68,7 @@ interface Props extends RouteComponentProps {
export const CredentialsTable = ({ canSave, history }: Props) => {
const [credentials, setCredentials] = React.useState<CredentialsTableItem[]>([]);
const [selectedCredentials, setSelectedCredentials] = React.useState<CredentialsTableItem[]>([]);
const [toasts, setToasts] = React.useState<EuiGlobalToastListToast[]>([]);

const { setBreadcrumbs } = useOpenSearchDashboards<CredentialManagementContext>().services;
setBreadcrumbs(getListBreadcrumbs());
Expand Down Expand Up @@ -135,11 +138,30 @@ export const CredentialsTable = ({ canSave, history }: Props) => {
};

const onClickDelete = async () => {
await deleteCredentials(savedObjects.client, selectedCredentials);
// TODO: https://github.com/opensearch-project/OpenSearch-Dashboards/issues/2055
const fetchedCredentials: CredentialsTableItem[] = await getCredentials(savedObjects.client);
setCredentials(fetchedCredentials);
setSelectedCredentials([]);
try {
await deleteCredentials(savedObjects.client, selectedCredentials);
// TODO: https://github.com/opensearch-project/OpenSearch-Dashboards/issues/2055
const fetchedCredentials: CredentialsTableItem[] = await getCredentials(savedObjects.client);
setCredentials(fetchedCredentials);
setSelectedCredentials([]);
} catch (e) {
const deleteCredentialsFailMsg = (
<FormattedMessage
id="credentialManagement.credentialsTable.loadDeleteCredentialsFailMsg"
defaultMessage="The credential saved objects delete failed with some errors. Please configure data_source.enabled and try it again."
/>
);
setToasts(
toasts.concat([
{
title: deleteCredentialsFailMsg,
id: deleteCredentialsFailMsg.props.id,
color: 'warning',
iconType: 'alert',
},
])
);
}
};

const deleteButton = renderDeleteButton();
Expand All @@ -153,39 +175,58 @@ export const CredentialsTable = ({ canSave, history }: Props) => {

const createButton = canSave ? <CreateButton history={history} /> : <></>;

const removeToast = (id: string) => {
setToasts(toasts.filter((toast) => toast.id !== id));
};

const renderContent = () => {
return (
<EuiPageContent data-test-subj="credentialsTable" role="region">
<EuiFlexGroup justifyContent="spaceBetween">
<EuiFlexItem grow={false}>
<EuiTitle>
<h2>{title}</h2>
</EuiTitle>
<EuiSpacer size="s" />
<EuiText>
<p>
<FormattedMessage
noCharger marked this conversation as resolved.
Show resolved Hide resolved
id="credentialManagement.credentialsTable.credentialManagementExplanation"
defaultMessage="Create and manage the credentials that help you retrieve your data from OpenSearch."
/>
</p>
</EuiText>
</EuiFlexItem>
<EuiFlexItem grow={false}>{createButton}</EuiFlexItem>
<EuiFlexItem grow={false}>{deleteButton}</EuiFlexItem>
</EuiFlexGroup>
<EuiSpacer />
<EuiInMemoryTable
allowNeutralSort={false}
itemId="id"
isSelectable={true}
selection={selection}
items={credentials}
columns={columns}
pagination={pagination}
sorting={sorting}
search={search}
/>
</EuiPageContent>
);
};

return (
<EuiPageContent data-test-subj="credentialsTable" role="region">
<EuiFlexGroup justifyContent="spaceBetween">
<EuiFlexItem grow={false}>
<EuiTitle>
<h2>{title}</h2>
</EuiTitle>
<EuiSpacer size="s" />
<EuiText>
<p>
<FormattedMessage
id="credentialManagement.credentialsTable.credentialManagementExplanation"
defaultMessage="Create and manage the credentials that help you retrieve your data from OpenSearch."
/>
</p>
</EuiText>
</EuiFlexItem>
<EuiFlexItem grow={false}>{createButton}</EuiFlexItem>
<EuiFlexItem grow={false}>{deleteButton}</EuiFlexItem>
</EuiFlexGroup>
<EuiSpacer />
<EuiInMemoryTable
allowNeutralSort={false}
itemId="id"
isSelectable={true}
selection={selection}
items={credentials}
columns={columns}
pagination={pagination}
sorting={sorting}
search={search}
<>
{renderContent()}
<EuiGlobalToastList
toasts={toasts}
dismissToast={({ id }) => {
removeToast(id);
}}
toastLifeTimeMs={6000}
/>
</EuiPageContent>
</>
);
};

Expand Down
9 changes: 5 additions & 4 deletions src/plugins/credential_management/public/components/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ export async function deleteCredentials(
savedObjectsClient: SavedObjectsClientContract,
selectedCredentials: CredentialsTableItem[]
) {
// TODO: Refactor with nonblocking IO
for (const credential of selectedCredentials) {
await savedObjectsClient.delete('credential', credential.id);
}
await Promise.all(
noCharger marked this conversation as resolved.
Show resolved Hide resolved
selectedCredentials.map(async (selectedCredential) => {
await savedObjectsClient.delete('credential', selectedCredential.id);
})
);
Comment on lines +47 to +51
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}