Skip to content

Commit

Permalink
feat: integrate with saved object management page
Browse files Browse the repository at this point in the history
Signed-off-by: SuZhoue-Joe <[email protected]>
  • Loading branch information
SuZhou-Joe committed Jun 25, 2023
1 parent 5c5d53e commit 902f088
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ export async function fetchExportByTypeAndSearch(
http: HttpStart,
types: string[],
search: string | undefined,
includeReferencesDeep: boolean = false
includeReferencesDeep: boolean = false,
body?: Record<string, unknown>
): Promise<Blob> {
return http.post('/api/saved_objects/_export', {
body: JSON.stringify({
...body,
type: types,
search,
includeReferencesDeep,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ import { HttpStart } from 'src/core/public';
export async function fetchExportObjects(
http: HttpStart,
objects: any[],
includeReferencesDeep: boolean = false
includeReferencesDeep: boolean = false,
body?: Record<string, unknown>
): Promise<Blob> {
return http.post('/api/saved_objects/_export', {
body: JSON.stringify({
...body,
objects,
includeReferencesDeep,
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export interface SavedObjectCountOptions {
typesToInclude: string[];
namespacesToInclude?: string[];
searchString?: string;
workspaces?: string[];
}

export async function getSavedObjectCounts(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
* under the License.
*/

import { HttpStart, SavedObjectsImportError } from 'src/core/public';
import { HttpStart, SavedObjectsImportError, WorkspacesStart } from 'src/core/public';
import { ImportMode } from '../management_section/objects_table/components/import_mode_control';

interface ImportResponse {
Expand All @@ -40,11 +40,11 @@ interface ImportResponse {
export async function importFile(
http: HttpStart,
file: File,
{ createNewCopies, overwrite }: ImportMode
{ createNewCopies, overwrite, workspaces }: ImportMode
) {
const formData = new FormData();
formData.append('file', file);
const query = createNewCopies ? { createNewCopies } : { overwrite };
const query = createNewCopies ? { createNewCopies, workspaces } : { overwrite, workspaces };
return await http.post<ImportResponse>('/api/saved_objects/_import', {
body: formData,
headers: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export interface FlyoutProps {
overlays: OverlayStart;
http: HttpStart;
search: DataPublicPluginStart['search'];
workspaces?: string[];
}

export interface FlyoutState {
Expand Down Expand Up @@ -183,13 +184,16 @@ export class Flyout extends Component<FlyoutProps, FlyoutState> {
* Does the initial import of a file, resolveImportErrors then handles errors and retries
*/
import = async () => {
const { http } = this.props;
const { http, workspaces } = this.props;
const { file, importMode } = this.state;
this.setState({ status: 'loading', error: undefined });

// Import the file
try {
const response = await importFile(http, file!, importMode);
const response = await importFile(http, file!, {
...importMode,
workspaces,
});
this.setState(processImportResponse(response), () => {
// Resolve import errors right away if there's no index patterns to match
// This will ask about overwriting each object, etc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export interface ImportModeControlProps {
export interface ImportMode {
createNewCopies: boolean;
overwrite: boolean;
workspaces?: string[];
}

const createNewCopiesDisabled = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export interface SavedObjectsTableState {
exportAllOptions: ExportAllOption[];
exportAllSelectedOptions: Record<string, boolean>;
isIncludeReferencesDeepChecked: boolean;
workspaceId: string | null;
}

export class SavedObjectsTable extends Component<SavedObjectsTableProps, SavedObjectsTableState> {
Expand Down Expand Up @@ -165,11 +166,21 @@ export class SavedObjectsTable extends Component<SavedObjectsTableProps, SavedOb
exportAllOptions: [],
exportAllSelectedOptions: {},
isIncludeReferencesDeepChecked: true,
workspaceId: this.props.workspaces.client.currentWorkspaceId$.getValue(),
};
}

private get workspaceIdQuery() {
return this.state.workspaceId ? ['public', this.state.workspaceId] : ['public'];
}

componentDidMount() {
this._isMounted = true;
this.props.workspaces.client.currentWorkspaceId$.subscribe((workspaceId) =>
this.setState({
workspaceId,
})
);
this.fetchSavedObjects();
this.fetchCounts();
}
Expand All @@ -190,6 +201,7 @@ export class SavedObjectsTable extends Component<SavedObjectsTableProps, SavedOb
const filteredCountOptions: SavedObjectCountOptions = {
typesToInclude: filteredTypes,
searchString: queryText,
workspaces: this.workspaceIdQuery,
};

if (availableNamespaces.length) {
Expand Down Expand Up @@ -222,6 +234,7 @@ export class SavedObjectsTable extends Component<SavedObjectsTableProps, SavedOb
const countOptions: SavedObjectCountOptions = {
typesToInclude: allowedTypes,
searchString: queryText,
workspaces: this.workspaceIdQuery,
};

if (availableNamespaces.length) {
Expand Down Expand Up @@ -261,6 +274,7 @@ export class SavedObjectsTable extends Component<SavedObjectsTableProps, SavedOb
page: page + 1,
fields: ['id'],
type: filteredTypes,
workspaces: this.workspaceIdQuery,
};

const availableNamespaces = namespaceRegistry.getAll()?.map((ns) => ns.id) || [];
Expand Down Expand Up @@ -403,7 +417,9 @@ export class SavedObjectsTable extends Component<SavedObjectsTableProps, SavedOb

let blob;
try {
blob = await fetchExportObjects(http, objectsToExport, includeReferencesDeep);
blob = await fetchExportObjects(http, objectsToExport, includeReferencesDeep, {
workspaces: this.workspaceIdQuery,
});
} catch (e) {
notifications.toasts.addDanger({
title: i18n.translate('savedObjectsManagement.objectsTable.export.dangerNotification', {
Expand Down Expand Up @@ -437,7 +453,10 @@ export class SavedObjectsTable extends Component<SavedObjectsTableProps, SavedOb
http,
exportTypes,
queryText ? `${queryText}*` : undefined,
isIncludeReferencesDeepChecked
isIncludeReferencesDeepChecked,
{
workspaces: this.workspaceIdQuery,
}
);
} catch (e) {
notifications.toasts.addDanger({
Expand Down Expand Up @@ -552,6 +571,7 @@ export class SavedObjectsTable extends Component<SavedObjectsTableProps, SavedOb
close={this.hideImportFlyout}
done={this.finishImport}
http={this.props.http}
workspaces={this.state.workspaceId ? [this.state.workspaceId] : undefined}
serviceRegistry={this.props.serviceRegistry}
indexPatterns={this.props.indexPatterns}
newIndexPatternUrl={newIndexPatternUrl}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export const registerScrollForCountRoute = (router: IRouter) => {
typesToInclude: schema.arrayOf(schema.string()),
namespacesToInclude: schema.maybe(schema.arrayOf(schema.string())),
searchString: schema.maybe(schema.string()),
workspaces: schema.maybe(schema.arrayOf(schema.string())),
}),
},
},
Expand All @@ -53,6 +54,7 @@ export const registerScrollForCountRoute = (router: IRouter) => {
const findOptions: SavedObjectsFindOptions = {
type: req.body.typesToInclude,
perPage: 1000,
workspaces: req.body.workspaces,
};

const requestHasNamespaces =
Expand Down

0 comments on commit 902f088

Please sign in to comment.