Skip to content

Commit

Permalink
feat: revert the workspace change
Browse files Browse the repository at this point in the history
Signed-off-by: SuZhou-Joe <[email protected]>
  • Loading branch information
SuZhou-Joe committed Jul 27, 2023
1 parent a3890b0 commit 765ab9d
Show file tree
Hide file tree
Showing 14 changed files with 38 additions and 29 deletions.
8 changes: 6 additions & 2 deletions src/core/public/saved_objects/saved_objects_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export interface SavedObjectsCreateOptions {
/** {@inheritDoc SavedObjectsMigrationVersion} */
migrationVersion?: SavedObjectsMigrationVersion;
references?: SavedObjectReference[];
workspace?: string;
workspaces?: string[];
}

/**
Expand Down Expand Up @@ -268,7 +268,11 @@ export class SavedObjectsClient {
attributes,
migrationVersion: options.migrationVersion,
references: options.references,
workspace: options.workspace || currentWorkspaceId || undefined,
...(options.workspaces || currentWorkspaceId
? {
workspaces: options.workspaces || [currentWorkspaceId],
}
: {}),
}),
});

Expand Down
6 changes: 3 additions & 3 deletions src/core/server/saved_objects/import/create_saved_objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ interface CreateSavedObjectsParams<T> {
importIdMap: Map<string, { id?: string; omitOriginId?: boolean }>;
namespace?: string;
overwrite?: boolean;
workspace?: string;
workspaces?: string[];
}
interface CreateSavedObjectsResult<T> {
createdObjects: Array<CreatedObject<T>>;
Expand All @@ -57,7 +57,7 @@ export const createSavedObjects = async <T>({
importIdMap,
namespace,
overwrite,
workspace,
workspaces,
}: CreateSavedObjectsParams<T>): Promise<CreateSavedObjectsResult<T>> => {
// filter out any objects that resulted in errors
const errorSet = accumulatedErrors.reduce(
Expand Down Expand Up @@ -105,7 +105,7 @@ export const createSavedObjects = async <T>({
const bulkCreateResponse = await savedObjectsClient.bulkCreate(objectsToCreate, {
namespace,
overwrite,
workspace,
workspaces,
});
expectedResults = bulkCreateResponse.saved_objects;
}
Expand Down
4 changes: 2 additions & 2 deletions src/core/server/saved_objects/import/import_saved_objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export async function importSavedObjectsFromStream({
savedObjectsClient,
typeRegistry,
namespace,
workspace,
workspaces,
}: SavedObjectsImportOptions): Promise<SavedObjectsImportResponse> {
let errorAccumulator: SavedObjectsImportError[] = [];
const supportedTypes = typeRegistry.getImportableAndExportableTypes().map((type) => type.name);
Expand Down Expand Up @@ -119,7 +119,7 @@ export async function importSavedObjectsFromStream({
importIdMap,
overwrite,
namespace,
workspace,
workspaces,
};
const createSavedObjectsResult = await createSavedObjects(createSavedObjectsParams);
errorAccumulator = [...errorAccumulator, ...createSavedObjectsResult.errors];
Expand Down
4 changes: 2 additions & 2 deletions src/core/server/saved_objects/import/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ export interface SavedObjectsImportOptions {
namespace?: string;
/** If true, will create new copies of import objects, each with a random `id` and undefined `originId`. */
createNewCopies: boolean;
/** if specified, will import in given workspace, else will import as global object */
workspace?: string;
/** if specified, will import in given workspaces, else will import as global object */
workspaces?: string[];
}

/**
Expand Down
8 changes: 5 additions & 3 deletions src/core/server/saved_objects/routes/bulk_create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import { schema } from '@osd/config-schema';
import { IRouter } from '../../http';
import { formatWorkspaces, workspacesValidator } from '../../workspaces';

export const registerBulkCreateRoute = (router: IRouter) => {
router.post(
Expand All @@ -38,7 +39,7 @@ export const registerBulkCreateRoute = (router: IRouter) => {
validate: {
query: schema.object({
overwrite: schema.boolean({ defaultValue: false }),
workspace: schema.maybe(schema.string()),
workspaces: workspacesValidator,
}),
body: schema.arrayOf(
schema.object({
Expand All @@ -62,10 +63,11 @@ export const registerBulkCreateRoute = (router: IRouter) => {
},
},
router.handleLegacyErrors(async (context, req, res) => {
const { overwrite, workspace } = req.query;
const { overwrite } = req.query;
const workspaces = formatWorkspaces(req.query.workspaces);
const result = await context.core.savedObjects.client.bulkCreate(req.body, {
overwrite,
workspace,
workspaces,
});
return res.ok({ body: result });
})
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/saved_objects/routes/copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export const registerCopyRoute = (router: IRouter, config: SavedObjectConfig) =>
objectLimit: maxImportExportSize,
overwrite: false,
createNewCopies: true,
workspace: targetWorkspace,
workspaces: [targetWorkspace],
});

return res.ok({ body: result });
Expand Down
6 changes: 3 additions & 3 deletions src/core/server/saved_objects/routes/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,22 +56,22 @@ export const registerCreateRoute = (router: IRouter) => {
)
),
initialNamespaces: schema.maybe(schema.arrayOf(schema.string(), { minSize: 1 })),
workspace: schema.maybe(schema.string()),
workspaces: schema.maybe(schema.arrayOf(schema.string(), { minSize: 1 })),
}),
},
},
router.handleLegacyErrors(async (context, req, res) => {
const { type, id } = req.params;
const { overwrite } = req.query;
const { attributes, migrationVersion, references, initialNamespaces, workspace } = req.body;
const { attributes, migrationVersion, references, initialNamespaces, workspaces } = req.body;

const options = {
id,
overwrite,
migrationVersion,
references,
initialNamespaces,
workspace,
workspaces,
};
const result = await context.core.savedObjects.client.create(type, attributes, options);
return res.ok({ body: result });
Expand Down
9 changes: 6 additions & 3 deletions src/core/server/saved_objects/routes/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { IRouter } from '../../http';
import { importSavedObjectsFromStream } from '../import';
import { SavedObjectConfig } from '../saved_objects_config';
import { createSavedObjectsStreamFromNdJson } from './utils';
import { formatWorkspaces, workspacesValidator } from '../../workspaces';

interface FileStream extends Readable {
hapi: {
Expand All @@ -60,7 +61,7 @@ export const registerImportRoute = (router: IRouter, config: SavedObjectConfig)
{
overwrite: schema.boolean({ defaultValue: false }),
createNewCopies: schema.boolean({ defaultValue: false }),
workspace: schema.maybe(schema.string()),
workspaces: workspacesValidator,
},
{
validate: (object) => {
Expand All @@ -76,7 +77,7 @@ export const registerImportRoute = (router: IRouter, config: SavedObjectConfig)
},
},
router.handleLegacyErrors(async (context, req, res) => {
const { overwrite, createNewCopies, workspace } = req.query;
const { overwrite, createNewCopies } = req.query;
const file = req.body.file as FileStream;
const fileExtension = extname(file.hapi.filename).toLowerCase();
if (fileExtension !== '.ndjson') {
Expand All @@ -92,14 +93,16 @@ export const registerImportRoute = (router: IRouter, config: SavedObjectConfig)
});
}

const workspaces = formatWorkspaces(req.query.workspaces);

const result = await importSavedObjectsFromStream({
savedObjectsClient: context.core.savedObjects.client,
typeRegistry: context.core.savedObjects.typeRegistry,
readStream,
objectLimit: maxImportExportSize,
overwrite,
createNewCopies,
workspace,
workspaces,
});

return res.ok({ body: result });
Expand Down
4 changes: 2 additions & 2 deletions src/core/server/saved_objects/service/lib/repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ export class SavedObjectsRepository {
originId,
initialNamespaces,
version,
workspace,
workspaces,
} = options;
const namespace = normalizeNamespace(options.namespace);

Expand Down Expand Up @@ -293,7 +293,7 @@ export class SavedObjectsRepository {
}
}

let savedObjectWorkspaces = workspace ? [workspace] : undefined;
let savedObjectWorkspaces = workspaces;

if (id && overwrite) {
try {
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/saved_objects/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export interface SavedObjectsFindOptions {
export interface SavedObjectsBaseOptions {
/** Specify the namespace for this operation */
namespace?: string;
workspace?: string;
workspaces?: string[];
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ interface ImportResponse {
export async function importFile(
http: HttpStart,
file: File,
{ createNewCopies, overwrite, workspace }: ImportMode
{ createNewCopies, overwrite, workspaces }: ImportMode
) {
const formData = new FormData();
formData.append('file', file);
const query = createNewCopies ? { createNewCopies, workspace } : { overwrite, workspace };
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,7 +92,7 @@ export interface FlyoutProps {
overlays: OverlayStart;
http: HttpStart;
search: DataPublicPluginStart['search'];
workspace?: string;
workspaces?: string[];
}

export interface FlyoutState {
Expand Down Expand Up @@ -184,15 +184,15 @@ export class Flyout extends Component<FlyoutProps, FlyoutState> {
* Does the initial import of a file, resolveImportErrors then handles errors and retries
*/
import = async () => {
const { http, workspace } = 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,
workspace,
workspaces,
});
this.setState(processImportResponse(response), () => {
// Resolve import errors right away if there's no index patterns to match
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export interface ImportModeControlProps {
export interface ImportMode {
createNewCopies: boolean;
overwrite: boolean;
workspace?: string;
workspaces?: string[];
}

const createNewCopiesDisabled = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ export class SavedObjectsTable extends Component<SavedObjectsTableProps, SavedOb
close={this.hideImportFlyout}
done={this.finishImport}
http={this.props.http}
workspace={this.state.workspaceId || undefined}
workspaces={this.state.workspaceId ? [this.state.workspaceId] : undefined}
serviceRegistry={this.props.serviceRegistry}
indexPatterns={this.props.indexPatterns}
newIndexPatternUrl={newIndexPatternUrl}
Expand Down

0 comments on commit 765ab9d

Please sign in to comment.